新增代理服务与边缘节点注册功能

This commit is contained in:
2025-05-13 18:48:17 +08:00
parent 536f36ae02
commit d69a77df38
17 changed files with 573 additions and 203 deletions

57
client/geo/cip.go Normal file
View File

@@ -0,0 +1,57 @@
package geo
import (
"bufio"
"github.com/pkg/errors"
"log/slog"
"net/http"
"net/textproto"
"strings"
)
func Cip() (prov, city, isp string, err error) {
const endpoint = "http://cip.cc"
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return "", "", "", errors.Wrap(err, "创建请求失败")
}
req.Header.Set("User-Agent", "curl/8.9.1")
req.Header.Set("Accept", "*/*")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", "", "", errors.Wrap(err, "请求失败")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", "", "", errors.New("请求失败,状态码: " + resp.Status)
}
reader := textproto.NewReader(bufio.NewReader(resp.Body))
_, err = reader.ReadLine()
if err != nil {
return "", "", "", errors.Wrap(err, "读取响应失败")
}
addrLine, err := reader.ReadLine()
if err != nil {
return "", "", "", errors.Wrap(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 "", "", "", errors.Wrap(err, "读取响应失败")
}
isp = strings.TrimSpace(strings.Split(ispLine, ":")[1])
if prov == "" || city == "" || isp == "" {
return "", "", "", errors.New("解析数据为空")
}
slog.Debug("获取归属地", "prov", prov, "city", city, "isp", isp)
return prov, city, isp, nil
}

3
client/geo/geo.go Normal file
View File

@@ -0,0 +1,3 @@
package geo
type Func func() (prov, city, isp string, err error)

40
client/geo/ipapi.go Normal file
View File

@@ -0,0 +1,40 @@
package geo
import (
"encoding/json"
"github.com/pkg/errors"
"net/http"
)
func Ipapi() (prov, city, isp string, err error) {
const endpoint = "http://ip-api.com/json/?fields=regionName,city,as&lang=zh-CN"
resp, err := http.Get(endpoint)
if err != nil {
return "", "", "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", "", "", err
}
var data struct {
RegionName string `json:"regionName"`
City string `json:"city"`
As string `json:"as"`
}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return "", "", "", err
}
prov = data.RegionName
city = data.City
isp = data.As
if prov == "" || city == "" || isp == "" {
return "", "", "", errors.New("解析数据为空")
}
return prov, city, isp, nil
}