2019-07-10 17:34:45 +08:00
|
|
|
import os,sys
|
|
|
|
import ssl
|
2021-05-17 10:58:51 +08:00
|
|
|
from socket import *
|
2019-07-10 17:34:45 +08:00
|
|
|
from aiohttp import web
|
|
|
|
|
|
|
|
from appPublic.folderUtils import ProgramPath
|
2021-05-17 10:58:51 +08:00
|
|
|
from appPublic.background import Background
|
2019-07-10 17:34:45 +08:00
|
|
|
from appPublic.jsonConfig import getConfig
|
|
|
|
|
|
|
|
from sqlor.dbpools import DBPools
|
|
|
|
|
|
|
|
from .processorResource import ProcessorResource
|
|
|
|
from .auth_api import AuthAPI
|
|
|
|
from .myTE import setupTemplateEngine
|
|
|
|
from .globalEnv import initEnv
|
2021-05-17 10:58:51 +08:00
|
|
|
from natpmp import NATPMP as pmp
|
2019-07-10 17:34:45 +08:00
|
|
|
|
|
|
|
class ConfiguredServer:
|
2019-07-10 18:42:54 +08:00
|
|
|
def __init__(self,auth_klass=AuthAPI):
|
2019-07-10 17:34:45 +08:00
|
|
|
pp = ProgramPath()
|
2021-05-17 10:58:51 +08:00
|
|
|
self.natpmp_loop = True
|
|
|
|
self.nat_heartbeat = False
|
2019-07-10 17:34:45 +08:00
|
|
|
workdir = pp
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
workdir = sys.argv[1]
|
|
|
|
config = getConfig(workdir,{'workdir':workdir,'ProgramPath':pp})
|
|
|
|
if config.databases:
|
|
|
|
DBPools(config.databases)
|
|
|
|
initEnv()
|
|
|
|
setupTemplateEngine()
|
|
|
|
self.app = web.Application()
|
2019-07-10 18:42:54 +08:00
|
|
|
auth = auth_klass()
|
2019-07-10 17:34:45 +08:00
|
|
|
auth.setupAuth(self.app)
|
|
|
|
self.configPath(config)
|
|
|
|
|
2021-05-17 10:58:51 +08:00
|
|
|
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()
|
|
|
|
print('gateway=', gateway)
|
|
|
|
try:
|
|
|
|
x = pmp.map_port(pmp.NATPMP_PROTOCOL_TCP,
|
|
|
|
config.natpmp.public_port, config.website.port,
|
|
|
|
t, gateway_ip=gateway)
|
|
|
|
print('gateway=', gateway, 'map_port()=', x)
|
|
|
|
except Exception as e:
|
|
|
|
print('mat_pmp():Exception:',e)
|
|
|
|
time.sleep(t - 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-07-10 17:34:45 +08:00
|
|
|
def run(self):
|
|
|
|
config = getConfig()
|
|
|
|
ssl_context = None
|
|
|
|
if config.website.ssl:
|
|
|
|
ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
|
|
|
|
ssl_context.load_cert_chain(config.website.ssl.crtfile,
|
|
|
|
config.website.ssl.keyfile)
|
2021-05-17 10:58:51 +08:00
|
|
|
if 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()
|
2019-07-10 17:34:45 +08:00
|
|
|
web.run_app(self.app,host=config.website.host or '0.0.0.0',
|
|
|
|
port=config.website.port or 8080,
|
|
|
|
ssl_context=ssl_context)
|
|
|
|
|
|
|
|
def configPath(self,config):
|
|
|
|
for p,prefix in config.website.paths:
|
|
|
|
res = ProcessorResource(prefix,p,show_index=True,
|
2019-12-03 11:30:15 +08:00
|
|
|
follow_symlinks=True,
|
2019-12-05 06:04:38 +08:00
|
|
|
indexes=config.website.indexes,
|
|
|
|
processors=config.website.processors)
|
2019-07-10 17:34:45 +08:00
|
|
|
self.app.router.register_resource(res)
|
|
|
|
|