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
195
196
197
198
199
package proxy
import (
"context"
"crypto/x509"
"errors"
"fmt"
"net"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestParseExitProbeResponse(t *testing.T) {
t.Parallel()
tests := []struct {
name string
body string
wantIP string
wantCountry string
}{
{
name: "cloudflare trace",
body: "fl=123\nip=203.0.113.8\nloc=jp\ntls=TLSv1.3\n",
wantIP: "203.0.113.8",
wantCountry: "JP",
},
{
name: "json fields",
body: `{"ip":"2001:db8::8","country_code":"DE"}`,
wantIP: "2001:db8::8",
wantCountry: "DE",
},
{
name: "alternate json fields",
body: `{"query":"198.51.100.9","countryCode":"US"}`,
wantIP: "198.51.100.9",
wantCountry: "US",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseExitProbeResponse([]byte(tt.body))
if err != nil {
t.Fatalf("parseExitProbeResponse returned error: %v", err)
}
if got.IPv4 != tt.wantIP || got.CountryCode != tt.wantCountry {
t.Fatalf("expected %s/%s, got %s/%s", tt.wantIP, tt.wantCountry, got.IPv4, got.CountryCode)
}
})
}
}
func TestParseExitProbeResponseRejectsMissingFields(t *testing.T) {
t.Parallel()
if _, err := parseExitProbeResponse([]byte("ip=not-an-ip\nloc=USA\n")); err == nil {
t.Fatal("expected invalid probe response to be rejected")
}
}
func TestOpenTunnelContextClosesConnectionThatArrivesAfterCancellation(t *testing.T) {
t.Parallel()
client, peer := net.Pipe()
defer peer.Close()
tracked := &closeTrackingConn{Conn: client}
opener := &delayedTunnelOpener{
started: make(chan struct{}),
release: make(chan struct{}),
conn: tracked,
}
ctx, cancel := context.WithCancel(context.Background())
type result struct {
conn net.Conn
err error
}
resultCh := make(chan result, 1)
go func() {
conn, err := openTunnelContext(ctx, opener, "example.com:443")
resultCh <- result{conn: conn, err: err}
}()
<-opener.started
cancel()
got := <-resultCh
if got.conn != nil || !errors.Is(got.err, context.Canceled) {
t.Fatalf("expected canceled dial, got conn=%v err=%v", got.conn, got.err)
}
close(opener.release)
waitForCondition(t, time.Second, tracked.closed.Load)
}
// The shape of an api.ip.sb/geoip answer (documentation addresses).
func TestParseIPSBGeoIP(t *testing.T) {
body := `{"region":"North Holland","organization":"Example Hosting","region_code":"NH","isp":"Example ISP","city":"Amsterdam","asn_organization":"Example AS","asn":64500,"latitude":52.3,"ip":"2001:db8::1","continent_code":"EU","offset":3600,"country":"Netherlands","timezone":"Europe\/Amsterdam","country_code":"NL","longitude":4.9}`
got, err := parseExitProbeResponse([]byte(body))
if err != nil {
t.Fatal(err)
}
if got.IPv4 != "2001:db8::1" || got.CountryCode != "NL" || got.City != "Amsterdam" || got.Network != "Example ISP" || got.ASN != 64500 {
t.Fatalf("got %+v", got)
}
if got.Place() != "NL, Amsterdam, Example ISP" {
t.Fatalf("place %q", got.Place())
}
}
// tunnelToAddr opens every tunnel to one local server.
type tunnelToAddr string
func (a tunnelToAddr) OpenTunnel(string) (net.Conn, error) { return net.Dial("tcp", string(a)) }
// The IPv4 and IPv6 addresses are asked separately; a failing service is
// replaced by the next one of its family, and an answer of the wrong family
// does not count.
func TestVerifyExitFindsBothFamilies(t *testing.T) {
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/v4-busy":
http.Error(w, "busy", http.StatusServiceUnavailable)
case "/v4-trace":
fmt.Fprint(w, "ip=203.0.113.8\nloc=BE\n")
case "/v6-geoip":
fmt.Fprint(w, `{"ip":"2001:db8::8","country_code":"BE","city":"Brussels","isp":"Example VPN"}`)
case "/v6-wrong-family":
fmt.Fprint(w, `{"ip":"203.0.113.9","country_code":"BE"}`)
}
}))
defer server.Close()
pool := x509.NewCertPool()
pool.AddCert(server.Certificate())
exitProbeRoots = pool
oldV4, oldV6, oldDetails := exitCheckIPv4URLs, exitCheckIPv6URLs, exitDetailsURLs
t.Cleanup(func() {
exitProbeRoots, exitCheckIPv4URLs, exitCheckIPv6URLs, exitDetailsURLs = nil, oldV4, oldV6, oldDetails
})
opener := tunnelToAddr(server.Listener.Addr().String())
exitDetailsURLs = []string{"https://example.com/v4-busy"}
exitCheckIPv4URLs = []string{"https://example.com/v4-busy", "https://example.com/v4-trace"}
exitCheckIPv6URLs = []string{"https://example.com/v6-wrong-family", "https://example.com/v6-geoip"}
got, err := VerifyExit(opener, 5*time.Second)
if err != nil || got.IPv4 != "203.0.113.8" || got.IPv6 != "2001:db8::8" {
t.Fatalf("got %+v %v", got, err)
}
// The place comes from the answer that names the city.
if got.Place() != "BE, Brussels, Example VPN" || got.IP() != "203.0.113.8" {
t.Fatalf("place %q ip %q", got.Place(), got.IP())
}
// An exit without IPv6 still reports its IPv4 address.
exitCheckIPv6URLs = []string{"https://example.com/v6-wrong-family"}
got, err = VerifyExit(opener, 5*time.Second)
if err != nil || got.IPv4 != "203.0.113.8" || got.IPv6 != "" || got.CountryCode != "BE" {
t.Fatalf("without IPv6: %+v %v", got, err)
}
// Nothing answers: the error names both families.
exitCheckIPv4URLs = []string{"https://example.com/v4-busy"}
if _, err := VerifyExit(opener, 5*time.Second); err == nil || !strings.Contains(err.Error(), "IPv4") || !strings.Contains(err.Error(), "HTTP 503") {
t.Fatalf("expected both families to fail: %v", err)
}
}
// Cloudflare's country wins; ip.sb, asked alongside, adds only the city and
// network, even when its database names another country.
func TestVerifyExitTakesTheCountryFromCloudflare(t *testing.T) {
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/trace4":
fmt.Fprint(w, "ip=203.0.113.8\nloc=GB\n")
case "/trace6":
fmt.Fprint(w, "ip=2001:db8::8\nloc=GB\n")
case "/geoip":
fmt.Fprint(w, `{"ip":"203.0.113.8","country_code":"US","city":"London","isp":"Example VPN"}`)
}
}))
defer server.Close()
pool := x509.NewCertPool()
pool.AddCert(server.Certificate())
exitProbeRoots = pool
oldV4, oldV6, oldDetails := exitCheckIPv4URLs, exitCheckIPv6URLs, exitDetailsURLs
t.Cleanup(func() {
exitProbeRoots, exitCheckIPv4URLs, exitCheckIPv6URLs, exitDetailsURLs = nil, oldV4, oldV6, oldDetails
})
exitCheckIPv4URLs = []string{"https://example.com/trace4"}
exitCheckIPv6URLs = []string{"https://example.com/trace6"}
exitDetailsURLs = []string{"https://example.com/geoip"}
got, err := VerifyExit(tunnelToAddr(server.Listener.Addr().String()), 5*time.Second)
if err != nil || got.CountryCode != "GB" || got.Place() != "GB, London, Example VPN" {
t.Fatalf("got %+v %v", got, err)
}
}