Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
149
rated 0 times [  154] [ 5]  / answers: 1 / hits: 20088  / 9 Years ago, sat, may 9, 2015, 12:00:00

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.



Here is the javascript code for connecting the socket:



ws = new WebSocket(123.123.123.123);



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


More From » websocket

 Answers
9

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.



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

[#66660] Thursday, May 7, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jenna

Total Points: 706
Total Questions: 107
Total Answers: 106

Location: Azerbaijan
Member since Tue, Sep 21, 2021
3 Years ago
;