Skip to content

Commit 1b3f818

Browse files
juzeonyuhan6665
authored andcommitted
better infinity mode implementation
1 parent 8e0e975 commit 1b3f818

File tree

3 files changed

+82
-14
lines changed

3 files changed

+82
-14
lines changed

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func main() {
6666
}
6767
var hostChan <-chan Host
6868
if addr != "" {
69-
hostChan = Iterate(strings.NewReader(addr))
69+
hostChan = IterateAddr(addr)
7070
} else if in != "" {
7171
f, err := os.Open(in)
7272
if err != nil {

scanner.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,12 @@ import (
1111

1212
func ScanTLS(host Host, out chan<- string) {
1313
if host.IP == nil {
14-
ips, err := net.LookupIP(host.Origin)
14+
ip, err := LookupIP(host.Origin)
1515
if err != nil {
16-
slog.Debug("Failed to lookup", "origin", host.Origin, "err", err)
16+
slog.Debug("Failed to get IP from the origin", "origin", host.Origin, "err", err)
1717
return
1818
}
19-
var arr []net.IP
20-
for _, ip := range ips {
21-
if ip.To4() != nil || enableIPv6 {
22-
arr = append(arr, ip)
23-
}
24-
}
25-
if len(arr) == 0 {
26-
slog.Debug("No IP found", "origin", host.Origin)
27-
return
28-
}
29-
host.IP = arr[0]
19+
host.IP = ip
3020
}
3121
hostPort := net.JoinHostPort(host.IP.String(), strconv.Itoa(port))
3222
conn, err := net.DialTimeout("tcp", hostPort, time.Duration(timeout)*time.Second)

utils.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ package main
33
import (
44
"bufio"
55
"errors"
6+
"fmt"
67
"io"
78
"log/slog"
9+
"math"
10+
"math/big"
811
"net"
912
"net/netip"
1013
"regexp"
@@ -108,6 +111,67 @@ func ExistOnlyOne(arr []string) bool {
108111
}
109112
return exist
110113
}
114+
func IterateAddr(addr string) <-chan Host {
115+
hostChan := make(chan Host)
116+
_, _, err := net.ParseCIDR(addr)
117+
if err == nil {
118+
// is CIDR
119+
return Iterate(strings.NewReader(addr))
120+
}
121+
ip := net.ParseIP(addr)
122+
if ip == nil {
123+
ip, err = LookupIP(addr)
124+
if err != nil {
125+
close(hostChan)
126+
slog.Error("Not a valid IP, IP CIDR or domain", "addr", addr)
127+
return hostChan
128+
}
129+
}
130+
go func() {
131+
slog.Info("Enable infinite mode", "init", ip.String())
132+
lowIP := ip
133+
highIP := ip
134+
hostChan <- Host{
135+
IP: ip,
136+
Origin: addr,
137+
Type: HostTypeIP,
138+
}
139+
for i := 0; i < math.MaxInt; i++ {
140+
if i%2 == 0 {
141+
lowIP = NextIP(lowIP, false)
142+
hostChan <- Host{
143+
IP: lowIP,
144+
Origin: lowIP.String(),
145+
Type: HostTypeIP,
146+
}
147+
} else {
148+
highIP = NextIP(highIP, true)
149+
hostChan <- Host{
150+
IP: highIP,
151+
Origin: highIP.String(),
152+
Type: HostTypeIP,
153+
}
154+
}
155+
}
156+
}()
157+
return hostChan
158+
}
159+
func LookupIP(addr string) (net.IP, error) {
160+
ips, err := net.LookupIP(addr)
161+
if err != nil {
162+
return nil, fmt.Errorf("failed to lookup: %w", err)
163+
}
164+
var arr []net.IP
165+
for _, ip := range ips {
166+
if ip.To4() != nil || enableIPv6 {
167+
arr = append(arr, ip)
168+
}
169+
}
170+
if len(arr) == 0 {
171+
return nil, errors.New("no IP found")
172+
}
173+
return arr[0], nil
174+
}
111175
func RemoveDuplicateStr(strSlice []string) []string {
112176
allKeys := make(map[string]bool)
113177
var list []string
@@ -128,3 +192,17 @@ func OutWriter(writer io.Writer) chan<- string {
128192
}()
129193
return ch
130194
}
195+
func NextIP(ip net.IP, increment bool) net.IP {
196+
// Convert to big.Int and increment
197+
ipb := big.NewInt(0).SetBytes(ip)
198+
if increment {
199+
ipb.Add(ipb, big.NewInt(1))
200+
} else {
201+
ipb.Sub(ipb, big.NewInt(1))
202+
}
203+
204+
// Add leading zeros
205+
b := ipb.Bytes()
206+
b = append(make([]byte, len(ip)-len(b)), b...)
207+
return b
208+
}

0 commit comments

Comments
 (0)