-
Notifications
You must be signed in to change notification settings - Fork 0
/
robtex.go
191 lines (154 loc) · 4.57 KB
/
robtex.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Package robtex a simple Robtex API client.
// https://www.robtex.com/api/
package robtex
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"
)
// API endpoints.
const (
FreeAPIBaseURL = "https://freeapi.robtex.com"
ProAPIBaseURL = "https://proapi.robtex.com"
)
// Client a Robtex API client.
type Client struct {
httpClient *http.Client
baseURL *url.URL
apiKey string
}
// New created a new Client.
func New(apiKey string) *Client {
baseURL, _ := url.Parse(FreeAPIBaseURL)
if apiKey != "" {
baseURL, _ = url.Parse(ProAPIBaseURL)
}
return &Client{
httpClient: &http.Client{Timeout: 5 * time.Second},
baseURL: baseURL,
apiKey: apiKey,
}
}
// IPQuery This API returns the current forward and reverse of an IP number, together with GEO-location data and network data.
// The format returned is JSON.
// Most keys are self-explanatory.
// ex: https://freeapi.robtex.com/ipquery/199.19.54.1
func (c Client) IPQuery(ctx context.Context, ip string) (*IPQueryResponse, error) {
endpoint := c.baseURL.JoinPath("ipquery", ip)
req, err := c.newRequest(ctx, endpoint)
if err != nil {
return nil, err
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
data, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("%d: %s", resp.StatusCode, string(data))
}
var r IPQueryResponse
err = json.NewDecoder(resp.Body).Decode(&r)
if err != nil {
return nil, err
}
return &r, nil
}
// ASQuery Returns an array of networks related to a specific AS number.
// Currently, only returns networks actually in global bgp table, but plans are to extend it.
// ex: https://freeapi.robtex.com/asquery/1234
func (c Client) ASQuery(ctx context.Context, number string) (*ASQueryResponse, error) {
endpoint := c.baseURL.JoinPath("asquery", number)
req, err := c.newRequest(ctx, endpoint)
if err != nil {
return nil, err
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
data, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("%d: %s", resp.StatusCode, string(data))
}
var r ASQueryResponse
err = json.NewDecoder(resp.Body).Decode(&r)
if err != nil {
return nil, err
}
return &r, nil
}
// PassiveDNSForward This API returns ldjson format, that is one JSON object per line.
// The format used is Passive DNS - Common Output Format (https://tools.ietf.org/html/draft-dulaunoy-dnsop-passive-dns-cof-03).
// ex: https://freeapi.robtex.com/pdns/forward/a.iana-servers.net
func (c Client) PassiveDNSForward(ctx context.Context, domain string) ([]PassiveDNS, error) {
endpoint := c.baseURL.JoinPath("pdns", "forward", domain)
req, err := c.newRequest(ctx, endpoint)
if err != nil {
return nil, err
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
data, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("%d: %s", resp.StatusCode, string(data))
}
decoder := json.NewDecoder(resp.Body)
var results []PassiveDNS
for decoder.More() {
var r PassiveDNS
err = decoder.Decode(&r)
if err != nil {
return nil, err
}
results = append(results, r)
}
return results, nil
}
// PassiveDNSReverse This API returns ldjson format, that is one JSON object per line.
// The format used is Passive DNS - Common Output Format (https://tools.ietf.org/html/draft-dulaunoy-dnsop-passive-dns-cof-03).
// ex: https://freeapi.robtex.com/pdns/reverse/199.43.132.53
func (c Client) PassiveDNSReverse(ctx context.Context, ip string) ([]PassiveDNS, error) {
endpoint := c.baseURL.JoinPath("pdns", "reverse", ip)
req, err := c.newRequest(ctx, endpoint)
if err != nil {
return nil, err
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
data, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("%d: %s", resp.StatusCode, string(data))
}
decoder := json.NewDecoder(resp.Body)
var results []PassiveDNS
for decoder.More() {
var r PassiveDNS
err = decoder.Decode(&r)
if err != nil {
return nil, err
}
results = append(results, r)
}
return results, nil
}
func (c Client) newRequest(ctx context.Context, endpoint *url.URL) (*http.Request, error) {
if c.apiKey != "" {
query := endpoint.Query()
query.Set("key", c.apiKey)
endpoint.RawQuery = query.Encode()
}
return http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), http.NoBody)
}