[~] Refactor
This commit is contained in:
parent
64ce890d44
commit
d059f0c25d
@ -1,4 +1,5 @@
|
||||
import json
|
||||
import os
|
||||
import io
|
||||
import sys
|
||||
|
||||
@ -30,12 +31,12 @@ def forward(
|
||||
sections = dict()
|
||||
|
||||
for entry in forward_nginx:
|
||||
location = None
|
||||
location_path = None
|
||||
|
||||
if entry['app_name'] != '':
|
||||
location = '/%s/' % entry['app_name']
|
||||
location_path = '/%s/' % entry['app_name']
|
||||
else:
|
||||
location = '/'
|
||||
location_path = '/'
|
||||
|
||||
if 'server_name' in entry:
|
||||
server_name = entry['server_name']
|
||||
@ -45,8 +46,29 @@ def forward(
|
||||
if not server_name in sections:
|
||||
sections[server_name] = []
|
||||
|
||||
|
||||
location_get = lambda location_body, location_path2, prefix=None,: (
|
||||
r'''
|
||||
location {location} {
|
||||
{location_body}
|
||||
}
|
||||
'''.replace(
|
||||
'{location_body}', location_body,
|
||||
).replace(
|
||||
'{location}', '%s %s' % (
|
||||
(
|
||||
'^~'
|
||||
if prefix is None
|
||||
else prefix
|
||||
),
|
||||
location_path2
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
if 'target_endpoint' in entry:
|
||||
section_body = r'''
|
||||
location_body_get = lambda target_endpoint: \
|
||||
r'''
|
||||
proxy_set_header Host $http_host;
|
||||
proxy_set_header X-Forwarded-For $t1;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
@ -56,27 +78,52 @@ def forward(
|
||||
proxy_buffering off;
|
||||
proxy_pass {target_endpoint};
|
||||
'''.replace(
|
||||
'{target_endpoint}', entry['target_endpoint'],
|
||||
'{target_endpoint}', target_endpoint,
|
||||
)
|
||||
|
||||
if 'fallback_endpoint' in entry:
|
||||
fallback_name = '@fallback-%s' % os.urandom(10).hex()
|
||||
|
||||
sections[server_name].append(
|
||||
location_get(
|
||||
location_body_get(entry['target_endpoint']) + r'''
|
||||
proxy_intercept_errors on;
|
||||
error_page 502 {fallback_name};
|
||||
'''.replace(
|
||||
'{fallback_name}', fallback_name,
|
||||
),
|
||||
location_path,
|
||||
)
|
||||
)
|
||||
|
||||
sections[server_name].append(
|
||||
location_get(
|
||||
location_body_get(entry['fallback_endpoint']),
|
||||
fallback_name,
|
||||
'',
|
||||
)
|
||||
)
|
||||
else:
|
||||
sections[server_name].append(
|
||||
location_get(
|
||||
location_body_get(entry['target_endpoint']),
|
||||
location_path,
|
||||
)
|
||||
)
|
||||
elif 'redirect_url' in entry:
|
||||
section_body = r'''
|
||||
sections[server_name].append(
|
||||
location_get(
|
||||
r'''
|
||||
return 302 {redirect_url}$request_uri;
|
||||
'''.replace(
|
||||
'{redirect_url}', entry['redirect_url'],
|
||||
),
|
||||
location_path,
|
||||
)
|
||||
)
|
||||
else:
|
||||
raise NotImplementedError
|
||||
|
||||
sections[server_name].append(r'''
|
||||
location ^~ {location} {
|
||||
{section_body}
|
||||
}
|
||||
'''.replace(
|
||||
'{section_body}', section_body,
|
||||
).replace(
|
||||
'{location}', location,
|
||||
))
|
||||
|
||||
servers = []
|
||||
|
||||
for server_name, current_sections in sections.items():
|
||||
@ -115,12 +162,12 @@ server {
|
||||
)
|
||||
|
||||
f.write(r'''
|
||||
events {
|
||||
events {
|
||||
multi_accept on;
|
||||
worker_connections 64;
|
||||
}
|
||||
}
|
||||
|
||||
http {
|
||||
http {
|
||||
log_format main
|
||||
'[$time_local][$remote_addr:$remote_port, $http_x_forwarded_for, $t1, $http_host]'
|
||||
'[$request_length,$bytes_sent,$request_time]'
|
||||
@ -138,7 +185,7 @@ server {
|
||||
default upgrade;
|
||||
'' close;
|
||||
}
|
||||
}
|
||||
}
|
||||
'''.replace(
|
||||
'{servers_config}', '\n'.join(servers)
|
||||
))
|
||||
|
@ -2883,10 +2883,16 @@ def share_wifi(argv):
|
||||
last_timestamp = datetime.datetime.now()
|
||||
hostapd = None
|
||||
restart = False
|
||||
shutdown = False
|
||||
|
||||
def on_interrupt(*args, **kwargs):
|
||||
def on_interrupt(sig, *args, **kwargs):
|
||||
nonlocal restart
|
||||
if sig == signal.SIGINT:
|
||||
restart = True
|
||||
elif sig == signal.SIGTERM:
|
||||
shutdown = True
|
||||
else:
|
||||
raise NotImplementedError
|
||||
|
||||
signal.signal(
|
||||
signal.SIGINT,
|
||||
@ -2906,7 +2912,7 @@ def share_wifi(argv):
|
||||
pw,
|
||||
]
|
||||
if not options.channel is None:
|
||||
hostapd_args.extend(['-c', '%d' % option.channell])
|
||||
hostapd_args.extend(['-c', '%d' % options.channel])
|
||||
|
||||
while True:
|
||||
try:
|
||||
@ -2914,7 +2920,7 @@ def share_wifi(argv):
|
||||
print('\n%s, start new' % last_timestamp)
|
||||
hostapd = subprocess.Popen(hostapd_args)
|
||||
else:
|
||||
if restart:
|
||||
if restart or shutdown:
|
||||
print('\n%s, shutdown current' % last_timestamp)
|
||||
os.kill(
|
||||
hostapd.pid,
|
||||
@ -2929,6 +2935,9 @@ def share_wifi(argv):
|
||||
if not hostapd.poll() is None:
|
||||
hostapd = None
|
||||
|
||||
if shutdown:
|
||||
break
|
||||
|
||||
if (
|
||||
datetime.datetime.now() - last_timestamp
|
||||
).total_seconds() > options.restart_delay:
|
||||
|
Loading…
Reference in New Issue
Block a user