Modify some bug and add some logging

pull/530/head
ibuler 2016-09-26 00:05:23 +08:00
parent e627b14e55
commit badd319bb4
1 changed files with 23 additions and 8 deletions

View File

@ -11,7 +11,7 @@ import time
from binascii import hexlify from binascii import hexlify
import sys import sys
import threading import threading
from multiprocessing import process from multiprocessing.process import Process
import traceback import traceback
import tty import tty
import termios import termios
@ -126,6 +126,9 @@ class SSHServer(paramiko.ServerInterface):
def check_channel_shell_request(self, channel): def check_channel_shell_request(self, channel):
self.event.set() self.event.set()
self.__class__.channel_pools.append(channel) self.__class__.channel_pools.append(channel)
channel.username = self.username
channel.addr = self.addr
return True return True
def check_channel_pty_request(self, channel, term, width, height, pixelwidth, def check_channel_pty_request(self, channel, term, width, height, pixelwidth,
@ -150,12 +153,15 @@ class BackendServer:
ssh.connect(hostname=self.host, port=self.port, username=self.username, password=self.host_password, ssh.connect(hostname=self.host, port=self.port, username=self.username, password=self.host_password,
pkey=self.host_private_key, look_for_keys=False, allow_agent=True, compress=True, timeout=timeout) pkey=self.host_private_key, look_for_keys=False, allow_agent=True, compress=True, timeout=timeout)
self.channel = channel = ssh.invoke_shell(term=term, width=width, height=height) self.channel = channel = ssh.invoke_shell(term=term, width=width, height=height)
logger.info('Connect %(username)s@%(host)s:%(port)s successfully' % { logger.info('Connect backend server %(username)s@%(host)s:%(port)s successfully' % {
'username': self.username, 'username': self.username,
'host': self.host, 'host': self.host,
'port': self.port, 'port': self.port,
}) })
channel.settimeout(100) channel.settimeout(100)
channel.host = self.host
channel.port = self.port
channel.username = self.username
return channel return channel
@property @property
@ -175,7 +181,8 @@ class Navigation:
def display_banner(self): def display_banner(self):
client_channel = self.client_channel client_channel = self.client_channel
client_channel.send('\r\n\r\n\t\tWelcome to use Jumpserver open source system !\r\n\r\n') client_channel.send('\r\n\r\n\t\tWelcome to use Jumpserver open source system !\r\n\r\n')
client_channel.send('If use find some bug please contact us <ibuler@qq.com>\r\n') client_channel.send('If you find some bug please contact us <ibuler@qq.com>\r\n')
client_channel.send('See more at https://www.jumpserver.org\r\n')
# client_channel.send(self.username) # client_channel.send(self.username)
def display(self): def display(self):
@ -211,7 +218,6 @@ class JumpServer:
transport.add_server_key(SSHServer.get_host_key()) transport.add_server_key(SSHServer.get_host_key())
ssh_server = SSHServer(client, addr) ssh_server = SSHServer(client, addr)
self.username = ssh_server.username
try: try:
transport.start_server(server=ssh_server) transport.start_server(server=ssh_server)
@ -259,12 +265,20 @@ class JumpServer:
if client_channel in r: if client_channel in r:
client_data = client_channel.recv(1024) client_data = client_channel.recv(1024)
if len(client_data) == 0: if len(client_data) == 0:
logger.info('Logout from ssh server %(host)s: %(username)s' % {
'host': addr[0],
'username': client_channel.username,
})
break break
backend_channel.send(client_data) backend_channel.send(client_data)
if backend_channel in r: if backend_channel in r:
backend_data = backend_channel.recv(1024) backend_data = backend_channel.recv(1024)
if len(backend_data) == 0: if len(backend_data) == 0:
logger.info('Logout from backend server %(host)s: %(username)s' % {
'host': backend_channel.host,
'username': backend_channel.username,
})
break break
client_channel.send(backend_data) client_channel.send(backend_data)
@ -281,8 +295,9 @@ class JumpServer:
# except IndexError: # except IndexError:
# pass # pass
# Todo: catch other exception
except IndexError: except IndexError:
logger.info('Close with server %s from %s' % ('127.0.0.1', '127.0.0.1')) logger.info('Close with server %s from %s' % (addr[0], addr[1]))
sys.exit(100) sys.exit(100)
def listen(self): def listen(self):
@ -299,9 +314,9 @@ class JumpServer:
while True: while True:
try: try:
client, addr = self.sock.accept() client, addr = self.sock.accept()
t = threading.Thread(target=self.handle_ssh_request, args=(client, addr)) process = Process(target=self.handle_ssh_request, args=(client, addr))
t.daemon = True process.daemon = True
t.start() process.start()
except Exception as e: except Exception as e:
logger.error('Bind failed: ' + str(e)) logger.error('Bind failed: ' + str(e))
traceback.print_exc() traceback.print_exc()