anyproxy/rule_sample/sample_modify_request_path.js

30 lines
978 B
JavaScript
Raw Normal View History

2017-12-01 21:30:49 +08:00
/*
sample:
redirect all https://httpbin.org/user-agent requests to http://localhost:8008/index.html
test:
curl https://httpbin.org/user-agent --proxy http://127.0.0.1:8001
expected response:
'hello world' from 127.0.0.1:8001/index.html
*/
module.exports = {
*beforeSendRequest(requestDetail) {
if (requestDetail.url.indexOf('https://httpbin.org/user-agent') === 0) {
2018-08-28 11:59:43 +08:00
console.info(requestDetail._req.connection._peername);
2017-12-01 21:30:49 +08:00
const newRequestOptions = requestDetail.requestOptions;
2018-08-28 11:59:43 +08:00
requestDetail.protocol = 'https';
2017-12-01 21:30:49 +08:00
newRequestOptions.hostname = '127.0.0.1'
2018-08-28 11:59:43 +08:00
newRequestOptions.port = '3001';
newRequestOptions.path = '/test';
2017-12-01 21:30:49 +08:00
newRequestOptions.method = 'GET';
return requestDetail;
}
2018-08-28 11:59:43 +08:00
if (requestDetail.url.indexOf('http://mobilegw.stable.alipay.net/mgw.htm') === 0) {
console.info(requestDetail.requestData.toString())
}
2017-12-01 21:30:49 +08:00
},
*beforeDealHttpsRequest(requestDetail) {
return true;
}
};