Commit 3f3d061d by Sheng

Refactored

parent 75361d87
...@@ -202,9 +202,11 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler): ...@@ -202,9 +202,11 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler):
ssh._system_host_keys = self.settings['system_host_keys'] ssh._system_host_keys = self.settings['system_host_keys']
ssh._host_keys = self.settings['host_keys'] ssh._host_keys = self.settings['host_keys']
ssh.set_missing_host_key_policy(self.settings['policy']) ssh.set_missing_host_key_policy(self.settings['policy'])
args = self.get_args() args = self.get_args()
dst_addr = (args[0], args[1]) dst_addr = (args[0], args[1])
logging.info('Connecting to {}:{}'.format(*dst_addr)) logging.info('Connecting to {}:{}'.format(*dst_addr))
try: try:
ssh.connect(*args, timeout=6) ssh.connect(*args, timeout=6)
except socket.error: except socket.error:
...@@ -213,6 +215,7 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler): ...@@ -213,6 +215,7 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler):
raise ValueError('Authentication failed.') raise ValueError('Authentication failed.')
except paramiko.BadHostKeyException: except paramiko.BadHostKeyException:
raise ValueError('Bad host key.') raise ValueError('Bad host key.')
chan = ssh.invoke_shell(term='xterm') chan = ssh.invoke_shell(term='xterm')
chan.setblocking(0) chan.setblocking(0)
worker = Worker(ssh, chan, dst_addr) worker = Worker(ssh, chan, dst_addr)
...@@ -313,8 +316,7 @@ def get_policy_class(policy): ...@@ -313,8 +316,7 @@ def get_policy_class(policy):
return cls return cls
def main(): def get_application_settings():
parse_command_line()
base_dir = os.path.dirname(__file__) base_dir = os.path.dirname(__file__)
filename = os.path.join(base_dir, 'known_hosts') filename = os.path.join(base_dir, 'known_hosts')
host_keys = get_host_keys(filename) host_keys = get_host_keys(filename)
...@@ -332,25 +334,29 @@ def main(): ...@@ -332,25 +334,29 @@ def main():
if not host_keys and not system_host_keys: if not host_keys and not system_host_keys:
raise ValueError('Empty known_hosts with reject policy?') raise ValueError('Empty known_hosts with reject policy?')
settings = { settings = dict(
'template_path': os.path.join(base_dir, 'templates'), template_path=os.path.join(base_dir, 'templates'),
'static_path': os.path.join(base_dir, 'static'), static_path=os.path.join(base_dir, 'static'),
'cookie_secret': uuid.uuid4().hex, cookie_secret=uuid.uuid4().hex,
'xsrf_cookies': True xsrf_cookies=True,
} host_keys=host_keys,
system_host_keys=system_host_keys,
policy=policy_class(),
debug=options.debug
)
return settings
def main():
parse_command_line()
settings = get_application_settings()
handlers = [ handlers = [
(r'/', IndexHandler), (r'/', IndexHandler),
(r'/ws', WsockHandler) (r'/ws', WsockHandler)
] ]
settings.update(
debug=options.debug,
host_keys=host_keys,
system_host_keys=system_host_keys,
policy=policy_class()
)
app = tornado.web.Application(handlers, **settings) app = tornado.web.Application(handlers, **settings)
app.listen(options.port, options.address) app.listen(options.port, options.address)
logging.info('Listening on {}:{}'.format(options.address, options.port)) logging.info('Listening on {}:{}'.format(options.address, options.port))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment