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

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

@@ -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 {