Skip to content

Commit d2842f1

Browse files
committed
Add back infinity mode (default for single address)
1 parent 7a987c9 commit d2842f1

3 files changed

Lines changed: 79 additions & 19 deletions

File tree

main.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,18 @@ func main() {
3131
flag.StringVar(&in, "in", "", "Specify a file that contains multiple "+
3232
"IPs, IP CIDRs or domains to scan, divided by line break")
3333
flag.IntVar(&port, "port", 443, "Specify a HTTPS port to check")
34-
flag.IntVar(&thread, "thread", 1, "Count of concurrent tasks")
34+
flag.IntVar(&thread, "thread", 2, "Count of concurrent tasks")
3535
flag.StringVar(&out, "out", "out.csv", "Output file to store the result")
3636
flag.IntVar(&timeout, "timeout", 10, "Timeout for every check")
3737
flag.BoolVar(&verbose, "v", false, "Verbose output")
3838
flag.BoolVar(&enableIPv6, "46", false, "Enable IPv6 in additional to IPv4")
3939
flag.StringVar(&url, "url", "", "Crawl the domain list from a URL, "+
4040
"e.g. https://launchpad.net/ubuntu/+archivemirrors")
4141
flag.Parse()
42+
s := Scanner{
43+
mu: new(sync.Mutex),
44+
}
45+
4246
if verbose {
4347
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
4448
Level: slog.LevelDebug,
@@ -66,15 +70,15 @@ func main() {
6670
}
6771
var hostChan <-chan Host
6872
if addr != "" {
69-
hostChan = Iterate(strings.NewReader(addr))
73+
hostChan = Iterate(strings.NewReader(addr), true)
7074
} else if in != "" {
7175
f, err := os.Open(in)
7276
if err != nil {
7377
slog.Error("Error reading file", "path", in)
7478
return
7579
}
7680
defer f.Close()
77-
hostChan = Iterate(f)
81+
hostChan = Iterate(f, false)
7882
} else {
7983
slog.Info("Fetching url...")
8084
resp, err := http.Get(url)
@@ -95,7 +99,7 @@ func main() {
9599
}
96100
domains = RemoveDuplicateStr(domains)
97101
slog.Info("Parsed domains", "count", len(domains))
98-
hostChan = Iterate(strings.NewReader(strings.Join(domains, "\n")))
102+
hostChan = Iterate(strings.NewReader(strings.Join(domains, "\n")), len(domains) <= 1)
99103
}
100104
outCh := OutWriter(outWriter)
101105
defer close(outCh)
@@ -104,7 +108,13 @@ func main() {
104108
for i := 0; i < thread; i++ {
105109
go func() {
106110
for ip := range hostChan {
107-
ScanTLS(ip, outCh)
111+
s.Scan(ip, outCh, true)
112+
if ip.Infinity { // only one ip
113+
for i := 0; i < thread - 1; i++ {
114+
go s.Scan(ip, outCh, i%2 == 1)
115+
}
116+
for {}
117+
}
108118
}
109119
wg.Done()
110120
}()

scanner.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,44 @@ package main
33
import (
44
"crypto/tls"
55
"log/slog"
6+
"math/big"
67
"net"
78
"strconv"
89
"strings"
10+
"sync"
911
"time"
1012
)
1113

12-
func ScanTLS(host Host, out chan<- string) {
14+
type Scanner struct {
15+
mu *sync.Mutex
16+
high net.IP
17+
low net.IP
18+
}
19+
20+
func (s *Scanner) Scan(host Host, out chan<- string, increment bool) {
21+
if host.Infinity && host.IP != nil {
22+
s.mu.Lock()
23+
if s.high == nil {
24+
s.high = host.IP
25+
s.low = host.IP
26+
host.Origin = ""
27+
host.Type = HostTypeIP
28+
} else if increment {
29+
s.high = nextIP(s.high, increment)
30+
host.IP = s.high
31+
} else {
32+
s.low = nextIP(s.low, increment)
33+
host.IP = s.low
34+
}
35+
s.mu.Unlock()
36+
}
37+
ScanTLS(host, out, increment)
38+
if host.Infinity && host.IP != nil {
39+
go s.Scan(host, out, increment)
40+
}
41+
}
42+
43+
func ScanTLS(host Host, out chan<- string, increment bool) {
1344
if host.IP == nil {
1445
ips, err := net.LookupIP(host.Origin)
1546
if err != nil {
@@ -71,3 +102,18 @@ func ScanTLS(host Host, out chan<- string) {
71102
"origin", host.Origin,
72103
"tls", tls.VersionName(state.Version), "alpn", alpn, "cert-domain", domain, "cert-issuer", issuers)
73104
}
105+
106+
func nextIP(ip net.IP, increment bool) net.IP {
107+
// Convert to big.Int and increment
108+
ipb := big.NewInt(0).SetBytes([]byte(ip))
109+
if increment {
110+
ipb.Add(ipb, big.NewInt(1))
111+
} else {
112+
ipb.Sub(ipb, big.NewInt(1))
113+
}
114+
115+
// Add leading zeros
116+
b := ipb.Bytes()
117+
b = append(make([]byte, len(ip)-len(b)), b...)
118+
return net.IP(b)
119+
}

utils.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ const (
2121
type HostType int
2222

2323
type Host struct {
24-
IP net.IP
25-
Origin string
26-
Type HostType
24+
IP net.IP
25+
Origin string
26+
Type HostType
27+
Infinity bool
2728
}
2829

29-
func Iterate(reader io.Reader) <-chan Host {
30+
func Iterate(reader io.Reader, infinity bool) <-chan Host {
3031
scanner := bufio.NewScanner(reader)
3132
hostChan := make(chan Host)
3233
go func() {
@@ -40,9 +41,10 @@ func Iterate(reader io.Reader) <-chan Host {
4041
if ip != nil && (ip.To4() != nil || enableIPv6) {
4142
// ip address
4243
hostChan <- Host{
43-
IP: ip,
44-
Origin: line,
45-
Type: HostTypeIP,
44+
IP: ip,
45+
Origin: line,
46+
Type: HostTypeIP,
47+
Infinity: infinity,
4648
}
4749
continue
4850
}
@@ -65,9 +67,10 @@ func Iterate(reader io.Reader) <-chan Host {
6567
ip = net.ParseIP(addr.String())
6668
if ip != nil {
6769
hostChan <- Host{
68-
IP: ip,
69-
Origin: line,
70-
Type: HostTypeCIDR,
70+
IP: ip,
71+
Origin: line,
72+
Type: HostTypeCIDR,
73+
Infinity: false,
7174
}
7275
}
7376
addr = addr.Next()
@@ -77,9 +80,10 @@ func Iterate(reader io.Reader) <-chan Host {
7780
if ValidateDomainName(line) {
7881
// domain
7982
hostChan <- Host{
80-
IP: nil,
81-
Origin: line,
82-
Type: HostTypeDomain,
83+
IP: nil,
84+
Origin: line,
85+
Type: HostTypeDomain,
86+
Infinity: infinity,
8387
}
8488
continue
8589
}

0 commit comments

Comments
 (0)