Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make ip update period modifiable #13

Merged
merged 2 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type AgentCliParam struct {
ReportDelay int // 报告间隔
TLS bool // 是否使用TLS加密传输至服务端
Version bool // 当前版本号
IPReportPeriod uint32 // 上报IP间隔
}

var (
Expand Down Expand Up @@ -164,6 +165,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 @@ -201,7 +203,7 @@ func run() {
// 上报服务器信息
go reportState()
// 更新IP信息
go monitor.UpdateIP()
go monitor.UpdateIP(agentCliParam.IPReportPeriod)

// 定时检查更新
if _, err := semver.Parse(version); err == nil && !agentCliParam.DisableAutoUpdate {
Expand Down
14 changes: 10 additions & 4 deletions pkg/monitor/myip.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package monitor
import (
"fmt"
"io"
"log"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -53,13 +54,18 @@ var (
httpClientV6 = util.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("NEZHA_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 @@ -72,7 +78,7 @@ func UpdateIP() {
} else if ipv6.CountryCode != "" {
cachedCountry = ipv6.CountryCode
}
time.Sleep(time.Minute * 30)
time.Sleep(time.Second * time.Duration(period))
}
}

Expand Down
Loading