加添功能:
把本机外网地址加入防火墙白名单,(如果是国外VPS)
This commit is contained in:
33
local_addr.go
Normal file
33
local_addr.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetLocalIpv4Addr() string {
|
||||
// 发送 HTTP GET 请求到外部服务
|
||||
resp, err := http.Get("https://inet-ip.aixiao.me/")
|
||||
if err != nil {
|
||||
fmt.Printf("请求失败: %s\n", err)
|
||||
return "NULL"
|
||||
}
|
||||
defer resp.Body.Close() // 确保响应体在函数结束时关闭
|
||||
|
||||
// 读取响应体内容
|
||||
ip, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Printf("读取响应失败: %s\n", err)
|
||||
return "NULL"
|
||||
}
|
||||
|
||||
// 去除字符串末尾的换行符
|
||||
ipStr := strings.TrimRight(string(ip), "\n")
|
||||
|
||||
// 输出获取到的公网 IP
|
||||
fmt.Printf("%s %d\n", ipStr, len(ipStr))
|
||||
|
||||
return ipStr
|
||||
}
|
||||
Reference in New Issue
Block a user