Unverified Commit 4b6f5c40 authored by 老广's avatar 老广 Committed by GitHub

Merge pull request #44 from jumpserver/dev

Dev
parents 2f1d29d6 eee7a4d0
...@@ -45,3 +45,4 @@ Thumbs.db ...@@ -45,3 +45,4 @@ Thumbs.db
publish.sh publish.sh
luna.tar.gz luna.tar.gz
luna/ luna/
package-lock.json
#!/usr/bin/env python3 #!/usr/bin/env python3
import eventlet
eventlet.monkey_patch()
import sys import sys
import threading
sys.path.append('/Users/guang/projects/coco') sys.path.append('/Users/guang/projects/coco')
from flask import Flask, send_from_directory, render_template, request, jsonify, \ from flask import Flask, send_from_directory, render_template, request, jsonify, \
redirect, send_file, abort redirect, send_file, abort
from flask_socketio import SocketIO, Namespace, emit, join_room, leave_room from flask_socketio import SocketIO, Namespace, emit, join_room, leave_room
import paramiko import paramiko
import uuid import uuid
from threading import Lock
from flask import Flask, request, current_app, redirect from flask import Flask, request, current_app, redirect
import eventlet
import time import time
import json import json
import socket import socket
import logging import logging
import select import select
from coco.models import WSProxy, Client, Request from coco.models import WSProxy, Client, Request, Connection
from coco.httpd import ProxyNamespace from coco.httpd import ProxyNamespace
logger = logging.getLogger(__file__) logger = logging.getLogger(__file__)
...@@ -26,9 +27,12 @@ logger.addHandler(sh) ...@@ -26,9 +27,12 @@ logger.addHandler(sh)
logger2 = logging.getLogger('coco') logger2 = logging.getLogger('coco')
logger2.setLevel(logging.DEBUG) logger2.setLevel(logging.DEBUG)
fmt = "%(asctime)s [%(module)s %(levelname)s] %(message)s"
dtfmt = "%Y-%m-%d %H:%M:%S"
fmter = logging.Formatter(fmt=fmt, datefmt=dtfmt)
sh.setFormatter(fmter)
logger2.addHandler(sh) logger2.addHandler(sh)
eventlet.monkey_patch()
# async_mode = 'threading' # async_mode = 'threading'
async_mode = 'eventlet' async_mode = 'eventlet'
...@@ -36,94 +40,76 @@ app = Flask(__name__, template_folder='dist') ...@@ -36,94 +40,76 @@ app = Flask(__name__, template_folder='dist')
socketio = None socketio = None
nodes = '[{"id":"03059e2e-06b8-4ef1-b949-72e230b706fa","key":"0:9:5","name":"部门1","value":"部门1","parent":"ea4688ef-2b65-40cd-944d-5fca39e34f42","assets_granted":[{"id":"ad594b10-9f64-4913-b7b1-135fe63561d1","hostname":"ali-windows","ip":"47.104.243.139","port":3389,"system_users_granted":[{"id":"8763b81a-bb5e-484a-abca-10514c7bb185","name":"组织1-部门1-Administrator","username":"administrator","priority":10,"protocol":"rdp","comment":""}],"is_active":true,"system_users_join":"administrator","os":null,"domain":null,"platform":"Windows","comment":""},{"id":"d9020939-1dd7-4b18-9165-5124f20d1f77","hostname":"newwindows","ip":"10.1.10.114","port":3389,"system_users_granted":[{"id":"46b57293-c662-46f9-8bc4-dcf64f01bedc","name":"newwindows","username":"administrator","priority":10,"protocol":"rdp","comment":""},{"id":"8763b81a-bb5e-484a-abca-10514c7bb185","name":"组织1-部门1-Administrator","username":"administrator","priority":10,"protocol":"rdp","comment":""}],"is_active":true,"system_users_join":"administrator, administrator","os":null,"domain":null,"platform":"Windows","comment":""},{"id":"9ef36bb3-1bed-455f-be09-3770d3f4bf97","hostname":"test-vm1","ip":"172.19.185.6","port":22,"system_users_granted":[{"id":"7e326f71-aee5-4688-8cc1-717919470a09","name":"root","username":"root","priority":10,"protocol":"ssh","comment":""}],"is_active":true,"system_users_join":"root","os":null,"domain":null,"platform":"Linux","comment":""},{"id":"1600ed6d-e3b6-434c-a960-c5bb818806b6","hostname":"windows1","ip":"10.1.10.178","port":3389,"system_users_granted":[{"id":"413ea1d2-ef73-4a90-bae3-571ac1b39d93","name":"2012-test-no-passwd-rdp","username":"administrator","priority":10,"protocol":"rdp","comment":""},{"id":"46b57293-c662-46f9-8bc4-dcf64f01bedc","name":"newwindows","username":"administrator","priority":10,"protocol":"rdp","comment":""},{"id":"8763b81a-bb5e-484a-abca-10514c7bb185","name":"组织1-部门1-Administrator","username":"administrator","priority":10,"protocol":"rdp","comment":""}],"is_active":true,"system_users_join":"administrator, administrator, administrator","os":null,"domain":null,"platform":"Windows","comment":""},{"id":"27e50edc-52d9-41ef-8c9e-1bff9d1628b2","hostname":"test-vm2","ip":"172.19.185.7","port":22,"system_users_granted":[{"id":"7e326f71-aee5-4688-8cc1-717919470a09","name":"root","username":"root","priority":10,"protocol":"ssh","comment":""}],"is_active":true,"system_users_join":"root","os":null,"domain":null,"platform":"Linux","comment":""},{"id":"b6f16269-d02a-4055-9cd8-460fa10b1540","hostname":"test-vm3","ip":"172.19.185.8","port":22,"system_users_granted":[{"id":"7e326f71-aee5-4688-8cc1-717919470a09","name":"root","username":"root","priority":10,"protocol":"ssh","comment":""}],"is_active":true,"system_users_join":"root","os":null,"domain":null,"platform":"Linux","comment":""},{"id":"b952a481-a624-467e-b97f-9435155f0d53","hostname":"testserver","ip":"10.1.10.192","port":22,"system_users_granted":[{"id":"7e326f71-aee5-4688-8cc1-717919470a09","name":"root","username":"root","priority":10,"protocol":"ssh","comment":""}],"is_active":true,"system_users_join":"root","os":"CentOS","domain":"8789580f-b5ca-4478-b6d3-d0dafc4c48e8","platform":"Linux","comment":""},{"id":"969247e0-3796-4090-9aa6-3248560079e6","hostname":"test01","ip":"123.123.123.1","port":22,"system_users_granted":[{"id":"7e326f71-aee5-4688-8cc1-717919470a09","name":"root","username":"root","priority":10,"protocol":"ssh","comment":""}],"is_active":true,"system_users_join":"root","os":null,"domain":null,"platform":"Linux","comment":""},{"id":"7e8451cb-8eb7-4c9d-b652-961a6fdce3c4","hostname":"wz-test","ip":"54.222.180.235","port":22,"system_users_granted":[{"id":"7e326f71-aee5-4688-8cc1-717919470a09","name":"root","username":"root","priority":10,"protocol":"ssh","comment":""}],"is_active":true,"system_users_join":"root","os":"RedHat","domain":null,"platform":"Linux","comment":""}],"assets_amount":9}]' nodes = '[{"id":"03059e2e-06b8-4ef1-b949-72e230b706fa","key":"0:9:5","name":"部门1","value":"部门1","parent":"ea4688ef-2b65-40cd-944d-5fca39e34f42","assets_granted":[{"id":"ad594b10-9f64-4913-b7b1-135fe63561d1","hostname":"ali-windows","ip":"47.104.243.139","port":3389,"system_users_granted":[{"id":"8763b81a-bb5e-484a-abca-10514c7bb185","name":"组织1-部门1-Administrator","username":"administrator","priority":10,"protocol":"rdp","comment":"windows 服务器而已"}],"is_active":true,"system_users_join":"administrator","os":null,"domain":null,"platform":"Windows","comment":"只是个windows而已"},{"id":"d9020939-1dd7-4b18-9165-5124f20d1f77","hostname":"newwindows","ip":"10.1.10.114","port":3389,"system_users_granted":[{"id":"46b57293-c662-46f9-8bc4-dcf64f01bedc","name":"newwindows","username":"administrator","priority":10,"protocol":"rdp","comment":"只是个windows而已"},{"id":"8763b81a-bb5e-484a-abca-10514c7bb185","name":"组织1-部门1-Administrator","username":"administrator","priority":10,"protocol":"rdp","comment":"只是个windows而已"}],"is_active":true,"system_users_join":"administrator, administrator","os":null,"domain":null,"platform":"Windows","comment":"只是个windows而已"},{"id":"9ef36bb3-1bed-455f-be09-3770d3f4bf97","hostname":"test-vm1","ip":"172.19.185.6","port":22,"system_users_granted":[{"id":"7e326f71-aee5-4688-8cc1-717919470a09","name":"root","username":"root","priority":10,"protocol":"ssh","comment":"只是个windows而已"}],"is_active":true,"system_users_join":"root","os":null,"domain":null,"platform":"Linux","comment":"只是个windows而已"},{"id":"1600ed6d-e3b6-434c-a960-c5bb818806b6","hostname":"windows1","ip":"10.1.10.178","port":3389,"system_users_granted":[{"id":"413ea1d2-ef73-4a90-bae3-571ac1b39d93","name":"2012-test-no-passwd-rdp","username":"administrator","priority":10,"protocol":"rdp","comment":"只是个windows而已"},{"id":"46b57293-c662-46f9-8bc4-dcf64f01bedc","name":"newwindows","username":"administrator","priority":10,"protocol":"rdp","comment":"只是个windows而已"},{"id":"8763b81a-bb5e-484a-abca-10514c7bb185","name":"组织1-部门1-Administrator","username":"administrator","priority":10,"protocol":"rdp","comment":"只是个windows而已"}],"is_active":true,"system_users_join":"administrator, administrator, administrator","os":null,"domain":null,"platform":"Windows","comment":"只是个windows而已"},{"id":"27e50edc-52d9-41ef-8c9e-1bff9d1628b2","hostname":"test-vm2","ip":"172.19.185.7","port":22,"system_users_granted":[{"id":"7e326f71-aee5-4688-8cc1-717919470a09","name":"root","username":"root","priority":10,"protocol":"ssh","comment":"只是个windows而已"}],"is_active":true,"system_users_join":"root","os":null,"domain":null,"platform":"Linux","comment":"只是个windows而已"},{"id":"b6f16269-d02a-4055-9cd8-460fa10b1540","hostname":"test-vm3","ip":"172.19.185.8","port":22,"system_users_granted":[{"id":"7e326f71-aee5-4688-8cc1-717919470a09","name":"root","username":"root","priority":10,"protocol":"ssh","comment":"只是个windows而已"}],"is_active":true,"system_users_join":"root","os":null,"domain":null,"platform":"Linux","comment":"只是个windows而已"},{"id":"b952a481-a624-467e-b97f-9435155f0d53","hostname":"testserver","ip":"10.1.10.192","port":22,"system_users_granted":[{"id":"7e326f71-aee5-4688-8cc1-717919470a09","name":"root","username":"root","priority":10,"protocol":"ssh","comment":"只是个windows而已"}],"is_active":true,"system_users_join":"root","os":"CentOS","domain":"8789580f-b5ca-4478-b6d3-d0dafc4c48e8","platform":"Linux","comment":"只是个windows而已"},{"id":"969247e0-3796-4090-9aa6-3248560079e6","hostname":"test01","ip":"123.123.123.1","port":22,"system_users_granted":[{"id":"7e326f71-aee5-4688-8cc1-717919470a09","name":"root","username":"root","priority":10,"protocol":"ssh","comment":"只是个windows而已"}],"is_active":true,"system_users_join":"root","os":null,"domain":null,"platform":"Linux","comment":"只是个windows而已"},{"id":"7e8451cb-8eb7-4c9d-b652-961a6fdce3c4","hostname":"wz-test","ip":"54.222.180.235","port":22,"system_users_granted":[{"id":"7e326f71-aee5-4688-8cc1-717919470a09","name":"root","username":"root","priority":10,"protocol":"ssh","comment":"只是个windows而已"}],"is_active":true,"system_users_join":"root","os":"RedHat","domain":null,"platform":"Linux","comment":"只是个windows而已"}],"assets_amount":9}]'
class Forwarder: class ProxyServer:
def __init__(self, client): def __init__(self, client, asset, system_user):
self.client = client self.client = client
width = client.request.meta['width'] width = client.request.meta['width']
height = client.request.meta['height'] height = client.request.meta['height']
self.server = self.ssh_with_password(width, height) self.server = self.ssh_with_password(width, height)
self.watch_win_size_change_async()
def proxy(self, asset, system_user): def proxy(self):
while True: while True:
r, w, x = select.select([self.server, self.client], [], []) r, w, x = select.select([self.server, self.client, self.client.change_size_evt], [], [])
if self.server in r: if self.server in r:
data = self.server.recv(1024) data = self.server.recv(1024)
if len(data) == 0: if len(data) == 0:
break break
self.client.send(data) self.client.send(data)
if self.client in r: elif self.client in r:
data = self.client.recv(1024) data = self.client.recv(1024)
if len(data) == 0: if len(data) == 0:
break break
self.server.send(data) self.server.send(data)
elif self.client.change_size_evt in r:
self.resize_win_size()
def watch_win_size_change_async(self): def resize_win_size(self):
thread = threading.Thread(target=self.watch_win_size_change) width, height = self.client.request.meta['width'], \
thread.daemon = True self.client.request.meta['height']
thread.start() logger.debug("Resize server chan size {}*{}".format(width, height))
self.server.resize_pty(width=width, height=height)
def ssh_with_password(self, width=80, height=24): def ssh_with_password(self, width=80, height=24):
ssh = paramiko.SSHClient() ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("192.168.244.128", 22, "web", "redhat") ssh.connect("192.168.244.177", 22, "root", "redhat123")
chan = ssh.invoke_shell(term='xterm', width=width, height=height) chan = ssh.invoke_shell(term='xterm', width=width, height=height)
return chan return chan
def watch_win_size_change(self):
while self.client.request.change_size_event.wait():
self.client.request.change_size_event.clear()
width = self.client.request.meta.get('width', 80)
height = self.client.request.meta.get('height', 24)
logger.debug("Change win size: %s - %s" % (width, height))
try:
self.server.resize_pty(width=width, height=height)
# self.server.chan.resize_pty(width=width, height=height)
except Exception:
break
class SSHws(ProxyNamespace): class SSHws(ProxyNamespace):
def on_connect(self): def connect_host(self, message):
logger.debug("ON connect")
self.new_connection()
def on_host(self, message):
# 此处获取主机的信息
logger.debug("On host event trigger")
asset_id = message.get('uuid', None) asset_id = message.get('uuid', None)
user_id = message.get('userid', None) system_user_id = message.get('userid', None)
secret = message.get('secret', None) secret = message.get('secret', None)
win_size = message.get('size', (80, 24)) cols, rows = message.get('size', (80, 24))
request.current_user = "admin"
req = self.make_coco_request(win_size[0], win_size[1])
room = self.new_room(req) connection = Connection.get_connection(request.sid)
client_id = str(uuid.uuid4())
self.emit('room', {'room': room["id"], 'secret': secret}) client = connection.new_client(client_id)
join_room(room["id"]) client.request.kind = 'session'
if not asset_id or not user_id: client.request.type = 'pty'
client.request.meta.update({
'pty': b'xterm', 'width': cols, 'height': rows,
})
ws_proxy = WSProxy(self, client_id)
client.chan = ws_proxy
self.emit('room', {'room': client_id, 'secret': secret})
join_room(client_id)
if not asset_id or not system_user_id:
return return
# asset = app_service.get_asset(asset_id) forwarder = ProxyServer(client, None, None)
# system_user = app_service.get_system_user(user_id)
def proxy():
# if not asset or not system_user: forwarder.proxy()
# self.on_connect() self.logout(client_id, connection)
# return self.socketio.start_background_task(proxy)
child, parent = socket.socketpair()
client = Client(parent, room["request"])
forwarder = Forwarder(client)
room["client"] = client
room["forwarder"] = forwarder
room["proxy"] = WSProxy(self, child, room["id"])
room["cols"], room["rows"] = win_size
self.socketio.start_background_task(
forwarder.proxy, None, None
)
def on_token(self, message): def on_token(self, message):
# 此处获取token含有的主机的信息 # 此处获取token含有的主机的信息
......
...@@ -2,5 +2,5 @@ export const environment = { ...@@ -2,5 +2,5 @@ export const environment = {
production: true production: true
}; };
// export const version = '1.3.0-{{BUILD_NUMBER}} GPLv2.'; // export const version = '1.3.0-{{BUILD_NUMBER}} GPLv2.';
// export const version = '1.3.3-101 GPLv2.'; // export const version = '1.3.3-101 GPLv2.';
export const version = '1.3.3-{{BUILD_NUMBER}} GPLv2.'; export const version = '1.4.1-{{BUILD_NUMBER}} GPLv2.';
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