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

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

@@ -14,7 +14,6 @@ import (
"os/signal"
"proxy-server/edge/core"
"proxy-server/edge/env"
"proxy-server/edge/geo"
"proxy-server/edge/report"
"proxy-server/utils"
"time"
@@ -31,13 +30,6 @@ func Start() error {
return fmt.Errorf("初始化环境变量失败: %w", err)
}
// 获取归属地
slog.Debug("获取节点归属地...")
err = geo.Query()
if err != nil {
return fmt.Errorf("获取节点归属地失败: %w", err)
}
// 注册节点
slog.Debug("注册节点...")
id, host, err := report.Online()

View File

@@ -1,159 +0,0 @@
package geo
import (
"bufio"
"encoding/json"
"fmt"
"io"
"net/http"
"net/textproto"
"proxy-server/edge/env"
"strings"
)
var (
Ip string
Prov string
City string
Isp string
)
func Query() (err error) {
switch env.Mode {
case "dev":
err = dev()
default:
err = ipapi()
}
return err
}
func dev() (err error) {
Prov = "河南省"
City = "郑州市"
Isp = "电信"
return nil
}
func cip() (err error) {
//goland:noinspection HttpUrlsUsage
const endpoint = "http://cip.cc"
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return fmt.Errorf("创建请求失败: %w", err)
}
req.Header.Set("User-Agent", "curl/8.9.1")
req.Header.Set("Accept", "*/*")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("执行请求失败: %w", err)
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("状态码: %s", resp.Status)
}
reader := textproto.NewReader(bufio.NewReader(resp.Body))
ipLine, err := reader.ReadLine()
if err != nil {
return fmt.Errorf("读取响应失败: %w", err)
}
Ip = strings.TrimSpace(strings.Split(ipLine, ":")[1])
addrLine, err := reader.ReadLine()
if err != nil {
return fmt.Errorf("读取响应失败: %w", err)
}
addr := strings.Split(strings.Split(addrLine, ":")[1], " ")
Prov = strings.TrimSpace(addr[1])
City = strings.TrimSpace(addr[2])
ispLine, err := reader.ReadLine()
if err != nil {
return fmt.Errorf("读取响应失败: %w", err)
}
Isp = strings.TrimSpace(strings.Split(ispLine, ":")[1])
return nil
}
func ipapi() (err error) {
//goland:noinspection HttpUrlsUsage
const endpoint = "http://ip-api.com/json/?fields=regionName,city,as,query&lang=zh-CN"
resp, err := http.Get(endpoint)
if err != nil {
return fmt.Errorf("执行请求失败: %w", err)
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("状态码: %s", resp.Status)
}
var data struct {
RegionName string `json:"regionName"`
City string `json:"city"`
As string `json:"as"`
Query string `json:"query"`
}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return fmt.Errorf("解析响应失败: %w", err)
}
Ip = data.Query
Prov = data.RegionName
City = data.City
var telecom = []string{"AS4134", "AS4812", "AS134419", "AS140292"}
var unicom = []string{"AS4837", "AS17621", "AS17816"}
var mobile = []string{
"AS9808", "AS24444", "AS24445", "AS24547", "AS38019",
"AS56040", "AS56041", "AS56042", "AS56044", "AS56046", "AS56047",
"AS132525", "AS134810",
}
var foreign = []string{
"AS9299",
}
for _, telecomAsn := range telecom {
if strings.HasPrefix(data.As, telecomAsn) {
Isp = "电信"
break
}
}
if Isp == "" {
for _, unicomAsn := range unicom {
if strings.HasPrefix(data.As, unicomAsn) {
Isp = "联通"
break
}
}
}
if Isp == "" {
for _, mobileAsn := range mobile {
if strings.HasPrefix(data.As, mobileAsn) {
Isp = "移动"
break
}
}
}
if Isp == "" {
for _, foreignAsn := range foreign {
if strings.HasPrefix(data.As, foreignAsn) {
Isp = "国外"
break
}
}
}
return nil
}