This commit is contained in:
Oleksii Shevchuk 2020-03-30 22:15:23 +03:00
commit 6494b8fb17
2 changed files with 52 additions and 20 deletions

View File

@ -16,25 +16,34 @@ def parser(server, handler, config):
return pupygen.get_parser(PupyArgumentParser, config=config)
def do(server, handler, config, args):
if not args.launcher or (args.launcher and args.launcher in ('connect', 'auto_proxy')):
args.launcher = args.launcher or 'connect'
transport = None
transport_idx = None
host = None
host_idx = None
port = None
preferred_ok = True
need_transport = False
need_hostport = False
handler.display(Info("Raw user arguments given for generation: {0}".format(str(args.launcher_args))))
if not args.launcher:
handler.display(Info("Launcher/connection method not given. It is set to 'connect' now"))
args.launcher = 'connect'
#launcher method 'connect' or 'auto_proxy'
if args.launcher and args.launcher in ('connect', 'auto_proxy'):
transport = None #For saving the transport method (default or given by user)
transport_idx = None
host = None #Host for listening point (not for launcher args)
port = None #Port for listening point (not for launcher args)
host_idx = None #For saving host:port from user args (if given by user)
preferred_ok = True
need_transport = False #For appending transport method in launcher args
need_hostport = False #For appending host & port for connection back in launcher args
if args.launcher_args:
#Some arguments are given in command line, saving host&port and transport method
total = len(args.launcher_args)
for idx,arg in enumerate(args.launcher_args):
if arg == '-t' and idx < total-1:
#Manage Transport
transport = args.launcher_args[idx+1]
transport_idx = idx+1
handler.display(Info(
"Launcher configuration: Transport for connection back will be set to {0}".format(
repr(transport))))
elif arg == '--host' and idx<total-1:
#Manage host & port for connection back
host_idx = idx+1
hostport = args.launcher_args[host_idx]
if ':' in hostport:
@ -45,21 +54,26 @@ def do(server, handler, config, args):
port = int(hostport)
except:
host = hostport
handler.display(Info(
"Launcher configuration: Host & port for connection back will be set to {0}:{1}".format(
host,port)))
need_transport = not bool(transport)
need_hostport = not all([host, port])
#If host, port or transport are missing
if not all([host, port, transport]):
default_listener = None
preferred_ok = False
if transport:
#Transport method is given, get the listener
default_listener = server.listeners.get(transport)
if not default_listener:
handler.display(Error(
'Requested transport {} is not active. Will use default'.format(
transport)))
#We need to save the transport method for the launcher
need_transport = True
if not default_listener:
@ -69,34 +83,38 @@ def do(server, handler, config, args):
pass
if default_listener:
#We have a listener, we can set host & port
transport = default_listener.name
handler.display(Info(
'Connection point: Transport={} Address={}:{}'.format(
'This local listening point will be used: Transport={} Address={}:{}'.format(
default_listener.name, default_listener.external,
default_listener.external_port)))
if host or port:
handler.display(Warn('Host and port will be ignored'))
handler.display(Warn('Host and port {0}:{1} are ignored for getting the valid local listening point but'.format(host, port)))
handler.display(Warn('they are kept for configuring the launcher for connection back'))
if args.prefer_external != default_listener.local:
host = default_listener.external
port = default_listener.external_port
preferred_ok = True
handler.display(Info("Host & port for listening point are set to: {0}:{1}".format(host,port)))
elif not args.prefer_external and not default_listener.local:
host = get_listener_ip(cache=False)
if host:
handler.display(Warn('Using {} as local IP'.format(host)))
port = default_listener.port
if host:
handler.display(Warn('Using {0}:{1} as local IP:PORT for the local listening point'.format(host, port)))
preferred_ok = True
else:
preferred_ok = not (default_listener.local and args.prefer_external)
#If transport is missing
if not transport:
handler.display(Error('No active transports. Explicitly choose one'))
handler.display(Error('No active transport method. You have to explicitly choose one. Impossible to continue.'))
return
#If host or port is missing or preferred_ok
if not all([host, port, preferred_ok]):
maybe_port = get_listener_port(config, external=args.prefer_external)
maybe_host, local = get_listener_ip_with_local(
@ -105,26 +123,34 @@ def do(server, handler, config, args):
)
if (not local and args.prefer_external) or not (host and port):
handler.display(Warn('Using configured/discovered external HOST:PORT'))
handler.display(Warn('Using configured/discovered: {0}:{1}'.format(maybe_host, maybe_port)))
host = maybe_host
port = maybe_port
else:
handler.display(Warn('Unable to find external HOST:PORT'))
#If need a transport method because not given by user for launcher
if need_transport:
if transport_idx is None:
args.launcher_args += ['-t', transport]
else:
args.launcher_args[transport_idx] = transport
#Transport method not given by user. Consequently,
handler.display(Info("Transport method {0} appended to launcher args".format(repr(transport))))
#If host and port are not given/find for connection back
if need_hostport:
hostport = '{}:{}'.format(host, port)
if host_idx is None:
args.launcher_args += ['--host', hostport]
else:
args.launcher_args[host_idx] = hostport
#Host & port method not given by user. Consequently,
handler.display(Info("Host & port {0} appended to launcher args".format(repr(hostport))))
#Enable HTTPD if required
if server.httpd:
handler.display(Info("HTTPD enabled"))
wwwroot = config.get_folder('wwwroot')
if not args.output_dir:
args.output_dir = wwwroot

View File

@ -167,7 +167,13 @@ class RemoteDesktopModule(PupyModule):
port, path = conninfo
self.success("Web handler started on http://127.0.0.1:%d%s"%(port, path))
self.info("By default, web handler accepts connections from localhost only")
self.info("Use the following pupy command for allowing another ip address to connect to web handler:")
self.info("'config set webserver local_ips X.Y.Z.A'")
if args.view:
config = self.client.pupsrv.config
viewer = config.get('default_viewers', 'browser')
subprocess.Popen([viewer, path])
try:
subprocess.Popen([viewer, path])
except Exception as e:
self.error("Impossible to execute '{0} {1}': {2}".format(viewer, path, str(e)))