Skip to content

Commit

Permalink
Merge pull request #39 from acemomiage/bind-address
Browse files Browse the repository at this point in the history
support specify bind address
  • Loading branch information
fuktommy authored Dec 13, 2023
2 parents 17a49b5 + 7461fe8 commit 1e0c208
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
7 changes: 6 additions & 1 deletion doc/sample.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#

[Network]
# Bind Address(Default: *)
bind_addr:

# Listening TCP port.
port: 8000

Expand All @@ -16,6 +19,9 @@ max_connection: 20
# Server name for shinGETsu protocol
dnsname:

# use X-FORWARDED-FOR if defined.
use_x_forwarded_for: no

[Path]
prefix: /usr/local
var: /var/local
Expand Down Expand Up @@ -90,7 +96,6 @@ archive_uri: https://archive.shingetsu.info/
# 2ch-interface
enable_2ch: no


[Application Thread]
# Time range for saving records. (seconds)
# 0 for infinity
Expand Down
2 changes: 2 additions & 0 deletions shingetsu/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ def _get_version():
# Application types
types = ("thread",)

bind_addr = _get_value(_extconf, 'Network', 'bind_addr', '')
port = _get_value(_extconf, 'Network', 'port', 8000, 'int')
dat_port = _get_value(_extconf, 'Network', 'dat_port', 8001, 'int')
max_connection = _get_value(_extconf, 'Network', 'max_connection', 20, 'int')
use_x_forwarded_for = _get_value(_extconf, 'Network', 'use_x_forwarded_for', False, 'boolean') # set true if behind a reverse proxy.

docroot = _get_value(_extconf, 'Path', 'docroot', './www', 'path')
log_dir = _get_value(_extconf, 'Path', 'log_dir', './log', 'path')
Expand Down
4 changes: 2 additions & 2 deletions shingetsu/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
from .tag import *
from .template import Template
from .updatequeue import UpdateQueue
from .util import opentext
from .util import opentext, get_http_remote_addr


dummyquery = str(int(time.time()));
Expand Down Expand Up @@ -138,7 +138,7 @@ def __init__(self,
else:
al = ""
self.message = search_message(al)
addr = self.environ.get("REMOTE_ADDR", "")
addr = get_http_remote_addr(self.environ)
self.remoteaddr = addr
self.isadmin = config.re_admin.search(addr)
self.isfriend = config.re_friend.search(addr)
Expand Down
2 changes: 1 addition & 1 deletion shingetsu/httpd.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(self):
HandlerClass = LightCGIHTTPServer.HTTPRequestHandler

ServerClass = LightCGIHTTPServer.HTTPServer
server_address = ("", config.port)
server_address = (config.bind_addr, config.port)
HandlerClass.server_version = config.version
HandlerClass.root_index = config.root_index
self.httpserv = ServerClass(server_address, HandlerClass)
Expand Down
17 changes: 12 additions & 5 deletions shingetsu/server_cgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from .cache import *
from .node import *
from .updatequeue import UpdateQueue
from .util import opentext
from .util import opentext, get_http_remote_addr


class CGI(basecgi.CGI):
Expand All @@ -52,7 +52,7 @@ def run(self):
httphost = self.environ["HTTP_HOST"]
if config.dnsname !='' and (config.dnsname + ":"+ str(config.port)) != httphost :
self.header("text/plain; charset=UTF-8")
self.stdout.write('error: invaild http host')
self.stdout.write('error: invalid http host')
return

if not self.environ["REQUEST_METHOD"] in ("GET", "HEAD"):
Expand All @@ -77,6 +77,8 @@ def run(self):
flag = self.do_update(path)
if flag:
self.stdout.write("OK\n")
elif path.startswith("version"):
self.do_version()

def path_info(self):
'''Parse PATH_INFO.'''
Expand Down Expand Up @@ -104,7 +106,8 @@ def do_motd(self):

def do_ping(self):
self.header("text/plain; charset=UTF-8")
self.stdout.write("PONG\n" + self.environ["REMOTE_ADDR"] + "\n")
remote_addr = get_http_remote_addr(self.environ)
self.stdout.write("PONG\n" + remote_addr + "\n")

def do_node(self):
nodes = NodeList()
Expand All @@ -116,7 +119,7 @@ def do_node(self):
self.stdout.write("%s\n" % inode)

def get_remote_hostname(self, host):
remote_addr = self.environ['REMOTE_ADDR']
remote_addr = get_http_remote_addr(self.environ)
if host == '':
return remote_addr
ipaddr = socket.gethostbyname(host)
Expand Down Expand Up @@ -299,9 +302,13 @@ def do_update(self, path_info):
queue.append(datfile, stamp, id, node)
queue.start()
return True

def do_version(self):
self.header("text/plain; charset=UTF-8")
self.stdout.write("{}".format(config._get_version()) + "\n")

def _seem_valid_relay_node(self, host, node, datfile):
remote_addr = self.environ['REMOTE_ADDR']
remote_addr = get_http_remote_addr(self.environ)
if host == remote_addr:
return True
ipaddr = socket.gethostbyname(host)
Expand Down
11 changes: 11 additions & 0 deletions shingetsu/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import hashlib
import os.path
import sys
from . import config

__all__ = ['md5digest', 'fsdiff', 'opentext']

Expand Down Expand Up @@ -73,3 +74,13 @@ def opentext(path, mode='r'):
return open(path, mode,
encoding='utf-8', errors='replace',
newline=newline)

def get_http_remote_addr(env):
if not config.use_x_forwarded_for:
return env['REMOTE_ADDR']
else:
if 'HTTP_X_FORWARDED_FOR' in env:
return env['HTTP_X_FORWARDED_FOR']
elif 'REMOTE_ADDR' in env:
return env['REMOTE_ADDR']
return None

0 comments on commit 1e0c208

Please sign in to comment.