This commit is contained in:
yumoqing 2022-06-23 15:33:34 +08:00
parent 14da6d0f98
commit 2db2b17a86

View File

@ -16,22 +16,15 @@ from .processorResource import ProcessorResource
from .auth_api import AuthAPI
from .myTE import setupTemplateEngine
from .globalEnv import initEnv
try:
from natpmp import NATPMP as pmp
except:
pmp = None
class ConfiguredServer(AppLogger):
def __init__(self, auth_klass=AuthAPI, workdir=None):
super().__init__()
pp = ProgramPath()
if workdir is None:
self.natpmp_loop = True
self.nat_heartbeat = False
workdir = pp
if len(sys.argv) > 1:
workdir = sys.argv[1]
config = getConfig(workdir,{'workdir':workdir,'ProgramPath':pp})
if workdir is not None:
pp = ProgramPath()
config = getConfig(workdir,{'workdir':workdir,'ProgramPath':pp})
else:
config = getConfig()
i18n = getI18N(path=workdir)
if config.databases:
DBPools(config.databases)
@ -42,34 +35,6 @@ class ConfiguredServer(AppLogger):
auth.setupAuth(self.app)
self.configPath(config)
def natpmp_heartbeat(self):
config = getConfig()
udpCliSock = socket(AF_INET,SOCK_DGRAM)
msg = f'{config.natpmp.appname}:{config.natpmp.nodename}'.encode('utf-8')
t = config.natpmp.heartbeat_period or 60
addr = (gethostbyname(config.natpmp.natserver), config.natpmp.natport)
while self.nat_heartbeat:
udpCliSock.sendto(msg, addr)
udpCliSock.recv(1024)
time.sleep(t)
def nat_pmp(self):
config = getConfig()
t = config.natpmp.portmap_period or 3600
while self.natpmp_loop:
gateway = pmp.get_gateway_addr()
self.debug('gateway=%s' % gateway)
try:
x = pmp.map_port(pmp.NATPMP_PROTOCOL_TCP,
config.natpmp.public_port, config.website.port,
t, gateway_ip=gateway)
self.debug('gateway=%s map_port=%s' %( gateway, x))
except Exception as e:
self.debug('mat_pmp():Exception:%s' % e)
time.sleep(t - 1)
def run(self):
config = getConfig()
ssl_context = None
@ -77,18 +42,7 @@ class ConfiguredServer(AppLogger):
ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ssl_context.load_cert_chain(config.website.ssl.crtfile,
config.website.ssl.keyfile)
if pmp and config.natpmp:
self.nat_heartbeat = True
b = Background(self.natpmp_heartbeat)
b.start()
if not config.natpmp:
self.natpmp_loop = False
elif config.natpmp.hardmap:
self.natpmp_loop = False
else:
b = Background(self.nat_pmp)
b.start()
web.run_app(self.app,host=config.website.host or '0.0.0.0',
port=config.website.port or 8080,
ssl_context=ssl_context)