-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip2provider.go
480 lines (406 loc) · 11.2 KB
/
ip2provider.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"github.com/fatih/color"
"strings"
//"github.com/BenjiTrapp/ip-to-cloudprovider/microsoft"
"github.com/spf13/cobra"
)
const banner = `
____ ______ _______ _____ _ __
/ _/__/_ __/__ / ___/ / ___ __ _____/ / _ \_______ _ __(_)__/ /__ ____
_/ // _ \/ / / _ \/ /__/ /__/ _ \/ // / _ / ___/ __/ _ \ |/ / / _ / -_) __/
/___/ .__/_/ \___/\___/____/\___/\_,_/\_,_/_/ /_/ \___/___/_/\_,_/\__/_/
/_/
`
type IPRange struct {
IPv4 []string `json:"ipv4"`
IPv6 []string `json:"ipv6"`
}
var debug bool
var updateAll bool
var providers = []struct {
name string
url string
parser func(data []byte) *IPRange
}{
{"amazon", "https://ip-ranges.amazonaws.com/ip-ranges.json", parseAmazon},
{"cloudflare", "https://api.cloudflare.com/client/v4/ips", parseCloudflare},
{"github", "https://api.github.com/meta", parseGitHub},
{"githubactions", "https://api.github.com/meta", parseGitHubActions},
{"githubhooks", "https://api.github.com/meta", parseGitHubHooks},
{"githubpages", "https://api.github.com/meta", parseGitHubPages},
{"googlecloud", "https://www.gstatic.com/ipranges/cloud.json", parseGoogleJson},
{"googlebot", "https://developers.google.com/search/apis/ipranges/googlebot.json", parseGoogleJson},
{"google", "https://www.gstatic.com/ipranges/goog.txt", parseGoogle},
{"openai", "https://openai.com/gptbot-ranges.txt", parseOpenAI},
{"microsoft", "NONE", nil},
}
func main() {
fmt.Print(banner)
fmt.Println("-------------------------------------------------------")
var rootCmd = &cobra.Command{
Use: "ipranges",
Short: "Manage IP ranges for various providers",
Run: func(cmd *cobra.Command, args []string) {
if updateAll {
for _, provider := range providers {
if provider.name != "microsoft" {
updateIPRanges(provider.name, provider.url)
}
}
//microsoft.Download()
} else {
cmd.Help()
}
},
}
rootCmd.PersistentFlags().BoolVarP(&updateAll, "update-all", "a", false, "Update IP ranges for all providers")
var checkIPCmd = &cobra.Command{
Use: "check-ip [ip]",
Short: "Check if an IP belongs to any provider's range",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
ip := args[0]
checkIP(ip)
},
}
checkIPCmd.Flags().BoolVarP(new(bool), "update", "c", false, "Check if an IP belongs to any provider's range")
rootCmd.AddCommand(checkIPCmd)
for _, provider := range providers {
provider := provider
providerCmd := &cobra.Command{
Use: provider.name,
Short: fmt.Sprintf("Manage %s IP ranges", provider.name),
Run: func(cmd *cobra.Command, args []string) {
update, _ := cmd.Flags().GetBool("update")
check, _ := cmd.Flags().GetBool("check")
if update {
updateIPRanges(provider.name, provider.url)
} else if check {
ip := args[0]
checkIP(ip)
} else {
cmd.Help()
}
},
}
providerCmd.Flags().BoolVarP(&updateAll, "update-all", "a", false, "Update IP ranges for all providers")
rootCmd.AddCommand(providerCmd)
}
var checkFileCmd = &cobra.Command{
Use: "check-file",
Short: "Check if IPs from a file belong to any provider's range",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
filePath := args[0]
checkIPsFromFile(filePath)
},
}
checkFileCmd.Flags().StringP("file", "f", "", "Path to the file containing IP addresses")
rootCmd.AddCommand(checkFileCmd)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func updateIPRanges(providerName, url string) {
resp, err := http.Get(url)
if err != nil {
fmt.Printf("Error fetching data for %s: %s\n", providerName, err)
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Error reading data for %s: %s\n", providerName, err)
return
}
ipRange := parseProviderData(providerName, body)
if ipRange == nil {
fmt.Printf("Error parsing data for %s\n", providerName)
return
}
saveIPRanges(providerName, ipRange)
fmt.Printf("%-20s IP ranges updated successfully\n", colorizeProviderName(capitalizeFirst(providerName)))
}
func capitalizeFirst(s string) string {
if len(s) == 0 {
return s
}
return strings.ToUpper(s[:1]) + s[1:]
}
func colorizeProviderName(providerName string) string {
var c *color.Color
switch providerName {
case "Microsoft":
c = color.New(color.FgBlue).Add(color.Bold)
case "Github":
c = color.New(color.FgBlack).Add(color.BgWhite).Add(color.Bold)
case "Amazon":
c = color.New(color.FgYellow).Add(color.Bold)
case "Cloudflare":
c = color.New(color.FgHiRed).Add(color.BgYellow).Add(color.Bold)
case "Google":
c = color.New(color.FgRed).Add(color.Bold)
case "Googlecloud":
c = color.New(color.FgRed).Add(color.Bold)
case "Googlebot":
c = color.New(color.FgRed).Add(color.Bold)
case "Openai":
c = color.New(color.FgCyan).Add(color.Bold)
default:
c = color.New(color.FgWhite)
}
return c.Sprint(providerName)
}
func checkIP(ip string) {
for _, provider := range providers {
ipRanges := loadIPRanges(provider.name)
if ipRanges != nil && (isIPInRange(ip, ipRanges.IPv4) || isIPInRange(ip, ipRanges.IPv6)) {
fmt.Printf("%-20s is in the range of %s\n", ip, colorizeProviderName(capitalizeFirst(provider.name)))
return
}
}
fmt.Printf("%-15s is not in the range of any provider\n", ip)
}
func checkIPsFromFile(filePath string) {
file, err := os.Open(filePath)
if err != nil {
fmt.Printf("Error opening file: %s\n", err)
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
ip := scanner.Text()
checkIP(ip)
}
if err := scanner.Err(); err != nil {
fmt.Printf("Error reading file: %s\n", err)
}
}
func isIPInRange(ip string, ranges []string) bool {
parsedIP := net.ParseIP(ip)
for _, cidr := range ranges {
_, ipNet, err := net.ParseCIDR(cidr)
if err != nil {
if debug {
fmt.Printf("Error parsing CIDR %s: %s\n", cidr, err)
}
continue
}
if ipNet.Contains(parsedIP) {
return true
}
}
return false
}
func saveIPRanges(providerName string, ipRange *IPRange) {
fileName := fmt.Sprintf("%s/ipranges.json", providerName)
data, err := json.Marshal(ipRange)
if err != nil {
fmt.Printf("Error marshalling data for %s: %s\n", providerName, err)
return
}
err = os.WriteFile(fileName, data, 0644)
if err != nil {
fmt.Printf("Error writing data to %s: %s\n", fileName, err)
}
}
func loadIPRanges(providerName string) *IPRange {
fileName := fmt.Sprintf("%s/ipranges.json", providerName)
data, err := os.ReadFile(fileName)
if err != nil {
fmt.Printf("Error reading data from %s: %s\n", fileName, err)
return nil
}
var ipRange IPRange
err = json.Unmarshal(data, &ipRange)
if err != nil {
fmt.Printf("Error unmarshalling data for %s: %s\n", providerName, err)
return nil
}
return &ipRange
}
func parseProviderData(providerName string, data []byte) *IPRange {
var parser func(data []byte) *IPRange
switch providerName {
case "amazon":
parser = parseAmazon
case "cloudflare":
parser = parseCloudflare
case "github":
parser = parseGitHub
case "githubactions":
parser = parseGitHubActions
case "githubhooks":
parser = parseGitHubHooks
case "githubpages":
parser = parseGitHubPages
case "google":
parser = parseGoogle
case "googlecloud":
parser = parseGoogleJson
case "googlebot":
parser = parseGoogleJson
case "openai":
parser = parseOpenAI
case "microsoft":
return nil
default:
fmt.Printf("Unknown provider: %s\n", providerName)
return nil
}
return parser(data)
}
func parseAmazon(data []byte) *IPRange {
var result struct {
Prefixes []struct {
IP string `json:"ip_prefix"`
} `json:"prefixes"`
IPv6Prefixes []struct {
IP string `json:"ipv6_prefix"`
} `json:"ipv6_prefixes"`
}
if err := json.Unmarshal(data, &result); err != nil {
fmt.Printf("Error unmarshalling Amazon data: %s\n", err)
return nil
}
ipRange := &IPRange{}
for _, prefix := range result.Prefixes {
ipRange.IPv4 = append(ipRange.IPv4, prefix.IP)
}
for _, prefix := range result.IPv6Prefixes {
ipRange.IPv6 = append(ipRange.IPv6, prefix.IP)
}
return ipRange
}
func parseGitHub(data []byte) *IPRange {
var result struct {
Web []string `json:"web"`
}
if err := json.Unmarshal(data, &result); err != nil {
fmt.Printf("Error unmarshalling GitHub data: %s\n", err)
return nil
}
ipRange := &IPRange{IPv4: result.Web}
return ipRange
}
func parseGitHubActions(data []byte) *IPRange {
var result struct {
Actions []string `json:"actions"`
}
if err := json.Unmarshal(data, &result); err != nil {
fmt.Printf("Error unmarshalling GitHub data: %s\n", err)
return nil
}
ipRange := &IPRange{IPv4: result.Actions}
return ipRange
}
func parseGitHubHooks(data []byte) *IPRange {
var result struct {
Hooks []string `json:"hooks"`
}
if err := json.Unmarshal(data, &result); err != nil {
fmt.Printf("Error unmarshalling GitHub data: %s\n", err)
return nil
}
ipRange := &IPRange{IPv4: result.Hooks}
return ipRange
}
func parseGitHubPages(data []byte) *IPRange {
var result struct {
Pages []string `json:"pages"`
}
if err := json.Unmarshal(data, &result); err != nil {
fmt.Printf("Error unmarshalling GitHub data: %s\n", err)
return nil
}
ipRange := &IPRange{IPv4: result.Pages}
return ipRange
}
func parseGoogle(data []byte) *IPRange {
lines := strings.Split(string(data), "\n")
ipRange := &IPRange{}
for _, line := range lines {
if strings.TrimSpace(line) == "" {
continue
}
ip := strings.Fields(line)[0]
if strings.Contains(ip, ":") {
ipRange.IPv6 = append(ipRange.IPv6, ip)
} else {
ipRange.IPv4 = append(ipRange.IPv4, ip)
}
}
return ipRange
}
func parseGoogleJson(data []byte) *IPRange {
var result struct {
Prefixes []struct {
IPv4Prefix string `json:"ipv4Prefix"`
IPv6Prefix string `json:"ipv6Prefix"`
} `json:"prefixes"`
}
if err := json.Unmarshal(data, &result); err != nil {
fmt.Printf("Error unmarshalling Google Cloud data: %s\n", err)
return nil
}
ipRange := &IPRange{}
for _, prefix := range result.Prefixes {
if prefix.IPv4Prefix != "" {
ipRange.IPv4 = append(ipRange.IPv4, prefix.IPv4Prefix)
}
if prefix.IPv6Prefix != "" {
ipRange.IPv6 = append(ipRange.IPv6, prefix.IPv6Prefix)
}
}
return ipRange
}
func parseOpenAI(data []byte) *IPRange {
lines := strings.Split(string(data), "\n")
ipRange := &IPRange{}
for _, line := range lines {
if strings.Contains(line, ":") {
continue
}
ipRange.IPv4 = append(ipRange.IPv4, line)
}
return ipRange
}
func parseCloudflare(data []byte) *IPRange {
var result map[string]interface{}
err := json.Unmarshal(data, &result)
if err != nil {
return nil
}
resultMap, ok := result["result"].(map[string]interface{})
if !ok {
return nil
}
ipRange := &IPRange{
IPv4: toStringArray(resultMap["ipv4_cidrs"]),
IPv6: toStringArray(resultMap["ipv6_cidrs"]),
}
return ipRange
}
func toStringArray(data interface{}) []string {
var result []string
if data != nil {
if dataSlice, ok := data.([]interface{}); ok {
for _, item := range dataSlice {
if str, ok := item.(string); ok {
result = append(result, str)
}
}
}
}
return result
}