简体   繁体   English

为什么我不能在运行由Openshift托管的Node.js + Websocket的服务器上获取连接客户端的IP地址(使用WebSocket-Node)

[英]Why can't I get the connecting client's ip address on my server running Node.js + Websocket hosted by Openshift (Using WebSocket-Node)

I'm using Node.js 0.10 + WebSocket-Node ( Seen here ) hosted by openshift and thats pretty much it, i'm not using express. 我正在使用由openshift托管的Node.js 0.10 + WebSocket-Node( 在这里看到 ),那就是它,我不使用express。 Other than being unable to get any connecting client's ip address the server is otherwise behaving exactly as intended and processing websocket requests and connections wonderfully. 除了无法获得任何连接客户端的IP地址之外,服务器在其他方面的行为完全符合预期并且处理websocket请求和连接非常好。

I've done my homework and I know openshift is behind a reverse proxy and the x-forwarded-for header should be consulted for the correct ip. 我已经完成了我的作业,我知道openshift是在反向代理后面,并且应该为正确的ip查询x-forwarded-for标头。 However the WebSocketRequest.remoteAddress property, (which is meant to take its returned ip from the X-Forwarded-For header if its present,) returns the value of OPENSHIFT_NODEJS_IP (as listed in rhc env), which is presumably the server's ip. 但是,WebSocketRequest.remoteAddress属性(其目的是从X-Forwarded-For标头获取其返回的ip)返回OPENSHIFT_NODEJS_IP的值(如rhc env中所列),这可能是服务器的ip。 Furthermore i've manually gone through the WebSocketRequest.httpRequest object, (which is meant to be a reference to the original Node HTTP request object) and can't find any trace of the x-forwarded-for header in there, (Though I won't pretend to be anywhere near knowledgable enough to understand what I did see in there.) 此外,我手动浏览了WebSocketRequest.httpRequest对象(这是对原始Node HTTP请求对象的引用),并且在那里找不到任何x-forwarded-for标头的跟踪,(虽然我我不会假装知识到足以了解我在那里看到的东西。)

My server also processes standard HTTP requests which DO return the clients ip when I check request.headers['x-forwarded-for'], sadly this doesn't help get the IP's of clients connected via websocket though, as they do not trigger a standard HTTP response. 我的服务器也处理该回客户端标准HTTP请求的ip当我检查request.headers [“X转发换”],遗憾的是这不利于获得通过WebSocket的连接,虽然客户端的IP的,因为他们不触发标准的HTTP响应。

I must be missing something here and would be extremely grateful for any new routes of investigation or cunning workarounds anyone can offer. 我必须在这里遗漏一些东西,并且非常感谢任何新的调查路线或任何人都可以提供的狡猾的解决方法。

You could probe each client for its ip address upon socket connection. 您可以在套接字连接时探测每个客户端的IP地址。
You could use something like : http://jsonip.com/?callback 您可以使用以下内容: http//jsonip.com/?callback
the jsonip.com request returns the following object : jsonip.com请求返回以下对象:

{"ip":"XXX.XXX.XXX.XXX","about":"/about","Pro!":"http://getjsonip.com"}

Even thou trusting clients isn't always the best approach, 即使你信任客户并不总是最好的方法,
here you have a client-side example of the probe scenario : 这里有一个探测器场景的客户端示例:

socket.on('PROBE', function(){
        $.getJSON('http://jsonip.com/?callback=?', function(r){ 
            var myIP = r.ip;
            socket.emit('C_IP', myIP);
        }); 

Heres my experience with node.js & socket.io @ OpenShift. 继承了我对node.js和socket.io @ OpenShift的体验。
https://stackoverflow.com/a/29285652/2976093 https://stackoverflow.com/a/29285652/2976093

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

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