feat(初始项目): 初始项目

main
xiaoqidun 1 year ago
parent acdaf16e48
commit acf6b10373

3
.gitignore vendored

@ -0,0 +1,3 @@
.idea/
.vscode/
.devcontainer/

@ -1 +1,2 @@
# tipdw
# tipdw
Golang 腾讯IP定位库

@ -0,0 +1,3 @@
module github.com/xiaoqidun/tipdw
go 1.17

@ -0,0 +1,72 @@
package tipdw
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
var client = &http.Client{Timeout: 5 * time.Second}
type Body struct {
Status int `json:"status"`
Message string `json:"message"`
Result Result `json:"result"`
}
type Result struct {
IP string `json:"ip"`
AdInfo AdInfo `json:"ad_info"`
Location Location `json:"location"`
}
type AdInfo struct {
Nation string `json:"nation"`
Province string `json:"province"`
City string `json:"city"`
District string `json:"district"`
Adcode int `json:"adcode"`
}
type Location struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
}
// QueryIP 使用腾讯位置服务查询IP
func QueryIP(sk string, key string, ip string) (result Result, err error) {
arg := &reqLBS{
SK: sk,
Path: "/ws/location/v1/ip",
Args: map[string]string{
"ip": ip,
"key": key,
},
}
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s?%s", "https://apis.map.qq.com/ws/location/v1/ip", arg.Encode()), nil)
if err != nil {
return
}
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
var bodyUnmarshal Body
err = json.Unmarshal(body, &bodyUnmarshal)
if err != nil {
return
}
if bodyUnmarshal.Status != 0 {
err = fmt.Errorf("resp code is %d, body is %s", bodyUnmarshal.Status, body)
return
}
result = bodyUnmarshal.Result
return
}

@ -0,0 +1,43 @@
package tipdw
import (
"crypto/md5"
"encoding/hex"
"fmt"
"net/url"
"sort"
"strings"
)
type reqLBS struct {
SK string
Path string
Args map[string]string
}
func (r *reqLBS) Encode() string {
r.Signature()
req := url.Values{}
for k, v := range r.Args {
req.Set(k, v)
}
return req.Encode()
}
func (r *reqLBS) Signature() {
if r.Args == nil {
r.Args = make(map[string]string)
}
var keys []string
for k := range r.Args {
keys = append(keys, k)
}
sort.Strings(keys)
var keyValue []string
for i := 0; i < len(keys); i++ {
keyValue = append(keyValue, fmt.Sprintf("%s=%s", keys[i], r.Args[keys[i]]))
}
signStr := r.Path + "?" + strings.Join(keyValue, "&") + r.SK
signMd5 := md5.Sum([]byte(signStr))
r.Args["sig"] = hex.EncodeToString(signMd5[:])
}
Loading…
Cancel
Save