-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipinfo.go
More file actions
114 lines (87 loc) · 3.31 KB
/
ipinfo.go
File metadata and controls
114 lines (87 loc) · 3.31 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package ipinfo
// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2026 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
import (
"errors"
"fmt"
"strings"
"github.com/essentialkaos/ek/v13/req"
)
// ////////////////////////////////////////////////////////////////////////////////// //
// Info contains basic information about IP address
type Info struct {
IP string `json:"ip"` // IP address being queried
ASN string `json:"asn"` // Autonomous System Number
ASName string `json:"as_name"` // Organization name
ASDomain string `json:"as_domain"` // Organization's domain name
Country string `json:"country"` // Full country name
CountryCode string `json:"country_code"` // Two-letter country code (ISO 3166-1 alpha-2)
Continent string `json:"continent"` // Full continent name
ContinentCode string `json:"continent_code"` // Two-letter continent code
}
// ////////////////////////////////////////////////////////////////////////////////// //
// apiError is API error
type apiError struct {
Error string `json:"error"`
}
// ////////////////////////////////////////////////////////////////////////////////// //
// API is URL of IPInfo Lite API
var API = "https://api.ipinfo.io/lite"
// ////////////////////////////////////////////////////////////////////////////////// //
// Token is API token
var Token string
// ////////////////////////////////////////////////////////////////////////////////// //
var (
ErrEmptyToken = errors.New("Token is empty")
ErrEmptyIP = errors.New("IP is empty")
)
// ////////////////////////////////////////////////////////////////////////////////// //
// Get gets info about IP from IPInfo Lite API
func Get(ip string) (*Info, error) {
switch {
case Token == "":
return nil, ErrEmptyToken
case ip == "":
return nil, ErrEmptyIP
case !isLooksLikeIP(ip):
return nil, fmt.Errorf("IP %q is not valid", ip)
}
resp, err := req.Request{
URL: API + "/" + ip,
Query: req.Query{"token": Token},
Accept: req.CONTENT_TYPE_JSON,
}.Get()
if err != nil {
return nil, fmt.Errorf("Can't send request to IPinfo API: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != req.STATUS_OK {
errData := &apiError{}
err = resp.JSON(errData)
if err != nil {
return nil, fmt.Errorf("IPinfo API returned non-ok status code (%d)", resp.StatusCode)
}
return nil, fmt.Errorf("IPinfo API returned error: %s", errData.Error)
}
info := &Info{}
err = resp.JSON(info)
if err != nil {
return nil, fmt.Errorf("Can't decode IPinfo API response: %w", err)
}
return info, nil
}
// ////////////////////////////////////////////////////////////////////////////////// //
// isLooksLikeIP is very simple IP validation
func isLooksLikeIP(ip string) bool {
switch {
case strings.Count(ip, ".") != 3:
return false
case strings.Trim(ip, "01234567890.") != "":
return false
}
return true
}