IPX - HelloZeroNet/Plugin-PeerMessage GitHub Wiki

IPX is a sample whatismyipaddress service on ZeroNet, using PeerMessage plugin.

There are several trusted peers which have static IP and static port. Their IP and port are stored in IPX database. So when you want to get your IP, you directly connect to a random IP:port pair, which tells you your IP.

Sample implementation

zeroFrame.onRequest = function(cmd, message) {
    if(cmd == "peerReceive") {
        // Reply with IP
        let fromIP = message.params.ip;
        let hash = message.params.hash;
        zeroFrame.cmd("peerSend", {ip: fromIP, to: hash, message: fromIP});

        // Valid
        zeroFrame.cmd("peerValid", [hash]);
    }
}

function getIP(cb) {
    let ips = ["1.2.3.4:5", "6.7.8.9:10"];
    let ip = ips[Math.floor(Math.random() * ips.length)]; // Get random IP


    zeroFrame.cmd("peerSend", {ip, message: "whoami"}, function(result) { // Ask
        cb(result.message);
    });
}

A better implementation would include message signing and validation, etc. However, it should give you a basic idea how it works.