Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening TCP socket from Firefox plugin

I'm afraid I'm having trouble opening a socket from my Firefox plugin. My goal is to have my plugin listen on the port, and a Python script will send it a short string once in a while to act on.

I have checked and no firewalls are running, and if I set netcat to listen on my desired port before launching this plugin, it behaves differently (it will print "opened port!"). Unfortunately I really am trying to have the plugin be the listener/server.

//TCPSocket API: https://developer.mozilla.org/en-US/docs/Web/API/TCPSocket
//Got me started: http://stackoverflow.com/q/28326301
const {Cu,Cc,Ci} = require("chrome");
var tcpSocket = Cc["@mozilla.org/tcp-socket;1"].createInstance(Ci.nsIDOMTCPSocket);
var socket = tcpSocket.open("127.0.0.1", 64515);
socket.ondata = function (event) {
    console.log('port received data!!!');
    if (typeof event.data === 'string') {
        console.log('Got: ' + event.data);
    }
}
socket.onopen = function() {console.log('opened port!');}
socket.onerror = function(event) {
    console.log('port error!');
    console.log(event.data)
}
socket.onclose = function() {console.log('port closed!');}
socket.ondrain = function() {console.log('port drained!');}

Right now though the immediate output is:

console.log: my-addon: port error!
console.log: my-addon: DOMError {}
console.log: my-addon: port closed!

where "DOMError" is the error received by socket.onerror.

I'm unsure whether this is my lack of understanding sockets or if there is something special to do with Firefox addons and permissions when it comes to opening a port.

like image 513
JKuin Avatar asked Mar 18 '23 02:03

JKuin


1 Answers

I'm the OP of the question you mentioned (TcpSocket listen on Firefox addon), and I finally got it working with a different approach:

var port = 3000; //whatever is your port
const {Cc, Ci} = require("chrome");
var serverSocket = Cc["@mozilla.org/network/server-socket;1"].createInstance(Ci.nsIServerSocket);
serverSocket.init(port, true, -1);
var listener = {
    onSocketAccepted: function(socket, transport) {
        var input = transport.openInputStream(Ci.nsITransport.OPEN_BLOCKING,0,0);
        var output = transport.openOutputStream(Ci.nsITransport.OPEN_BLOCKING, 0, 0);
        var tm = Cc["@mozilla.org/thread-manager;1"].getService();
        input.asyncWait({
            onInputStreamReady: function(inp) {
                try
                {
                    var sin = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(Ci.nsIScriptableInputStream);
                    sin.init(inp);
                    sin.available();

                    //Get request message
                    var request = '';
                    while (sin.available()) { request = request + sin.read(5120); }
                    var reqObj = { type: null, info: [] };
                    if(request != null && request.trim() != "") {
                        console.log(request);
                    }
                }
                catch(ex) { }           
                finally
                {
                    sin.close();
                    input.close();
                    output.close();
                }
            }
        }, 0, 0, tm.mainThread);
    },
    onStopListening: function(socket, status) {
    }
};
serverSocket.asyncListen(listener);
like image 98
LcSalazar Avatar answered Mar 23 '23 17:03

LcSalazar