2017-07-30 65 views
0

我需要一种方法来保护我的WebSockets。拥有自己凭据的Websocket安全认证

喜欢的东西onConnect->checkcredentials if(credentials===false){die()}

但随着自己的凭证数据发送到服务器。我怎么才能意识到,没有令牌或饼干?如果没有,是否有其他解决方案可以实时进行安全通信?

回答

2

我需要一个方法来保护我的WebSockets。

解决方法:插座握手查询

用这种方法,只知道参数和秘密将通过握手网关获取客户端(中间件)

[编辑:使用新socket.io 2.0it has fixed an issue regarding the query]

@客户

io('http://216.157.91.131:8080/', { query: "s=$€CR€T" }); 

@服务器

var theSecret = "S€CR€T"; 
io.use(function(socket, next) { 
    var handshakeSecret = socket.request._query['s']; 
    console.log("middleware:", handshakeSecret); 
    try{ 
    if(handshakeSecret=theSecret){next();}else{socket.disconnect();} 
    }catch(e){socket.disconnect();} //prevent error - crash 
}); 

在这种情况下theSecret是都是一样的,可以在数据库中的特定检查。

解决方案:Auth或GTFO! (Kick)

如果在超时时间内没有提供正确的凭据,您也可以连接到断开连接(计时器踢)的厄运客户端。 实施例:

const SOCKET_AUTH_TIMEOUT = 120000; //2 minutes in ms 
io.on('connection', function(socket){ 
    //.: ALL SOCKET CONNECTIONS :. 
    console.log("[+] (unauthorized) client connected : "+socket.id); 
    //begin doom countdown... 
    socket.doom = setTimeout(KickSocketClient.bind(null, socket.id), SOCKET_AUTH_TIMEOUT); 
    //warn this client : (example) 
    //@ client : 'You got '+TimeoutInMinutes+' minutes to authorize before being kicked.' 
    socket.emit('auth_required', SOCKET_AUTH_TIMEOUT); 
    //.: Handle Authorization :. 
    socket.on('auth',function(authRequest){ 
    /* your logic to verify the incoming authRequest goes here, for example :*/ 
    if(DATABASE[authRequest.user]){ //user exists in imaginary in-memory database 
     if(DATABASE[authRequest.user].password == authRequest.password){ //correct password, success! 
     socket.emit('authed', DATABASE[authRequest.user]); //maybe return user data 
     socket.authed = true; //set this socket client as authorized (useful for future requests) 
     //now clear the timeout of d00m! (prevent the disconnection, now the client is authed) 
     clearTimeout(socket.doom); 
     }else{socket.emit('error','credentials');} 
    }else{socket.emit('error','credentials');} 
    }); 
    //.: Handle Disconnections :. 
    socket.on('disconnect', function(){ 
    if(socket.authed){console.log("[+] client disconnected : "+socket.id);} 
    else{console.log("[+] (unauthorized) client disconnected : "+socket.id);} 
    }); 
    //.: Only for Authorized Clients :. 
    socket.on('secret', function(){ 
    if(socket.authed){ 
     console.log("[o] client : "+socket.id+" requested the secret"); 
     socket.emit('return','the secret to life'); //here you go! 
    }else{socket.disconnect();} // disconnect the unauthorized client 
    }); 
}); 

function KickSocketClient(sid){ 
if (io.sockets.connected[sid]) { 
    console.log("[kick] client ("+sid+") : unauthorized status for too long"); 
    io.sockets.connected[sid].disconnect(); 
}else{ console.log("<!> : [kick] client ("+sid+") not found!"); } 
} 

如果客户端不在SOCKET_AUTH_TIMEOUT指定的时间内不AUTH它将被断开。

我也包含在例如,对于一个只有授权的客户端小的演示(而unauthed =断开连接请求)

你也可以让他们加入特定的私有命名空间一旦authed,使全球广播到所有客户端不包括未知的侦听器。

祝你好运,希望它有帮助。

注:

有没有其他的解决方案,实时进行安全通信?

要回答这个问题,您需要先考虑威胁。有很多安全的方法,例如,通过SSL使用socket.io。

我提到的解决方案是为了避免未经授权的客户端继续在线并访问事件/资源/等等...