Skip to content

Commit 8e0e975

Browse files
juzeonyuhan6665
authored andcommitted
revert
1 parent fa310c5 commit 8e0e975

File tree

3 files changed

+23
-85
lines changed

3 files changed

+23
-85
lines changed

main.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ func main() {
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-
4642
if verbose {
4743
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
4844
Level: slog.LevelDebug,
@@ -70,15 +66,15 @@ func main() {
7066
}
7167
var hostChan <-chan Host
7268
if addr != "" {
73-
hostChan = Iterate(strings.NewReader(addr), true)
69+
hostChan = Iterate(strings.NewReader(addr))
7470
} else if in != "" {
7571
f, err := os.Open(in)
7672
if err != nil {
7773
slog.Error("Error reading file", "path", in)
7874
return
7975
}
8076
defer f.Close()
81-
hostChan = Iterate(f, false)
77+
hostChan = Iterate(f)
8278
} else {
8379
slog.Info("Fetching url...")
8480
resp, err := http.Get(url)
@@ -99,7 +95,7 @@ func main() {
9995
}
10096
domains = RemoveDuplicateStr(domains)
10197
slog.Info("Parsed domains", "count", len(domains))
102-
hostChan = Iterate(strings.NewReader(strings.Join(domains, "\n")), len(domains) <= 1)
98+
hostChan = Iterate(strings.NewReader(strings.Join(domains, "\n")))
10399
}
104100
outCh := OutWriter(outWriter)
105101
defer close(outCh)
@@ -108,13 +104,7 @@ func main() {
108104
for i := 0; i < thread; i++ {
109105
go func() {
110106
for ip := range hostChan {
111-
ip = 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-
}
107+
ScanTLS(ip, outCh)
118108
}
119109
wg.Done()
120110
}()

scanner.go

Lines changed: 6 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,18 @@ package main
33
import (
44
"crypto/tls"
55
"log/slog"
6-
"math/big"
76
"net"
87
"strconv"
98
"strings"
10-
"sync"
119
"time"
1210
)
1311

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) Host {
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-
host = ScanTLS(host, out, increment)
38-
if host.Infinity && host.IP != nil {
39-
go s.Scan(host, out, increment)
40-
}
41-
return host
42-
}
43-
44-
func ScanTLS(host Host, out chan<- string, increment bool) Host {
12+
func ScanTLS(host Host, out chan<- string) {
4513
if host.IP == nil {
4614
ips, err := net.LookupIP(host.Origin)
4715
if err != nil {
4816
slog.Debug("Failed to lookup", "origin", host.Origin, "err", err)
49-
return host
17+
return
5018
}
5119
var arr []net.IP
5220
for _, ip := range ips {
@@ -56,21 +24,21 @@ func ScanTLS(host Host, out chan<- string, increment bool) Host {
5624
}
5725
if len(arr) == 0 {
5826
slog.Debug("No IP found", "origin", host.Origin)
59-
return host
27+
return
6028
}
6129
host.IP = arr[0]
6230
}
6331
hostPort := net.JoinHostPort(host.IP.String(), strconv.Itoa(port))
6432
conn, err := net.DialTimeout("tcp", hostPort, time.Duration(timeout)*time.Second)
6533
if err != nil {
6634
slog.Debug("Cannot dial", "target", hostPort)
67-
return host
35+
return
6836
}
6937
defer conn.Close()
7038
err = conn.SetDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
7139
if err != nil {
7240
slog.Error("Error setting deadline", "err", err)
73-
return host
41+
return
7442
}
7543
tlsCfg := &tls.Config{
7644
InsecureSkipVerify: true,
@@ -84,7 +52,7 @@ func ScanTLS(host Host, out chan<- string, increment bool) Host {
8452
err = c.Handshake()
8553
if err != nil {
8654
slog.Debug("TLS handshake failed", "target", hostPort)
87-
return host
55+
return
8856
}
8957
state := c.ConnectionState()
9058
alpn := state.NegotiatedProtocol
@@ -102,20 +70,4 @@ func ScanTLS(host Host, out chan<- string, increment bool) Host {
10270
log("Connected to target", "feasible", feasible, "ip", host.IP.String(),
10371
"origin", host.Origin,
10472
"tls", tls.VersionName(state.Version), "alpn", alpn, "cert-domain", domain, "cert-issuer", issuers)
105-
return host
106-
}
107-
108-
func nextIP(ip net.IP, increment bool) net.IP {
109-
// Convert to big.Int and increment
110-
ipb := big.NewInt(0).SetBytes([]byte(ip))
111-
if increment {
112-
ipb.Add(ipb, big.NewInt(1))
113-
} else {
114-
ipb.Sub(ipb, big.NewInt(1))
115-
}
116-
117-
// Add leading zeros
118-
b := ipb.Bytes()
119-
b = append(make([]byte, len(ip)-len(b)), b...)
120-
return net.IP(b)
12173
}

utils.go

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

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

30-
func Iterate(reader io.Reader, infinity bool) <-chan Host {
29+
func Iterate(reader io.Reader) <-chan Host {
3130
scanner := bufio.NewScanner(reader)
3231
hostChan := make(chan Host)
3332
go func() {
@@ -41,10 +40,9 @@ func Iterate(reader io.Reader, infinity bool) <-chan Host {
4140
if ip != nil && (ip.To4() != nil || enableIPv6) {
4241
// ip address
4342
hostChan <- Host{
44-
IP: ip,
45-
Origin: line,
46-
Type: HostTypeIP,
47-
Infinity: infinity,
43+
IP: ip,
44+
Origin: line,
45+
Type: HostTypeIP,
4846
}
4947
continue
5048
}
@@ -67,10 +65,9 @@ func Iterate(reader io.Reader, infinity bool) <-chan Host {
6765
ip = net.ParseIP(addr.String())
6866
if ip != nil {
6967
hostChan <- Host{
70-
IP: ip,
71-
Origin: line,
72-
Type: HostTypeCIDR,
73-
Infinity: false,
68+
IP: ip,
69+
Origin: line,
70+
Type: HostTypeCIDR,
7471
}
7572
}
7673
addr = addr.Next()
@@ -80,10 +77,9 @@ func Iterate(reader io.Reader, infinity bool) <-chan Host {
8077
if ValidateDomainName(line) {
8178
// domain
8279
hostChan <- Host{
83-
IP: nil,
84-
Origin: line,
85-
Type: HostTypeDomain,
86-
Infinity: infinity,
80+
IP: nil,
81+
Origin: line,
82+
Type: HostTypeDomain,
8783
}
8884
continue
8985
}

0 commit comments

Comments
 (0)