proxying-agent
v2.4.0
Published
Node HTTP/HTTPS Forward Proxy Agent
Downloads
90,575
Readme
Node HTTP/HTTPS Forward Proxy Agent
This is a node http agent capable of forward proxying HTTP/HTTPS requests.
It supports the following:
- Connect to a proxy with either HTTP or HTTPS
- Proxying to a remote server using SSL tunneling (via the http CONNECT method)
- Authenticate with a proxy with Basic authentication
- Authenticate with a proxy with NTLM authentication (beta)
- Set a global agent for all http and https requests
The agent inherits directly from the http.Agent
Node object so it benefits from all
the socket handling goodies that come with it.
Installation
npm install proxying-agent
Usage
create(options, target)
Returns a new agent configured correctly to proxy to the specified target.
options
- (string|object) proxy url string or object with the following options:proxy
- Specifies the proxy url. The supported format ishttp[s]://[auth@]host:port
whereauth
is the authentication information in the form ofusername:password
. The authentication information can also be in the form of a Base64 encodeduser:password
, e.g.http://[email protected]:8080
. if the username for NTLM needs to be in thedomain\username
format, specifydomain%5Cusername
insteadtlsOptions
- TLS connection options to use when the target server protocol ishttps
. See http://nodejs.org/api/tls.html#tls_tls_connect_options_callback for a list of available optionsauthType
- Proxy authentication type. Possible values arebasic
andntlm
(default isbasic
)ntlm
- (beta) applicable only ifauthType
isntlm
. Supported fields:domain
(required) - the NTLM domainworkstation
(optional) - the local machine hostname (os.hostname() is not specified)
target
- the target url that the agent is to proxy
globalize(options)
Set a global agent to forward all http and https requests through the specified proxy.
Make sure to call this method before invoking any other http request.
After globalize
is invoked, all http and https requests will automatically tunnel through the proxy.
options
- Seecreate
method
require('proxying-agent').globalize('http://proxy.example.com:8080');
HTTP Server
var proxyingAgent = require('proxying-agent').create('http://proxy.example.com:8080', 'http://example.com');
var req = http.request({
host: 'example.com',
port: 80,
agent: proxyingAgent
});
HTTPS Server
var proxyingAgent = require('proxying-agent').create('http://proxy.example.com:8080', 'https://example.com');
var req = https.request({
host: 'example.com',
port: 443,
agent: proxyingAgent
});
Basic Authentication
var proxyingAgent = require('proxying-agent').create('http://username:[email protected]:8080', 'https://example.com');
var req = https.request({
host: 'example.com',
port: 443,
agent: proxyingAgent
});
NTLM Authentication
When authenticating using NTLM it is important to delay sending the request data until the socket is assigned to the request. Failing to do so will result in the socket being prematurely closed, preventing the NTLM handshake from completing.
var proxyOptions = {
proxy: 'http://username:[email protected]:8080',
authType: 'ntlm',
ntlm: {
domain: 'MYDOMAIN'
}
};
var proxyingAgent = require('proxying-agent').create(proxyOptions, 'https://example.com');
var req = https.request({
host: 'example.com',
port: 443,
agent: proxyingAgent
});
req.on('socket', function(socket) {
req.write('DATA');
req.end();
});
References
- NTLM code was forked from https://github.com/SamDecrock/node-http-ntlm.git
- NTLM Authentication Scheme for HTTP - http://www.innovation.ch/personal/ronald/ntlm.html
Copyright and License
Copyright 2016 Capriza. Code released under the MIT license