websocket auth add

pull/7/merge
ibuler 2015-05-28 17:00:03 +08:00
parent ce99bf2266
commit 1662269153
7 changed files with 94 additions and 32 deletions

View File

@ -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

View File

@ -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())
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')

View File

@ -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'),
)

View File

@ -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:

View File

@ -142,8 +142,22 @@
return elem.innerHTML;
};
var tag = $('<div id="log" style="height: 500px;overflow: auto;background-color: rgba(0, 0, 0, 0);"></div>');
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')

View File

@ -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});

View File

@ -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"
}
}