You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

26 lines
478 B

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 ""
}