initial import

This commit is contained in:
kevin
2020-07-26 17:09:05 +08:00
commit 7e3a369a8f
647 changed files with 54754 additions and 0 deletions

39
core/netx/ip.go Normal file
View File

@@ -0,0 +1,39 @@
package netx
import "net"
func InternalIp() string {
infs, err := net.Interfaces()
if err != nil {
return ""
}
for _, inf := range infs {
if isEthDown(inf.Flags) || isLoopback(inf.Flags) {
continue
}
addrs, err := inf.Addrs()
if err != nil {
continue
}
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
}
return ""
}
func isEthDown(f net.Flags) bool {
return f&net.FlagUp != net.FlagUp
}
func isLoopback(f net.Flags) bool {
return f&net.FlagLoopback == net.FlagLoopback
}

11
core/netx/ip_test.go Normal file
View File

@@ -0,0 +1,11 @@
package netx
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestInternalIp(t *testing.T) {
assert.True(t, len(InternalIp()) > 0)
}