Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverse proxy? #591

Open
koteesy opened this issue Jul 17, 2021 · 1 comment
Open

Reverse proxy? #591

koteesy opened this issue Jul 17, 2021 · 1 comment

Comments

@koteesy
Copy link

koteesy commented Jul 17, 2021

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;
  },
};
@wally94
Copy link

wally94 commented Aug 5, 2021

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
};
},
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants