Search code examples
websocketfirefox-addon-webextensions

How to connect a websocket client in a Firefox extension through an IP address


I have a Firefox extension which creates a websocket client and sends a message to the server. The extension and the server are on the same system. If I use local, the websocket does connect to the server. If the IP address of the interface is used, the client cannot connect to the server. As proof I can connect with the IP address, I have other client and server applications which connect with each other through raw sockets with the IP address and messages are received by the server.

I use secured websockets. I added the certificate to Firefox's certificate manager.

I am not sure if the problem is that I am formatting the address incorrectly or that there are missing permissions in the extension manifest.

manifest.json:

{
  "description": "weblogging app",
  "manifest_version": 2,
  "name": "weblogger",
  "version": "1.0",

  "browser_specific_settings": {
    "gecko": {
      "id": "[email protected]",
      "strict_min_version": "50.0"
    }
  },

  "background": {
    "scripts": ["background.js"]
  },

  "permissions": []
}

background.js:

var websocketArguments = 'wss://192.168.1.123:9501';
var webSocket;
createWebsocket();

function onError(error)
{
   console.log(`Error: ${error}`);
}

function createWebsocket()
{
   webSocket = new WebSocket(websocketArguments);
   webSocket.onerror = onWebSocketError;
   webSocket.onopen = onWebSocketOpen;
}

function onWebSocketError(event)
{
   console.log("WebSocket error observed:", event);
};

function onWebSocketOpen(event)
{
   console.log("WebSocket open: ", webSocket.readyState);
   webSocket.send("hello");
};

Solution

  • As I said in the post, I added the SSL certificate to the Firefox manager. I followed this post's advice: I browsed to the https of the websocket server address with Firefox and allowed the security exception. The websocket now connects.

    https://192.168.1.123:9501