-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathmain.go
526 lines (449 loc) · 13.2 KB
/
main.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
package main
import (
"bufio"
"bytes"
"crypto/tls"
"flag"
"fmt"
"github.com/ilyakaznacheev/cleanenv"
"github.com/projectdiscovery/goflags"
"github.com/projectdiscovery/gologger"
"io"
"net"
"net/http"
"os"
"regexp"
"strings"
"sync"
"time"
)
type fetcher func(url string) []*net.IPNet
type CDN struct {
url string
sender fetcher
}
type Config struct {
SendRequest []string `yaml:"SendRequest"`
ReadFileUrl []string `yaml:"ReadFileUrl"`
}
var (
isSilent, showVersion, activeMode, updateAll, updateRanges bool
input, output, savePath string
homeDIR, _ = os.UserHomeDir()
config Config
thread int
CDNS []CDN
wg sync.WaitGroup
)
const (
colorReset = "\033[0m"
colorRed = "\033[31m"
colorGreen = "\033[32m"
colorYellow = "\033[33m"
colorBlue = "\033[34m"
VERSION = "1.0.31"
)
func main() {
var allRange []*net.IPNet
flagSet := goflags.NewFlagSet()
flagSet.SetDescription("Removing CDN IPs from the list of IP addresses")
createGroup(flagSet, "input", "Input",
flagSet.StringVarP(&input, "ip", "i", "", "Input [Filename | IP]"),
)
createGroup(flagSet, "rate-limit", "Rate-Limit",
flagSet.IntVarP(&thread, "thread", "t", 1, "Number Of Thread [Number]"),
)
flagSet.CreateGroup("configs", "Configurations",
flagSet.BoolVarP(&activeMode, "active", "a", false, "Active mode for check akamai"),
flagSet.BoolVarP(&updateAll, "update-all", "ua", false, "Update CUT-CDN Data (providers & ranges)"),
flagSet.BoolVarP(&updateRanges, "update-ranges", "ur", false, "Update CUT-CDN Data (just ranges)"),
)
createGroup(flagSet, "output", "Output",
flagSet.StringVarP(&output, "output", "o", "CLI", "File to write output to (optional)"),
)
createGroup(flagSet, "debug", "Debug",
flagSet.BoolVarP(&isSilent, "silent", "q", false, "Show only IPs in output"),
flagSet.BoolVarP(&showVersion, "version", "v", false, "Show version of cut-cdn"),
)
_ = flagSet.Parse()
baseConfig(updateAll, updateRanges)
fi, err := os.Stdin.Stat()
checkError(err)
if showVersion {
printText(false, "Current Version: v"+VERSION, "Info")
os.Exit(0)
}
if input == "" && fi.Mode()&os.ModeNamedPipe == 0 && savePath == "" {
printText(isSilent, "Input is empty!\n\n", "Error")
flag.PrintDefaults()
os.Exit(1)
}
checkUpdate(isSilent)
printText(isSilent, "Loading All CDN Range", "Info")
allRange = loadAllCDN()
printText(isSilent, "All CDN Range Loaded", "Info")
if input == "" && fi.Mode()&os.ModeNamedPipe == 0 {
os.Exit(0)
}
if output != "CLI" {
_, err := os.Create(output)
checkError(err)
}
var allIpInput []string
if fi.Mode()&os.ModeNamedPipe != 0 {
allIpInput = readInput(isSilent, "STDIN")
} else {
allIpInput = readInput(isSilent, input)
}
channel := make(chan string, len(allIpInput))
for _, ip := range allIpInput {
channel <- ip
}
close(channel)
printText(isSilent, "Start Checking IPs", "Info")
if output == "CLI" {
printText(isSilent, "", "Print")
printText(isSilent, colorGreen+"[⚡] All IPs Not Behind CDN ⤵"+colorReset, "Print")
}
for i := 0; i < thread; i++ {
wg.Add(1)
go checkAndWrite(allRange, channel, output)
}
wg.Wait()
}
func loadAllCDN() []*net.IPNet {
var allRanges []*net.IPNet
data, err := os.ReadFile(homeDIR + "/.config/cut-cdn/ranges.txt")
checkError(err)
for _, cidr := range strings.Split(string(data), "\n") {
if cidr != "" {
_, cidr, _ := net.ParseCIDR(string(cidr))
allRanges = append(allRanges, cidr)
}
}
return allRanges
}
func loadAllCDNOnline() []*net.IPNet {
var (
allRanges []*net.IPNet
wg sync.WaitGroup
)
cleanenv.ReadConfig(homeDIR+"/.config/cut-cdn/providers.yaml", &config)
sendReqs := config.SendRequest
readFiles := config.ReadFileUrl
for _, v := range sendReqs {
if v != "" {
CDNS = append(CDNS, CDN{v, sendRequest})
}
}
for _, v := range readFiles {
if v != "" {
CDNS = append(CDNS, CDN{v, readFileUrl})
}
}
cidrChan := make(chan []*net.IPNet, len(CDNS)+1)
wg.Add(len(CDNS))
for _, cdn := range CDNS {
cdn := cdn
go func() {
defer wg.Done()
cidr := cdn.sender(cdn.url)
cidrChan <- cidr
}()
}
wg.Add(1)
go func() {
defer wg.Done()
incapsulaIPUrl := "https://my.incapsula.com/api/integration/v1/ips"
client := &http.Client{
Timeout: 30 * time.Second,
}
resp, err := client.Post(incapsulaIPUrl, "application/x-www-form-urlencoded", bytes.NewBuffer([]byte("resp_format=text")))
if !checkError(err) {
body, err := io.ReadAll(resp.Body)
checkError(err)
cidr := regexIp(string(body))
cidrChan <- cidr
}
}()
wg.Wait()
close(cidrChan)
for cidr := range cidrChan {
allRanges = append(allRanges, cidr...)
}
return allRanges
}
func sendRequest(url string) []*net.IPNet {
req, err := http.NewRequest("GET", url, nil)
checkError(err)
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:103.0) Gecko/20100101 Firefox/103.0")
client := &http.Client{
Timeout: 30 * time.Second,
}
resp, err := client.Do(req)
if checkError(err) {
return []*net.IPNet{}
}
defer func(Body io.ReadCloser) {
err := Body.Close()
checkError(err)
}(resp.Body)
body, err := io.ReadAll(resp.Body)
checkError(err)
return regexIp(string(body))
}
func readFileUrl(url string) []*net.IPNet {
client := http.Client{
CheckRedirect: func(r *http.Request, via []*http.Request) error {
r.URL.Opaque = r.URL.Path
return nil
},
Timeout: 60 * time.Second,
}
// Put content on file
resp, err := client.Get(url)
if checkError(err) {
return []*net.IPNet{}
}
defer func(Body io.ReadCloser) {
err := Body.Close()
checkError(err)
}(resp.Body)
data, err := io.ReadAll(resp.Body)
checkError(err)
return regexIp(string(data))
}
func regexIp(body string) []*net.IPNet {
body = strings.Replace(body, "\\/", "/", -1)
re, e := regexp.Compile(`(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/(3[0-2]|[1-2][0-9]|[0-9]))`)
checkError(e)
var ranges []*net.IPNet
for _, v := range re.FindAll([]byte(body), -1) {
_, cidr, err := net.ParseCIDR(string(v))
checkError(err)
ranges = append(ranges, cidr)
}
return ranges
}
func checkAndWrite(allCidr []*net.IPNet, channel chan string, output string) {
var isIpForCDN bool
defer wg.Done()
for ip := range channel {
isIpForCDN = false
for _, cidr := range allCidr {
if cidr.Contains(net.ParseIP(ip)) {
isIpForCDN = true
}
}
if activeMode && !isIpForCDN {
ptrRecords := getPtrRecord(string(ip))
for _, v := range ptrRecords {
if strings.Contains(v, "akamaitechnologies.com") {
isIpForCDN = true
}
}
}
if activeMode && !isIpForCDN {
httpServerHeader := getHttpHeader("http://" + string(ip))
if httpServerHeader == "AkamaiGHost" || httpServerHeader == "CloudFront" || httpServerHeader == "cloudflare" {
isIpForCDN = true
}
}
if !isIpForCDN {
if output == "CLI" {
fmt.Println(ip)
} else {
file, err := os.OpenFile(output, os.O_APPEND|os.O_WRONLY, 0666)
checkError(err)
_, err = fmt.Fprintln(file, ip)
checkError(err)
err = file.Close()
checkError(err)
}
}
}
}
func readInput(isSilent bool, input string) []string {
printText(isSilent, "Input Parsing", "Info")
defer printText(isSilent, "Input Parsed", "Info")
input = strings.TrimSpace(input)
if _, err := os.Stat(input); err == nil {
fileByte, err := os.ReadFile(input)
checkError(err)
var result []string
fileData := strings.Split(string(fileByte), "\n")
for _, v := range fileData {
v = strings.TrimSpace(v)
if v == "" {
continue
}
if isValidIP(v) {
result = append(result, v)
} else {
v = strings.TrimPrefix(v, "https://")
v = strings.TrimPrefix(v, "http://")
domainRegex := regexp.MustCompile(`^[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z]{2,})+$`)
if domainRegex.MatchString(v) {
ips, err := net.LookupIP(v)
if err == nil {
result = append(result, convertIPListToStringList(ips)...)
}
}
}
}
return result
}
if input == "STDIN" {
var result []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
ip := strings.TrimSpace(scanner.Text())
if ip == "" {
continue
}
if isValidIP(ip) {
result = append(result, ip)
} else {
ip = strings.TrimPrefix(ip, "https://")
ip = strings.TrimPrefix(ip, "http://")
domainRegex := regexp.MustCompile(`^[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z]{2,})+$`)
if domainRegex.MatchString(ip) {
ips, err := net.LookupIP(ip)
if err == nil {
result = append(result, convertIPListToStringList(ips)...)
}
}
}
}
return result
}
if isValidIP(input) {
return []string{input}
}
input = strings.TrimPrefix(input, "https://")
input = strings.TrimPrefix(input, "http://")
domainRegex := regexp.MustCompile(`^[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z]{2,})+$`)
if domainRegex.MatchString(input) {
ips, err := net.LookupIP(input)
if err == nil {
return convertIPListToStringList(ips)
}
}
return nil
}
func checkUpdate(isSilent bool) {
// Check Updates
resp, err := http.Get("https://github.com/ImAyrix/cut-cdn")
checkError(err)
respByte, err := io.ReadAll(resp.Body)
checkError(err)
body := string(respByte)
re, e := regexp.Compile(`cut-cdn\s+v(\d\.\d\.\d+)`)
checkError(e)
if re.FindStringSubmatch(body)[1] != VERSION {
printText(isSilent, "", "Print")
printText(isSilent, "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -", "Print")
printText(isSilent, fmt.Sprintf("| %v🔥 Please update Cut-CDN!%v |", colorGreen, colorReset), "Print")
printText(isSilent, "| 💣 Run: go install github.com/ImAyrix/cut-cdn@latest |", "Print")
printText(isSilent, "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -", "Print")
printText(isSilent, "", "Print")
}
}
func checkError(e error) bool {
if e != nil {
gologger.Error().Msg(e.Error())
return true
}
return false
}
func printText(isSilent bool, text string, textType string) {
if !isSilent {
switch textType {
case "Info":
gologger.Info().Msg(text)
case "Print":
gologger.Print().Msg(text)
case "Error":
gologger.Error().Msg(text)
}
}
}
func createGroup(flagSet *goflags.FlagSet, groupName, description string, flags ...*goflags.FlagData) {
flagSet.SetGroup(groupName, description)
for _, currentFlag := range flags {
currentFlag.Group(groupName)
}
}
func getPtrRecord(ip string) []string {
ptr, _ := net.LookupAddr(ip)
return ptr
}
func getHttpHeader(url string) string {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:103.0) Gecko/20100101 Firefox/103.0")
resp, err := http.Get(url)
if err == nil {
return resp.Header.Get("Server")
}
return ""
}
func convertIPListToStringList(ips []net.IP) []string {
var result []string
for _, ip := range ips {
if !IsIPv6Valid(ip.String()) {
result = append(result, ip.String())
}
}
return result
}
func IsIPv6Valid(ip string) bool {
parsedIP := net.ParseIP(ip)
return (parsedIP != nil) && (parsedIP.To16() != nil) && (parsedIP.To4() == nil)
}
func isValidIP(ip string) bool {
validatedIp := net.ParseIP(ip)
return validatedIp != nil
}
func baseConfig(updateAll bool, updateRanges bool) {
func() {
if _, err := os.Stat(homeDIR + "/.config"); os.IsNotExist(err) {
_ = os.Mkdir(homeDIR+"/.config", os.ModePerm)
}
if _, err := os.Stat(homeDIR + "/.config/cut-cdn"); os.IsNotExist(err) {
printText(isSilent, "Create Cut-CDN DIR", "Info")
_ = os.Mkdir(homeDIR+"/.config/cut-cdn", os.ModePerm)
}
}()
if updateAll {
_ = os.Remove(homeDIR + "/.config/cut-cdn/providers.yaml")
_ = os.Remove(homeDIR + "/.config/cut-cdn/ranges.txt")
} else if updateRanges {
_ = os.Remove(homeDIR + "/.config/cut-cdn/ranges.txt")
}
func() {
if _, err := os.Stat(homeDIR + "/.config/cut-cdn/providers.yaml"); os.IsNotExist(err) {
printText(isSilent, "Create Cut-CDN Providers File", "Info")
_, _ = os.Create(homeDIR + "/.config/cut-cdn/providers.yaml")
req, _ := http.NewRequest("GET", "https://raw.githubusercontent.com/ImAyrix/cut-cdn/master/static/providers.yaml", nil)
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:103.0) Gecko/20100101 Firefox/103.0")
resp, _ := http.Get("https://raw.githubusercontent.com/ImAyrix/cut-cdn/master/static/providers.yaml")
body, _ := io.ReadAll(resp.Body)
_ = os.WriteFile(homeDIR+"/.config/cut-cdn/providers.yaml", body, 0644)
}
}()
func() {
if _, err := os.Stat(homeDIR + "/.config/cut-cdn/ranges.txt"); os.IsNotExist(err) {
printText(isSilent, "Create CDN CIDRs File", "Info")
file, _ := os.Create(homeDIR + "/.config/cut-cdn/ranges.txt")
allRanges := loadAllCDNOnline()
data := ""
for _, cidr := range allRanges {
if !strings.Contains(data, cidr.String()) {
data += cidr.String() + "\n"
}
}
_, _ = file.WriteString(data)
}
}()
}