实现网关监听并读取更新队列,定时发送更新数据到平台

This commit is contained in:
2025-05-28 16:12:50 +08:00
parent 5b5b674293
commit 1831c792ad
7 changed files with 106 additions and 101 deletions

View File

@@ -91,7 +91,7 @@ func OfflineEdge(id int32) error {
return fmt.Errorf("边缘节点 %d 不存在", id)
}
*edge.Status = core.EdgeOffline
edge.Status = &core.EdgeOffline
EdgeUpdates <- &core.Edge{
Id: id,
Status: &core.EdgeOffline,
@@ -101,12 +101,14 @@ func OfflineEdge(id int32) error {
}
func StoreEdge(edge *core.Edge) error {
if edge == nil || edge.Id == 0 || edge.Port == nil {
if edge == nil || edge.Id == 0 {
return fmt.Errorf("无效的边缘节点: %+v", edge)
}
Edges.Store(edge.Id, edge)
Assigns.Store(*edge.Port, edge.Id)
if edge.Port != nil {
Assigns.Store(*edge.Port, edge.Id)
}
return nil
}

View File

@@ -1,18 +1,13 @@
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()
@@ -26,50 +21,29 @@ func IpGeo(ip net.IP) (info *IpGeoInfo, err error) {
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 info = &IpGeoInfo{}
if values[2] != "0" {
info.Prov = values[2]
}
if values[3] != "0" {
info.City = values[3]
}
if values[4] != "0" {
info.Isp = values[4]
}
return info, 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
return nil, fmt.Errorf("本地归属地查询为空")
}
type IpGeoInfo struct {

102
gateway/app/report.go Normal file
View File

@@ -0,0 +1,102 @@
package app
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"proxy-server/gateway/core"
"proxy-server/gateway/env"
"strings"
)
func Online(name string) (err error) {
var resp string
resp, err = call(env.EndpointOnline, map[string]any{
"name": name,
"version": core.Version,
})
if err != nil {
return err
}
var body struct {
Id int32 `json:"id"`
Secret string `json:"secret"`
Permits []*core.PermitDef `json:"permits"`
Edges []*core.Edge `json:"edges"`
}
err = json.Unmarshal([]byte(resp), &body)
if err != nil {
return err
}
Id = body.Id
PlatformSecret = body.Secret
for _, def := range body.Permits {
StorePermit(def)
}
for _, edge := range body.Edges {
err := StoreEdge(edge)
if err != nil {
slog.Error("存储边缘节点失败", "err", err, "edge", edge)
}
}
return nil
}
func Offline() (err error) {
_, err = call(env.EndpointOffline, map[string]any{
"id": Id,
})
return err
}
func Update(data []*core.Edge) (err error) {
_, err = call(env.EndpointUpdate, map[string]any{
"id": Id,
"edges": data,
})
if err != nil {
err = fmt.Errorf("更新节点数据失败:%w", err)
}
return err
}
func call(endpoint string, body any) (string, error) {
bodyStr, err := json.Marshal(body)
if err != nil {
panic(err)
}
req, err := http.NewRequest("POST", endpoint, strings.NewReader(string(bodyStr)))
if err != nil {
panic(err)
}
var auth = base64.RawURLEncoding.EncodeToString([]byte(env.ClientId + ":" + env.ClientSecret))
var basic = fmt.Sprintf("Basic %s", auth)
req.Header.Set("Authorization", basic)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("请求失败,状态码:%d", resp.StatusCode)
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(respBody), nil
}