Last active
February 16, 2021 17:28
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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