Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get active Websockets of a Website possible?

I would like to know if its possible to get active WebSockets of a Website. An example would be: var x = document.findWebSocket(). The websockets would be listed in Chrome under the Network Tab (In the dev tools section). From there the websockets are listed under "WS". I want to be able to do x.emit(..); as well.

So far i could only come up with var x = new WebSocket("wss://exampleUrl.com/socket.io/?EIO=3&transport=websocket", "protocol1");. But this only adds a new Websocket with a different sid from the one that i want to emit messages from.

adding "&sid = {SID of Active Websocket}" would not work.

like image 453
yup123321 Avatar asked Jan 26 '20 06:01

yup123321


People also ask

What are WebSockets in web applications?

Web sockets are defined as two-way communication between the servers and clients, which means both parties can communicate and exchange data at the same time. WebSockets are the alternative to HTTP communication in the Web Applications.

How do I send and receive data from a WebSocket?

Once you get a Web Socket connection with the web server, you can send data from browser to server by calling a send() method, and receive data from server to browser by an onmessage event handler. Following is the API which creates a new WebSocket object. var Socket = new WebSocket(url, [protocal] );

How to run a WebSocket server in Chrome browser?

Now using Chrome browser open the html file your created in the beginning. If your browser supports WebSocket (), then you would get alert indicating that your browser supports WebSocket and finally when you click on "Run WebSocket" you would get Goodbye message sent by the server script.

How to create a web socket server instance?

Creating a Web Socket Server Instance Every Web Socket server needs a valid host and port. An example of creating a Web Socket instance in server is as follows − var server = new WebSocketServer("ws://localhost:8181"); Any valid URL can be used with the specification of a port, which was not used earlier.


2 Answers

It's a bit hacky, but if you can inject code that runs before the site's code does (for example, with Tampermonkey and @run-at document-start), you can monkeypatch window.WebSocket so that whenever it's called, you add the created websocket to an array which you can examine later. For example, running the following on Stack Overflow:

// ==UserScript==
// @name             0 New Userscript
// @include          /^https://stackoverflow.com
// @run-at           document-start
// @grant            none
// ==/UserScript==

const sockets = [];
const nativeWebSocket = window.WebSocket;
window.WebSocket = function(...args){
  const socket = new nativeWebSocket(...args);
  sockets.push(socket);
  return socket;
};
setTimeout(() => {
  // or create a button which, when clicked, does something with the sockets
  console.log(sockets);
}, 1000);

results in [WebSocket] being logged (and you could proceed to do whatever you wanted to do with the instance, such as call emit).

like image 91
CertainPerformance Avatar answered Oct 18 '22 21:10

CertainPerformance


You could collect all sockets in an array as soon as they send something:

const originalSend = WebSocket.prototype.send;
window.sockets = [];
WebSocket.prototype.send = function(...args) {
  if (window.sockets.indexOf(this) === -1)
    window.sockets.push(this);
  return originalSend.call(this, ...args);
};

Then later:

if (sockets.length > 0)
  sockets[0].send('hello');
like image 5
trollkotze Avatar answered Oct 18 '22 21:10

trollkotze