[~] Refactor
This commit is contained in:
parent
64ce890d44
commit
d059f0c25d
@ -1,4 +1,5 @@
|
|||||||
import json
|
import json
|
||||||
|
import os
|
||||||
import io
|
import io
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
@ -30,12 +31,12 @@ def forward(
|
|||||||
sections = dict()
|
sections = dict()
|
||||||
|
|
||||||
for entry in forward_nginx:
|
for entry in forward_nginx:
|
||||||
location = None
|
location_path = None
|
||||||
|
|
||||||
if entry['app_name'] != '':
|
if entry['app_name'] != '':
|
||||||
location = '/%s/' % entry['app_name']
|
location_path = '/%s/' % entry['app_name']
|
||||||
else:
|
else:
|
||||||
location = '/'
|
location_path = '/'
|
||||||
|
|
||||||
if 'server_name' in entry:
|
if 'server_name' in entry:
|
||||||
server_name = entry['server_name']
|
server_name = entry['server_name']
|
||||||
@ -45,8 +46,29 @@ def forward(
|
|||||||
if not server_name in sections:
|
if not server_name in sections:
|
||||||
sections[server_name] = []
|
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:
|
if 'target_endpoint' in entry:
|
||||||
section_body = r'''
|
location_body_get = lambda target_endpoint: \
|
||||||
|
r'''
|
||||||
proxy_set_header Host $http_host;
|
proxy_set_header Host $http_host;
|
||||||
proxy_set_header X-Forwarded-For $t1;
|
proxy_set_header X-Forwarded-For $t1;
|
||||||
proxy_set_header X-Forwarded-Proto $scheme;
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
@ -56,27 +78,52 @@ def forward(
|
|||||||
proxy_buffering off;
|
proxy_buffering off;
|
||||||
proxy_pass {target_endpoint};
|
proxy_pass {target_endpoint};
|
||||||
'''.replace(
|
'''.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:
|
elif 'redirect_url' in entry:
|
||||||
section_body = r'''
|
sections[server_name].append(
|
||||||
|
location_get(
|
||||||
|
r'''
|
||||||
return 302 {redirect_url}$request_uri;
|
return 302 {redirect_url}$request_uri;
|
||||||
'''.replace(
|
'''.replace(
|
||||||
'{redirect_url}', entry['redirect_url'],
|
'{redirect_url}', entry['redirect_url'],
|
||||||
|
),
|
||||||
|
location_path,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
sections[server_name].append(r'''
|
|
||||||
location ^~ {location} {
|
|
||||||
{section_body}
|
|
||||||
}
|
|
||||||
'''.replace(
|
|
||||||
'{section_body}', section_body,
|
|
||||||
).replace(
|
|
||||||
'{location}', location,
|
|
||||||
))
|
|
||||||
|
|
||||||
servers = []
|
servers = []
|
||||||
|
|
||||||
for server_name, current_sections in sections.items():
|
for server_name, current_sections in sections.items():
|
||||||
|
@ -2883,10 +2883,16 @@ def share_wifi(argv):
|
|||||||
last_timestamp = datetime.datetime.now()
|
last_timestamp = datetime.datetime.now()
|
||||||
hostapd = None
|
hostapd = None
|
||||||
restart = False
|
restart = False
|
||||||
|
shutdown = False
|
||||||
|
|
||||||
def on_interrupt(*args, **kwargs):
|
def on_interrupt(sig, *args, **kwargs):
|
||||||
nonlocal restart
|
nonlocal restart
|
||||||
|
if sig == signal.SIGINT:
|
||||||
restart = True
|
restart = True
|
||||||
|
elif sig == signal.SIGTERM:
|
||||||
|
shutdown = True
|
||||||
|
else:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
signal.signal(
|
signal.signal(
|
||||||
signal.SIGINT,
|
signal.SIGINT,
|
||||||
@ -2906,7 +2912,7 @@ def share_wifi(argv):
|
|||||||
pw,
|
pw,
|
||||||
]
|
]
|
||||||
if not options.channel is None:
|
if not options.channel is None:
|
||||||
hostapd_args.extend(['-c', '%d' % option.channell])
|
hostapd_args.extend(['-c', '%d' % options.channel])
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
@ -2914,7 +2920,7 @@ def share_wifi(argv):
|
|||||||
print('\n%s, start new' % last_timestamp)
|
print('\n%s, start new' % last_timestamp)
|
||||||
hostapd = subprocess.Popen(hostapd_args)
|
hostapd = subprocess.Popen(hostapd_args)
|
||||||
else:
|
else:
|
||||||
if restart:
|
if restart or shutdown:
|
||||||
print('\n%s, shutdown current' % last_timestamp)
|
print('\n%s, shutdown current' % last_timestamp)
|
||||||
os.kill(
|
os.kill(
|
||||||
hostapd.pid,
|
hostapd.pid,
|
||||||
@ -2929,6 +2935,9 @@ def share_wifi(argv):
|
|||||||
if not hostapd.poll() is None:
|
if not hostapd.poll() is None:
|
||||||
hostapd = None
|
hostapd = None
|
||||||
|
|
||||||
|
if shutdown:
|
||||||
|
break
|
||||||
|
|
||||||
if (
|
if (
|
||||||
datetime.datetime.now() - last_timestamp
|
datetime.datetime.now() - last_timestamp
|
||||||
).total_seconds() > options.restart_delay:
|
).total_seconds() > options.restart_delay:
|
||||||
|
Loading…
Reference in New Issue
Block a user