init
This commit is contained in:
commit
45545e6933
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
.idea
|
17
README.md
Normal file
17
README.md
Normal file
@ -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
|
||||
```
|
5
go.mod
Normal file
5
go.mod
Normal file
@ -0,0 +1,5 @@
|
||||
module git.ssgfgtfy.com/public/ssgf_utils
|
||||
|
||||
go 1.24.0
|
||||
|
||||
require github.com/pkg/errors v0.9.1
|
2
go.sum
Normal file
2
go.sum
Normal file
@ -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=
|
112
ip_client/client.go
Normal file
112
ip_client/client.go
Normal file
@ -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"`
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user