移除节点归属地查询功能,归属地查询放到网关实现;优化节点上线流程,网关根据节点状态提交增量更新数据

This commit is contained in:
2025-05-28 14:28:57 +08:00
parent 36013d7d04
commit 49f60b3e87
13 changed files with 160 additions and 193 deletions

View File

@@ -32,40 +32,56 @@ var (
EdgeUpdates = make(chan *core.Edge, 1000) // 节点更新通知通道
)
func NewEdge(id int32, port uint16, addr *net.TCPAddr) {
var host = addr.IP.String()
var edge = &core.Edge{
Id: id,
Host: &host,
Port: &port,
func NewEdge(id int32, port uint16, addr *net.TCPAddr) error {
if addr == nil {
return fmt.Errorf("边缘节点 %d 地址无效", id)
}
info, err := IpGeo(addr.IP)
if err != nil {
return fmt.Errorf("查询 geo 信息失败:%w", err)
}
// todo 查询 geo 信息
var host = addr.IP.String()
var edge = &core.Edge{
Id: id,
Host: &host,
Port: &port,
Prov: &info.Prov,
City: &info.City,
Isp: &info.Isp,
Status: &core.EdgeOnline,
}
Edges.Store(id, edge)
Assigns.Store(port, id)
EdgeUpdates <- edge
return nil
}
func TryUpdateEdge(id int32, addr *net.TCPAddr) error {
if addr == nil {
return fmt.Errorf("边缘节点 %d 地址无效", id)
}
edge, ok := Edges.Load(id)
if !ok {
return fmt.Errorf("边缘节点 %d 不存在", id)
}
edge.Status = &core.EdgeOnline
toUpdate := &core.Edge{
Id: edge.Id,
Status: &core.EdgeOnline,
}
host := addr.IP.String()
if edge.Host == nil || *edge.Host != host {
edge.Host = &host
EdgeUpdates <- &core.Edge{
Id: edge.Id,
Host: &host,
}
toUpdate.Host = &host
}
EdgeUpdates <- toUpdate
return nil
}

79
gateway/app/geo.go Normal file
View File

@@ -0,0 +1,79 @@
package app
import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net"
"net/http"
g "proxy-server/gateway/globals"
"strings"
)
const base = "https://whois.pconline.com.cn/ipJson.jsp?json=true&ip="
func IpGeo(ip net.IP) (info *IpGeoInfo, err error) {
defer func() {
var rs = recover()
if reErr, ok := rs.(error); ok {
err = fmt.Errorf("执行归属地查询异常 %w", reErr)
}
}()
// 本地归属地查询
str, err := g.Geo.SearchByStr(ip.String())
if err != nil {
return nil, err
}
if str != "" {
slog.Debug("本地解析归属地结果", "str", str)
values := strings.Split(str, "|")
if len(values) != 5 {
return nil, fmt.Errorf("本地归属地查询解析失败")
}
return &IpGeoInfo{
Prov: values[2],
City: values[3],
Isp: values[4],
}, nil
}
// 归属地查询
var url = base + ip.String()
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("查询归属地失败: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("查询归属地失败: %s", resp.Status)
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
// 返回节点信息
var bytes []byte
_, err = io.ReadFull(resp.Body, bytes)
if err != nil {
return nil, fmt.Errorf("读取归属地响应失败: %w", err)
}
var body = make(map[string]any)
err = json.Unmarshal(bytes, &body)
if err != nil {
return nil, fmt.Errorf("解析归属地响应失败: %w", err)
}
return &IpGeoInfo{
Prov: body["pro"].(string),
City: body["city"].(string),
Isp: strings.Split(body["addr"].(string), " ")[1],
}, nil
}
type IpGeoInfo struct {
Prov string
City string
Isp string
}

View File

@@ -193,7 +193,10 @@ func onOpen(ctx context.Context, writer io.Writer, edgeId int32, addr net.Addr)
return errors.New("没有可用的端口")
}
app.NewEdge(edgeId, port, tcpAddr)
err := app.NewEdge(edgeId, port, tcpAddr)
if err != nil {
return fmt.Errorf("新增边缘节点失败:%w", err)
}
app.LockPortAssign.Unlock()
} else {
@@ -201,7 +204,7 @@ func onOpen(ctx context.Context, writer io.Writer, edgeId int32, addr net.Addr)
port = *edge.Port
err := app.TryUpdateEdge(edgeId, tcpAddr)
if err != nil {
return fmt.Errorf("尝试更新边缘节点地址失败: %w", err)
return fmt.Errorf("尝试更新边缘节点失败: %w", err)
}
}

View File

@@ -118,8 +118,9 @@ func (s *server) Run() (err error) {
slog.Error("服务下线失败", "err", err)
}
// 关闭 redis
// 关闭服务
g.ExitRedis()
g.ExitGeo()
}()
// 等待其它服务关闭
@@ -143,6 +144,7 @@ func (s *server) init() error {
log.Init()
env.Init()
g.InitRedis()
g.InitGeo()
return nil
}

23
gateway/globals/geo.go Normal file
View File

@@ -0,0 +1,23 @@
package globals
import "github.com/lionsoul2014/ip2region/binding/golang/xdb"
var Geo *xdb.Searcher
func InitGeo() {
var err error
buff, err := xdb.LoadContentFromFile("ip2region.xdb")
if err != nil {
panic("读取 geo 数据库文件失败")
}
Geo, err = xdb.NewWithBuffer(buff)
if err != nil {
panic("初始化 geo 查询工具失败")
}
}
func ExitGeo() {
Geo.Close()
}

View File

@@ -16,7 +16,6 @@ type InfoResp struct {
DataConnections int `json:"data_connections"`
// Edges []EdgeResp `json:"edges"`
Assigns map[uint16]int32 `json:"assigns"`
Edges map[int32]*core.Edge `json:"edges"`
Permits map[int32]*core.Permit `json:"permits"`
}
@@ -29,12 +28,6 @@ type EdgeResp struct {
func Info(c *fiber.Ctx) error {
var assigns = make(map[uint16]int32)
app.Assigns.Range(func(port uint16, id int32) bool {
assigns[port] = id
return true
})
var edges = make(map[int32]*core.Edge)
app.Edges.Range(func(id int32, edge *core.Edge) bool {
edges[id] = edge
@@ -54,7 +47,6 @@ func Info(c *fiber.Ctx) error {
UserConnections: int(app.UserConnWg.Count()),
CtrlConnections: int(app.CtrlConnWg.Count()),
DataConnections: int(app.DataConnWg.Count()),
Assigns: assigns,
Edges: edges,
Permits: permits,
})