Commit 50645630 authored by ibuler's avatar ibuler

[Update] 修改coco,如果system user没有密码需要用户交互输入

parent 3cdb78ef
...@@ -26,7 +26,6 @@ class SSHConnection: ...@@ -26,7 +26,6 @@ class SSHConnection:
ssh = paramiko.SSHClient() ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
sock = None sock = None
self.get_system_user_auth(system_user)
if asset.domain: if asset.domain:
sock = self.get_proxy_sock(asset) sock = self.get_proxy_sock(asset)
...@@ -83,14 +82,6 @@ class SSHConnection: ...@@ -83,14 +82,6 @@ class SSHConnection:
else: else:
return None, msg return None, msg
def get_system_user_auth(self, system_user):
"""
获取系统用户的认证信息,密码或秘钥
:return: system user have full info
"""
system_user.password, system_user.private_key = \
self.app.service.get_system_user_auth_info(system_user)
def get_proxy_sock(self, asset): def get_proxy_sock(self, asset):
sock = None sock = None
domain = self.app.service.get_domain_detail_with_gateway( domain = self.app.service.get_domain_detail_with_gateway(
......
...@@ -184,12 +184,6 @@ class ProxyNamespace(BaseNamespace): ...@@ -184,12 +184,6 @@ class ProxyNamespace(BaseNamespace):
self.clients[request.sid]["request"].user = self.current_user self.clients[request.sid]["request"].user = self.current_user
logger.debug(self.current_user) logger.debug(self.current_user)
# {
# "user": {UUID},
# "asset": {UUID},
# "system_user": {UUID}
# }
self.on_host({'secret': secret, 'uuid': host['asset'], 'userid': host['system_user']}) self.on_host({'secret': secret, 'uuid': host['asset'], 'userid': host['system_user']})
def on_resize(self, message): def on_resize(self, message):
......
...@@ -263,6 +263,7 @@ class WSProxy: ...@@ -263,6 +263,7 @@ class WSProxy:
def close(self): def close(self):
self.stop_event.set() self.stop_event.set()
self.child.shutdown(1)
self.child.close() self.child.close()
self.ws.logout(self.connection) self.ws.logout(self.connection)
logger.debug("Proxy {} closed".format(self)) logger.debug("Proxy {} closed".format(self))
......
...@@ -12,7 +12,7 @@ from .session import Session ...@@ -12,7 +12,7 @@ from .session import Session
from .models import Server from .models import Server
from .connection import SSHConnection from .connection import SSHConnection
from .utils import wrap_with_line_feed as wr, wrap_with_warning as warning, \ from .utils import wrap_with_line_feed as wr, wrap_with_warning as warning, \
get_logger get_logger, net_input
logger = get_logger(__file__) logger = get_logger(__file__)
...@@ -32,7 +32,21 @@ class ProxyServer: ...@@ -32,7 +32,21 @@ class ProxyServer:
def app(self): def app(self):
return self._app() return self._app()
def get_system_user_auth(self, system_user):
"""
获取系统用户的认证信息,密码或秘钥
:return: system user have full info
"""
password, private_key = \
self.app.service.get_system_user_auth_info(system_user)
if not password and not private_key:
prompt = "{}'s password: ".format(system_user.username)
password = net_input(self.client, prompt=prompt, sensitive=True)
system_user.password = password
system_user.private_key = private_key
def proxy(self, asset, system_user): def proxy(self, asset, system_user):
self.get_system_user_auth(system_user)
self.send_connecting_message(asset, system_user) self.send_connecting_message(asset, system_user)
self.server = self.get_server_conn(asset, system_user) self.server = self.get_server_conn(asset, system_user)
if self.server is None: if self.server is None:
......
...@@ -4,26 +4,17 @@ ...@@ -4,26 +4,17 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import hashlib
import logging import logging
import re import re
import os import os
import threading
import base64
import calendar
import time
import datetime
import gettext import gettext
from io import StringIO from io import StringIO
from binascii import hexlify from binascii import hexlify
import paramiko import paramiko
import pyte import pyte
import pytz
from email.utils import formatdate
from queue import Queue, Empty
from .exception import NoAppException from . import char
BASE_DIR = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) BASE_DIR = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
...@@ -302,4 +293,66 @@ def len_display(s): ...@@ -302,4 +293,66 @@ def len_display(s):
return length return length
def net_input(client, prompt='Opt> ', sensitive=False):
"""实现了一个ssh input, 提示用户输入, 获取并返回
:return user input string
"""
input_data = []
parser = TtyIOParser()
client.send(wrap_with_line_feed(prompt, before=0, after=0))
while True:
data = client.recv(10)
if len(data) == 0:
break
# Client input backspace
if data in char.BACKSPACE_CHAR:
# If input words less than 0, should send 'BELL'
if len(input_data) > 0:
data = char.BACKSPACE_CHAR[data]
input_data.pop()
else:
data = char.BELL_CHAR
client.send(data)
continue
if data.startswith(b'\x03'):
# Ctrl-C
client.send('^C\r\n{} '.format(prompt).encode())
input_data = []
continue
elif data.startswith(b'\x04'):
# Ctrl-D
return 'q'
# Todo: Move x1b to char
if data.startswith(b'\x1b') or data in char.UNSUPPORTED_CHAR:
client.send(b'')
continue
# handle shell expect
multi_char_with_enter = False
if len(data) > 1 and data[-1] in char.ENTER_CHAR_ORDER:
if sensitive:
client.send(len(data) * '*')
else:
client.send(data)
input_data.append(data[:-1])
multi_char_with_enter = True
# If user type ENTER we should get user input
if data in char.ENTER_CHAR or multi_char_with_enter:
client.send(wrap_with_line_feed(b'', after=2))
option = parser.parse_input(input_data)
del input_data[:]
return option.strip()
else:
if sensitive:
client.send(len(data) * '*')
else:
client.send(data)
input_data.append(data)
ugettext = _gettext() ugettext = _gettext()
...@@ -30,7 +30,7 @@ pycparser==2.18 ...@@ -30,7 +30,7 @@ pycparser==2.18
PyNaCl==1.2.1 PyNaCl==1.2.1
pyte==0.7.0 pyte==0.7.0
python-dateutil==2.6.1 python-dateutil==2.6.1
python-engineio==2.0.1 python-engineio==2.1.0
python-gssapi==0.6.4 python-gssapi==0.6.4
python-socketio==1.8.3 python-socketio==1.8.3
pytz==2017.3 pytz==2017.3
......
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