-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathsni.go
69 lines (63 loc) · 1.56 KB
/
sni.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
package main
import (
"crypto/tls"
"net"
"net/http"
"net/http/httputil"
"time"
)
func testSni(ip string, config *ScanConfig, record *ScanRecord) bool {
tlscfg := &tls.Config{
InsecureSkipVerify: true,
}
for _, serverName := range config.ServerName {
start := time.Now()
conn, err := net.DialTimeout("tcp", net.JoinHostPort(ip, "443"), config.ScanMaxRTT)
if err != nil {
return false
}
tlscfg.ServerName = serverName
tlsconn := tls.Client(conn, tlscfg)
tlsconn.SetDeadline(time.Now().Add(config.HandshakeTimeout))
if err = tlsconn.Handshake(); err != nil {
tlsconn.Close()
return false
}
if config.Level > 1 {
pcs := tlsconn.ConnectionState().PeerCertificates
if len(pcs) == 0 || pcs[0].Subject.CommonName != serverName {
tlsconn.Close()
return false
}
}
if config.Level > 2 {
req, err := http.NewRequest(http.MethodHead, "https://"+serverName, nil)
if err != nil {
tlsconn.Close()
return false
}
tlsconn.SetDeadline(time.Now().Add(config.ScanMaxRTT - time.Since(start)))
resp, err := httputil.NewClientConn(tlsconn, nil).Do(req)
if err != nil {
tlsconn.Close()
return false
}
// io.Copy(os.Stdout, resp.Body)
// if resp.Body != nil {
// io.Copy(ioutil.Discard, resp.Body)
// resp.Body.Close()
// }
if resp.StatusCode >= 400 {
tlsconn.Close()
return false
}
}
tlsconn.Close()
rtt := time.Since(start)
if rtt < config.ScanMinRTT {
return false
}
record.RTT += rtt
}
return true
}