-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipgeo.go
88 lines (81 loc) · 1.91 KB
/
ipgeo.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
83
84
85
86
87
88
package ipipgo
import (
"encoding/json"
"errors"
"fmt"
"io"
"net"
"strings"
emojiflag "github.com/jayco/go-emoji-flag"
)
var (
ErrInvalidIP = errors.New("invalid IP address")
)
type IPGeo struct {
Organization string `json:"organization"`
Longitude float64 `json:"longitude"`
City string `json:"city"`
Timezone string `json:"timezone"`
Isp string `json:"isp"`
Offset int `json:"offset"`
Region string `json:"region"`
Asn int `json:"asn"`
AsnOrganization string `json:"asn_organization"`
Country string `json:"country"`
Ip string `json:"ip"`
Latitude float64 `json:"latitude"`
ContinentCode string `json:"continent_code"`
CountryCode string `json:"country_code"`
RegionCode string `json:"region_code"`
}
func (geo *IPGeo) String() string {
var ls []string
if geo.Country != "" {
ls = append(ls, geo.Country+" "+emojiflag.GetFlag(geo.CountryCode))
}
if geo.Region != "" {
ls = append(ls, geo.Region)
}
if geo.City != "" {
ls = append(ls, geo.City)
}
if geo.Organization != "" {
ls = append(ls, geo.Organization)
}
return strings.Join(ls, " / ")
}
func GetHostIP() (net.IP, error) {
resp, err := httpGet("https://api.ip.sb/ip")
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
ipStr := strings.TrimSpace(string(body))
ip := net.ParseIP(ipStr)
if ip == nil {
return nil, ErrInvalidIP
}
return ip, nil
}
func GetGeo(ipStr string) (*IPGeo, error) {
ip := net.ParseIP(ipStr)
if ip == nil {
return nil, ErrInvalidIP
}
url := fmt.Sprintf("https://api.ip.sb/geoip/%s", ipStr)
res, err := httpGet(url)
if err != nil {
return nil, err
}
defer res.Body.Close()
geo := new(IPGeo)
err = json.NewDecoder(res.Body).Decode(geo)
if err != nil {
return nil, err
}
return geo, nil
}