Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Bootstrapped firefox extension low level API, how to get machine IP Address (ifconfig)

28 views
Skip to first unread message

SuvP

unread,
Jul 26, 2016, 9:18:37 AM7/26/16
to
I am trying to get the IP of the machine in my restartless bootstrapped extension ( the one we get from ifconfig or ipconfig)

I am doing exactly this

https://groups.google.com/forum/#!searchin/mozilla.dev.extensions/nsiDNSService/mozilla.dev.extensions/GEnsiBtQVCY/GzlSG0tTj7cJ

var dns = Components.classes["@mozilla.org/network/dns-service;1"]
.getService(Components.interfaces.nsIDNSService);
var myName = dns.myHostName;
console.log(myName);
var record = dns.resolve(myName, RESOLVE_BYPASS_CACHE);
while (record.hasMore())
console.log(record.getNextAddrAsString());

and I keep getting 127.0.0.1 because that the entry in my /etc/host. It doesn't make sense to alter that because not every machine on which the extension will be installed will have this entry.

Is there any other cross platform way to find user ip? ( apart from window API)?

Rob Wu

unread,
Jul 31, 2016, 3:19:38 AM7/31/16
to SuvP, dev-ext...@lists.mozilla.org
You don't need an addon to get the IP addresses of your local network
interfaces. WebRTC offers the ability to get what you want from any normal
web page as shown at https://stackoverflow.com/a/29514292/938089 .

If you're using the addon SDK, create a page-worker and use the getLocalIPs
function from the above link.
If you use WebExtensions, you have to insert a content script in the
current page (or open a new tab) and do your trick there, since WebRTC from
the background page seems to not be supported yet (at least not in Firefox
Nightly (50)).

Here is some sample code using the addon SDK (create the following files,
use jpm run and you'll see your local IP addresses printed in the console):

// index.js
var localIpWorker = require('sdk/page-worker').Page({
contentURL: require('sdk/self').data.url('getlocalips.html'),
});

// Example, get the IP addresses and print it to the console.
localIpWorker.port.once('getlocalips-result', ips => {
console.log('Local IP addresses:\n ' + ips.join('\n '));
});
localIpWorker.port.emit('getlocalips');

getlocalips.html (the page loaded in page-worker):

<script>
// ...put function getLocalIPs from
https://stackoverflow.com/a/29514292/938089 here...

addon.port.on('getlocalips', () => {
getLocalIPs(ips => addon.port.emit('getlocalips-result', ips));
});
</script>

Kind regards,
Rob
https://robwu.nl
> _______________________________________________
> dev-extensions mailing list
> dev-ext...@lists.mozilla.org
> https://lists.mozilla.org/listinfo/dev-extensions
>
0 new messages