Merge pull request #87 from liuzheng712/feat_run_one

整合Tornado和Django,修复web阻断功能
pull/94/head
ibuler 2016-02-26 10:12:23 +08:00
commit 29953eb7c7
6 changed files with 50 additions and 22 deletions

View File

@ -67,7 +67,7 @@ def log_list(request, offset):
contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request)
web_monitor_uri = '%s/monitor' % WEB_SOCKET_HOST
web_kill_uri = 'http://%s/kill' % WEB_SOCKET_HOST
web_kill_uri = '/kill'
session_id = request.session.session_key
return render_to_response('jlog/log_%s.html' % offset, locals(), context_instance=RequestContext(request))

View File

@ -344,7 +344,7 @@ def download(request):
def exec_cmd(request):
role = request.GET.get('role')
check_assets = request.GET.get('check_assets', '')
web_terminal_uri = '%s/exec?role=%s' % (WEB_SOCKET_HOST, role)
web_terminal_uri = '/ws/exec?role=%s' % (role)
return my_render('exec_cmd.html', locals(), request)
@ -356,7 +356,8 @@ def web_terminal(request):
if asset:
print asset
hostname = asset.hostname
web_terminal_uri = '%s/terminal?id=%s&role=%s' % (WEB_SOCKET_HOST, asset_id, role_name)
# web_terminal_uri = '%s/ws/terminal?id=%s&role=%s' % (WEB_SOCKET_HOST, asset_id, role_name)
web_terminal_uri = '/ws/terminal?id=%s&role=%s' % (asset_id, role_name)
return render_to_response('jlog/web_terminal.html', locals())

View File

@ -1,3 +1,4 @@
#!/usr/bin/env python
# coding: utf-8
import time
@ -31,7 +32,7 @@ try:
except ImportError:
import json
os.environ['DJANGO_SETTINGS_MODULE'] = 'jumpserver.settings'
define("port", default=3000, help="run on the given port", type=int)
define("host", default='0.0.0.0', help="run port on given host", type=str)
@ -314,7 +315,8 @@ class WebTerminalHandler(tornado.websocket.WebSocketHandler):
logger.debug('Websocket: request web terminal Host: %s User: %s Role: %s' % (asset.hostname, self.user.username,
login_role.name))
self.term = WebTty(self.user, asset, login_role, login_type='web')
self.term.remote_ip = self.request.remote_ip
# self.term.remote_ip = self.request.remote_ip
self.term.remote_ip = self.request.headers.get("X-Real_IP")
self.ssh = self.term.get_connection()
self.channel = self.ssh.invoke_shell(term='xterm')
WebTerminalHandler.tasks.append(MyThread(target=self.forward_outbound))
@ -425,12 +427,40 @@ class Application(tornado.web.Application):
tornado.web.Application.__init__(self, handlers, **setting)
if __name__ == '__main__':
tornado.options.parse_command_line()
app = Application()
server = tornado.httpserver.HTTPServer(app)
server.bind(options.port, options.host)
#server.listen(options.port)
server.start(num_processes=5)
print "Run server on %s:%s" % (options.host, options.port)
def main():
from django.core.wsgi import get_wsgi_application
import tornado.wsgi
wsgi_app = get_wsgi_application()
container = tornado.wsgi.WSGIContainer(wsgi_app)
setting = {
'cookie_secret': 'DFksdfsasdfkasdfFKwlwfsdfsa1204mx',
'template_path': os.path.join(os.path.dirname(__file__), 'templates'),
'static_path': os.path.join(os.path.dirname(__file__), 'static'),
'debug': False,
}
tornado_app = tornado.web.Application(
[
(r'/monitor', MonitorHandler),
(r'/ws/terminal', WebTerminalHandler),
(r'/kill', WebTerminalKillHandler),
(r'/ws/exec', ExecHandler),
(r"/static/(.*)", tornado.web.StaticFileHandler,
dict(path=os.path.join(os.path.dirname(__file__), "static"))),
('.*', tornado.web.FallbackHandler, dict(fallback=container)),
], **setting)
server = tornado.httpserver.HTTPServer(tornado_app)
server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == '__main__':
# tornado.options.parse_command_line()
# app = Application()
# server = tornado.httpserver.HTTPServer(app)
# server.bind(options.port, options.host)
# #server.listen(options.port)
# server.start(num_processes=5)
# print "Run server on %s:%s" % (options.host, options.port)
# tornado.ioloop.IOLoop.instance().start()
main()

View File

@ -29,7 +29,7 @@
protocol = 'wss://';
}
var wsUri = protocol + "{{ web_terminal_uri }}"; //请求的websocket url
var wsUri = protocol + document.URL.match(RegExp('//(.*?)/'))[1] + "{{ web_terminal_uri }}"; //请求的websocket url
var ws = new WebSocket(wsUri);
function createSystemMessage(message) {

View File

@ -213,15 +213,12 @@
if (login_type=='web'){
var g_url = '{{ web_kill_uri }}' + '?id=' + num;
} else {
var g_url = "{% url 'log_kill' %} }?id=" + num;
var g_url = "{% url 'log_kill' %}?id=" + num;
}
$.ajax({
type: "GET",
url: g_url+"&sessionid={{ session_id }}",
success: window.open("{% url 'log_list' 'online' %}", "_self")
});
$.get(g_url+"&sessionid={{ session_id }}", function () {
window.open("{% url 'log_list' 'online' %}", "_self")
})
}
</script>
{% endblock %}

View File

@ -48,7 +48,7 @@
protocol = 'wss://';
}
var endpoint = protocol + '{{ web_terminal_uri }}';
var endpoint = protocol + document.URL.match(RegExp('//(.*?)/'))[1] + '{{ web_terminal_uri }}';
if (window.WebSocket) {
this._connection = new WebSocket(endpoint);