增强IP获取健壮性

This commit is contained in:
naiba
2021-03-22 21:40:50 +08:00
parent 228f74f4ac
commit 22904e5035
3 changed files with 67 additions and 103 deletions

View File

@@ -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()