-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
122 lines (101 loc) · 2.27 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"bufio"
"context"
"crypto/tls"
"flag"
quic "github.com/lucas-clemente/quic-go"
"golang.org/x/sync/semaphore"
"log"
"net"
"os"
"sync"
"time"
)
const (
VersionDoQ00 = "doq-i00"
VersionDoQ01 = "doq-i01"
VersionDoQ02 = "doq-i02"
VersionDoQ03 = "doq-i03"
VersionDoQ04 = "doq-i04"
VersionDoQ05 = "doq-i05"
VersionDoQ06 = "doq-i06"
)
var DefaultDoQVersions = []string{VersionDoQ06, VersionDoQ05, VersionDoQ04, VersionDoQ03, VersionDoQ02, VersionDoQ01, VersionDoQ00}
var DefaultQUICVersions = []quic.VersionNumber{
quic.Version1,
quic.VersionDraft34,
quic.VersionDraft32,
quic.VersionDraft29,
}
var port853Flag = flag.Bool("port853", false, "verify on port 853")
func establishConnection(ip net.IP) bool {
tlsConf := &tls.Config{
InsecureSkipVerify: true,
NextProtos: DefaultDoQVersions,
}
quicConf := &quic.Config{
HandshakeIdleTimeout: time.Second * 2,
Versions: DefaultQUICVersions,
}
var ports []string
if *port853Flag {
ports = []string{"853"}
} else {
ports = []string{"784", "8853"}
}
reachable := make(chan bool)
go func() {
for _, port := range ports {
session, err := quic.DialAddr(ip.String() + ":" + port, tlsConf, quicConf)
if err != nil {
continue
}
reachable <- true
session.CloseWithError(0, "")
return
}
reachable <- false
}()
return <- reachable
}
func main() {
parallelLimit := flag.Int("parallel", 30, "sets the limit for parallel processes")
flag.Parse()
args := flag.Args()
if len(args) != 2 {
println("need 2 arguments: [in file] [out file]")
os.Exit(1)
}
inFile, err := os.Open(args[0])
if err != nil {
log.Fatal(err)
}
defer inFile.Close()
outFile, err := os.OpenFile(args[1], os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
defer outFile.Close()
var sem = semaphore.NewWeighted(int64(*parallelLimit))
var wg sync.WaitGroup
scanner := bufio.NewScanner(inFile)
for scanner.Scan() {
ip := net.ParseIP(scanner.Text())
if ip.To4() != nil {
sem.Acquire(context.Background(), 1)
wg.Add(1)
go func() {
reachable := establishConnection(ip)
if reachable {
if _, err := outFile.WriteString(ip.String() + "\n"); err != nil {
log.Println(err)
}
}
sem.Release(1)
wg.Done()
}()
}
}
wg.Wait()
}