mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-06 05:30:05 +00:00
增强IP获取健壮性
This commit is contained in:
@@ -1,10 +1,7 @@
|
||||
package monitor
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
@@ -19,47 +16,7 @@ import (
|
||||
"github.com/naiba/nezha/service/dao"
|
||||
)
|
||||
|
||||
type ipDotSbGeoIP struct {
|
||||
CountryCode string `json:"country_code,omitempty"`
|
||||
IP string `json:"ip,omitempty"`
|
||||
}
|
||||
|
||||
var netInSpeed, netOutSpeed, netInTransfer, netOutTransfer, lastUpdate uint64
|
||||
var cachedIP, cachedCountry string
|
||||
|
||||
func UpdateIP() {
|
||||
var ip ipDotSbGeoIP
|
||||
for {
|
||||
var tempIP string
|
||||
var tempCountry string
|
||||
resp, err := http.Get("https://api-ipv4.ip.sb/geoip")
|
||||
if err == nil {
|
||||
defer resp.Body.Close()
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
json.Unmarshal(body, &ip)
|
||||
tempIP = ip.IP
|
||||
tempCountry = ip.CountryCode
|
||||
} else {
|
||||
cachedIP = ""
|
||||
}
|
||||
time.Sleep(time.Second * 10)
|
||||
resp, err = http.Get("https://api-ipv6.ip.sb/geoip")
|
||||
if err == nil {
|
||||
defer resp.Body.Close()
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
json.Unmarshal(body, &ip)
|
||||
if tempIP == "" {
|
||||
tempIP = ip.IP
|
||||
} else {
|
||||
tempIP = fmt.Sprintf("ip(v4: %s, v6: %s)", tempIP, ip.IP)
|
||||
}
|
||||
tempCountry = ip.CountryCode
|
||||
}
|
||||
cachedIP = tempIP
|
||||
cachedCountry = tempCountry
|
||||
time.Sleep(time.Minute * 10)
|
||||
}
|
||||
}
|
||||
|
||||
func GetHost() *model.Host {
|
||||
hi, _ := host.Info()
|
||||
|
||||
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