const http = require('http');
const qs = require('querystring');
const md5 = require('md5-node');
//第一步:获取nonce
let authenticate = '';
http.get("http://admin:woshimima123456@110.110.110.110", function (gres) {
gres.on('data', function (d) {
}).on('end', function () {
http.get(gres.headers.location, function (inres) {
inres.on('data', function (d) {
}).on('end', function () {
//获取nonce
authenticate = inres.headers['www-authenticate'];
// console.log(authenticate);
// let nonce = authenticate.match(/\snonce="([^"]+)/)[1];
// let opaque = authenticate.match(/\sopaque="([^"]+)/)[1];
// console.log(nonce, opaque);
});
});
});
}).on('error', function (e) {
console.log("Got error: " + e.message);
});
//----------------------------------------------------------------
//第二步:获取使用nonce生成response
let index = 66;
let tno = 9230;
let outno = '867316666666';
let post_data = {
SlcType: 1,
CompanyGroup: 0,
DepartmentGroup: 0,
ExtCaller: outno,
IntCaller: tno,
Password: '0000',
LowClass: 0,
uatt_fxs_class_high_mask: 1,
HighClass: 11,
uatt_ptt_active_high: 1,
ptt_talk_class: 0,
ptt_call_mode: 0,
uatt_em_four_line: 0,
uatt_em_tle: 0,
uatt_em_mode: 0,
uatt_em_tleid: 0,
uatt_em_protocol: 0,
uatt_em_wink_tck: 0,
em_ptt_talk_class: 0,
em_ptt_call_mode: 0,
uatt_cs_ring_count: 0,
uatt_cs_ring_interval: 0,
outGain_selobj: 0,
outGain: 255,
inGain_selobj: 0,
inGain: 255,
HotPhone: "",
ok: "%C8%B7%B6%A8",
}; //这是需要提交的数据
var content = qs.stringify(post_data);
let SetTemplate = 'Digest username="admin", realm="woshi_realm", nonce="51f900843858f5a59c93c19ddf96f4e1", uri="/ndms/CfgLineUserSetItem?index=66", algorithm=MD5, response="e25910a4aec0bb4e11372fe5ff8e92cd", opaque="5ccc069c403ebaf9f0171e9517f40e41", qop=auth, nc=0000000d, cnonce="00b090339f294b5c"';
let nonce = authenticate.match(/\snonce="([^"]+)/)[1]; //从服务器获取的nonce
let uri = `/ndms/CfgLineUserSetItem?index=${index}`;
let cnonce = SetTemplate.match(/\scnonce="([^"]+)/)[1];
let nc = SetTemplate.match(/\snc=([^,]+)/)[1];
let a1Hash = md5("admin: :woshimima123456");
let a2Hash = md5(`POST:${uri}`);
//这里的response计算是关键!!!通过md5计算拼接好的字符串
let response = md5(`${a1Hash}:${nonce}:${nc}:${cnonce}:auth:${a2Hash}`);
let finalSetAuth = SetTemplate.replace(/index=\d+/, `index=${index}`).replace(/response="[^"]+"/, `response="${response}"`)
.replace(/\snonce="[^"]+"/, ` nonce="${nonce}"`);
// console.log(response);
// console.log(finalSetAuth);
var options = {
host: '110.110.110.240', //请求服务器地址
port: 80,
path: uri,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': content.length,
'Authorization': finalSetAuth
}
};
// console.log("post options:\n", options);
// console.log("content:", content);
// console.log("\n");
var req = http.request(options, function (res) {
// console.log( "statusCode: " , res.statusCode);
// console.log( "headers: " , res.headers);
var _data = '';
res.on('data', function (chunk) {
_data += chunk;
});
res.on('end', function () {
console.log("--->>")
if (res.statusCode == '302') {
console.log("result:", "OK");
} else {
console.log("\n--->>\nresult:", _data);
}
resolve("ook");
});
});
req.write(content);
req.end();
参考:
HTTP认证之摘要认证——Digest(一)
https://www.cnblogs.com/xiaoxiaotank/p/11078571.html
HTTP认证之摘要认证——Digest(二)