简体   繁体   English

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

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

Generalizing that would be the question... how to make websockets to go through a proxy in node.js? 概括那将是一个问题......如何让websockets通过node.js中的代理?

In my particular case I'm using pusher.com with the node.js client library they recommend. 在我的特定情况下,我正在使用pusher.com和他们推荐的node.js客户端库 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 查看代码内部我想知道一些关于我应该更改的提示,以使这个库与代理一起工作...你可以在这里查看代码

Maybe I should somehow replace or modified the websockets module that 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) 我只是想在公司内部使用现有的代理服务器来代理我的websockets请求(特别是pusher.com)
  • Just to let you know, if I use a proxifier like the one for windows Proxifier and 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. 只是为了让你知道,如果我使用像Windows Proxifier那样的接收器 ,我设置规则来检查所有到端口443的连接,以通过代理服务器proxy-my.coporate.com:1080(类型SOCKS5)它就像一个魅力。
  • But I don't want to go this way. 但我不想这样走。 I want to programatically configuring this proxy server within my node js code (even if that involved to modified the pusher library I mentioned) 我想在我的节点js代码中以编程方式配置此代理服务器(即使涉及修改我提到的推送器库)
  • I know how to do this for HTTP using Request module (look for the section that mentions how to use a proxy). 我知道如何使用Request模块为HTTP执行此操作(查找提及如何使用代理的部分)。
    • I want a similarly thing for websockets . 我想要一个类似于websockets的东西。

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 来源: 链接

Most web proxies don't support websockets yet. 大多数Web代理尚不支持websockets。 The best workaround is to use encryption by specifying wss:// (websocket secure protocol): 最好的解决方法是通过指定wss://(websocket安全协议)来使用加密:

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

Using a proxy for websockets should work roughly the same as for https connections; 使用websockets的代理应该与https连接大致相同; you should use the CONNECT method. 你应该使用CONNECT方法。 At least that's what both the HTTP and HTML5 specs say. 至少这是HTTPHTML5规范所说的。 So if your proxy implements CONNECT, you're good to go. 因此,如果您的代理实施CONNECT,那么您很高兴。

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();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM