-
Notifications
You must be signed in to change notification settings - Fork 131
/
utils.go
208 lines (202 loc) · 3.9 KB
/
utils.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
package main
import (
"bufio"
"errors"
"fmt"
"io"
"log/slog"
"math"
"math/big"
"net"
"net/netip"
"regexp"
"strings"
)
const (
_ = iota
HostTypeIP
HostTypeCIDR
HostTypeDomain
)
type HostType int
type Host struct {
IP net.IP
Origin string
Type HostType
}
func Iterate(reader io.Reader) <-chan Host {
scanner := bufio.NewScanner(reader)
hostChan := make(chan Host)
go func() {
defer close(hostChan)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
ip := net.ParseIP(line)
if ip != nil && (ip.To4() != nil || enableIPv6) {
// ip address
hostChan <- Host{
IP: ip,
Origin: line,
Type: HostTypeIP,
}
continue
}
_, _, err := net.ParseCIDR(line)
if err == nil {
// ip cidr
p, err := netip.ParsePrefix(line)
if err != nil {
slog.Warn("Invalid cidr", "cidr", line, "err", err)
}
if !p.Addr().Is4() && !enableIPv6 {
continue
}
p = p.Masked()
addr := p.Addr()
for {
if !p.Contains(addr) {
break
}
ip = net.ParseIP(addr.String())
if ip != nil {
hostChan <- Host{
IP: ip,
Origin: line,
Type: HostTypeCIDR,
}
}
addr = addr.Next()
}
continue
}
if ValidateDomainName(line) {
// domain
hostChan <- Host{
IP: nil,
Origin: line,
Type: HostTypeDomain,
}
continue
}
slog.Warn("Not a valid IP, IP CIDR or domain", "line", line)
}
if err := scanner.Err(); err != nil && !errors.Is(err, io.EOF) {
slog.Error("Read file error", "err", err)
}
}()
return hostChan
}
func ValidateDomainName(domain string) bool {
r := regexp.MustCompile(`(?m)^[A-Za-z0-9\-.]+$`)
return r.MatchString(domain)
}
func ExistOnlyOne(arr []string) bool {
exist := false
for _, item := range arr {
if item != "" {
if exist {
return false
} else {
exist = true
}
}
}
return exist
}
func IterateAddr(addr string) <-chan Host {
hostChan := make(chan Host)
_, _, err := net.ParseCIDR(addr)
if err == nil {
// is CIDR
return Iterate(strings.NewReader(addr))
}
ip := net.ParseIP(addr)
if ip == nil {
ip, err = LookupIP(addr)
if err != nil {
close(hostChan)
slog.Error("Not a valid IP, IP CIDR or domain", "addr", addr)
return hostChan
}
}
go func() {
slog.Info("Enable infinite mode", "init", ip.String())
lowIP := ip
highIP := ip
hostChan <- Host{
IP: ip,
Origin: addr,
Type: HostTypeIP,
}
for i := 0; i < math.MaxInt; i++ {
if i%2 == 0 {
lowIP = NextIP(lowIP, false)
hostChan <- Host{
IP: lowIP,
Origin: lowIP.String(),
Type: HostTypeIP,
}
} else {
highIP = NextIP(highIP, true)
hostChan <- Host{
IP: highIP,
Origin: highIP.String(),
Type: HostTypeIP,
}
}
}
}()
return hostChan
}
func LookupIP(addr string) (net.IP, error) {
ips, err := net.LookupIP(addr)
if err != nil {
return nil, fmt.Errorf("failed to lookup: %w", err)
}
var arr []net.IP
for _, ip := range ips {
if ip.To4() != nil || enableIPv6 {
arr = append(arr, ip)
}
}
if len(arr) == 0 {
return nil, errors.New("no IP found")
}
return arr[0], nil
}
func RemoveDuplicateStr(strSlice []string) []string {
allKeys := make(map[string]bool)
var list []string
for _, item := range strSlice {
if _, value := allKeys[item]; !value {
allKeys[item] = true
list = append(list, item)
}
}
return list
}
func OutWriter(writer io.Writer) chan<- string {
ch := make(chan string)
go func() {
for s := range ch {
_, _ = io.WriteString(writer, s)
}
}()
return ch
}
func NextIP(ip net.IP, increment bool) net.IP {
// Convert to big.Int and increment
ipb := big.NewInt(0).SetBytes(ip)
if increment {
ipb.Add(ipb, big.NewInt(1))
} else {
ipb.Sub(ipb, big.NewInt(1))
}
// Add leading zeros
b := ipb.Bytes()
b = append(make([]byte, len(ip)-len(b)), b...)
return b
}