From 45545e69333db22952cf82e330c869dab967974a Mon Sep 17 00:00:00 2001 From: yuguojian <18126816215> Date: Mon, 12 May 2025 17:47:28 +0800 Subject: [PATCH] init --- .gitignore | 1 + README.md | 17 +++++++ go.mod | 5 ++ go.sum | 2 + ip_client/client.go | 112 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 137 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 ip_client/client.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..497da60 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +## window开发者执行 +```bash +git config --global core.autocrlf true # 提交时转 CRLF 为 LF,检出时转 LF 为 CRLF +``` + +## mac开发者执行 +```bash +git config --global core.autocrlf input # 提交时转 CRLF 为 LF,检出时不转换 +``` + +## 格式化 +```bash +gofumpt -l -w . +``` +```bash +go install mvdan.cc/gofumpt@latest +``` diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..42f95b8 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.ssgfgtfy.com/public/ssgf_utils + +go 1.24.0 + +require github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7c401c3 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= diff --git a/ip_client/client.go b/ip_client/client.go new file mode 100644 index 0000000..c97b7a1 --- /dev/null +++ b/ip_client/client.go @@ -0,0 +1,112 @@ +package ip_client + +import ( + "encoding/json" + "fmt" + "io" + "log" + "net/http" + "net/url" + "time" + + "github.com/pkg/errors" +) + +type HuaChenIpInfo struct { + AppCode string + Host string +} + +func NewHuaChenIpInfo(appCode string) *HuaChenIpInfo { + return &HuaChenIpInfo{ + AppCode: appCode, + Host: "https://c2ba.api.huachen.cn", + } +} + +func (h *HuaChenIpInfo) GetIpInfo(ip string) (res *ApiResult, err error) { + if ip == "" { + return nil, errors.New("请输入ip地址") + } + // 设置参数 + params := url.Values{} + params.Add("ip", ip) + // 拼接URL + var fullURL string + fullURL, err = url.JoinPath(h.Host, "ip") + if err != nil { + return nil, errors.Wrapf(err, "获取ip:%s信息失败,拼接路径错误", ip) + } + // 拼接参数 + fullURL = fmt.Sprintf("%s?%s", fullURL, params.Encode()) + // 创建HTTP客户端 + client := &http.Client{} + // 创建请求 + var req *http.Request + req, err = http.NewRequest("GET", fullURL, nil) + if err != nil { + return nil, errors.Wrapf(err, "获取ip:%s信息失败,创建请求失败", ip) + } + + // 设置请求头 + req.Header.Add("Authorization", "APPCODE "+h.AppCode) + req.Header.Add("Content-Type", "application/json") + + // 发送请求 + client.Timeout = 5 * time.Second + var resp *http.Response + resp, err = client.Do(req) + if err != nil { + return nil, errors.Wrapf(err, "获取ip:%s信息失败,发送请求失败", ip) + } + defer func(Body io.ReadCloser) { + err = Body.Close() + if err != nil { + log.Printf("获取ip:%s信息,关闭响应体失败: %+v\n", ip, err) + } + }(resp.Body) + + // 读取响应体 + var body []byte + body, err = io.ReadAll(resp.Body) + if err != nil { + return nil, errors.Wrapf(err, "获取ip:%s信息失败,读取响应体失败", ip) + } + + // 解析JSON响应 + var apiResult ApiResult + err = json.Unmarshal(body, &apiResult) + if err != nil { + return nil, errors.Wrapf(err, "获取ip:%s信息失败,解析JSON响应失败,%s", ip, string(body)) + } + + // 检查API返回状态 + if apiResult.Ret != 200 { + return &apiResult, errors.New(fmt.Sprintf("获取ip:%s信息失败,%+v", ip, apiResult)) + } + return &apiResult, nil +} + +type ApiResult struct { + Ret int `json:"ret"` + Msg string `json:"msg"` + Data IpInfo `json:"data"` + LogId string `json:"log_id"` +} + +type IpInfo struct { + Ip string `json:"ip"` + LongIp string `json:"long_ip"` + Isp string `json:"isp"` + Area string `json:"area"` + RegionId string `json:"region_id"` + Region string `json:"region"` + CityId string `json:"city_id"` + City string `json:"city"` + District string `json:"district"` + DistrictId string `json:"district_id"` + CountryId string `json:"country_id"` + Country string `json:"country"` + Lat string `json:"lat"` + Lng string `json:"lng"` +}