-
Notifications
You must be signed in to change notification settings - Fork 4
/
ipapi.go
82 lines (68 loc) · 2.31 KB
/
ipapi.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package ipapi
import (
"fmt"
jsoniter "github.com/json-iterator/go"
"github.com/valyala/fasthttp"
)
var baseURL = "https://api.ipquery.io/"
var json = jsoniter.ConfigCompatibleWithStandardLibrary
// ISPInfo represents information about the ISP of an IP address.
type ISPInfo struct {
ASN string `json:"asn,omitempty"`
Org string `json:"org,omitempty"`
ISP string `json:"isp,omitempty"`
}
// LocationInfo represents geographical information about an IP address.
type LocationInfo struct {
Country string `json:"country,omitempty"`
CountryCode string `json:"country_code,omitempty"`
City string `json:"city,omitempty"`
State string `json:"state,omitempty"`
ZipCode string `json:"zipcode,omitempty"`
Latitude float64 `json:"latitude,omitempty"`
Longitude float64 `json:"longitude,omitempty"`
Timezone string `json:"timezone,omitempty"`
Localtime string `json:"localtime,omitempty"`
}
// RiskInfo represents risk information about an IP address.
type RiskInfo struct {
IsMobile bool `json:"is_mobile,omitempty"`
IsVPN bool `json:"is_vpn,omitempty"`
IsTor bool `json:"is_tor,omitempty"`
IsProxy bool `json:"is_proxy,omitempty"`
IsDatacenter bool `json:"is_datacenter,omitempty"`
RiskScore int `json:"risk_score,omitempty"`
}
// IPInfo represents all the information returned by the API.
type IPInfo struct {
IP string `json:"ip"`
ISP *ISPInfo `json:"isp,omitempty"`
Location *LocationInfo `json:"location,omitempty"`
Risk *RiskInfo `json:"risk,omitempty"`
}
// QueryIP fetches information for a specific IP address.
func QueryIP(ip string) (*IPInfo, error) {
statusCode, body, err := fasthttp.Get(nil, baseURL+ip)
if err != nil {
return nil, err
}
if statusCode != fasthttp.StatusOK {
return nil, fmt.Errorf("failed to fetch IP info: status code %d", statusCode)
}
var ipInfo IPInfo
if err := json.Unmarshal(body, &ipInfo); err != nil {
return nil, err
}
return &ipInfo, nil
}
// QueryOwnIP fetches information about the current machine's public IP.
func QueryOwnIP() (string, error) {
statusCode, body, err := fasthttp.Get(nil, baseURL)
if err != nil {
return "", err
}
if statusCode != fasthttp.StatusOK {
return "", fmt.Errorf("failed to fetch own IP: status code %d", statusCode)
}
return string(body), nil
}