[~] Refactor

This commit is contained in:
Siarhei Siniak 2021-11-07 02:00:39 +03:00
parent 010a1e7f4b
commit d7084d3c96

@ -20,46 +20,46 @@ def forward(config_dir, config):
config_dir, config_dir,
config['ssh_key'] config['ssh_key']
) )
ssh_command = 'ssh -o "ExitOnForwardFailure yes" -o "StrictHostKeyChecking no"' ssh_command = [
'ssh',
'-o',
'ExitOnForwardFailure yes',
'-o',
'StrictHostKeyChecking no'
]
ports = dict( ports = dict(
target=r''' target=[
%s -i %s \ *ssh_command,
%s@%s \ '-i', ssh_key,
%s \ '%s@%s' % (username, server_address),
-N; *sum([
''' % ( [
ssh_command, '-R', '0.0.0.0:%d:%s:%d' % (
ssh_key,
username,
server_address,
' '.join([
'-R 0.0.0.0:%d:%s:%d' % (
pair[0], pair[0],
target_address, target_address,
pair[1], pair[1],
) )
]
for pair in target_ports for pair in target_ports
]), ], []),
), '-N'
blank=r''' ],
%s -i %s \ blank=[
%s@%s \ *ssh_command,
%s \ '-i', ssh_key,
-N; '%s@%s' % (username, server_address),
''' % ( *sum([
ssh_command, [
ssh_key, '-R', '0.0.0.0:%d:%s' % (
username,
server_address,
' '.join([
'-R 0.0.0.0:%d:%s' % (
pair[0], pair[0],
blank_endpoint, blank_endpoint,
) )
]
for pair in target_ports for pair in target_ports
]), ], []),
) '-N'
]
) )
has_server = lambda : subprocess.call([ has_server = lambda : subprocess.call([
@ -72,21 +72,21 @@ def forward(config_dir, config):
while True: while True:
notify('started') notify('started')
if has_server(): if has_server():
t6 = ports['target'] t6 = 'target'
notify('has_server') notify('has_server')
else: else:
t6 = ports['blank'] t6 = 'blank'
notify('blank_app') notify('blank_app')
t2 = t6 t2 = ports[t6]
with subprocess.Popen(t2, shell=True) as p: with subprocess.Popen(t2) as p:
try: try:
while True: while True:
time.sleep(10) time.sleep(10)
t3 = has_server() t3 = has_server()
t4 = None t4 = None
if t6 == ports['target'] and not t3: if t6 == 'target' and not t3:
t4 = 'no server' t4 = 'no server'
elif t6 == ports['blank'] and t3: elif t6 == 'blank' and t3:
t4 = 'server found' t4 = 'server found'
if not t4 is None: if not t4 is None:
notify(t4) notify(t4)