2017-11-12 5 views
1

リクエストモジュールには、localAddressパラメータがあります。ニードルモジュールでリクエストを別のIP経由で送信することはできますか?

options = { 
     url: "https://ru.tradeskinsfast.com/ajax/botsinventory", 
     method: "post", 
     headers: { 
      'accept': 'application/json, text/javascript, */*; q=0.01', 
      'accept-encoding' : 'gzip :deflate, br', 
      'accept-language': 'ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4', 
     }, 
     localAdress: someIp, 
} 
request(options, function(error, response, body){} 

ニードルモジュールでどうすればいいですか?

答えて

0

針は、ラッパーの周りrequestようhttp.requestノードがありますが、具体的localAddressを渡すか、http.requestに至るまで、任意のオプションを渡すことneedleことはできませんまだです。

ニードルサポートかかわらず、要求のためのカスタムhttp.Agentを追加しないとagent.createConnection方法は、それがstandard socket connectを使用するようlocalAddressを渡すことをサポートしています。

セットアップは少し複雑ですが、デフォルトの動作を変更することは可能です。

const http = require('http') 
const https = require('https') 
const needle = require('needle') 

class HttpAgentLocal extends http.Agent { 

    constructor(options){ 
    super(options) 
    if (options && options.localAddress) this._localAddress = options.localAddress 
    } 

    createConnection(options, callback){ 
    if (this._localAddress) options.localAddress = this._localAddress 
    return super.createConnection(options, callback) 
    } 
} 

class HttpsAgentLocal extends https.Agent { 

    constructor(options){ 
    this._localAddress = options.localAddress 
    } 

    createConnection(options, callback){ 
    options.localAddress = this._localAddress 
    return super.createConnection(options, callback) 
    } 
} 


let server = http.createServer((req, res) => { 
    console.log('request: %s - %s', req.method,req.url, req.connection.remoteAddress) 
    res.end('hello\n') 
}) 

server.listen(3121, async()=> { 
    console.log('listening') 
    const agent = new HttpAgentLocal({ localAddress: '10.8.8.8' }) 
    let res = await needle('get','http://localhost:3121/', null, { agent: agent }) 
    console.log(res.body.toString()) 
    server.close() 
}) 
関連する問題