forked from getlantern/lantern
/
verify.go
145 lines (134 loc) · 3.85 KB
/
verify.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"bytes"
"io/ioutil"
"net/http"
"net/http/httputil"
"github.com/getlantern/flashlight/client"
)
var (
// https://github.com/getlantern/lantern/issues/3147
redirectSites = []string{
// sites that redirect to http when accessed through https
"http://www.bbc.co.uk",
"http://www.speedtest.net",
// sites that redirect to itself if the "Host:" HTTP header contains port
"http://lowendbox.com",
"http://sourceforge.net",
"http://krypted.com/mac-security/manage-profiles-from-the-command-line-in-os-x-10-9/",
}
atsDefaultBody = []byte(`<HTML>
<HEAD>
<TITLE>Not Found on Accelerator</TITLE>
</HEAD>
<BODY BGCOLOR="white" FGCOLOR="black">
<H1>Not Found on Accelerator</H1>
<HR>
<FONT FACE="Helvetica,Arial"><B>
Description: Your request on the specified host was not found.
Check the location and try again.
</B></FONT>
<HR>
</BODY>
`)
)
func verifyFallback(fb *client.ChainedServerInfo, c *http.Client) {
verifyMimicWhenNoAuthToken(fb, c)
verifyMimicWithInvalidAuthToken(fb, c)
for _, s := range redirectSites {
verifyRedirectSites(fb, c, s)
}
}
func verifyMimicWhenNoAuthToken(fb *client.ChainedServerInfo, c *http.Client) {
req, err := http.NewRequest("GET", "http://www.google.com/humans.txt", nil)
if err != nil {
log.Errorf("%v: NewRequest() error : %v", fb.Addr, err)
return
}
// intentionally not set auth token
doVerifyMimic(fb.Addr, req, c)
}
func verifyMimicWithInvalidAuthToken(fb *client.ChainedServerInfo, c *http.Client) {
req, err := http.NewRequest("GET", "http://www.google.com/humans.txt", nil)
if err != nil {
log.Errorf("%v: NewRequest() error : %v", fb.Addr, err)
return
}
req.Header.Set("X-LANTERN-AUTH-TOKEN", "invalid")
doVerifyMimic(fb.Addr, req, c)
}
func doVerifyMimic(addr string, req *http.Request, c *http.Client) {
resp, err := c.Do(req)
if err != nil {
log.Errorf("%v: requesting humans.txt failed: %v", addr, err)
return
}
defer func() {
if err := resp.Body.Close(); err != nil {
log.Debugf("Unable to close response body: %v", err)
}
}()
if resp.StatusCode != 404 {
log.Errorf("%v: should get 404 if auth failed", addr)
if *verbose {
respStr, _ := httputil.DumpResponse(resp, true)
log.Debug(string(respStr))
}
return
}
if resp.Header.Get("Content-Type") != "text/html" ||
resp.Header.Get("Cache-Control") != "no-store" ||
resp.Header.Get("Connection") != "keep-alive" ||
resp.Header.Get("Content-Language") != "en" ||
resp.Header.Get("Content-Length") != "297" {
log.Errorf("%v: should have correct headers present", addr)
if *verbose {
respStr, _ := httputil.DumpResponse(resp, true)
log.Debug(string(respStr))
}
return
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Errorf("%v: error reading response body: %v", addr, err)
return
}
if !bytes.Equal(b, atsDefaultBody) {
log.Errorf("%v: response body mismatch for invalid request", addr)
log.Debugf("Body expected: %v", string(atsDefaultBody))
log.Debugf("Body got: %v", string(b))
return
}
log.Debugf("%v: OK.", addr)
}
func verifyRedirectSites(fb *client.ChainedServerInfo, c *http.Client, url string) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Errorf("error make request to %s: %v", url, err)
return
}
req.Header.Set("X-LANTERN-AUTH-TOKEN", fb.AuthToken)
resp, err := c.Do(req)
if err != nil {
log.Errorf("%v: requesting %s failed: %v", fb.Addr, url, err)
if *verbose {
reqStr, _ := httputil.DumpRequestOut(req, true)
log.Debug(string(reqStr))
}
return
}
defer func() {
if err := resp.Body.Close(); err != nil {
log.Debugf("Unable to close response body: %v", err)
}
}()
if resp.StatusCode != 200 {
log.Errorf("%v: bad status code %v for %s", fb.Addr, resp.StatusCode, url)
if *verbose {
respStr, _ := httputil.DumpResponse(resp, true)
log.Debug(string(respStr))
}
return
}
log.Debugf("%v via %s: OK.", fb.Addr, url)
}