diff options
Diffstat (limited to 'src/server/Utils.hx')
| -rw-r--r-- | src/server/Utils.hx | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/server/Utils.hx b/src/server/Utils.hx new file mode 100644 index 0000000..c3510c9 --- /dev/null +++ b/src/server/Utils.hx @@ -0,0 +1,30 @@ +package server; + +import js.node.Http; +import js.node.Os; + +class Utils { + + public static function getGlobalIp(callback:(ip:String)->Void):Void { + Http.get("http://myexternalip.com/raw", r -> { + r.setEncoding("utf8"); + r.on("data", callback); + }); + } + + public static function getLocalIp():String { + final ifaces = Os.networkInterfaces(); + for (field in Reflect.fields(ifaces)) { + final type = Reflect.field(ifaces, field); + + for (ifname in Reflect.fields(type)) { + final iface = Reflect.field(type, ifname); + // skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses + if ('IPv4' != iface.family || iface.internal != false) continue; + // this interface has only one ipv4 adress + return iface.address; + } + } + return "127.0.0.1"; + } +} |
