anyproxy icon indicating copy to clipboard operation
anyproxy copied to clipboard

How to implement upstream proxy in any proxy

Open vegabird opened this issue 8 years ago • 10 comments

Hi,

how to implement outgoing proxy in any proxy ? for an example.

if i want to connect to google.com, follwing steps are needed for me. browser --> anyproxy --> internal proxy --> google.com

my question is how to redirect anyproxy request through our company internal proxy ?

I saw your reply in this question https://github.com/alibaba/anyproxy/issues/314

I have tried to modify the requestOption using following code as per your suggestion.

*beforeSendRequest(requestDetail) {

  const newOption = requestDetail.requestOptions;
  newOption.port = 8080;
  newOption.hostname = 'http://192.168.0.107';        
  return {        
    requestOptions: newOption
  }        
},

It shows following error from anyproxy.

Error: getaddrinfo ENOTFOUND http://192.168.0.107 http://192.168.0.107:8080

My upstream proxy running on http://192.168.0.107:8080

Could you please help me ?

vegabird avatar Jul 23 '18 11:07 vegabird

@vegabird The hostname should be just 192.168.0.107, you can also refer to the sample from AnyProxy.io modify-the-request-target

codingfishman avatar Jul 24 '18 15:07 codingfishman

@codingfishman Its working perfect with http, how can we implement same with https???

thepraveenpratap avatar Jul 25 '18 05:07 thepraveenpratap

@codingfishman Great. Its working properly with HTTP sites. But I tried with https sites. It does not work. this is my code snippent.

  • beforeSendRequest(requestDetail) {

      const newRequestOptions = requestDetail.requestOptions; 
      newRequestOptions.hostname = '127.0.0.1'
      newRequestOptions.port = '8099';
      newRequestOptions.path = requestDetail.url;
      return requestDetail;           
    

    },

could you please help me ??

vegabird avatar Jul 25 '18 09:07 vegabird

@vegabird @impraveensingh Can you post the error info when you implement the same with https?

If you are seeing the error Error: unable to verify the first certificate, it means the cert for your https site is not a public trusted one, you can turn off the verification manually by --ignore-unauthorized-ssl in command line, try anyproxy -h for the detail.

Or if you run AnyProxy as a npm module, please set option dangerouslyIgnoreUnauthorized:true, see Run-as-npm-module:options for detail.

codingfishman avatar Jul 29 '18 14:07 codingfishman

I need help with this as well as my code does not go to new location. What am I doing wrong.

 forceProxyHttps: true,
 dangerouslyIgnoreUnauthorized: false

Tried also:

 forceProxyHttps: false,
 dangerouslyIgnoreUnauthorized: true

And also withouth beforeDealHttpsRequest

 *beforeDealHttpsRequest(requestDetail) { //eslint-disable-line
        return true;
    },
    *beforeSendRequest(requestDetail) { //eslint-disable-line
        if (requestDetail.url === "https://api-qa.com/api/v5/config/config") {
            let body = JSON.parse(requestDetail.requestData.toString());
            body.app.id = "57e13d65ee991d8438000061";
            const newRequestOptions = requestDetail.requestOptions;
            newRequestOptions.hostname = "apiqa.com";
            newRequestOptions.path = "api/v5/config";
            return Object.assign({
                url: "https://apiqa.com/api/v5/config",
                requestOptions: newRequestOptions,
                requestData: JSON.stringify(body)
            }, requestDetail);
        }
    },

puppeteer701vungle avatar Nov 20 '18 18:11 puppeteer701vungle

@vegabird @impraveensingh Has this problem been solved? ‘https’ does not work

zhuzongzhi avatar Mar 19 '19 03:03 zhuzongzhi

`# AnyProxy Inner Error Oops! Error happend when AnyProxy handle the request. This is an error occurred inside AnyProxy, not from your target website.

Error: Error: socket hang up URL: https://jianshu.com/ Error: socket hang up at TLSSocket.onHangUp (_tls_wrap.js:1148:19) at Object.onceWrapper (events.js:313:30) at emitNone (events.js:111:20) at TLSSocket.emit (events.js:208:7) at endReadableNT (_stream_readable.js:1064:12) at _combinedTickCallback (internal/process/next_tick.js:139:11) at process._tickCallback (internal/process/next_tick.js:181:9) @codingfishman ‘https’ does not work. beforeSendRequest(requestDetail) {

const newRequestOptions = requestDetail.requestOptions; newRequestOptions.hostname = '127.0.0.1' newRequestOptions.port = '8099'; newRequestOptions.path = requestDetail.url; return requestDetail;
},`

zhuzongzhi avatar Mar 19 '19 03:03 zhuzongzhi

@zhuzongzhi Hi, no. it was not working with https sites. but we found a workaround for this issue. We used httpsproxyagent plugin. Please check the below code.

				if (requestDetail.protocol == "http") {
					var proxy = process.env.http_proxy || proxyData.proxyProtocol + '://' + proxyData.proxyHost + ':' + proxyData.proxyPort;
					var agent = new HttpProxyAgent(proxy);
					const newRequestOptions = requestDetail.requestOptions;
					newRequestOptions.agent = agent;
					return requestDetail;
				}
				else if (requestDetail.protocol == "https") {
					var proxy = process.env.http_proxy || proxyData.proxyProtocol + '://' + proxyData.proxyHost + ':' + proxyData.proxyPort;
					var agent = new HttpsProxyAgent(proxy);
					const newRequestOptions = requestDetail.requestOptions;
					newRequestOptions.agent = agent;
					return requestDetail;
				}

But this is a temporary solution because it doesn't work with proxy authentications like basic, ntlm. @codingfishman has to help us to solve it permanently.

vegabird avatar Jul 02 '19 11:07 vegabird

HI @codingfishman, we used below code.

beforeSendRequest(requestDetail) {

const newRequestOptions = requestDetail.requestOptions; newRequestOptions.hostname = '127.0.0.1' newRequestOptions.port = '8099'; newRequestOptions.path = requestDetail.url; return requestDetail;
},

This code only work with http. in https, request is not reaching to the server. I checked the reason for this. The reason is CONNECT method should be called before making https request. It seems that your plugin missing to call connect method. Can you please solve this issue ? Anyproxy is a wonderful proxy in the node modules. If this issue is solved, it will be the top node plugin in the world.

vegabird avatar Jul 02 '19 11:07 vegabird

I also met with the issue described above, try to find some solutions and share it here, if anyone has updates related to this case, It would be much appreciated

oleza avatar Aug 02 '19 14:08 oleza