增强IP获取健壮性
This commit is contained in:
parent
228f74f4ac
commit
22904e5035
60
.github/ISSUE_TEMPLATE/default-issue-template.md
vendored
60
.github/ISSUE_TEMPLATE/default-issue-template.md
vendored
@ -1,60 +0,0 @@
|
||||
---
|
||||
name: 报告模板 Issue template
|
||||
about: 用户要上万,奶爸很忙乱;提问表周全,省的添麻烦。
|
||||
title: ''
|
||||
labels: ''
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
<!--
|
||||
First of all:
|
||||
|
||||
确认你的 管理面板 及 Agent 都是最新版本。
|
||||
Make sure your dashboard and agent is up to date.
|
||||
|
||||
确认你已再次看过一遍 README 文档,因为有些问题可能在你离开的这段时间已被解决。
|
||||
Make sure you have read the README file again, because some issues may have been resolved during your absence.
|
||||
-->
|
||||
|
||||
### 描述问题 Describe the problem
|
||||
|
||||
<!--
|
||||
请尽量清晰精准地描述你碰到的问题。
|
||||
Please describe your problem as clearly and accurately as possible.
|
||||
|
||||
主题问题请 @ 主题的作者,在 README 截图顶部。
|
||||
For system theme questions, please @ the author of the theme, the author information is at the top of the README screenshot.
|
||||
-->
|
||||
|
||||
### 期待的结果 Expected result
|
||||
|
||||
<!--
|
||||
请尽量清晰精准地描述你所期待的结果。
|
||||
Please be as clear and accurate as possible to describe the results you are looking for.
|
||||
-->
|
||||
|
||||
### 截屏或录像 Screenshot or video
|
||||
|
||||
<!--
|
||||
如果可能,请尽量附加截图或录像来描述你遇到的问题。
|
||||
If possible, please try to attach screenshots or videos to describe the problem you are experiencing.
|
||||
|
||||
(Windows 下推荐使用 [Screen2Gif](https://www.screentogif.com/) 进行录屏。如果是编辑器输入相关问题,使用 Screen2Gif 录制结束后请打开`图像 - 按键`)
|
||||
(It is recommended to use [Screen2Gif](https://www.screentogif.com/) to record the screen under Windows. If it is related to the editor input, please open the `Image - Key Strokes` after recording with Screen2Gif)
|
||||
-->
|
||||
|
||||
### 版本环境 Version environment
|
||||
|
||||
* 版本 Version:
|
||||
* 操作系统 Operating system:
|
||||
* 浏览器(如果使用)Browser (if used):
|
||||
|
||||
### 其他信息 Other information
|
||||
|
||||
<!--
|
||||
请提供其他附加信息帮助我们诊断问题。
|
||||
Please provide additional information to help us diagnose the problem.
|
||||
-->
|
||||
|
||||
<!-- 模板来自老同志 @88250 的仓库 -->
|
@ -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
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user