Commit 09dbf394 authored by ibuler's avatar ibuler

try to use gevent

parent 68174ad5
...@@ -6,8 +6,8 @@ Luna 是Jumpserver项目的web terminal子项目, 用户登陆 Luna可以获取 ...@@ -6,8 +6,8 @@ Luna 是Jumpserver项目的web terminal子项目, 用户登陆 Luna可以获取
### Quick start ### Quick start
``` ```
$ brew install $(cat mac_requirements.txt) $ brew install $(cat mac_requirements.txt)
$ pip install -r requirements.py $ pip install --user -r requirements.py -i https://pypi.douban.com/simple
$ export JUMPSERVER_ENDPOINT='http://a-jumpserver-url:port' $ export JUMPSERVER_ENDPOINT='http://a-jumpserver-url:port'
# 或修改配置文件设置 JUMPSERVER_ENDPOINT # 或修改配置文件设置 JUMPSERVER_ENDPOINT
......
File added
...@@ -360,6 +360,7 @@ var AppService = (function () { ...@@ -360,6 +360,7 @@ var AppService = (function () {
} }
else { else {
socket.emit('resize', [col, row]); socket.emit('resize', [col, row]);
console.log('resize');
exports.DataStore.term[id]["term"].resize(col, row); exports.DataStore.term[id]["term"].resize(col, row);
ng2_cookies_1.Cookie.set('cols', String(col), 99, '/', document.domain); ng2_cookies_1.Cookie.set('cols', String(col), 99, '/', document.domain);
ng2_cookies_1.Cookie.set('rows', String(row), 99, '/', document.domain); ng2_cookies_1.Cookie.set('rows', String(row), 99, '/', document.domain);
......
...@@ -5,7 +5,7 @@ import logging ...@@ -5,7 +5,7 @@ import logging
import time import time
from flask import Flask from flask import Flask
from flask_socketio import SocketIO import socketio
from jms import AppService, UserService from jms import AppService, UserService
from jms.mixin import AppMixin from jms.mixin import AppMixin
...@@ -55,5 +55,7 @@ class Luna(Flask, AppMixin): ...@@ -55,5 +55,7 @@ class Luna(Flask, AppMixin):
return Flask.run(self, host=host, port=port, debug=debug, **options) return Flask.run(self, host=host, port=port, debug=debug, **options)
async_mode = None
app = Luna(__name__, template_folder='dist') app = Luna(__name__, template_folder='dist')
socket_io = SocketIO(app) socket_io = socketio.Server(logger=True, async_mode=async_mode)
app.wsgi_app = socketio.Middleware(socket_io, app.wsgi_app)
#!/usr/bin/env python #!/usr/bin/env python
# coding: utf-8 # coding: utf-8
import time
import select
import threading
import collections
import paramiko
from flask.globals import _request_ctx_stack, _app_ctx_stack
from flask import request
from flask import render_template, send_from_directory, jsonify, g from flask import render_template, send_from_directory, jsonify, g
from flask_socketio import emit from flask_socketio import emit
from . import app, socket_io from . import app, socket_io
thread = None
@app.route('/api/token') clients = collections.defaultdict(dict)
def get_token():
if g.current_user is None:
return jsonify({'Error': 'Auth failed or use old token'})
else:
g.token = g.current_user.token
return jsonify({'token': g.current_user.token})
@app.route('/api/asset/list')
def get_asset_list():
asset_list = api.get_user_assets_granted(g.current_user)
return jsonify(asset_list)
@app.route('/') @app.route('/')
...@@ -30,7 +26,6 @@ def index(): ...@@ -30,7 +26,6 @@ def index():
@app.route('/get_site') @app.route('/get_site')
def get_site(): def get_site():
print(app.config.get('JUMPSERVER_URL'))
return jsonify({"hello": "world"}) return jsonify({"hello": "world"})
...@@ -44,40 +39,77 @@ def send_dist(path): ...@@ -44,40 +39,77 @@ def send_dist(path):
return send_from_directory('dist', path) return send_from_directory('dist', path)
@socket_io.on('data')
def handle_data(message):
print('received data: ' + message)
emit('data', message)
@socket_io.on('machine')
def handle_machine(message):
print('received machine: ' + message)
@socket_io.on('api') @socket_io.on('api')
def handle_api(api): def handle_api(sid, api):
if api == 'nav': if api == 'nav':
emit('nav', socket_io.emit('nav',
'[{"id":"File","name":"Server","children":[{"id":"NewConnection","href":"Aaaa","name":"New connection","disable":true},{"id":"Connect","href":"Aaaa","name":"Connect","disable":true},{"id":"Disconnect","click":"Disconnect","name":"Disconnect"},{"id":"DisconnectAll","click":"DisconnectAll","name":"Disconnect all"},{"id":"Duplicate","href":"Aaaa","name":"Duplicate","disable":true},{"id":"Upload","href":"Aaaa","name":"Upload","disable":true},{"id":"Download","href":"Aaaa","name":"Download","disable":true},{"id":"Search","href":"Aaaa","name":"Search","disable":true},{"id":"Reload","click":"ReloadLeftbar","name":"Reload"}]},{"id":"View","name":"View","children":[{"id":"HindLeftManager","click":"HideLeft","name":"Hind left manager"},{"id":"SplitVertical","href":"Aaaa","name":"Split vertical","disable":true},{"id":"CommandBar","href":"Aaaa","name":"Command bar","disable":true},{"id":"ShareSession","href":"Aaaa","name":"Share session (read/write)","disable":true},{"id":"Language","href":"Aaaa","name":"Language","disable":true}]},{"id":"Help","name":"Help","children":[{"id":"EnterLicense","click":"EnterLicense","name":"Enter License"},{"id":"Website","click":"Website","name":"Website"},{"id":"BBS","click":"BBS","name":"BBS"}]}]') '[{"id":"File","name":"Server","children":[{"id":"NewConnection","href":"Aaaa","name":"New connection","disable":true},{"id":"Connect","href":"Aaaa","name":"Connect","disable":true},{"id":"Disconnect","click":"Disconnect","name":"Disconnect"},{"id":"DisconnectAll","click":"DisconnectAll","name":"Disconnect all"},{"id":"Duplicate","href":"Aaaa","name":"Duplicate","disable":true},{"id":"Upload","href":"Aaaa","name":"Upload","disable":true},{"id":"Download","href":"Aaaa","name":"Download","disable":true},{"id":"Search","href":"Aaaa","name":"Search","disable":true},{"id":"Reload","click":"ReloadLeftbar","name":"Reload"}]},{"id":"View","name":"View","children":[{"id":"HindLeftManager","click":"HideLeft","name":"Hind left manager"},{"id":"SplitVertical","href":"Aaaa","name":"Split vertical","disable":true},{"id":"CommandBar","href":"Aaaa","name":"Command bar","disable":true},{"id":"ShareSession","href":"Aaaa","name":"Share session (read/write)","disable":true},{"id":"Language","href":"Aaaa","name":"Language","disable":true}]},{"id":"Help","name":"Help","children":[{"id":"EnterLicense","click":"EnterLicense","name":"Enter License"},{"id":"Website","click":"Website","name":"Website"},{"id":"BBS","click":"BBS","name":"BBS"}]}]', room=sid)
# print('received json: ' + str(json)) # print('received json: ' + str(json))
elif api == 'all': elif api == 'all':
emit('nav', socket_io.emit('nav',
'[{"id":"File","name":"Server","children":[{"id":"NewConnection","href":"Aaaa","name":"New connection","disable":true},{"id":"Connect","href":"Aaaa","name":"Connect","disable":true},{"id":"Disconnect","click":"Disconnect","name":"Disconnect"},{"id":"DisconnectAll","click":"DisconnectAll","name":"Disconnect all"},{"id":"Duplicate","href":"Aaaa","name":"Duplicate","disable":true},{"id":"Upload","href":"Aaaa","name":"Upload","disable":true},{"id":"Download","href":"Aaaa","name":"Download","disable":true},{"id":"Search","href":"Aaaa","name":"Search","disable":true},{"id":"Reload","click":"ReloadLeftbar","name":"Reload"}]},{"id":"View","name":"View","children":[{"id":"HindLeftManager","click":"HideLeft","name":"Hind left manager"},{"id":"SplitVertical","href":"Aaaa","name":"Split vertical","disable":true},{"id":"CommandBar","href":"Aaaa","name":"Command bar","disable":true},{"id":"ShareSession","href":"Aaaa","name":"Share session (read/write)","disable":true},{"id":"Language","href":"Aaaa","name":"Language","disable":true}]},{"id":"Help","name":"Help","children":[{"id":"EnterLicense","click":"EnterLicense","name":"Enter License"},{"id":"Website","click":"Website","name":"Website"},{"id":"BBS","click":"BBS","name":"BBS"}]}]') '[{"id":"File","name":"Server","children":[{"id":"NewConnection","href":"Aaaa","name":"New connection","disable":true},{"id":"Connect","href":"Aaaa","name":"Connect","disable":true},{"id":"Disconnect","click":"Disconnect","name":"Disconnect"},{"id":"DisconnectAll","click":"DisconnectAll","name":"Disconnect all"},{"id":"Duplicate","href":"Aaaa","name":"Duplicate","disable":true},{"id":"Upload","href":"Aaaa","name":"Upload","disable":true},{"id":"Download","href":"Aaaa","name":"Download","disable":true},{"id":"Search","href":"Aaaa","name":"Search","disable":true},{"id":"Reload","click":"ReloadLeftbar","name":"Reload"}]},{"id":"View","name":"View","children":[{"id":"HindLeftManager","click":"HideLeft","name":"Hind left manager"},{"id":"SplitVertical","href":"Aaaa","name":"Split vertical","disable":true},{"id":"CommandBar","href":"Aaaa","name":"Command bar","disable":true},{"id":"ShareSession","href":"Aaaa","name":"Share session (read/write)","disable":true},{"id":"Language","href":"Aaaa","name":"Language","disable":true}]},{"id":"Help","name":"Help","children":[{"id":"EnterLicense","click":"EnterLicense","name":"Enter License"},{"id":"Website","click":"Website","name":"Website"},{"id":"BBS","click":"BBS","name":"BBS"}]}]', room=sid)
@socket_io.on('connect', namespace='/')
def handle_term_connect(sid, environ):
clients[sid] = collections.defaultdict(dict)
@socket_io.on('machine')
def handle_machine(sid, message):
clients[sid]['host'] = host = '192.168.152.129'
clients[sid]['port'] = port = 22
# t = threading.Thread(target=forward, args=(sid,))
# t.setDaemon(True)
# t.start()
global thread
if thread is None:
thread = socket_io.start_background_task(forward, sid)
socket_io.emit('data', 'Connect to %s:%s' % (host, port), room=sid)
@socket_io.on('connect') @socket_io.on('data')
def handle_term_connect(): def handle_data(sid, message):
print('term connect') print('Receive data: %s' % message)
if clients[sid]['chan']:
clients[sid]['chan'].send(message)
@socket_io.on('disconnect') @socket_io.on('disconnect')
def handle_term_disconnect(): def handle_term_disconnect(sid):
del clients[sid]
print('term disconnect') print('term disconnect')
@socket_io.on('resize') @socket_io.on('resize')
def handle_term_resize(json): def handle_term_resize(sid, json):
print('term resize: ' + str(json)) print('term resize: ' + str(json))
pass pass
def forward(sid):
try:
host = clients[sid]['host']
port = clients[sid]['port']
except KeyError as e:
socket_io.emit('data', e, room=sid)
return
for i in range(1, 10):
socket_io.emit('data', 'Forwarding\r\n')
socket_io.emit('data', 'Forwarding\r\n')
socket_io.sleep(1)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, port=port, username='root', password='redhat')
clients[sid]['ssh'] = ssh
clients[sid]['chan'] = chan = ssh.invoke_shell()
while True:
r, w, x = select.select([chan], [], [])
if chan in r:
data = chan.recv(1024)
if not len(data):
break
print('Sending data: %s' % data)
socket_io.emit('data', 'What is')
...@@ -11,6 +11,11 @@ host = app.config['BIND_HOST'] ...@@ -11,6 +11,11 @@ host = app.config['BIND_HOST']
port = app.config['LISTEN_PORT'] port = app.config['LISTEN_PORT']
if __name__ == '__main__': if __name__ == '__main__':
# subprocess.call('gunicorn -k eventlet -b %s:%s -w 4 -n luna run_server:app' % (host, port), shell=True) if socket_io.async_mode == 'threading':
app.bootstrap() app.run(threaded=True)
socket_io.run(app, host='0.0.0.0') elif socket_io.async_mode == 'eventlet':
\ No newline at end of file import eventlet
import eventlet.wsgi
eventlet.wsgi.server(eventlet.listen(('', 5000)), app)
else:
print('Unkonw async_mode: ' + socket_io.async_mode)
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