Add --ipv6-countrycode runtime parameter to set the region flag using IPv6 by default (#36)

* Update main.go

Add --ipv6-countrycode runtime parameter to set the region flag using IPv6 by default.

* Update service.go

Add --ipv6-countrycode runtime parameter to set the region flag using IPv6 by default.

* Update myip.go

Add --ipv6-countrycode runtime parameter to set the region flag using IPv6 by default.

* chore: rename

---------

Co-authored-by: naiba <hi@nai.ba>
This commit is contained in:
xykt 2024-07-10 23:29:35 +08:00 committed by GitHub
parent 09c4ef1764
commit 1bf702cfaa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 12 deletions

View File

@ -53,6 +53,7 @@ type AgentCliParam struct {
InsecureTLS bool // 是否禁用证书检查
Version bool // 当前版本号
IPReportPeriod uint32 // 上报IP间隔
UseIPv6CountryCode bool // 默认优先展示IPv6旗帜
}
var (
@ -149,6 +150,7 @@ func init() {
agentCmd.PersistentFlags().BoolVar(&agentCliParam.DisableCommandExecute, "disable-command-execute", false, "禁止在此机器上执行命令")
agentCmd.PersistentFlags().BoolVar(&agentCliParam.DisableAutoUpdate, "disable-auto-update", false, "禁用自动升级")
agentCmd.PersistentFlags().BoolVar(&agentCliParam.DisableForceUpdate, "disable-force-update", false, "禁用强制升级")
agentCmd.PersistentFlags().BoolVar(&agentCliParam.UseIPv6CountryCode, "use-ipv6-countrycode", false, "使用IPv6的位置上报")
agentCmd.PersistentFlags().BoolVar(&agentConfig.GPU, "gpu", false, "启用GPU监控")
agentCmd.PersistentFlags().Uint32VarP(&agentCliParam.IPReportPeriod, "ip-report-period", "u", 30*60, "本地IP更新间隔, 上报频率依旧取决于report-delay的值")
agentCmd.Flags().BoolVarP(&agentCliParam.Version, "version", "v", false, "查看当前版本号")
@ -217,7 +219,7 @@ func run() {
// 上报服务器信息
go reportState()
// 更新IP信息
go monitor.UpdateIP(agentCliParam.IPReportPeriod)
go monitor.UpdateIP(agentCliParam.UseIPv6CountryCode, agentCliParam.IPReportPeriod)
// 定时检查更新
if _, err := semver.Parse(version); err == nil && !agentCliParam.DisableAutoUpdate {

View File

@ -86,6 +86,7 @@ func serviceActions(cmd *cobra.Command, args []string) {
{agentCliParam.DisableCommandExecute, "--disable-command-execute", ""},
{agentCliParam.DisableAutoUpdate, "--disable-auto-update", ""},
{agentCliParam.DisableForceUpdate, "--disable-force-update", ""},
{agentCliParam.UseIPv6CountryCode, "--use-ipv6-countrycode", ""},
{agentConfig.GPU, "--gpu", ""},
{agentCliParam.IPReportPeriod != 30*60, "-u", fmt.Sprint(agentCliParam.IPReportPeriod)},
}

View File

@ -55,12 +55,19 @@ var (
)
// UpdateIP 按设置时间间隔更新IP地址与国家码的缓存
func UpdateIP(period uint32) {
func UpdateIP(useIPv6CountryCode bool, period uint32) {
for {
log.Println("NEZHA_AGENT>> 正在更新本地缓存IP信息")
ipv4 := fetchGeoIP(geoIPApiList, false)
ipv6 := fetchGeoIP(geoIPApiList, true)
if ipv4.IP == "" && ipv6.IP == "" {
var primaryIP, secondaryIP geoIP
if useIPv6CountryCode {
primaryIP = fetchGeoIP(geoIPApiList, true)
secondaryIP = fetchGeoIP(geoIPApiList, false)
} else {
primaryIP = fetchGeoIP(geoIPApiList, false)
secondaryIP = fetchGeoIP(geoIPApiList, true)
}
if primaryIP.IP == "" && secondaryIP.IP == "" {
if period > 60 {
time.Sleep(time.Minute)
} else {
@ -68,15 +75,16 @@ func UpdateIP(period uint32) {
}
continue
}
if ipv4.IP == "" || ipv6.IP == "" {
CachedIP = fmt.Sprintf("%s%s", ipv4.IP, ipv6.IP)
if primaryIP.IP == "" || secondaryIP.IP == "" {
CachedIP = fmt.Sprintf("%s%s", primaryIP.IP, secondaryIP.IP)
} else {
CachedIP = fmt.Sprintf("%s/%s", ipv4.IP, ipv6.IP)
CachedIP = fmt.Sprintf("%s/%s", primaryIP.IP, secondaryIP.IP)
}
if ipv4.CountryCode != "" {
cachedCountry = ipv4.CountryCode
} else if ipv6.CountryCode != "" {
cachedCountry = ipv6.CountryCode
if primaryIP.CountryCode != "" {
cachedCountry = primaryIP.CountryCode
} else if secondaryIP.CountryCode != "" {
cachedCountry = secondaryIP.CountryCode
}
time.Sleep(time.Second * time.Duration(period))
}