How to get a protocol from IP and port(Proxy server with port) with nodejs?

岁酱吖の 提交于 2021-01-29 14:14:06

问题


I need to get protocol(HTTP or HTTPS) from a proxy server(IP address and port) with NodeJs.

I have a Nodejs server application. while installing the application in any windows machine, it should fetch the proxy configuration from the machine automatically and allows all the requests through the proxy server.

I am able to find the proxy server Ip and port from the windows registry(HKEY_USERS.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Internet Settings) but I don't how to identify the protocol from the proxy Ip and port.

Why do I need to identify the protocol(Http or HTTPS)? I am using the https://www.npmjs.com/package/https-proxy-agent npm module to create an agent to make any head and post requests.

Example: copied from https://www.npmjs.com/package/https-proxy-agent

var url = require('url');
var https = require('https');
var HttpsProxyAgent = require('https-proxy-agent');

// HTTP/HTTPS proxy to connect to
var proxy = process.env.http_proxy || 'http://168.63.76.32:3128';
console.log('using proxy server %j', proxy);

// HTTPS endpoint for the proxy to connect to
var endpoint = process.argv[2] || 'https://graph.facebook.com/tootallnate';
console.log('attempting to GET %j', endpoint);
var options = url.parse(endpoint);

// create an instance of the `HttpsProxyAgent` class with the proxy server information
var agent = new HttpsProxyAgent(proxy);
options.agent = agent;

https.get(options, function (res) {
  console.log('"response" event!', res.headers);
  res.pipe(process.stdout);
});

This below line is expecting the protocol with proxy info

 // HTTP/HTTPS proxy to connect to
    var proxy = process.env.http_proxy || 'http://168.63.76.32:3128';

I tried with https://www.npmjs.com/package/check-proxy but this is not suitable for my requirements.

Thanks,

来源:https://stackoverflow.com/questions/61212859/how-to-get-a-protocol-from-ip-and-portproxy-server-with-port-with-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!