From 7103888409757604d75a42fd922f05f86b259449 Mon Sep 17 00:00:00 2001 From: juzeon Date: Thu, 8 Feb 2024 21:47:39 +0800 Subject: [PATCH] fix writer race condition --- main.go | 4 +++- scanner.go | 5 ++--- utils.go | 9 +++++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 141078e..82bfdad 100644 --- a/main.go +++ b/main.go @@ -98,12 +98,14 @@ func main() { slog.Info("Parsed domains", "count", len(domains)) ipChan = Iterate(strings.NewReader(strings.Join(domains, "\n"))) } + outCh := OutWriter(outWriter) + defer close(outCh) var wg sync.WaitGroup wg.Add(thread) for i := 0; i < thread; i++ { go func() { for ip := range ipChan { - ScanTLS(ip, outWriter) + ScanTLS(ip, outCh) } wg.Done() }() diff --git a/scanner.go b/scanner.go index 9852c1e..a0e6e20 100644 --- a/scanner.go +++ b/scanner.go @@ -2,7 +2,6 @@ package main import ( "crypto/tls" - "io" "log/slog" "net" "strconv" @@ -17,7 +16,7 @@ var TLSDictionary = map[uint16]string{ 0x0304: "1.3", } -func ScanTLS(ip net.IP, out io.Writer) { +func ScanTLS(ip net.IP, out chan<- string) { hostPort := net.JoinHostPort(ip.String(), strconv.Itoa(port)) conn, err := net.DialTimeout("tcp", hostPort, time.Duration(timeout)*time.Second) if err != nil { @@ -50,7 +49,7 @@ func ScanTLS(ip net.IP, out io.Writer) { log = slog.Debug feasible = false } else { - _, _ = io.WriteString(out, strings.Join([]string{ip.String(), domain, "\"" + issuers + "\""}, ",")+"\n") + out <- strings.Join([]string{ip.String(), domain, "\"" + issuers + "\""}, ",") + "\n" } log("Connected to target", "feasible", feasible, "host", ip.String(), "tls", TLSDictionary[state.Version], diff --git a/utils.go b/utils.go index 56f5f19..4d73a50 100644 --- a/utils.go +++ b/utils.go @@ -92,3 +92,12 @@ func RemoveDuplicateStr(strSlice []string) []string { } 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 +}