如何从Javascript设置WebSocket Origin Header? [英] How to set WebSocket Origin Header from Javascript?

查看:105
本文介绍了如何从Javascript设置WebSocket Origin Header?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用javascript代表向本地ip 123.123.123.123 运行的服务器发出从本地 test.dev 页发出的Websocket请求> test.com .请求通过,但是 123.123.123.123 服务器在websocket请求中看到 Origin:test.dev 标头,并拒绝连接,因为它想查看 Origin:test.com .

I'm trying to use javascript to make a websocket request from a local test.dev page to a server running at ip 123.123.123.123 on behalf of test.com. The request goes through, but the 123.123.123.123 server sees the Origin: test.dev header in the websocket request and rejects the connection because it wants to see Origin: test.com.

这是用于连接套接字的javascript代码:

Here is the javascript code for connecting the socket:

ws = new WebSocket("123.123.123.123");

如何使用javascript通过 Origin:test.com 的不诚实的 Origin 标头启动Websocket连接?

How can I use javascript to start a websocket connection with a dishonest Origin header of Origin: test.com?

我希望这样的事情行得通,但我找不到这样的东西:

I was hoping something like this would work, but I can't find any such:

ws = new WebSocket("123.123.123.123","test.com");

推荐答案

简单的解决方案是在您的 hosts 文件中创建一个条目以映射 test.com 123.123.123.123 .当您要连接真实的" test.com 时,需要在以后删除此条目.

The simple solution would be to simply create an entry in your hosts file to map test.com to 123.123.123.123. You would need to remove this entry later when you want to connect the "real" test.com.

一个不太hacky的解决方案将需要使用代理,该代理可以为您即时重写您的标头.考虑在您的系统上安装 nginx ,然后将请求转发到 123.123.123.123 ,使Origin头文件的所有内容均保持 不变.这是您在nginx配置文件中需要的条目:

A less hacky solution would require the use of a proxy which can re-write your headers for you on-the-fly. Consider install nginx on your system, and then proxing the request to 123.123.123.123 keeping everything the same except for the Origin header. Here's the entry you would need in your nginx config file:

server {
    server_name test.dev;

    location / {
        proxy_pass http://123.123.123.123;
        proxy_set_header Origin test.com;

        # the following 3 are required to proxy WebSocket connections.
        # See more here: http://nginx.com/blog/websocket-nginx/

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

这篇关于如何从Javascript设置WebSocket Origin Header?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆