-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
182 lines (156 loc) · 3.51 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
package main
import (
"errors"
"flag"
"io/ioutil"
"log"
"net"
"os"
"os/user"
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/mazlum/cdnstrip/cdn"
"github.com/briandowns/spinner"
)
func init() {
runtime.GOMAXPROCS(runtime.NumCPU()) // Run faster !
}
// Initialize global variables
var cdnRanges []*net.IPNet
var mutex sync.Mutex
var wg sync.WaitGroup
var validIP int
var invalidIP int
var cdnIP int
var s *spinner.Spinner = spinner.New(spinner.CharSets[11], 100*time.Millisecond)
func main() {
cacheFilePath := getCacheFilePath()
thread := flag.Int("t", 1, "Number of threads")
input := flag.String("i", "", "Input [FileName|IP|CIDR]")
out := flag.String("o", "filtered.txt", "Output file name")
skipCache := flag.Bool("skip-cache", false, "Skip loading cache file for CDN IP ranges")
flag.Parse()
if *input == "" {
flag.PrintDefaults()
os.Exit(1)
}
// Start spinner
print("\n")
s.Writer = os.Stdout
s.Start()
// First check if cache exists
s.Suffix = " Loading for cache file..."
cahceFile, err := ioutil.ReadFile(cacheFilePath)
if err == nil || *skipCache {
// read cache file
c := strings.Split(string(cahceFile), "\n")
if len(c) == 0 {
fatal(errors.New("empty cache file"))
}
for _, i := range c {
_, cidr, err := net.ParseCIDR(i)
if err == nil {
// append range
cdnRanges = append(cdnRanges, cidr)
}
}
} else {
// Create new cache file
s.Suffix = " Loading all CDN ranges..."
ranges, err := cdn.LoadAll()
fatal(err)
cdnRanges = ranges
s.Suffix = " Creating new cache file..."
cahceFile, err := os.OpenFile(cacheFilePath, os.O_APPEND|os.O_RDWR|os.O_CREATE, 0664)
fatal(err)
for i, r := range cdnRanges {
cahceFile.WriteString(r.String())
if i != len(cdnRanges)-1 {
cahceFile.WriteString("\n")
}
}
cahceFile.Close()
}
outFile, err := os.Create(*out)
fatal(err)
defer outFile.Close()
list := loadInput(*input)
channel := make(chan string, len(list))
for _, ip := range list {
channel <- ip
}
close(channel)
for i := 0; i < *thread; i++ {
wg.Add(1)
go strip(channel, outFile)
}
wg.Wait()
s.Stop()
print("[✔]" + s.Suffix + "\n")
}
func strip(channel chan string, file *os.File) {
defer wg.Done()
for ip := range channel {
i := net.ParseIP(ip)
if i != nil {
if cdn.Check(cdnRanges, i) {
mutex.Lock()
cdnIP++
mutex.Unlock()
} else {
mutex.Lock()
validIP++
file.WriteString(i.String() + "\n")
mutex.Unlock()
}
} else {
mutex.Lock()
invalidIP++
mutex.Unlock()
}
// Update spinner
updateSpinnerStats()
}
}
func updateSpinnerStats() {
mutex.Lock()
s.Suffix = " [ VALID: " + strconv.Itoa(validIP) + " | INVALID: " + strconv.Itoa(invalidIP) + " | CDN: " + strconv.Itoa(cdnIP) + " ]"
mutex.Unlock()
}
func getCacheFilePath() string {
usr, err := user.Current()
if err != nil {
fatal(err)
}
return usr.HomeDir + "/.config/cdnstrip.cache"
}
func loadInput(param string) []string {
s.Suffix = " Loading input..."
ip := net.ParseIP(param)
if ip != nil {
return []string{ip.String()}
}
ips, err := cdn.ExpandCIDR(param)
if err == nil {
return ips
}
file, err := ioutil.ReadFile(param)
fatal(err)
return strings.Split(string(file), "\n")
}
func fatal(err error) {
if err != nil {
s.Stop()
pc, _, _, ok := runtime.Caller(1)
details := runtime.FuncForPC(pc)
if ok && details != nil {
log.Printf("[%s] ERROR: %s\n", strings.ToUpper(strings.Split(details.Name(), ".")[1]), err)
} else {
log.Printf("[UNKOWN] ERROR: %s\n", err)
}
os.Exit(1)
}
}