ahserver_g/ahserver/configuredServer.py

102 lines
2.8 KiB
Python
Raw Normal View History

2019-07-10 17:34:45 +08:00
import os,sys
2021-06-24 11:01:37 +08:00
import time
2019-07-10 17:34:45 +08:00
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
2022-05-20 20:03:54 +08:00
from appPublic.i18n import getI18N
2019-07-10 17:34:45 +08:00
from sqlor.dbpools import DBPools
from .processorResource import ProcessorResource
from .auth_api import AuthAPI
from .myTE import setupTemplateEngine
from .globalEnv import initEnv
2021-06-26 21:57:12 +08:00
try:
from natpmp import NATPMP as pmp
except:
pmp = None
2019-07-10 17:34:45 +08:00
class ConfiguredServer:
2022-05-20 17:53:06 +08:00
def __init__(self, auth_klass=AuthAPI, workdir=None):
pp = ProgramPath()
2022-04-07 15:02:41 +08:00
if workdir is None:
self.natpmp_loop = True
self.nat_heartbeat = False
workdir = pp
if len(sys.argv) > 1:
workdir = sys.argv[1]
2019-07-10 17:34:45 +08:00
config = getConfig(workdir,{'workdir':workdir,'ProgramPath':pp})
2022-05-20 20:03:54 +08:00
i18n = getI18N(path=workdir)
2019-07-10 17:34:45 +08:00
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-06-26 21:57:12 +08:00
if pmp and config.natpmp:
2021-05-17 10:58:51 +08:00
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)