TooTallNate / proxy-agents

Node.js HTTP Proxy Agents Monorepo
https://proxy-agents.n8.io
919 stars 239 forks source link

errors with https #100

Closed pinilpypinilpy closed 4 years ago

pinilpypinilpy commented 4 years ago

when using the sample code to connect to echo.websockets.org and an http proxy everything goes fine, but when attempting to use https and wss instead of ws, I get:

(node:6142) UnhandledPromiseRejectionWarning: Error: write EPROTO 140166426584960:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:../deps/openssl/openssl/ssl/record/ssl3_record.c:332:

   at WriteWrap.onWriteComplete [as oncomplete] (internal/stream_base_commons.js:92:16)
(node:6142) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag "--unhandled-rejections=strict" (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:6142) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Thanks in advance

TooTallNate commented 4 years ago

Can you show the exact code you're using?

pinilpypinilpy commented 4 years ago

sure: const WebSocket = require('ws'); var url = require('url'); var https = require('https'); var HttpsProxyAgent = require('https-proxy-agent'); var proxy = process.env.http_proxy || 'https://198.98.56.71:8080'; console.log('using proxy server %j', proxy); var endpoint = process.argv[2] || 'wss://echo.websocket.org/'; var parsed = url.parse(endpoint); console.log('attempting to connect to WebSocket %j', endpoint); var options = url.parse(proxy); var agent = new HttpsProxyAgent(options); const ws = new WebSocket('wss://echo.websocket.org/', { origin: 'https://websocket.org', agent: agent }); ws.on('open', function open() { console.log('connected'); ws.send(Date.now()); });

ws.on('close', function close() { console.log('disconnected'); });

ws.on('message', function incoming(data) { console.log(Roundtrip time: ${Date.now() - data} ms);

setTimeout(function timeout() { ws.send(Date.now()); }, 500); });

however, I've had a far better time with the socks proxy, so I think I'll use that instead. Also, I got the same error using the sample https code instead of ws

pinilpypinilpy commented 4 years ago

ok, I think it was because I put https instead of http, but that's kinda counter-intuitive, gotta say thanks for the quick response.