MCPcopy Create free account
hub / github.com/crossoverJie/gscript / externalIP

Function externalIP

internal_http.go:250–285  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

248}
249
250func externalIP() string {
251 ifaces, err := net.Interfaces()
252 if err != nil {
253 return ""
254 }
255 for _, iface := range ifaces {
256 if iface.Flags&net.FlagUp == 0 {
257 continue // interface down
258 }
259 if iface.Flags&net.FlagLoopback != 0 {
260 continue // loopback interface
261 }
262 addrs, err := iface.Addrs()
263 if err != nil {
264 return ""
265 }
266 for _, addr := range addrs {
267 var ip net.IP
268 switch v := addr.(type) {
269 case *net.IPNet:
270 ip = v.IP
271 case *net.IPAddr:
272 ip = v.IP
273 }
274 if ip == nil || ip.IsLoopback() {
275 continue
276 }
277 ip = ip.To4()
278 if ip == nil {
279 continue // not an ipv4 address
280 }
281 return ip.String()
282 }
283 }
284 return ""
285}

Callers 1

httpRunFunction · 0.85

Calls 1

StringMethod · 0.65

Tested by

no test coverage detected