This commit is contained in:
cdle
2023-06-29 21:59:20 +08:00
parent d2c599dd68
commit 70bf5a33a6
4 changed files with 50 additions and 6 deletions
+33
View File
@@ -8,6 +8,7 @@ import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
@@ -444,3 +445,35 @@ func Itoa(i interface{}) string {
return fmt.Sprint(i)
}
}
func GetPublicIP() (string, error) {
var ip string
// 使用 ipapi.co 获取公网IP地址
resp, err := http.Get("https://ipapi.co/ip/")
if err == nil {
defer resp.Body.Close()
ipBytes, err := ioutil.ReadAll(resp.Body)
if err == nil {
ip = string(ipBytes)
}
}
// 使用 ifconfig.co 获取公网IP地址
if ip == "" {
resp, err = http.Get("https://ifconfig.co/ip")
if err == nil {
defer resp.Body.Close()
ipBytes, err := ioutil.ReadAll(resp.Body)
if err == nil {
ip = string(ipBytes)
}
}
}
if ip == "" {
return "", fmt.Errorf("获取IP地址失败")
}
return ip, nil
}