From 22904e503505cb4520d527d8982827e93abfef04 Mon Sep 17 00:00:00 2001 From: naiba Date: Mon, 22 Mar 2021 21:40:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=BC=BAIP=E8=8E=B7=E5=8F=96=E5=81=A5?= =?UTF-8?q?=E5=A3=AE=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ISSUE_TEMPLATE/default-issue-template.md | 60 ----------------- cmd/agent/monitor/monitor.go | 43 ------------ cmd/agent/monitor/myip.go | 67 +++++++++++++++++++ 3 files changed, 67 insertions(+), 103 deletions(-) delete mode 100644 .github/ISSUE_TEMPLATE/default-issue-template.md create mode 100644 cmd/agent/monitor/myip.go diff --git a/.github/ISSUE_TEMPLATE/default-issue-template.md b/.github/ISSUE_TEMPLATE/default-issue-template.md deleted file mode 100644 index 7b7a4bb..0000000 --- a/.github/ISSUE_TEMPLATE/default-issue-template.md +++ /dev/null @@ -1,60 +0,0 @@ ---- -name: 报告模板 Issue template -about: 用户要上万,奶爸很忙乱;提问表周全,省的添麻烦。 -title: '' -labels: '' -assignees: '' - ---- - - - -### 描述问题 Describe the problem - - - -### 期待的结果 Expected result - - - -### 截屏或录像 Screenshot or video - - - -### 版本环境 Version environment - -* 版本 Version: -* 操作系统 Operating system: -* 浏览器(如果使用)Browser (if used): - -### 其他信息 Other information - - - - diff --git a/cmd/agent/monitor/monitor.go b/cmd/agent/monitor/monitor.go index 178feb5..b8a1c7b 100644 --- a/cmd/agent/monitor/monitor.go +++ b/cmd/agent/monitor/monitor.go @@ -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() diff --git a/cmd/agent/monitor/myip.go b/cmd/agent/monitor/myip.go new file mode 100644 index 0000000..765bd5e --- /dev/null +++ b/cmd/agent/monitor/myip.go @@ -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 +}