NET net.NET

master
sre 2 years ago
parent dee606e6ab
commit 291a4ebac2

@ -4,6 +4,7 @@ import (
"git.sre.ink/go/gtool/convert"
"git.sre.ink/go/gtool/crypto"
"git.sre.ink/go/gtool/file"
"git.sre.ink/go/gtool/net"
"git.sre.ink/go/gtool/rand"
"git.sre.ink/go/gtool/regx"
"git.sre.ink/go/gtool/str"
@ -17,4 +18,5 @@ var (
Conv convert.Conv
Regx regx.Regx
File file.File
NET net.NET
)

@ -0,0 +1,4 @@
package net
type NET struct {
}

@ -0,0 +1,3 @@
package net
var net NET

@ -0,0 +1,25 @@
package net
import (
"fmt"
"io/ioutil"
"net/http"
"regexp"
)
// PublicIP 获取公网IP信息
func (n *NET) PublicIP() string {
pip, err := http.Get("https://cip.cc")
if err != nil {
fmt.Println("获取IP地址错误: ", err)
return ""
}
defer pip.Body.Close()
content, _ := ioutil.ReadAll(pip.Body)
extract := regexp.MustCompile("(\\d{1,3}\\.){3}\\d{1,3}")
publicIP := extract.FindString(string(content))
if publicIP != "" {
return publicIP
}
return ""
}

@ -0,0 +1,10 @@
package net
import (
"fmt"
"testing"
)
func TestNET_PublicIP(t *testing.T) {
fmt.Println(net.PublicIP())
}
Loading…
Cancel
Save