mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-06 05:30:05 +00:00
增强IP获取健壮性
This commit is contained in:
67
cmd/agent/monitor/myip.go
Normal file
67
cmd/agent/monitor/myip.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package monitor
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type geoIP struct {
|
||||
CountryCode string `json:"country_code,omitempty"`
|
||||
IP string `json:"ip,omitempty"`
|
||||
}
|
||||
|
||||
var ipv4Servers = []string{
|
||||
"https://api-ipv4.ip.sb/geoip",
|
||||
"https://ip4.seeip.org/geoip",
|
||||
}
|
||||
|
||||
var ipv6Servers = []string{
|
||||
"https://ip6.seeip.org/geoip",
|
||||
"https://api-ipv6.ip.sb/geoip",
|
||||
}
|
||||
|
||||
var cachedIP, cachedCountry string
|
||||
|
||||
func UpdateIP() {
|
||||
go func() {
|
||||
for {
|
||||
log.Println(cachedIP, cachedCountry)
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}()
|
||||
for {
|
||||
ipv4 := fetchGeoIP(ipv4Servers)
|
||||
ipv6 := fetchGeoIP(ipv6Servers)
|
||||
cachedIP = fmt.Sprintf("ip(v4:%s,v6:%s)", ipv4.IP, ipv6.IP)
|
||||
if ipv4.CountryCode != "" {
|
||||
cachedCountry = ipv4.CountryCode
|
||||
} else if ipv6.CountryCode != "" {
|
||||
cachedCountry = ipv6.CountryCode
|
||||
}
|
||||
time.Sleep(time.Minute * 10)
|
||||
}
|
||||
}
|
||||
|
||||
func fetchGeoIP(servers []string) geoIP {
|
||||
var ip geoIP
|
||||
for i := 0; i < len(servers); i++ {
|
||||
resp, err := http.Get(servers[i])
|
||||
if err == nil {
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
resp.Body.Close()
|
||||
err = json.Unmarshal(body, &ip)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
return ip
|
||||
}
|
||||
}
|
||||
return ip
|
||||
}
|
||||
Reference in New Issue
Block a user