javascript 如何让 websockets 通过 node.js 中的代理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23686379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:23:26  来源:igfitidea点击:

How to make websockets to go through a proxy in node.js

javascriptnode.jsproxywebsocketpush-notification

提问by Santiago Agüero

Generalizingthat would be the question... how to make websockets to go through a proxy in node.js?

概括这将是一个问题......如何使 websockets 通过 node.js 中的代理?

In my particular caseI'm using pusher.com with the node.js client librarythey recommend. Looking inside the code I would like to know some hints on what I should change in order to make this library to work with a proxy... you can take a look in the code here

在我的特殊情况下,我将 pusher.com 与他们推荐的node.js 客户端库一起使用。查看代码内部,我想知道一些关于我应该更改什么以使此库与代理一起使用的提示...您可以在此处查看代码

Maybe I should somehow replace or modified the websockets modulethat is being used by the library?

也许我应该以某种方式替换或修改库正在使用的websockets 模块

EDIT

编辑

Thanks for your answers/comments! A couple of things to take into consideration (excuse me if I'm wrong with some/all of them, just learning):

感谢您的回答/评论!需要考虑的几件事(如果我对其中的一些/全部有误,请原谅我,只是学习):

  • I don't want to create a proxy server. I just want to use an existent proxy server within my company in order to proxified my websockets requests (particularly pusher.com)
  • Just to let you know, if I use a proxifier like the one for windows Proxifierand I set up the rule to inspect for all connections to port 443 to go through the proxy server proxy-my.coporate.com:1080 (type SOCKS5)it works like a charm.
  • But I don't want to go this way. I want to programaticallyconfiguring this proxy server within my node js code (even if that involved to modified the pusher library I mentioned)
  • I know how to do this for HTTP using Request module(look for the section that mentions how to use a proxy).
    • I want a similarly thing for websockets.
  • 我不想创建代理服务器。我只想使用我公司内现有的代理服务器来代理我的 websockets 请求(特别是 pusher.com)
  • 只是为了让您知道,如果我使用类似于 windows Proxifier的代理程序,并且我设置了规则来检查到端口 443 的所有连接以通过代理服务器proxy-my.coporate.com:1080(类型 SOCKS5)它就像一个魅力。
  • 但我不想走这条路。我想在我的节点 js 代码中以编程方式配置这个代理服务器(即使这涉及修改我提到的推送库)
  • 我知道如何使用Request 模块为 HTTP 执行此操作(查找提到如何使用代理的部分)。
    • 我想要websockets类似的东西。

回答by ThePixelPony

Try node-http-proxy

试试node-http-proxy

It allows you to send http or websocket requests through a proxy.

它允许您通过代理发送 http 或 websocket 请求。

var http = require('http'),  
httpProxy = require('http-proxy');
//
// Create a basic proxy server in one line of code...
//
// This listens on port 8000 for incoming HTTP requests 
// and proxies them to port 9000
httpProxy.createServer(9000, 'localhost').listen(8000);

//
// ...and a simple http server to show us our request back.
//
http.createServer(function (req, res) {  
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
  res.end();
}).listen(9000);

Source: link

来源:链接

回答by Rudolf Meijering

Most web proxies don't support websockets yet. The best workaround is to use encryption by specifying wss:// (websocket secure protocol):

大多数网络代理还不支持 websockets。最好的解决方法是通过指定 wss://(websocket 安全协议)来使用加密:

wss://ws.pusherapp.com:[port]/app/[key]

回答by Chris Wesseling

Using a proxy for websockets should work roughly the same as for https connections; you should use the CONNECT method. At least that's what both the HTTPand HTML5specs say. So if your proxy implements CONNECT, you're good to go.

为 websockets 使用代理应该与 https 连接的工作方式大致相同;您应该使用 CONNECT 方法。至少HTTPHTML5规范都是这么说的。所以如果你的代理实现了 CONNECT,你就可以开始了。

回答by YourBestBet

From https://www.npmjs.com/package/https-proxy-agent

来自 https://www.npmjs.com/package/https-proxy-agent

var url = require('url');
var WebSocket = require('ws');
var HttpsProxyAgent = require('https-proxy-agent');

// HTTP/HTTPS proxy to connect to
var proxy = process.env.http_proxy || 'http://168.63.76.32:3128';
console.log('using proxy server %j', proxy);

// WebSocket endpoint for the proxy to connect to
var endpoint = process.argv[2] || 'ws://echo.websocket.org';
var parsed = url.parse(endpoint);
console.log('attempting to connect to WebSocket %j', endpoint);

// create an instance of the `HttpsProxyAgent` class with the proxy server information
var options = url.parse(proxy);

var agent = new HttpsProxyAgent(options);

// finally, initiate the WebSocket connection
var socket = new WebSocket(endpoint, { agent: agent });

socket.on('open', function () {
  console.log('"open" event!');
  socket.send('hello world');
});

socket.on('message', function (data, flags) {
  console.log('"message" event! %j %j', data, flags);
  socket.close();
});