/
info.go
58 lines (47 loc) · 1.37 KB
/
info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package goip
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
// https://topic.alibabacloud.com/a/go-combat-golang-get-public-ip-view-intranet-ip-detect-ip-type-verify-ip-range-ip-address-string-and-int-conversion-judge-by-ip_1_38_10267608.html
// Info ...
type Info struct {
Code int `json:"code"`
Data IP `json:"data"`
}
// IP ...
type IP struct {
Country string `json:"country"`
CountryID string `json:"country_id"`
Area string `json:"area"`
AreaID string `json:"area_id"`
Region string `json:"region"`
RegionID string `json:"region_id"`
City string `json:"city"`
CityID string `json:"city_id"`
Isp string `json:"isp"`
}
// TabaoAPI ...
func TabaoAPI(ip string) (*Info, error) {
ctx, cncl := context.WithTimeout(context.Background(), time.Second*10)
defer cncl()
addr := "http://ip.taobao.com/service/getIpInfo.php?ip=" + ip
resp, err := http.NewRequestWithContext(ctx, http.MethodGet, addr, nil)
if err != nil {
return nil, fmt.Errorf("failed http.Get(%s), × err: %w", addr, err)
}
defer resp.Body.Close()
out, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed ioutil.ReadAll, × err: %w", err)
}
var result Info
if err := json.Unmarshal(out, &result); err != nil {
return nil, fmt.Errorf("failed json.Unmarshal %s, × err: %w", string(out), err)
}
return &result, nil
}