anyproxy Reverse proxy?

pobjuy32  于 4个月前  发布在  其他
关注(0)|答案(1)|浏览(109)

Hello, for example, i have few proxies:

const proxies = [
      '51.222.21.93:80',
      '107.1.80.135:80',
      '34.234.164.65:80'
];

And i have anyProxy server on 127.0.0.1:5050

I want do something like:

Request come to 127.0.0.1:5050 then i'll get random proxy from my proxy list, then anyProxy request with that proxy.

How i can implement that?

module.exports = {
  summary: 'a rule to inject proxy',
  * beforeSendRequest (requestDetail) {
    const proxies = [
      '51.222.21.93:80',
      '107.1.80.135:80',
      '34.234.164.65:80'
    ];

    const item = proxies[Math.floor(Math.random()*proxies.length)];

    // Where i can put my proxy?

    console.log(item);

    return null;
  },
};
r8xiu3jd

r8xiu3jd1#

My English is not good, please use Google direct translation, I hope it can help you.

你可以了解一下node的 http模块的request类。
根据anyproxy的源码我发现,请求远程服务器使用的是http.request,requestDetail.requestOptions就是http.request 和https.request 的options。
http.request 示例
const req = https.request(options, res => {

})
req.end()

查询了相关文档 https://www.cnblogs.com/ference/p/4014210.html,可以如下实现
http相关实现方法:
module.exports = {
summary: 'a rule to inject proxy',

  • beforeSendRequest (requestDetail) {
    let Options=requestDetail.requestOptions;
    //修改Options ,add proxyOptions
    ......
    requestDetail.requestOptions=Options;
    return requestDetail
    },
    };

https根据文档介绍自行实现代理类并修改Options即可。

另一种实现方法更为简单,直接自己使用请求库(如Axios、Superagent、ruquest等)进行请求,并直接返回
const request = require('request'),
module.exports = {
summary: 'a rule to inject proxy',

  • beforeSendRequest (requestDetail) {

// 要访问的目标地址
let url = requestDetail.url
let method = requestDetail.requestOptions.method
// 代理服务器ip和端口
let proxy_ip = '47.115.36.94';
let proxy_port = 16816;

// 完整代理服务器url
let proxy = util.format('http://%s:%d', proxy_ip, proxy_port);

function req () {
return new Promise((resolve, reject) => {
// 发起请求
request({
url: url,
method: 'GET',
data: {
.....
},
proxy: proxy,
headers: {
....
},
encoding: null, // 方便解压缩返回的数据
}, function (error, res, body) {
//.....todo....
resolve(body)

});
})

}

let response=await req();

return {
response: response
};
},
};

相关问题