Commit 2029ef9b authored by xzj's avatar xzj

支持_ip_参数,重写请求

parent 76c67b5d
'use strict' 'use strict'
const request = require('request') const request = require('request')
const debug = require('debug')('request-proxy') const debug = require('debug')('request-proxy')
const qs = require('querystring')
const Agent = require('agentkeepalive'); const Agent = require('agentkeepalive');
const HttpsAgent = require('agentkeepalive').HttpsAgent; const HttpsAgent = require('agentkeepalive').HttpsAgent;
...@@ -48,6 +49,11 @@ module.exports = function (options) { ...@@ -48,6 +49,11 @@ module.exports = function (options) {
timeout: options.timeout timeout: options.timeout
} }
const isProduction = process.env.NODE_ENV === 'production'
const rewriteUrl = function (source, to) {
return source.replace(/\/\/([^\/]+)\//, `//${to}/`).replace(/_ip_=[\w\.:%]+&?/, '')
}
const httpAgent = new Agent(options.agent); const httpAgent = new Agent(options.agent);
const httpsAgent = new HttpsAgent(); const httpsAgent = new HttpsAgent();
...@@ -67,6 +73,10 @@ module.exports = function (options) { ...@@ -67,6 +73,10 @@ module.exports = function (options) {
option = Object.assign({}, curlDefault, option) option = Object.assign({}, curlDefault, option)
extra = Object.assign({}, extraDefault, extra) extra = Object.assign({}, extraDefault, extra)
if (!isProduction && ctx.request.query['_ip_']) {
option.url = rewriteUrl(option.url, ctx.request.query['_ip_'])
}
debug(option) debug(option)
if (!option.agent && !/^https/.test(option.url)) { if (!option.agent && !/^https/.test(option.url)) {
...@@ -100,6 +110,10 @@ module.exports = function (options) { ...@@ -100,6 +110,10 @@ module.exports = function (options) {
} }
option = Object.assign({}, pipeDefault, option) option = Object.assign({}, pipeDefault, option)
if (!isProduction && ctx.request.query['_ip_']) {
option.url = rewriteUrl(option.url, ctx.request.query['_ip_'])
}
if (!option.method) { if (!option.method) {
option.method = ctx.method option.method = ctx.method
} }
......
{ {
"name": "request-proxy", "name": "request-proxy",
"version": "1.0.4", "version": "1.0.5",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment