forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintelligence_ip.go
156 lines (133 loc) · 4.84 KB
/
intelligence_ip.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
package cloudflare
import (
"context"
"fmt"
"net/http"
"github.com/goccy/go-json"
)
// IPIntelligence represents IP intelligence information.
type IPIntelligence struct {
IP string `json:"ip"`
BelongsToRef BelongsToRef `json:"belongs_to_ref"`
RiskTypes []RiskTypes `json:"risk_types"`
}
// BelongsToRef represents information about who owns an IP address.
type BelongsToRef struct {
ID string `json:"id"`
Value int `json:"value"`
Type string `json:"type"`
Country string `json:"country"`
Description string `json:"description"`
}
// RiskTypes represent risk types for an IP.
type RiskTypes struct {
ID int `json:"id"`
SuperCategoryID int `json:"super_category_id"`
Name string `json:"name"`
}
// IPPassiveDNS represent DNS response.
type IPPassiveDNS struct {
ReverseRecords []ReverseRecords `json:"reverse_records,omitempty"`
Count int `json:"count,omitempty"`
Page int `json:"page,omitempty"`
PerPage int `json:"per_page,omitempty"`
}
// ReverseRecords represent records for passive DNS.
type ReverseRecords struct {
FirstSeen string `json:"first_seen,omitempty"`
LastSeen string `json:"last_seen,omitempty"`
Hostname string `json:"hostname,omitempty"`
}
// IPIntelligenceParameters represents parameters for an IP Intelligence request.
type IPIntelligenceParameters struct {
AccountID string `url:"-"`
IPv4 string `url:"ipv4,omitempty"`
IPv6 string `url:"ipv6,omitempty"`
}
// IPIntelligenceResponse represents an IP Intelligence API response.
type IPIntelligenceResponse struct {
Response
Result []IPIntelligence `json:"result,omitempty"`
}
// IPIntelligenceListParameters represents the parameters for an IP list request.
type IPIntelligenceListParameters struct {
AccountID string
}
// IPIntelligenceItem represents an item in an IP list.
type IPIntelligenceItem struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}
// IPIntelligenceListResponse represents the response for an IP list API response.
type IPIntelligenceListResponse struct {
Response
Result []IPIntelligenceItem `json:"result,omitempty"`
}
// IPIntelligencePassiveDNSParameters represents the parameters for a passive DNS request.
type IPIntelligencePassiveDNSParameters struct {
AccountID string `url:"-"`
IPv4 string `url:"ipv4,omitempty"`
Start string `url:"start,omitempty"`
End string `url:"end,omitempty"`
Page int `url:"page,omitempty"`
PerPage int `url:"per_page,omitempty"`
}
// IPIntelligencePassiveDNSResponse represents a passive API response.
type IPIntelligencePassiveDNSResponse struct {
Response
Result IPPassiveDNS `json:"result,omitempty"`
}
// IntelligenceGetIPOverview gets information about ipv4 or ipv6 address.
//
// API Reference: https://api.cloudflare.com/#ip-intelligence-get-ip-overview
func (api *API) IntelligenceGetIPOverview(ctx context.Context, params IPIntelligenceParameters) ([]IPIntelligence, error) {
if params.AccountID == "" {
return []IPIntelligence{}, ErrMissingAccountID
}
uri := buildURI(fmt.Sprintf("/accounts/%s/intel/ip", params.AccountID), params)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []IPIntelligence{}, err
}
var ipDetails IPIntelligenceResponse
if err := json.Unmarshal(res, &ipDetails); err != nil {
return []IPIntelligence{}, err
}
return ipDetails.Result, nil
}
// IntelligenceGetIPList gets intelligence ip-lists.
//
// API Reference: https://api.cloudflare.com/#ip-list-get-ip-lists
func (api *API) IntelligenceGetIPList(ctx context.Context, params IPIntelligenceListParameters) ([]IPIntelligenceItem, error) {
if params.AccountID == "" {
return []IPIntelligenceItem{}, ErrMissingAccountID
}
uri := fmt.Sprintf("/accounts/%s/intel/ip-list", params.AccountID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []IPIntelligenceItem{}, err
}
var ipListItem IPIntelligenceListResponse
if err := json.Unmarshal(res, &ipListItem); err != nil {
return []IPIntelligenceItem{}, err
}
return ipListItem.Result, nil
}
// IntelligencePassiveDNS gets a history of DNS for an ip.
//
// API Reference: https://api.cloudflare.com/#passive-dns-by-ip-get-passive-dns-by-ip
func (api *API) IntelligencePassiveDNS(ctx context.Context, params IPIntelligencePassiveDNSParameters) (IPPassiveDNS, error) {
if params.AccountID == "" {
return IPPassiveDNS{}, ErrMissingAccountID
}
uri := buildURI(fmt.Sprintf("/accounts/%s/intel/dns", params.AccountID), params)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return IPPassiveDNS{}, err
}
var passiveDNS IPIntelligencePassiveDNSResponse
if err := json.Unmarshal(res, &passiveDNS); err != nil {
return IPPassiveDNS{}, err
}
return passiveDNS.Result, nil
}