我有以下 Node.js 代码:
var exec=require('child_process').exec;
var base_ctrl_port=8118;
var base_data_port=9050;
var nConns=10;
watchdogCycle();
setInterval(watchdogCycle, 60000); //check health every 60 seconds...
function watchdogCycle(){
console.log("\n");
for(var i=0;i<nConns;i++){
var data_port=base_data_port+i;
data_port=data_port+"";
var curl=exec('curl -b -s --socks5 localhost:'+data_port+' http://ifconfig.me',{timeout:10000},
function(err,stdout,stderr){
console.log(stdout);
if(err!=null){
getNewIP(i); //PROBLEM: i is always 10!!!
}
});
}
}
function getNewIP(offset){
console.log("Getting new IP address for tor data port: "+(base_data_port+offset+0)+"...");
var ctrl_port=base_ctrl_port+offset;
var nc=exec('(echo AUTHENTICATE \'\"xxxxxx\"\'; echo SIGNAL NEWNYM; echo quit) | nc localhost '+ctrl_port,
function(err,stdout,stderr){
console.log(stdout);
});
}
问题是参数i
, intogetNewIP(i)
总是 10!
我已经阅读了一些关于递归的内容,但我不知道如何修改此代码,使其i
为 0..9 而并非总是 10。
提前谢谢了,