⚡️ 优化Agent体积和逻辑
This commit is contained in:
		
							parent
							
								
									e08585b4aa
								
							
						
					
					
						commit
						42b6768979
					
				@ -24,7 +24,6 @@ import (
 | 
				
			|||||||
	"github.com/naiba/nezha/model"
 | 
						"github.com/naiba/nezha/model"
 | 
				
			||||||
	"github.com/naiba/nezha/pkg/utils"
 | 
						"github.com/naiba/nezha/pkg/utils"
 | 
				
			||||||
	pb "github.com/naiba/nezha/proto"
 | 
						pb "github.com/naiba/nezha/proto"
 | 
				
			||||||
	"github.com/naiba/nezha/service/dao"
 | 
					 | 
				
			||||||
	"github.com/naiba/nezha/service/rpc"
 | 
						"github.com/naiba/nezha/service/rpc"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -37,6 +36,7 @@ var (
 | 
				
			|||||||
	server       string
 | 
						server       string
 | 
				
			||||||
	clientSecret string
 | 
						clientSecret string
 | 
				
			||||||
	version      string
 | 
						version      string
 | 
				
			||||||
 | 
						debug        bool
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var (
 | 
					var (
 | 
				
			||||||
@ -60,19 +60,14 @@ const (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
	// 来自于 GoReleaser 的版本号
 | 
						// 来自于 GoReleaser 的版本号
 | 
				
			||||||
	dao.Version = version
 | 
						monitor.Version = version
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var debug bool
 | 
					 | 
				
			||||||
	flag.String("i", "", "unused 旧Agent配置兼容")
 | 
						flag.String("i", "", "unused 旧Agent配置兼容")
 | 
				
			||||||
	flag.BoolVar(&debug, "d", false, "开启调试信息")
 | 
						flag.BoolVar(&debug, "d", false, "开启调试信息")
 | 
				
			||||||
	flag.StringVar(&server, "s", "localhost:5555", "管理面板RPC端口")
 | 
						flag.StringVar(&server, "s", "localhost:5555", "管理面板RPC端口")
 | 
				
			||||||
	flag.StringVar(&clientSecret, "p", "", "Agent连接Secret")
 | 
						flag.StringVar(&clientSecret, "p", "", "Agent连接Secret")
 | 
				
			||||||
	flag.Parse()
 | 
						flag.Parse()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	dao.Conf = &model.Config{
 | 
					 | 
				
			||||||
		Debug: debug,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if server == "" || clientSecret == "" {
 | 
						if server == "" || clientSecret == "" {
 | 
				
			||||||
		flag.Usage()
 | 
							flag.Usage()
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
@ -310,7 +305,7 @@ func doSelfUpdate() {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func println(v ...interface{}) {
 | 
					func println(v ...interface{}) {
 | 
				
			||||||
	if dao.Conf.Debug {
 | 
						if debug {
 | 
				
			||||||
		log.Println(v...)
 | 
							log.Println(v...)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -14,9 +14,9 @@ import (
 | 
				
			|||||||
	"github.com/shirou/gopsutil/v3/net"
 | 
						"github.com/shirou/gopsutil/v3/net"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/naiba/nezha/model"
 | 
						"github.com/naiba/nezha/model"
 | 
				
			||||||
	"github.com/naiba/nezha/service/dao"
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var Version string = "debug"
 | 
				
			||||||
var netInSpeed, netOutSpeed, netInTransfer, netOutTransfer, lastUpdate uint64
 | 
					var netInSpeed, netOutSpeed, netInTransfer, netOutTransfer, lastUpdate uint64
 | 
				
			||||||
var expectDiskFsTypes = []string{
 | 
					var expectDiskFsTypes = []string{
 | 
				
			||||||
	"apfs", "ext4", "ext3", "ext2", "f2fs", "reiserfs", "jfs", "btrfs", "fuseblk", "zfs", "simfs", "ntfs", "fat32", "exfat", "xfs",
 | 
						"apfs", "ext4", "ext3", "ext2", "f2fs", "reiserfs", "jfs", "btrfs", "fuseblk", "zfs", "simfs", "ntfs", "fat32", "exfat", "xfs",
 | 
				
			||||||
@ -59,7 +59,7 @@ func GetHost() *model.Host {
 | 
				
			|||||||
		BootTime:        hi.BootTime,
 | 
							BootTime:        hi.BootTime,
 | 
				
			||||||
		IP:              cachedIP,
 | 
							IP:              cachedIP,
 | 
				
			||||||
		CountryCode:     strings.ToLower(cachedCountry),
 | 
							CountryCode:     strings.ToLower(cachedCountry),
 | 
				
			||||||
		Version:         dao.Version,
 | 
							Version:         Version,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -13,18 +13,17 @@ import (
 | 
				
			|||||||
type geoIP struct {
 | 
					type geoIP struct {
 | 
				
			||||||
	CountryCode string `json:"country_code,omitempty"`
 | 
						CountryCode string `json:"country_code,omitempty"`
 | 
				
			||||||
	IP          string `json:"ip,omitempty"`
 | 
						IP          string `json:"ip,omitempty"`
 | 
				
			||||||
 | 
						Query       string `json:"query,omitempty"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var (
 | 
					var (
 | 
				
			||||||
	ipv4Servers = []string{
 | 
						geoIPApiList = []string{
 | 
				
			||||||
		"https://api.ip.sb/geoip",
 | 
					 | 
				
			||||||
		"https://ip.seeip.org/geoip",
 | 
					 | 
				
			||||||
		"https://ipapi.co/json",
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	ipv6Servers = []string{
 | 
					 | 
				
			||||||
		"https://api.ip.sb/geoip",
 | 
							"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/",
 | 
				
			||||||
 | 
							"http://ip-api.com/json/",
 | 
				
			||||||
 | 
							"https://extreme-ip-lookup.com/json/",
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	cachedIP, cachedCountry string
 | 
						cachedIP, cachedCountry string
 | 
				
			||||||
	httpClientV4            = utils.NewSingleStackHTTPClient(time.Second*20, time.Second*5, time.Second*10, false)
 | 
						httpClientV4            = utils.NewSingleStackHTTPClient(time.Second*20, time.Second*5, time.Second*10, false)
 | 
				
			||||||
@ -33,8 +32,8 @@ var (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func UpdateIP() {
 | 
					func UpdateIP() {
 | 
				
			||||||
	for {
 | 
						for {
 | 
				
			||||||
		ipv4 := fetchGeoIP(ipv4Servers, false)
 | 
							ipv4 := fetchGeoIP(geoIPApiList, false)
 | 
				
			||||||
		ipv6 := fetchGeoIP(ipv6Servers, true)
 | 
							ipv6 := fetchGeoIP(geoIPApiList, true)
 | 
				
			||||||
		cachedIP = fmt.Sprintf("ip(v4:%s,v6:[%s])", ipv4.IP, ipv6.IP)
 | 
							cachedIP = fmt.Sprintf("ip(v4:%s,v6:[%s])", ipv4.IP, ipv6.IP)
 | 
				
			||||||
		if ipv4.CountryCode != "" {
 | 
							if ipv4.CountryCode != "" {
 | 
				
			||||||
			cachedCountry = ipv4.CountryCode
 | 
								cachedCountry = ipv4.CountryCode
 | 
				
			||||||
@ -65,6 +64,9 @@ func fetchGeoIP(servers []string, isV6 bool) geoIP {
 | 
				
			|||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				continue
 | 
									continue
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
								if ip.IP == "" && ip.Query != "" {
 | 
				
			||||||
 | 
									ip.IP = ip.Query
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
			return ip
 | 
								return ip
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user