[~] Refactor
This commit is contained in:
parent
2339d03038
commit
010a1e7f4b
187
d1/forward.py
187
d1/forward.py
@ -1,4 +1,5 @@
|
|||||||
import subprocess
|
import subprocess
|
||||||
|
import threading
|
||||||
import io
|
import io
|
||||||
import json
|
import json
|
||||||
import sys
|
import sys
|
||||||
@ -8,86 +9,114 @@ import traceback
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
|
|
||||||
with io.open(sys.argv[1], 'r') as f:
|
def forward(config_dir, config):
|
||||||
config = json.load(f)
|
server_address = config['server_address']
|
||||||
|
username = config['username']
|
||||||
server_address = config['server_address']
|
target_address = config['target_address']
|
||||||
username = config['username']
|
blank_endpoint = config['blank_endpoint']
|
||||||
target_address = config['target_address']
|
target_ports = config['target_ports']
|
||||||
blank_endpoint = config['blank_endpoint']
|
app_name = config['app_name']
|
||||||
target_ports = config['target_ports']
|
ssh_key = os.path.join(
|
||||||
|
config_dir,
|
||||||
ports = dict(
|
config['ssh_key']
|
||||||
target=r'''
|
|
||||||
ssh \
|
|
||||||
%s@%s \
|
|
||||||
%s \
|
|
||||||
-v -N;
|
|
||||||
''' % (
|
|
||||||
username,
|
|
||||||
server_address,
|
|
||||||
' '.join([
|
|
||||||
'-R 0.0.0.0:%d:%s:%d' % (
|
|
||||||
pair[0],
|
|
||||||
target_address,
|
|
||||||
pair[1],
|
|
||||||
)
|
|
||||||
for pair in target_ports
|
|
||||||
]),
|
|
||||||
),
|
|
||||||
blank=r'''
|
|
||||||
ssh \
|
|
||||||
%s@%s \
|
|
||||||
%s \
|
|
||||||
-v -N;
|
|
||||||
''' % (
|
|
||||||
username,
|
|
||||||
server_address,
|
|
||||||
' '.join([
|
|
||||||
'-R 0.0.0.0:%d:%s' % (
|
|
||||||
pair[0],
|
|
||||||
blank_endpoint,
|
|
||||||
)
|
|
||||||
for pair in target_ports
|
|
||||||
]),
|
|
||||||
)
|
)
|
||||||
)
|
ssh_command = 'ssh -o "ExitOnForwardFailure yes" -o "StrictHostKeyChecking no"'
|
||||||
|
|
||||||
app_name = config['app_name']
|
ports = dict(
|
||||||
has_server = lambda : subprocess.call([
|
target=r'''
|
||||||
'ping','-c', '1', target_address,
|
%s -i %s \
|
||||||
]) == 0
|
%s@%s \
|
||||||
|
%s \
|
||||||
|
-N;
|
||||||
|
''' % (
|
||||||
|
ssh_command,
|
||||||
|
ssh_key,
|
||||||
|
username,
|
||||||
|
server_address,
|
||||||
|
' '.join([
|
||||||
|
'-R 0.0.0.0:%d:%s:%d' % (
|
||||||
|
pair[0],
|
||||||
|
target_address,
|
||||||
|
pair[1],
|
||||||
|
)
|
||||||
|
for pair in target_ports
|
||||||
|
]),
|
||||||
|
),
|
||||||
|
blank=r'''
|
||||||
|
%s -i %s \
|
||||||
|
%s@%s \
|
||||||
|
%s \
|
||||||
|
-N;
|
||||||
|
''' % (
|
||||||
|
ssh_command,
|
||||||
|
ssh_key,
|
||||||
|
username,
|
||||||
|
server_address,
|
||||||
|
' '.join([
|
||||||
|
'-R 0.0.0.0:%d:%s' % (
|
||||||
|
pair[0],
|
||||||
|
blank_endpoint,
|
||||||
|
)
|
||||||
|
for pair in target_ports
|
||||||
|
]),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
notify = lambda msg : subprocess.check_output(['notify-send', '-t', '5000', app_name, msg])
|
has_server = lambda : subprocess.call([
|
||||||
|
'ping','-c', '1', target_address,
|
||||||
|
], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) == 0
|
||||||
|
|
||||||
while True:
|
#notify = lambda msg : subprocess.check_output(['notify-send', '-t', '5000', app_name, msg])
|
||||||
notify('started')
|
notify = lambda msg: subprocess.call(['echo', app_name, msg])
|
||||||
if has_server():
|
|
||||||
t6 = ports['target']
|
while True:
|
||||||
notify('has_server')
|
notify('started')
|
||||||
else:
|
if has_server():
|
||||||
t6 = ports['blank']
|
t6 = ports['target']
|
||||||
notify('blank_app')
|
notify('has_server')
|
||||||
t2 = t6
|
else:
|
||||||
with subprocess.Popen(t2, shell=True) as p:
|
t6 = ports['blank']
|
||||||
try:
|
notify('blank_app')
|
||||||
while True:
|
t2 = t6
|
||||||
time.sleep(10)
|
with subprocess.Popen(t2, shell=True) as p:
|
||||||
t3 = has_server()
|
try:
|
||||||
t4 = None
|
while True:
|
||||||
if t6 == ports['target'] and not t3:
|
time.sleep(10)
|
||||||
t4 = 'no server'
|
t3 = has_server()
|
||||||
elif t6 == ports['blank'] and t3:
|
t4 = None
|
||||||
t4 = 'server found'
|
if t6 == ports['target'] and not t3:
|
||||||
if not t4 is None:
|
t4 = 'no server'
|
||||||
notify(t4)
|
elif t6 == ports['blank'] and t3:
|
||||||
raise RuntimeError(t4)
|
t4 = 'server found'
|
||||||
assert p.poll() is None
|
if not t4 is None:
|
||||||
except KeyboardInterrupt:
|
notify(t4)
|
||||||
break
|
raise RuntimeError(t4)
|
||||||
except:
|
assert p.poll() is None
|
||||||
pprint.pprint(traceback.format_exc())
|
except KeyboardInterrupt:
|
||||||
continue
|
break
|
||||||
finally:
|
except:
|
||||||
p.terminate()
|
pprint.pprint(traceback.format_exc())
|
||||||
notify('stopped')
|
continue
|
||||||
|
finally:
|
||||||
|
p.terminate()
|
||||||
|
notify('stopped')
|
||||||
|
|
||||||
|
config_path = sys.argv[1]
|
||||||
|
config_dir = os.path.split(
|
||||||
|
config_path
|
||||||
|
)[0]
|
||||||
|
|
||||||
|
with io.open(config_path, 'r') as f:
|
||||||
|
configs = json.load(f)
|
||||||
|
|
||||||
|
t1 = []
|
||||||
|
for config in configs:
|
||||||
|
t2 = threading.Thread(
|
||||||
|
target=forward,
|
||||||
|
args=[config_dir, config],
|
||||||
|
)
|
||||||
|
t2.start()
|
||||||
|
t1.append(t2)
|
||||||
|
|
||||||
|
for o in t1:
|
||||||
|
o.join()
|
||||||
|
@ -4,8 +4,14 @@ services:
|
|||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
dockerfile: ./docker/blank-app/Dockerfile
|
dockerfile: ./docker/blank-app/Dockerfile
|
||||||
ports:
|
|
||||||
- 80:80
|
|
||||||
volumes:
|
volumes:
|
||||||
- .:/app:rw
|
- .:/app:rw
|
||||||
restart: always
|
restart: always
|
||||||
|
forward:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: ./docker/forward/Dockerfile
|
||||||
|
volumes:
|
||||||
|
- ./d1/forward.py:/app/d1/forward.py:ro
|
||||||
|
- ./tmp/cache/forward_data:/app/tmp/cache/forward_data:ro
|
||||||
|
restart: always
|
||||||
|
7
docker/forward/Dockerfile
Normal file
7
docker/forward/Dockerfile
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
FROM python:3
|
||||||
|
WORKDIR /app
|
||||||
|
ENV DEBIAN_FRONTEND=noninteractive
|
||||||
|
RUN apt-get update && apt-get install -y iputils-ping
|
||||||
|
ENTRYPOINT sh -c ' \
|
||||||
|
python d1/forward.py tmp/cache/forward_data/forward.config.json \
|
||||||
|
'
|
Loading…
Reference in New Issue
Block a user