Opening TCP socket from Firefox plugin
Asked Answered
B

2

6

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: https://mcmap.net/q/1774031/-tcpsocket-listen-on-firefox-addon
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.

Behave answered 10/2, 2015 at 20:53 Comment(0)
L
9

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);
Loren answered 10/2, 2015 at 21:3 Comment(4)
Thank you so much - this was perfect! I'm also glad you found this solution for your own purposes :)Behave
I apologize I can't give the solution an upvote - my account is too new it looks like - but I did accept it as a correct answer. Again, thank you!Behave
Is this for Firefox OS only or browser? The API docs say it's for Firefox OS.Celebration
I get error for line require("chrome"); it doesn't work! where did you put this. as a background script?Cistaceous
C
0

Currently from both Firefox and Chrome there is no way to open raw TCP or UDP connection even in extension mode. you can either use websocket or use runtime.connectNative

Cistaceous answered 20/12, 2023 at 8:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.