2023-02-28 09:56:20 +08:00
|
|
|
package monitor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"environmentCaptureAgent/pkg/utils"
|
|
|
|
"fmt"
|
2023-06-17 09:40:49 +08:00
|
|
|
"io"
|
|
|
|
"net"
|
2023-02-28 09:56:20 +08:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type geoIP struct {
|
|
|
|
CountryCode string `json:"country_code,omitempty"`
|
|
|
|
IP string `json:"ip,omitempty"`
|
|
|
|
Query string `json:"query,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
geoIPApiList = []string{
|
|
|
|
"https://ip.seeip.org/geoip",
|
|
|
|
"https://ipapi.co/json",
|
|
|
|
"https://freegeoip.app/json/",
|
|
|
|
"http://ip-api.com/json/",
|
|
|
|
"https://extreme-ip-lookup.com/json/",
|
|
|
|
}
|
|
|
|
cachedIP, cachedCountry string
|
|
|
|
httpClientV4 = utils.NewSingleStackHTTPClient(time.Second*20, time.Second*5, time.Second*10, false)
|
|
|
|
httpClientV6 = utils.NewSingleStackHTTPClient(time.Second*20, time.Second*5, time.Second*10, true)
|
|
|
|
)
|
|
|
|
|
|
|
|
func UpdateIP() {
|
2023-06-17 09:40:49 +08:00
|
|
|
var count = 0
|
2023-02-28 09:56:20 +08:00
|
|
|
for {
|
|
|
|
ipv4 := fetchGeoIP(geoIPApiList, false)
|
|
|
|
ipv6 := fetchGeoIP(geoIPApiList, true)
|
|
|
|
if ipv4.IP == "" && ipv6.IP == "" {
|
2023-06-17 09:40:49 +08:00
|
|
|
time.Sleep(time.Second)
|
2023-02-28 09:56:20 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ipv4.IP == "" || ipv6.IP == "" {
|
|
|
|
cachedIP = fmt.Sprintf("%s%s", ipv4.IP, ipv6.IP)
|
|
|
|
} else {
|
|
|
|
cachedIP = fmt.Sprintf("%s/%s", ipv4.IP, ipv6.IP)
|
|
|
|
}
|
|
|
|
if ipv4.CountryCode != "" {
|
|
|
|
cachedCountry = ipv4.CountryCode
|
|
|
|
} else if ipv6.CountryCode != "" {
|
|
|
|
cachedCountry = ipv6.CountryCode
|
|
|
|
}
|
2023-06-17 09:40:49 +08:00
|
|
|
if len(cachedIP) > 0 || len(cachedCountry) > 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
count += 1
|
|
|
|
if count > 3 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second * 30)
|
2023-02-28 09:56:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchGeoIP(servers []string, isV6 bool) geoIP {
|
|
|
|
var ip geoIP
|
|
|
|
var resp *http.Response
|
|
|
|
var err error
|
|
|
|
for i := 0; i < len(servers); i++ {
|
|
|
|
if isV6 {
|
|
|
|
resp, err = httpClientV6.Get(servers[i])
|
|
|
|
} else {
|
|
|
|
resp, err = httpClientV4.Get(servers[i])
|
|
|
|
}
|
|
|
|
if err == nil {
|
2023-06-17 09:40:49 +08:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2023-02-28 09:56:20 +08:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-06-17 09:40:49 +08:00
|
|
|
_ = resp.Body.Close()
|
2023-02-28 09:56:20 +08:00
|
|
|
err = json.Unmarshal(body, &ip)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ip.IP == "" && ip.Query != "" {
|
|
|
|
ip.IP = ip.Query
|
|
|
|
}
|
|
|
|
// 没取到 v6 IP
|
|
|
|
if isV6 && !strings.Contains(ip.IP, ":") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// 没取到 v4 IP
|
|
|
|
if !isV6 && !strings.Contains(ip.IP, ".") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return ip
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ip
|
|
|
|
}
|
2023-06-17 09:40:49 +08:00
|
|
|
|
|
|
|
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, ";")
|
|
|
|
}
|