Unverified Commit 98e8df01 authored by 老广's avatar 老广 Committed by GitHub

Merge pull request #42 from jumpserver/dev

支持网域 并修复一些bug
parents f1c7d000 4cc45e03
...@@ -91,30 +91,31 @@ class SSHInterface(paramiko.ServerInterface): ...@@ -91,30 +91,31 @@ class SSHInterface(paramiko.ServerInterface):
def check_channel_direct_tcpip_request(self, chanid, origin, destination): def check_channel_direct_tcpip_request(self, chanid, origin, destination):
logger.debug("Check channel direct tcpip request: %d %s %s" % logger.debug("Check channel direct tcpip request: %d %s %s" %
(chanid, origin, destination)) (chanid, origin, destination))
self.request.type = 'direct-tcpip' self.request.type.append('direct-tcpip')
self.request.meta = { self.request.meta.update({
'chanid': chanid, 'origin': origin, 'chanid': chanid, 'origin': origin,
'destination': destination, 'destination': destination,
} })
self.event.set() self.event.set()
return 0 return 0
def check_channel_env_request(self, channel, name, value): def check_channel_env_request(self, channel, name, value):
logger.debug("Check channel env request: %s, %s, %s" % logger.debug("Check channel env request: %s, %s, %s" %
(channel, name, value)) (channel, name, value))
self.request.type.append('env')
return False return False
def check_channel_exec_request(self, channel, command): def check_channel_exec_request(self, channel, command):
logger.debug("Check channel exec request: `%s`" % command) logger.debug("Check channel exec request: `%s`" % command)
self.request.type = 'exec' self.request.type.append('exec')
self.request.meta = {'channel': channel, 'command': command} self.request.meta.update({'channel': channel.get_id(), 'command': command})
self.event.set() self.event.set()
return False return False
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.append("forward-agent")
self.request.meta = {'channel': channel} self.request.meta.update({'channel': channel.get_id()})
self.event.set() self.event.set()
return False return False
...@@ -123,12 +124,12 @@ class SSHInterface(paramiko.ServerInterface): ...@@ -123,12 +124,12 @@ class SSHInterface(paramiko.ServerInterface):
pixelwidth, pixelheight, modes): pixelwidth, pixelheight, modes):
logger.info("Check channel pty request: %s %s %s %s %s" % logger.info("Check channel pty request: %s %s %s %s %s" %
(term, width, height, pixelwidth, pixelheight)) (term, width, height, pixelwidth, pixelheight))
self.request.type = 'pty' self.request.type.append('pty')
self.request.meta = { self.request.meta.update({
'channel': channel, 'term': term, 'width': width, 'channel': channel, 'term': term, 'width': width,
'height': height, 'pixelwidth': pixelwidth, 'height': height, 'pixelwidth': pixelwidth,
'pixelheight': pixelheight, 'pixelheight': pixelheight,
} })
self.event.set() self.event.set()
return True return True
...@@ -143,17 +144,19 @@ class SSHInterface(paramiko.ServerInterface): ...@@ -143,17 +144,19 @@ class SSHInterface(paramiko.ServerInterface):
def check_channel_subsystem_request(self, channel, name): def check_channel_subsystem_request(self, channel, name):
logger.info("Check channel subsystem request: %s %s" % (channel, name)) logger.info("Check channel subsystem request: %s %s" % (channel, name))
self.request.type = 'subsystem' self.request.type.append('subsystem')
self.request.meta = {'channel': channel, 'name': name} self.request.meta.update({'channel': channel.get_id(), 'name': name})
self.event.set() self.event.set()
return False return False
def check_channel_window_change_request(self, channel, width, height, def check_channel_window_change_request(self, channel, width, height,
pixelwidth, pixelheight): pixelwidth, pixelheight):
self.request.meta['width'] = width self.request.meta.update({
self.request.meta['height'] = height 'width': width,
self.request.meta['pixelwidth'] = pixelwidth 'height': height,
self.request.meta['pixelheight'] = pixelheight 'pixelwidth': pixelwidth,
'pixelheight': pixelheight,
})
self.request.change_size_event.set() self.request.change_size_event.set()
return True return True
...@@ -162,19 +165,19 @@ class SSHInterface(paramiko.ServerInterface): ...@@ -162,19 +165,19 @@ class SSHInterface(paramiko.ServerInterface):
logger.info("Check channel x11 request %s %s %s %s %s" % logger.info("Check channel x11 request %s %s %s %s %s" %
(channel, single_connection, auth_protocol, (channel, single_connection, auth_protocol,
auth_cookie, screen_number)) auth_cookie, screen_number))
self.request.type = 'x11' self.request.type.append('x11')
self.request.meta = { self.request.meta.update({
'channel': channel, 'single_connection': single_connection, 'channel': channel.get_id(), 'single_connection': single_connection,
'auth_protocol': auth_protocol, 'auth_cookie': auth_cookie, 'auth_protocol': auth_protocol, 'auth_cookie': auth_cookie,
'screen_number': screen_number, 'screen_number': screen_number,
} })
self.event.set() self.event.set()
return False return False
def check_port_forward_request(self, address, port): def check_port_forward_request(self, address, port):
logger.info("Check channel port forward request: %s %s" % (address, port)) logger.info("Check channel port forward request: %s %s" % (address, port))
self.request.type = 'port-forward' self.request.type.append('port-forward')
self.request.meta = {'address': address, 'port': port} self.request.meta.update({'address': address, 'port': port})
self.event.set() self.event.set()
return False return False
...@@ -185,16 +188,3 @@ class SSHInterface(paramiko.ServerInterface): ...@@ -185,16 +188,3 @@ class SSHInterface(paramiko.ServerInterface):
# print("GC: SSH interface gc") # print("GC: SSH interface gc")
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
import threading import threading
import datetime import datetime
import weakref import weakref
import time
from . import char from . import char
from . import utils from . import utils
...@@ -13,7 +14,7 @@ logger = utils.get_logger(__file__) ...@@ -13,7 +14,7 @@ logger = utils.get_logger(__file__)
class Request: class Request:
def __init__(self, addr): def __init__(self, addr):
self.type = "" self.type = []
self.meta = {"width": 80, "height": 24} self.meta = {"width": 80, "height": 24}
self.user = None self.user = None
self.addr = addr self.addr = addr
...@@ -250,7 +251,10 @@ class WSProxy: ...@@ -250,7 +251,10 @@ class WSProxy:
continue continue
if len(data) == 0: if len(data) == 0:
self.close() self.close()
self.ws.emit("data", {'data': data.decode("utf-8"), 'room': self.connection}, room=self.room) data = data.decode(errors="ignore")
self.ws.emit("data", {'data': data, 'room': self.connection}, room=self.room)
if len(data) == BUF_SIZE:
time.sleep(0.1)
def auto_forward(self): def auto_forward(self):
thread = threading.Thread(target=self.forward, args=()) thread = threading.Thread(target=self.forward, args=())
......
...@@ -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,53 @@ class ProxyServer: ...@@ -84,17 +86,53 @@ class ProxyServer:
def get_telnet_server_conn(self, asset, system_user): def get_telnet_server_conn(self, asset, system_user):
pass pass
def get_proxy_sock(self, asset):
domain = self.app.service.get_domain_detail_with_gateway(
asset.domain
)
sock = None
for i in domain.gateways:
gateway = 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)
try:
sock = paramiko.ProxyCommand(proxy_command)
break
except (paramiko.AuthenticationException,
paramiko.BadAuthenticationType, SSHException,
TimeoutError) as e:
logger.error(e)
continue
return sock
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:
sock = self.get_proxy_sock(asset)
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 +150,7 @@ class ProxyServer: ...@@ -112,7 +150,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:
......
...@@ -97,13 +97,9 @@ class SSHServer: ...@@ -97,13 +97,9 @@ class SSHServer:
def dispatch(self, client): def dispatch(self, client):
request_type = client.request.type request_type = client.request.type
if request_type == 'pty' or request_type == 'x11': if 'pty' in request_type:
logger.info("Request type `pty`, dispatch to interactive mode") logger.info("Request type `pty`, dispatch to interactive mode")
InteractiveServer(self.app, client).interact() InteractiveServer(self.app, client).interact()
elif request_type == 'exec':
pass
elif request_type == 'subsystem':
pass
else: else:
client.send("Not support request type: %s" % request_type) client.send("Not support request type: %s" % request_type)
......
...@@ -20,7 +20,7 @@ Jinja2==2.10 ...@@ -20,7 +20,7 @@ Jinja2==2.10
jmespath==0.9.3 jmespath==0.9.3
jms-es-sdk==0.5.2 jms-es-sdk==0.5.2
jms-storage==0.0.12 jms-storage==0.0.12
jumpserver-python-sdk==0.0.32 jumpserver-python-sdk==0.0.34
MarkupSafe==1.0 MarkupSafe==1.0
oss2==2.4.0 oss2==2.4.0
paramiko==2.4.0 paramiko==2.4.0
......
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