moved
[gscan_quic.git] / sni.go
bloba7e8e2e12206587c4ff76640e82db1ec135ebb16
1 package main
3 import (
4 "crypto/tls"
5 "fmt"
6 "math/rand"
7 "net"
8 "net/http"
9 "time"
12 func testSni(ip string, config *ScanConfig, record *ScanRecord) bool {
13 tlscfg := &tls.Config{
14 InsecureSkipVerify: true,
16 tr := &http.Transport{
17 TLSClientConfig: tlscfg,
18 ResponseHeaderTimeout: config.ScanMaxRTT,
20 httpconn := &http.Client{
21 CheckRedirect: func(req *http.Request, via []*http.Request) error {
22 return http.ErrUseLastResponse
24 Transport: tr,
26 var Host string
27 var VerifyCN string
28 var Path string
29 var Code int
30 if len(config.HTTPVerifyHosts) == 0 {
31 Host = randomHost()
32 } else {
33 Host = config.HTTPVerifyHosts[rand.Intn(len(config.HTTPVerifyHosts))]
35 VerifyCN = config.VerifyCommonName
36 Code = config.ValidStatusCode
37 Path = config.HTTPPath
39 for _, serverName := range config.ServerName {
40 start := time.Now()
41 conn, err := net.DialTimeout("tcp", net.JoinHostPort(ip, "443"), config.ScanMaxRTT)
42 if err != nil {
43 return false
46 tlscfg.ServerName = serverName
47 tlsconn := tls.Client(conn, tlscfg)
48 tlsconn.SetDeadline(time.Now().Add(config.HandshakeTimeout))
49 if err = tlsconn.Handshake(); err != nil {
50 tlsconn.Close()
51 return false
53 if config.Level > 1 {
54 pcs := tlsconn.ConnectionState().PeerCertificates
55 if len(pcs) == 0 || pcs[0].Subject.CommonName != VerifyCN {
56 fmt.Println("CN:", pcs[0].Subject.CommonName)
57 tlsconn.Close()
58 return false
61 if config.Level > 2 {
62 req, err := http.NewRequest(http.MethodHead, "https://"+ip+Path, nil)
63 req.Host = Host
64 if err != nil {
65 tlsconn.Close()
66 //fmt.Println("build req error")
67 return false
69 tlsconn.SetDeadline(time.Now().Add(config.ScanMaxRTT - time.Since(start)))
70 //resp, err := httputil.NewClientConn(tlsconn, nil).Do(req)
71 resp, err := httpconn.Do(req)
72 if err != nil {
73 //fmt.Println("httpconn error")
74 //fmt.Println(err)
75 tlsconn.Close()
76 return false
78 // io.Copy(os.Stdout, resp.Body)
79 // if resp.Body != nil {
80 // io.Copy(ioutil.Discard, resp.Body)
81 // resp.Body.Close()
82 // }
83 if resp.StatusCode != Code {
84 fmt.Println("Status Code:", resp.StatusCode)
85 tlsconn.Close()
86 return false
90 tlsconn.Close()
91 httpconn.CloseIdleConnections()
93 rtt := time.Since(start)
94 if rtt < config.ScanMinRTT {
95 return false
97 record.RTT += rtt
99 return true