parent
dee606e6ab
commit
291a4ebac2
@ -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…
Reference in new issue