blob: c3510c93ac8cb3039891d39251e99cf4b05a823b (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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";
}
}
|