-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_test.go
156 lines (138 loc) · 3.75 KB
/
proxy_test.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
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package socks
import (
"bytes"
"context"
"errors"
"fmt"
"net"
"net/url"
"os"
"strings"
"testing"
)
type proxyFromEnvTest struct {
allProxyEnv string
noProxyEnv string
wantTypeOf Dialer
}
func (t proxyFromEnvTest) String() string {
var buf bytes.Buffer
space := func() {
if buf.Len() > 0 {
buf.WriteByte(' ')
}
}
if t.allProxyEnv != "" {
fmt.Fprintf(&buf, "all_proxy=%q", t.allProxyEnv)
}
if t.noProxyEnv != "" {
space()
fmt.Fprintf(&buf, "no_proxy=%q", t.noProxyEnv)
}
return strings.TrimSpace(buf.String())
}
func TestFromEnvironment(t *testing.T) {
ResetProxyEnv()
type dummyDialer struct {
direct
}
RegisterDialerType("irc", func(_ *url.URL, _ Dialer) (Dialer, error) {
return dummyDialer{}, nil
})
proxyFromEnvTests := []proxyFromEnvTest{
{allProxyEnv: "127.0.0.1:8080", noProxyEnv: "localhost, 127.0.0.1", wantTypeOf: direct{}},
{allProxyEnv: "ftp://example.com:8000", noProxyEnv: "localhost, 127.0.0.1", wantTypeOf: direct{}},
{allProxyEnv: "socks5://example.com:8080", noProxyEnv: "localhost, 127.0.0.1", wantTypeOf: &PerHost{}},
{allProxyEnv: "socks5h://example.com", wantTypeOf: &SocksDialer{}},
{allProxyEnv: "irc://example.com:8000", wantTypeOf: dummyDialer{}},
{noProxyEnv: "localhost, 127.0.0.1", wantTypeOf: direct{}},
{wantTypeOf: direct{}},
}
for _, tt := range proxyFromEnvTests {
os.Setenv("ALL_PROXY", tt.allProxyEnv)
os.Setenv("NO_PROXY", tt.noProxyEnv)
ResetCachedEnvironment()
d := FromEnvironment()
if got, want := fmt.Sprintf("%T", d), fmt.Sprintf("%T", tt.wantTypeOf); got != want {
t.Errorf("%v: got type = %T, want %T", tt, d, tt.wantTypeOf)
}
}
}
func TestFromURL(t *testing.T) {
ss, err := NewServer(NoAuthRequired, NoProxyRequired)
if err != nil {
t.Fatal(err)
}
defer ss.Close()
url, err := url.Parse("socks5://user:password@" + ss.Addr().String())
if err != nil {
t.Fatal(err)
}
proxy, err := FromURL(url, nil)
if err != nil {
t.Fatal(err)
}
c, err := proxy.Dial("tcp", "fqdn.doesnotexist:5963")
if err != nil {
t.Fatal(err)
}
c.Close()
}
func TestSOCKS5(t *testing.T) {
ss, err := NewServer(NoAuthRequired, NoProxyRequired)
if err != nil {
t.Fatal(err)
}
defer ss.Close()
proxy, err := SOCKS5("tcp", ss.Addr().String(), nil, nil)
if err != nil {
t.Fatal(err)
}
c, err := proxy.Dial("tcp", ss.TargetAddr().String())
if err != nil {
t.Fatal(err)
}
c.Close()
}
type funcFailDialer func(context.Context) error
func (f funcFailDialer) Dial(net, addr string) (net.Conn, error) {
panic("shouldn't see a call to Dial")
}
func (f funcFailDialer) DialContext(ctx context.Context, net, addr string) (net.Conn, error) {
return nil, f(ctx)
}
// Check that FromEnvironmentUsing uses our dialer.
func TestFromEnvironmentUsing(t *testing.T) {
ResetProxyEnv()
errFoo := errors.New("some error to check our dialer was used)")
type key string
ctx := context.WithValue(context.Background(), key("foo"), "bar")
dialer := FromEnvironmentUsing(funcFailDialer(func(ctx context.Context) error {
if got := ctx.Value(key("foo")); got != "bar" {
t.Errorf("Resolver context = %T %v, want %q", got, got, "bar")
}
return errFoo
}))
_, err := dialer.(ContextDialer).DialContext(ctx, "tcp", "foo.tld:123")
if err == nil {
t.Fatalf("unexpected success")
}
if !strings.Contains(err.Error(), errFoo.Error()) {
t.Errorf("got unexpected error %q; want substr %q", err, errFoo)
}
}
func ResetProxyEnv() {
for _, env := range []*envOnce{allProxyEnv, noProxyEnv} {
for _, v := range env.names {
os.Setenv(v, "")
}
}
ResetCachedEnvironment()
}
func ResetCachedEnvironment() {
allProxyEnv.reset()
noProxyEnv.reset()
}