修改bug
This commit is contained in:
		
							parent
							
								
									2a22781b46
								
							
						
					
					
						commit
						23219a5874
					
				|  | @ -67,7 +67,7 @@ func GetHost(cfg *config.AgentConfig) *model.Node { | ||||||
| 	if cachedBootTime.IsZero() { | 	if cachedBootTime.IsZero() { | ||||||
| 		cachedBootTime = time.Unix(int64(hi.BootTime), 0) | 		cachedBootTime = time.Unix(int64(hi.BootTime), 0) | ||||||
| 	} | 	} | ||||||
| 
 | 	UpdateIP() | ||||||
| 	return &model.Node{ | 	return &model.Node{ | ||||||
| 		NodeGuid:        hi.HostID, | 		NodeGuid:        hi.HostID, | ||||||
| 		NodeName:        cfg.Point, | 		NodeName:        cfg.Point, | ||||||
|  | @ -82,6 +82,7 @@ func GetHost(cfg *config.AgentConfig) *model.Node { | ||||||
| 		Virtualization:  hi.VirtualizationSystem, | 		Virtualization:  hi.VirtualizationSystem, | ||||||
| 		BootTime:        hi.BootTime, | 		BootTime:        hi.BootTime, | ||||||
| 		IP:              cachedIP, | 		IP:              cachedIP, | ||||||
|  | 		LocalIP:         GetLocalIp(), | ||||||
| 		CountryCode:     strings.ToLower(cachedCountry), | 		CountryCode:     strings.ToLower(cachedCountry), | ||||||
| 		Version:         Version, | 		Version:         Version, | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -4,7 +4,8 @@ import ( | ||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| 	"environmentCaptureAgent/pkg/utils" | 	"environmentCaptureAgent/pkg/utils" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io/ioutil" | 	"io" | ||||||
|  | 	"net" | ||||||
| 	"net/http" | 	"net/http" | ||||||
| 	"strings" | 	"strings" | ||||||
| 	"time" | 	"time" | ||||||
|  | @ -18,7 +19,6 @@ type geoIP struct { | ||||||
| 
 | 
 | ||||||
| var ( | var ( | ||||||
| 	geoIPApiList = []string{ | 	geoIPApiList = []string{ | ||||||
| 		"https://api.ip.sb/geoip", |  | ||||||
| 		"https://ip.seeip.org/geoip", | 		"https://ip.seeip.org/geoip", | ||||||
| 		"https://ipapi.co/json", | 		"https://ipapi.co/json", | ||||||
| 		"https://freegeoip.app/json/", | 		"https://freegeoip.app/json/", | ||||||
|  | @ -31,11 +31,12 @@ var ( | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func UpdateIP() { | func UpdateIP() { | ||||||
|  | 	var count = 0 | ||||||
| 	for { | 	for { | ||||||
| 		ipv4 := fetchGeoIP(geoIPApiList, false) | 		ipv4 := fetchGeoIP(geoIPApiList, false) | ||||||
| 		ipv6 := fetchGeoIP(geoIPApiList, true) | 		ipv6 := fetchGeoIP(geoIPApiList, true) | ||||||
| 		if ipv4.IP == "" && ipv6.IP == "" { | 		if ipv4.IP == "" && ipv6.IP == "" { | ||||||
| 			time.Sleep(time.Minute) | 			time.Sleep(time.Second) | ||||||
| 			continue | 			continue | ||||||
| 		} | 		} | ||||||
| 		if ipv4.IP == "" || ipv6.IP == "" { | 		if ipv4.IP == "" || ipv6.IP == "" { | ||||||
|  | @ -48,7 +49,14 @@ func UpdateIP() { | ||||||
| 		} else if ipv6.CountryCode != "" { | 		} else if ipv6.CountryCode != "" { | ||||||
| 			cachedCountry = ipv6.CountryCode | 			cachedCountry = ipv6.CountryCode | ||||||
| 		} | 		} | ||||||
| 		time.Sleep(time.Minute * 30) | 		if len(cachedIP) > 0 || len(cachedCountry) > 0 { | ||||||
|  | 			break | ||||||
|  | 		} | ||||||
|  | 		count += 1 | ||||||
|  | 		if count > 3 { | ||||||
|  | 			break | ||||||
|  | 		} | ||||||
|  | 		time.Sleep(time.Second * 30) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -63,11 +71,11 @@ func fetchGeoIP(servers []string, isV6 bool) geoIP { | ||||||
| 			resp, err = httpClientV4.Get(servers[i]) | 			resp, err = httpClientV4.Get(servers[i]) | ||||||
| 		} | 		} | ||||||
| 		if err == nil { | 		if err == nil { | ||||||
| 			body, err := ioutil.ReadAll(resp.Body) | 			body, err := io.ReadAll(resp.Body) | ||||||
| 			if err != nil { | 			if err != nil { | ||||||
| 				continue | 				continue | ||||||
| 			} | 			} | ||||||
| 			resp.Body.Close() | 			_ = resp.Body.Close() | ||||||
| 			err = json.Unmarshal(body, &ip) | 			err = json.Unmarshal(body, &ip) | ||||||
| 			if err != nil { | 			if err != nil { | ||||||
| 				continue | 				continue | ||||||
|  | @ -88,3 +96,21 @@ func fetchGeoIP(servers []string, isV6 bool) geoIP { | ||||||
| 	} | 	} | ||||||
| 	return ip | 	return ip | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func GetLocalIp() string { | ||||||
|  | 	ipAddr := make([]string, 0) | ||||||
|  | 	addrs, err := net.InterfaceAddrs() | ||||||
|  | 	if err != nil { | ||||||
|  | 		fmt.Println(err) | ||||||
|  | 		return "" | ||||||
|  | 	} | ||||||
|  | 	for _, address := range addrs { | ||||||
|  | 		// 检查ip地址判断是否回环地址
 | ||||||
|  | 		if ipNet, ok := address.(*net.IPNet); ok && !ipNet.IP.IsLoopback() { | ||||||
|  | 			if ipNet.IP.To4() != nil { | ||||||
|  | 				ipAddr = append(ipAddr, ipNet.IP.String()) | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	return strings.Join(ipAddr, ";") | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -17,6 +17,7 @@ type Node struct { | ||||||
| 	Virtualization  string   `json:"virtualization,omitempty"` | 	Virtualization  string   `json:"virtualization,omitempty"` | ||||||
| 	BootTime        uint64   `json:"bootTime,omitempty"` | 	BootTime        uint64   `json:"bootTime,omitempty"` | ||||||
| 	IP              string   `json:"ip"` | 	IP              string   `json:"ip"` | ||||||
|  | 	LocalIP         string   `json:"localIP"` | ||||||
| 	CountryCode     string   `json:"countryCode,omitempty"` | 	CountryCode     string   `json:"countryCode,omitempty"` | ||||||
| 	Version         string   `json:"version,omitempty"` | 	Version         string   `json:"version,omitempty"` | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue