This commit is contained in:
juzeon
2024-02-12 10:13:18 -05:00
committed by yuhan6665
parent fa310c5c0f
commit 8e0e975703
3 changed files with 23 additions and 85 deletions
+4 -14
View File
@@ -39,10 +39,6 @@ func main() {
flag.StringVar(&url, "url", "", "Crawl the domain list from a URL, "+
"e.g. https://launchpad.net/ubuntu/+archivemirrors")
flag.Parse()
s := Scanner{
mu: new(sync.Mutex),
}
if verbose {
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
@@ -70,7 +66,7 @@ func main() {
}
var hostChan <-chan Host
if addr != "" {
hostChan = Iterate(strings.NewReader(addr), true)
hostChan = Iterate(strings.NewReader(addr))
} else if in != "" {
f, err := os.Open(in)
if err != nil {
@@ -78,7 +74,7 @@ func main() {
return
}
defer f.Close()
hostChan = Iterate(f, false)
hostChan = Iterate(f)
} else {
slog.Info("Fetching url...")
resp, err := http.Get(url)
@@ -99,7 +95,7 @@ func main() {
}
domains = RemoveDuplicateStr(domains)
slog.Info("Parsed domains", "count", len(domains))
hostChan = Iterate(strings.NewReader(strings.Join(domains, "\n")), len(domains) <= 1)
hostChan = Iterate(strings.NewReader(strings.Join(domains, "\n")))
}
outCh := OutWriter(outWriter)
defer close(outCh)
@@ -108,13 +104,7 @@ func main() {
for i := 0; i < thread; i++ {
go func() {
for ip := range hostChan {
ip = s.Scan(ip, outCh, true)
if ip.Infinity { // only one ip
for i := 0; i < thread - 1; i++ {
go s.Scan(ip, outCh, i%2 == 1)
}
for {}
}
ScanTLS(ip, outCh)
}
wg.Done()
}()
+6 -54
View File
@@ -3,50 +3,18 @@ package main
import (
"crypto/tls"
"log/slog"
"math/big"
"net"
"strconv"
"strings"
"sync"
"time"
)
type Scanner struct {
mu *sync.Mutex
high net.IP
low net.IP
}
func (s *Scanner) Scan(host Host, out chan<- string, increment bool) Host {
if host.Infinity && host.IP != nil {
s.mu.Lock()
if s.high == nil {
s.high = host.IP
s.low = host.IP
host.Origin = ""
host.Type = HostTypeIP
} else if increment {
s.high = nextIP(s.high, increment)
host.IP = s.high
} else {
s.low = nextIP(s.low, increment)
host.IP = s.low
}
s.mu.Unlock()
}
host = ScanTLS(host, out, increment)
if host.Infinity && host.IP != nil {
go s.Scan(host, out, increment)
}
return host
}
func ScanTLS(host Host, out chan<- string, increment bool) Host {
func ScanTLS(host Host, out chan<- string) {
if host.IP == nil {
ips, err := net.LookupIP(host.Origin)
if err != nil {
slog.Debug("Failed to lookup", "origin", host.Origin, "err", err)
return host
return
}
var arr []net.IP
for _, ip := range ips {
@@ -56,7 +24,7 @@ func ScanTLS(host Host, out chan<- string, increment bool) Host {
}
if len(arr) == 0 {
slog.Debug("No IP found", "origin", host.Origin)
return host
return
}
host.IP = arr[0]
}
@@ -64,13 +32,13 @@ func ScanTLS(host Host, out chan<- string, increment bool) Host {
conn, err := net.DialTimeout("tcp", hostPort, time.Duration(timeout)*time.Second)
if err != nil {
slog.Debug("Cannot dial", "target", hostPort)
return host
return
}
defer conn.Close()
err = conn.SetDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
if err != nil {
slog.Error("Error setting deadline", "err", err)
return host
return
}
tlsCfg := &tls.Config{
InsecureSkipVerify: true,
@@ -84,7 +52,7 @@ func ScanTLS(host Host, out chan<- string, increment bool) Host {
err = c.Handshake()
if err != nil {
slog.Debug("TLS handshake failed", "target", hostPort)
return host
return
}
state := c.ConnectionState()
alpn := state.NegotiatedProtocol
@@ -102,20 +70,4 @@ func ScanTLS(host Host, out chan<- string, increment bool) Host {
log("Connected to target", "feasible", feasible, "ip", host.IP.String(),
"origin", host.Origin,
"tls", tls.VersionName(state.Version), "alpn", alpn, "cert-domain", domain, "cert-issuer", issuers)
return host
}
func nextIP(ip net.IP, increment bool) net.IP {
// Convert to big.Int and increment
ipb := big.NewInt(0).SetBytes([]byte(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 net.IP(b)
}
+13 -17
View File
@@ -21,13 +21,12 @@ const (
type HostType int
type Host struct {
IP net.IP
Origin string
Type HostType
Infinity bool
IP net.IP
Origin string
Type HostType
}
func Iterate(reader io.Reader, infinity bool) <-chan Host {
func Iterate(reader io.Reader) <-chan Host {
scanner := bufio.NewScanner(reader)
hostChan := make(chan Host)
go func() {
@@ -41,10 +40,9 @@ func Iterate(reader io.Reader, infinity bool) <-chan Host {
if ip != nil && (ip.To4() != nil || enableIPv6) {
// ip address
hostChan <- Host{
IP: ip,
Origin: line,
Type: HostTypeIP,
Infinity: infinity,
IP: ip,
Origin: line,
Type: HostTypeIP,
}
continue
}
@@ -67,10 +65,9 @@ func Iterate(reader io.Reader, infinity bool) <-chan Host {
ip = net.ParseIP(addr.String())
if ip != nil {
hostChan <- Host{
IP: ip,
Origin: line,
Type: HostTypeCIDR,
Infinity: false,
IP: ip,
Origin: line,
Type: HostTypeCIDR,
}
}
addr = addr.Next()
@@ -80,10 +77,9 @@ func Iterate(reader io.Reader, infinity bool) <-chan Host {
if ValidateDomainName(line) {
// domain
hostChan <- Host{
IP: nil,
Origin: line,
Type: HostTypeDomain,
Infinity: infinity,
IP: nil,
Origin: line,
Type: HostTypeDomain,
}
continue
}