Skip to content

Instantly share code, notes, and snippets.

@meotimdihia
Created July 1, 2023 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meotimdihia/283b582e05d5981e2b08ece1d5a7f486 to your computer and use it in GitHub Desktop.
Save meotimdihia/283b582e05d5981e2b08ece1d5a7f486 to your computer and use it in GitHub Desktop.
const ProxyChain = require("proxy-chain")
const server = new ProxyChain.Server({
// Port where the server will listen. By default 8000.
port: 8000,
// Enables verbose logging
verbose: true,
// Custom user-defined function to authenticate incoming proxy requests,
// and optionally provide the URL to chained upstream proxy.
// The function must return an object (or promise resolving to the object) with the following signature:
// { requestAuthentication: Boolean, upstreamProxyUrl: String }
// If the function is not defined or is null, the server runs in simple mode.
// Note that the function takes a single argument with the following properties:
// * request - An instance of http.IncomingMessage class with information about the client request
// (which is either HTTP CONNECT for SSL protocol, or other HTTP request)
// * username - Username parsed from the Proxy-Authorization header. Might be empty string.
// * password - Password parsed from the Proxy-Authorization header. Might be empty string.
// * hostname - Hostname of the target server
// * port - Port of the target server
// * isHttp - If true, this is a HTTP request, otherwise it's a HTTP CONNECT tunnel for SSL
// or other protocols
// * connectionId - Unique ID of the HTTP connection. It can be used to obtain traffic statistics.
prepareRequestFunction: ({
request,
username,
password,
hostname,
port,
isHttp,
connectionId
}) => {
return {
// Sets up an upstream HTTP proxy to which all the requests are forwarded.
// If null, the proxy works in direct mode, i.e. the connection is forwarded directly
// to the target server. This field is ignored if "requestAuthentication" is true.
// The username and password must be URI-encoded.
upstreamProxyUrl: `http://username:password@p.webshare.io`,
// If "requestAuthentication" is true, you can use the following property
// to define a custom error message to return to the client instead of the default "Proxy credentials required"
failMsg: "Bad username or password, please try again."
}
}
})
server.listen(() => {
console.log(`Proxy server is listening on port ${server.port}`)
})
// Emitted when HTTP connection is closed
server.on("connectionClosed", ({ connectionId, stats }) => {
console.log(`Connection ${connectionId} closed`)
console.dir(stats)
})
// Emitted when HTTP request fails
server.on("requestFailed", ({ request, error }) => {
console.log(`Request ${request.url} failed`)
console.error(error)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment