report geoip separately, fix server creation & deletion bugs (#14)

* new geoip method

* report geoip separately, fix server creation & deletion bugs

* fix struct tag

* fix write name

* remove deleteion list

* remove rpc realip header

* Revert "remove rpc realip header"

This reverts commit 8a5f86cf2d7df87f28cfa2a3b3430f449dd6ed73.
This commit is contained in:
UUBulb
2024-11-22 22:40:43 +08:00
committed by GitHub
parent d699d0ee87
commit fc98c0919f
16 changed files with 427 additions and 309 deletions

View File

@@ -1,6 +1,8 @@
package model
import (
"fmt"
pb "github.com/naiba/nezha/proto"
)
@@ -105,8 +107,6 @@ type Host struct {
Arch string `json:"arch,omitempty"`
Virtualization string `json:"virtualization,omitempty"`
BootTime uint64 `json:"boot_time,omitempty"`
IP string `json:"ip,omitempty"`
CountryCode string `json:"country_code,omitempty"`
Version string `json:"version,omitempty"`
GPU []string `json:"gpu,omitempty"`
}
@@ -122,8 +122,6 @@ func (h *Host) PB() *pb.Host {
Arch: h.Arch,
Virtualization: h.Virtualization,
BootTime: h.BootTime,
Ip: h.IP,
CountryCode: h.CountryCode,
Version: h.Version,
Gpu: h.GPU,
}
@@ -140,9 +138,36 @@ func PB2Host(h *pb.Host) Host {
Arch: h.GetArch(),
Virtualization: h.GetVirtualization(),
BootTime: h.GetBootTime(),
IP: h.GetIp(),
CountryCode: h.GetCountryCode(),
Version: h.GetVersion(),
GPU: h.GetGpu(),
}
}
type IP struct {
IPv4Addr string `json:"ipv4_addr,omitempty"`
IPv6Addr string `json:"ipv6_addr,omitempty"`
}
func (p *IP) Join() string {
if p.IPv4Addr != "" && p.IPv6Addr != "" {
return fmt.Sprintf("%s/%s", p.IPv4Addr, p.IPv6Addr)
} else if p.IPv4Addr != "" {
return p.IPv4Addr
}
return p.IPv6Addr
}
type GeoIP struct {
IP IP `json:"ip,omitempty"`
CountryCode string `json:"country_code,omitempty"`
}
func PB2GeoIP(p *pb.GeoIP) GeoIP {
pbIP := p.GetIp()
return GeoIP{
IP: IP{
IPv4Addr: pbIP.GetIpv4(),
IPv6Addr: pbIP.GetIpv6(),
},
}
}

View File

@@ -193,7 +193,7 @@ func (ns *NotificationServerBundle) replaceParamsInString(str string, message st
str = strings.ReplaceAll(str, "#SERVER.UDPCONNCOUNT#", mod(fmt.Sprintf("%d", ns.Server.State.UdpConnCount)))
var ipv4, ipv6, validIP string
ipList := strings.Split(ns.Server.Host.IP, "/")
ipList := strings.Split(ns.Server.GeoIP.IP.Join(), "/")
if len(ipList) > 1 {
// 双栈
ipv4 = ipList[0]
@@ -201,7 +201,7 @@ func (ns *NotificationServerBundle) replaceParamsInString(str string, message st
validIP = ipv4
} else if len(ipList) == 1 {
// 仅ipv4|ipv6
if strings.Contains(ipList[0], ":") {
if strings.IndexByte(ipList[0], ':') != -1 {
ipv6 = ipList[0]
validIP = ipv6
} else {

View File

@@ -49,8 +49,6 @@ func execCase(t *testing.T, item testSt) {
Arch: "",
Virtualization: "",
BootTime: 0,
IP: "1.1.1.1",
CountryCode: "",
Version: "",
},
State: &HostState{
@@ -70,6 +68,12 @@ func execCase(t *testing.T, item testSt) {
UdpConnCount: 0,
ProcessCount: 0,
},
GeoIP: &GeoIP{
IP: IP{
IPv4Addr: "1.1.1.1",
},
CountryCode: "",
},
LastActive: time.Time{},
TaskClose: nil,
TaskStream: nil,

View File

@@ -27,6 +27,7 @@ type Server struct {
Host *Host `gorm:"-" json:"host,omitempty"`
State *HostState `gorm:"-" json:"state,omitempty"`
GeoIP *GeoIP `gorm:"-" json:"geoip,omitempty"`
LastActive time.Time `gorm:"-" json:"last_active,omitempty"`
TaskClose chan error `gorm:"-" json:"-"`
@@ -40,6 +41,7 @@ type Server struct {
func (s *Server) CopyFromRunningServer(old *Server) {
s.Host = old.Host
s.State = old.State
s.GeoIP = old.GeoIP
s.LastActive = old.LastActive
s.TaskClose = old.TaskClose
s.TaskCloseLock = old.TaskCloseLock