Skip to content

Instantly share code, notes, and snippets.

@jancurn
Last active February 16, 2021 17:28
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 jancurn/5e5b9955cdfc27d504ef2a8b02c0ac29 to your computer and use it in GitHub Desktop.
Save jancurn/5e5b9955cdfc27d504ef2a8b02c0ac29 to your computer and use it in GitHub Desktop.
This example demonstrates how to create a tunnel via Apify's HTTP proxy service. For details, see https://blog.apify.com/tunneling-arbitrary-protocols-over-http-proxy-with-static-ip-address-b3a2222191ff
const { createTunnel, closeTunnel, redactUrl } = require('proxy-chain');
(async () => {
// Select the proxy to tunnel through. Note that some proxies do not allow
// HTTP traffic (port 80) over the HTTP CONNECT tunnel, or might not allow connection
// to target on any other port than 80 (HTTP) or 443 (HTTPS).
// You might want to try different proxy groups.
const PROXY_URL = 'http://auto:<PROXY_PASSWORD>@proxy.apify.com:8000';
// Target server to connect to. Here we use www.example.com and port 443 for HTTPS.
// You can use any other host and port.
const TARGET_HOST = 'www.example.com:443';
// Create tunnel for the service, this call will start local tunnel and
// return a string in format localhost:<selected-port>.
// Here we set "port" to 9999, but you can use 0 to get a random port.
// The "verbose" option causes a lot of logging
const tunnelInfo = await createTunnel(PROXY_URL, TARGET_HOST, { port: 9999, verbose: true });
console.log(`Tunnel to ${TARGET_HOST} via ${redactUrl(PROXY_URL)} established at ${tunnelInfo}...`);
// Here we assume por 443 from above, otherwise the service will not be accessible via HTTPS!
console.log(`To test it, you can run: curl --verbose https://${tunnelInfo}`);
// Wait forever...
await new Promise(() => {});
// Normally, you'd also want to close the tunnel and all open connections
await closeTunnel(tunnelInfo, true);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment