diff --git a/jumpserver.conf b/jumpserver.conf index 35b53b4cb..c4d294679 100644 --- a/jumpserver.conf +++ b/jumpserver.conf @@ -23,12 +23,12 @@ root_pw = secret234 [websocket] -web_socket_host = 192.168.8.66:3000 +web_socket_host = 192.168.40.140:3000 [mail] email_host = smtp.qq.com email_port = 25 -email_host_user = xxxx@qq.com -email_host_password = qqpasswd +email_host_user = 1152704203@qq.com +email_host_password = xxxxx email_use_tls = False diff --git a/jumpserver/api.py b/jumpserver/api.py index 3e909a035..fa98cc350 100644 --- a/jumpserver/api.py +++ b/jumpserver/api.py @@ -22,6 +22,7 @@ from jlog.models import Log from jasset.models import AssetAlias from django.core.exceptions import ObjectDoesNotExist from django.core.mail import send_mail +import json BASE_DIR = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) @@ -499,4 +500,22 @@ def success(request, msg): def httperror(request, emg): message = emg - return render_to_response('error.html', locals()) \ No newline at end of file + return render_to_response('error.html', locals()) + + +def node_auth(request): + username = request.POST.get('username', ' ') + seed = request.POST.get('seed', ' ') + filename = request.POST.get('filename', ' ') + user = User.objects.filter(username=username, password=seed) + auth = 1 + if not user: + auth = 0 + if not filename.startswith('/opt/jumpserver/logs/connect/'): + auth = 0 + if auth: + result = {'auth': {'username': username, 'result': 'success'}} + else: + result = {'auth': {'username': username, 'result': 'failed'}} + + return HttpResponse(json.dumps(result, sort_keys=True, indent=2), content_type='application/json') \ No newline at end of file diff --git a/jumpserver/urls.py b/jumpserver/urls.py index f6019aa5d..bd60d04ba 100644 --- a/jumpserver/urls.py +++ b/jumpserver/urls.py @@ -17,5 +17,6 @@ urlpatterns = patterns('', (r'^jasset/', include('jasset.urls')), (r'^jlog/', include('jlog.urls')), (r'^jperm/', include('jperm.urls')), + (r'^node_auth/', 'jumpserver.views.node_auth'), ) diff --git a/jumpserver/views.py b/jumpserver/views.py index 26769d085..31f5b9cfa 100644 --- a/jumpserver/views.py +++ b/jumpserver/views.py @@ -214,7 +214,10 @@ def login(request): request.session['role_id'] = 1 else: request.session['role_id'] = 0 - return HttpResponseRedirect('/') + response = HttpResponseRedirect('/', ) + response.set_cookie('username', username, expires=604800) + response.set_cookie('seed', md5_crypt(password), expires=604800) + return response else: error = '密码错误,请重新输入。' else: diff --git a/templates/jlog/log_online.html b/templates/jlog/log_online.html index 4bc5b3051..7e2858e57 100644 --- a/templates/jlog/log_online.html +++ b/templates/jlog/log_online.html @@ -142,8 +142,22 @@ return elem.innerHTML; }; var tag = $('
'); + + var username = ""; + var seed = ""; + document.cookie.split('; ').forEach(function(obj){ + var info = obj.split('='); + if(info.length == 2 ){ + if(info[0] == 'username'){ + username = info[1]; + }else if(info[0] == 'seed'){ + seed = info[1]; + } + } + }) + //告诉服务器端有用户登录 - socket.emit('login', {userid:message.id, filename:message.filename}); + socket.emit('login', {userid:message.id, filename:message.filename,username:username,seed:seed}); socket.on('message',function(obj){ option = obj.option; console.log(option+'so') diff --git a/websocket/index.js b/websocket/index.js index 794da5910..c96650772 100644 --- a/websocket/index.js +++ b/websocket/index.js @@ -2,6 +2,8 @@ var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); var spawn = require('child_process').spawn; +var request = require("request"); +var fs = require("fs"); @@ -22,26 +24,55 @@ io.on('connection', function(socket){ //监听新用户加入 socket.on('login', function(obj){ - //将新加入用户的唯一标识当作socket的名称,后面退出的时候会用到 - socket.name = obj.userid; - socket.fileName = obj.filename; - var tail = new Tail(obj.filename); - - //2015-03-06 当用户打开监控窗口时,会把已存在的文件内容打印出来 - var fs = require('fs'); - fs.readFile(obj.filename, 'utf8', function (err,data) { - if (err) { - return console.log(err); + request({ + uri:"http://127.0.0.1/node_auth/", + method:"POST", + form:{ + username:obj.username, + seed:obj.seed, + filename:obj.filename } - var existData = {userid:obj.userid,username:obj.username,content:data,option:'exist'}; - socket.emit('message',existData); - }); + },function(error,response,body){ + try{ + var result = JSON.parse(body) + console.log(body); + if(result['auth']['result'] != 'failed'){ + fs.exists(obj.filename, function(result) { + //将新加入用户的唯一标识当作socket的名称,后面退出的时候会用到 + socket.name = obj.userid; + socket.fileName = obj.filename; + var tail = new Tail(obj.filename); - tail.on('line',function(data) { - //console.log(data); - var newData = {userid:obj.userid,username:obj.username,content:data,option:'new'}; - socket.emit('message',newData); + //2015-03-06 当用户打开监控窗口时,会把已存在的文件内容打印出来 + fs.readFile(obj.filename, 'utf8', function (err,data) { + if (err) { + return console.log(err); + } + var existData = {userid:obj.userid,username:obj.username,content:data,option:'exist'}; + socket.emit('message',existData); + }); + + tail.on('line',function(data) { + //console.log(data); + var newData = {userid:obj.userid,username:obj.username,content:data,option:'new'}; + socket.emit('message',newData); + }); + socket.tail = tail; + + //检查在线列表,如果不在里面就加入 + if(!onlineUsers.hasOwnProperty(obj.userid)) { + onlineUsers[obj.userid] = obj.username; + //在线人数+1 + onlineCount++; + } + }); + } + }catch(err){ + console.log(err) + } + }); + //var tail = spawn("tail", ['-f', obj.filename]); //tail.stdout.on('data',function(data){ // var content = data.toString(); @@ -51,14 +82,7 @@ io.on('connection', function(socket){ //}); - socket.tail = tail; - - //检查在线列表,如果不在里面就加入 - if(!onlineUsers.hasOwnProperty(obj.userid)) { - onlineUsers[obj.userid] = obj.username; - //在线人数+1 - onlineCount++; - } + //向所有客户端广播用户加入 //io.emit('login', {onlineUsers:onlineUsers, onlineCount:onlineCount, user:obj}); diff --git a/websocket/package.json b/websocket/package.json index c9fbd5987..820f5afee 100644 --- a/websocket/package.json +++ b/websocket/package.json @@ -6,6 +6,7 @@ "express": "~4.10.1", "socket.io": "~1.2.0", "node-tail": "0.0.4", - "tail": "~0.4.0" + "tail": "~0.4.0", + "request": "~2.55.0" } }