Commit 7a3ef527 authored by ibuler's avatar ibuler

[Update] Using gateway

parent c0a47c33
...@@ -114,7 +114,7 @@ class SSHInterface(paramiko.ServerInterface): ...@@ -114,7 +114,7 @@ class SSHInterface(paramiko.ServerInterface):
def check_channel_forward_agent_request(self, channel): def check_channel_forward_agent_request(self, channel):
logger.debug("Check channel forward agent request: %s" % channel) logger.debug("Check channel forward agent request: %s" % channel)
self.request.type = "forward-agent" self.request.type = "forward-agent"
self.request.meta = {'channel': channel} self.request.meta['channel'] = channel.get_id()
self.event.set() self.event.set()
return False return False
......
...@@ -6,6 +6,8 @@ import socket ...@@ -6,6 +6,8 @@ import socket
import threading import threading
import time import time
import weakref import weakref
import os
import paramiko import paramiko
from paramiko.ssh_exception import SSHException from paramiko.ssh_exception import SSHException
...@@ -16,7 +18,7 @@ from .utils import wrap_with_line_feed as wr, wrap_with_warning as warning, \ ...@@ -16,7 +18,7 @@ from .utils import wrap_with_line_feed as wr, wrap_with_warning as warning, \
logger = get_logger(__file__) logger = get_logger(__file__)
TIMEOUT = 8 TIMEOUT = 10
BUF_SIZE = 4096 BUF_SIZE = 4096
...@@ -84,17 +86,45 @@ class ProxyServer: ...@@ -84,17 +86,45 @@ class ProxyServer:
def get_telnet_server_conn(self, asset, system_user): def get_telnet_server_conn(self, asset, system_user):
pass pass
def make_proxy_command(self, asset):
gateway = asset.domain.random_gateway()
proxy_command = [
"ssh", "-p", str(gateway.port),
"{}@{}".format(gateway.username, gateway.ip),
"-W", "{}:{}".format(asset.ip, asset.port), "-q",
]
if gateway.password:
proxy_command.insert(0, "sshpass -p {}".format(gateway.password))
if gateway.private_key:
gateway.set_key_dir(os.path.join(self.app.root_path, 'keys'))
proxy_command.append("-i {}".format(gateway.private_key_file))
proxy_command = ' '.join(proxy_command)
return proxy_command
def get_ssh_server_conn(self, asset, system_user): def get_ssh_server_conn(self, asset, system_user):
ssh = paramiko.SSHClient() ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
sock = None
if asset.domain:
asset.domain = self.app.service.get_domain_detail_with_gateway(asset.domain)
try:
proxy_command = self.make_proxy_command(asset)
sock = paramiko.ProxyCommand(proxy_command)
except (paramiko.AuthenticationException,
paramiko.BadAuthenticationType, SSHException,
TimeoutError) as e:
logger.error(e)
try: try:
ssh.connect( ssh.connect(
asset.ip, port=asset.port, username=system_user.username, asset.ip, port=asset.port, username=system_user.username,
password=system_user.password, pkey=system_user.private_key, password=system_user.password, pkey=system_user.private_key,
timeout=TIMEOUT, compress=True, auth_timeout=10, timeout=TIMEOUT, compress=True, auth_timeout=TIMEOUT,
look_for_keys=False look_for_keys=False, sock=sock
) )
except (paramiko.AuthenticationException, paramiko.BadAuthenticationType, SSHException, TimeoutError): except (paramiko.AuthenticationException, paramiko.BadAuthenticationType, SSHException):
admins = self.app.config['ADMINS'] or 'administrator' admins = self.app.config['ADMINS'] or 'administrator'
self.client.send(warning(wr( self.client.send(warning(wr(
"Authenticate with server failed, contact {}".format(admins), "Authenticate with server failed, contact {}".format(admins),
...@@ -112,7 +142,7 @@ class ProxyServer: ...@@ -112,7 +142,7 @@ class ProxyServer:
password_short, key_fingerprint, password_short, key_fingerprint,
)) ))
return None return None
except socket.error as e: except (socket.error, TimeoutError) as e:
self.client.send(wr(" {}".format(e))) self.client.send(wr(" {}".format(e)))
return None return None
finally: finally:
......
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