aboutsummaryrefslogtreecommitdiff
path: root/internal/qna/qna.go
blob: 3ad942cb76e45b77f9aa698c835bccd8c0b38ddf (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package qna

import (
	"context"
	"crypto/tls"
	"errors"
	"fmt"
	"net"
	"net/url"
	"os"
	"strings"
)

type Question struct {
	Domain string // domain name to visit via HTTPS
}

type Answer struct {
	Domain string // domain name of the visited HTTPS site
	HTTP   string // value set in the Onion-Location HTTP header (if any)
	HTML   string // value set in the Onion-Location HTML attribute (if any)

	ReqErr error // nil if HTTP GET request could be constructed
	DoErr  error // nil if HTTP GET request could be executed
}

func (a Answer) String() string {
	if a.ReqErr != nil {
		return fmt.Sprintf("%s: %v", a.ReqErr)
	}
	if a.DoErr != nil {
		return fmt.Sprintf("%s: %v", a.DoErr)
	}
	if a.HTTP == "" && a.HTML == "" {
		return fmt.Sprintf("%s: connected but found no Onion-Location")
	}
	return fmt.Sprintf("%s header=%s attribute=%s", a.Domain, a.HTTP, a.HTML)
}

type Progress struct {
	NumOK    int
	NumOnion int

	NumMakeReqErr     int
	NumDNSNotFoundErr int
	NumDNSTimeoutErr  int
	NumDNSOtherErr    int
	NumConnErr        int
	NumTLSCertErr     int
	NumTLSOtherErr    int
	Num3xxErr         int
	NumDeadlineErr    int
	NumOtherErr       int
}

func (p Progress) String() string {
	str := fmt.Sprintf("  Processed: %d\n", p.NumProcess())
	str += fmt.Sprintf("    Success: %d (Onion-Location:%d)\n", p.NumOK, p.NumOnion)
	str += fmt.Sprintf("    Failure: %d (See breakdown below)\n", p.NumError())
	str += fmt.Sprintf("        Req: %d (Before sending request)\n", p.NumMakeReqErr)
	str += fmt.Sprintf("        DNS: %d (NotFound:%d Timeout:%d Other:%d)\n",
		p.NumDNSErr(), p.NumDNSNotFoundErr, p.NumDNSTimeoutErr, p.NumDNSOtherErr)
	str += fmt.Sprintf("        TCP: %d (Connection error)\n", p.NumConnErr)
	str += fmt.Sprintf("        TLS: %d (Cert:%d Other:%d)\n", p.NumTLSErr(), p.NumTLSCertErr, p.NumTLSOtherErr)
	str += fmt.Sprintf("        3xx: %d (Too many redirects)\n", p.Num3xxErr)
	str += fmt.Sprintf("        CTX: %d (Deadline exceeded)\n", p.NumDeadlineErr)
	str += fmt.Sprintf("        ???: %d (Other errors)", p.NumOtherErr)
	return str
}

func (p *Progress) NumDNSErr() int {
	return p.NumDNSNotFoundErr + p.NumDNSTimeoutErr + p.NumDNSOtherErr
}

func (p *Progress) NumTLSErr() int {
	return p.NumTLSCertErr + p.NumTLSOtherErr
}

func (p *Progress) NumError() int {
	return p.NumMakeReqErr + p.NumDNSErr() + p.NumConnErr + p.NumTLSErr() + p.Num3xxErr + p.NumDeadlineErr + p.NumOtherErr
}

func (p *Progress) NumProcess() int {
	return p.NumOK + p.NumError()
}

func (p *Progress) AddAnswer(a Answer) {
	if err := a.ReqErr; err != nil {
		p.NumMakeReqErr++
		return
	}
	if err := a.DoErr; err != nil {
		if e := dnsError(err); e != nil {
			if e.IsTimeout {
				p.NumDNSTimeoutErr++
			} else if e.IsNotFound {
				p.NumDNSNotFoundErr++
			} else {
				p.NumDNSOtherErr++
			}
		} else if isConnError(err) {
			p.NumConnErr++
		} else if isTLSError(err) {
			if isTLSCertError(err) {
				p.NumTLSCertErr++
			} else {
				p.NumTLSOtherErr++
			}
		} else if is3xxErr(err) {
			p.Num3xxErr++
		} else if isDeadlineError(err) {
			p.NumDeadlineErr++
		} else {
			p.NumOtherErr++
		}
		return
	}
	p.NumOK++
	if a.HTTP != "" || a.HTML != "" {
		p.NumOnion++
	}
}

func dnsError(err error) *net.DNSError {
	urlErr, ok := err.(*url.Error)
	if !ok {
		return nil
	}
	opErr, ok := urlErr.Err.(*net.OpError)
	if !ok {
		return nil
	}
	dnsErr, ok := opErr.Err.(*net.DNSError)
	if !ok {
		return nil
	}
	return dnsErr
}

func isConnError(err error) bool {
	urlErr, ok := err.(*url.Error)
	if !ok {
		return false
	}
	if urlErr.Err.Error() == "EOF" {
		return true
	}
	opErr, ok := urlErr.Err.(*net.OpError)
	if !ok {
		return false
	}
	syscallErr, ok := opErr.Err.(*os.SyscallError)
	if !ok {
		return false
	}
	return syscallErr.Syscall == "connect" || syscallErr.Syscall == "read"
}

func isTLSError(err error) bool {
	urlErr, ok := err.(*url.Error)
	if !ok {
		return false
	}
	if urlErr.Error() == "http: server gave HTTP response to HTTPS client" {
		return true
	}
	return strings.Contains(urlErr.Error(), "tls: ")
}

func isTLSCertError(err error) bool {
	urlErr, ok := err.(*url.Error)
	if !ok {
		return false
	}
	_, ok = urlErr.Err.(*tls.CertificateVerificationError)
	return ok
}

func is3xxErr(err error) bool {
	urlErr, ok := err.(*url.Error)
	return ok && urlErr.Err.Error() == "stopped after 10 redirects"
}

func isDeadlineError(err error) bool {
	urlErr, ok := err.(*url.Error)
	return ok && errors.Is(urlErr.Err, context.DeadlineExceeded)
}

func TrimWildcard(san string) string {
	if len(san) >= 2 && san[:2] == "*." {
		return san[2:]
	}
	return san
}