0

我正在使用一个简单的套接字脚本来建立到我的战地英雄服务器的 rcon 连接。我需要从返回的数据中获取种子,然后将其与密码连接起来,以用于创建我需要的登录哈希。但是字符串不会与普通的字符串方法连接。如果我单独输出字符串,它们会显示得很好,但它们根本不会组合,所以我可以散列它们。

var net = require('net');
var crypto = require('crypto');
var md5sum = crypto.createHash('md5');

var HOST = '<ip address>', PORT = <port>,
PASSWORD = '<password>';
var client = new net.Socket();
client.connect(PORT, HOST, function() {
    console.log('CONNECTED TO: ' + HOST + ':' + PORT);
});

client.on('data', function(data) {
    console.log(data.toString());
    response = data.toString();
    if (data.toString().slice(0,10) == "### Digest") {
        var seed = response.slice(17); 
        auth = seed.concat(PASSWORD);
        console.log('auth: '+auth);
        hash = require('crypto').createHash('md5').update(auth).digest("hex");
        console.log(hash);
        //client.write('login '+hash+' \n');
    }
    //client.destroy();
});

client.on('close', function() {
    //do something on close
});

client.on('error', function(err) {
    console.log(err);
});
4

1 回答 1

0

我找到了一个适合我的解决方案。不确定我是否结束了,但我决定将两个字符串都传递给新的缓冲区。然后我将它们传递给一个数组来组合它们。(耸耸肩)我认为会的。有更好的解决方案吗?

var net = require('net');
var crypto = require('crypto');
var md5sum = crypto.createHash('md5');

var HOST = '<ip address>', PORT = <port>,
PASSWORD = '<password>';
var client = new net.Socket();
client.connect(PORT, HOST, function() {
    console.log('CONNECTED TO: ' + HOST + ':' + PORT);
});

client.on('data', function(data) {
    console.log(data.toString());
    response = data.toString();
    if (data.toString().slice(0,10) == "### Digest") {
        var a = new Buffer(response.slice(17));
        var b = new Buffer(PASSWORD);
        var auth = new Array();
        auth += a.toString().replace(/\n/g,'');
        auth += b.toString();
        hash = require('crypto').createHash('md5').update(auth).digest("hex");
        client.write('login '+hash+' \n');
    }
    //client.destroy();
});

client.on('close', function() {
    //do something on close
});

client.on('error', function(err) {
    console.log(err);
});
于 2012-10-18T15:33:30.883 回答