Skip to content

Commit

Permalink
Update.
Browse files Browse the repository at this point in the history
  • Loading branch information
xOS committed Feb 27, 2024
1 parent d2f492f commit b43a1e7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
6 changes: 4 additions & 2 deletions cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type AgentCliParam struct {
ReportDelay int // 报告间隔
TLS bool // 是否使用TLS加密传输至服务端
Version bool // 当前版本号
IPReportPeriod uint32 // 上报IP间隔
}

var (
Expand Down Expand Up @@ -145,6 +146,7 @@ func main() {
flag.BoolVar(&agentCliParam.DisableForceUpdate, "disable-force-update", false, "禁用强制升级")
flag.BoolVar(&agentCliParam.TLS, "tls", false, "启用SSL/TLS加密")
flag.BoolVarP(&agentCliParam.Version, "version", "v", false, "查看当前版本号")
flag.Uint32VarP(&agentCliParam.IPReportPeriod, "ip-report-period", "u", 30*60, "本地IP更新间隔, 上报频率依旧取决于report-delay的值")
flag.Parse()

if agentCliParam.Version {
Expand Down Expand Up @@ -182,13 +184,13 @@ func run() {
// 上报服务器信息
go reportState()
// 更新IP信息
go monitor.UpdateIP()
go monitor.UpdateIP(agentCliParam.IPReportPeriod)

// 定时检查更新
if _, err := semver.Parse(version); err == nil && !agentCliParam.DisableAutoUpdate {
doSelfUpdate(true)
go func() {
for range time.Tick(30 * time.Minute) {
for range time.Tick(20 * time.Minute) {
doSelfUpdate(true)
}
}()
Expand Down
16 changes: 11 additions & 5 deletions cmd/agent/monitor/myip.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package monitor
import (
"fmt"
"io"
"log"
"net/http"
"strings"
"time"

"github.com/xos/serverstatus/pkg/utils"
)

const MacOSChromeUA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
const MacOSChromeUA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"

type geoIP struct {
CountryCode string `json:"country_code,omitempty"`
Expand Down Expand Up @@ -52,14 +53,19 @@ var (
httpClientV6 = utils.NewSingleStackHTTPClient(time.Second*20, time.Second*5, time.Second*10, true)
)

// UpdateIP 每30分钟更新一次IP地址与国家码的缓存
func UpdateIP() {
// UpdateIP 按设置时间间隔更新IP地址与国家码的缓存
func UpdateIP(period uint32) {
for {
log.Println("NG_AGENT>> 正在更新本地缓存IP信息")
ipv4 := fetchGeoIP(geoIPApiList, false)
ipv6 := fetchGeoIP(geoIPApiList, true)

if ipv4.IP == "" && ipv6.IP == "" {
time.Sleep(time.Minute)
if period > 60 {
time.Sleep(time.Minute)
} else {
time.Sleep(time.Second * time.Duration(period))
}
continue
}
if ipv4.IP != "" && ipv6.IP == "" {
Expand All @@ -74,7 +80,7 @@ func UpdateIP() {
} else if ipv6.CountryCode != "" {
cachedCountry = ipv6.CountryCode
}
time.Sleep(time.Minute * 30)
time.Sleep(time.Second * time.Duration(period))
}
}

Expand Down

0 comments on commit b43a1e7

Please sign in to comment.