fix writer race condition

This commit is contained in:
juzeon
2024-02-11 20:29:56 -05:00
committed by yuhan6665
parent da1efd8a25
commit 7103888409
3 changed files with 14 additions and 4 deletions
+3 -1
View File
@@ -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()
}()
+2 -3
View File
@@ -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],
+9
View File
@@ -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
}