-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_test.go
137 lines (124 loc) · 2.64 KB
/
service_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
package main
import (
"crypto/tls"
"io"
"net"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/chenen3/yeager/config"
)
func localAddr() string {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
panic(err)
}
ln.Close()
return ln.Addr().String()
}
func TestProxyToGRPC(t *testing.T) {
cc, sc, err := config.Generate("127.0.0.1")
if err != nil {
t.Fatal(err)
}
httpProxyAddr := localAddr()
socks5ProxyAddr := localAddr()
grpcAddr := localAddr()
for i := range cc.Listen {
switch cc.Listen[i].Protocol {
case config.ProtoHTTP:
cc.Listen[i].Address = httpProxyAddr
case config.ProtoSOCKS5:
cc.Listen[i].Address = socks5ProxyAddr
}
}
for i := range cc.Transport {
if cc.Transport[i].Protocol == config.ProtoGRPC {
cc.Transport[i].Address = grpcAddr
}
}
for i := range sc.Listen {
if sc.Listen[i].Protocol == config.ProtoGRPC {
sc.Listen[i].Address = grpcAddr
}
}
stop, err := start(cc)
if err != nil {
t.Fatal(err)
}
defer stop()
stop, err = start(sc)
if err != nil {
t.Fatal(err)
}
defer stop()
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = io.WriteString(w, "1")
}))
defer ts.Close()
t.Run("https2grpc", func(t *testing.T) {
pu, err := url.Parse("http://" + httpProxyAddr)
if err != nil {
t.Error(err)
return
}
client := http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(pu),
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Timeout: time.Second,
}
// the proxy services may not started yet
time.Sleep(time.Millisecond)
resp, err := client.Get(ts.URL)
if err != nil {
t.Error(err)
return
}
defer resp.Body.Close()
bs, err := io.ReadAll(resp.Body)
if err != nil {
t.Error(err)
return
}
if string(bs) != "1" {
t.Errorf("want 1, got %s", bs)
return
}
})
// how it works: client request -> socks proxy -> grpc client -> grpc server -> http test server
t.Run("socks2grpc", func(t *testing.T) {
pu, err := url.Parse("socks5://" + socks5ProxyAddr)
if err != nil {
t.Error(err)
return
}
client := http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(pu),
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Timeout: time.Second,
}
// the proxy services may not started yet
time.Sleep(time.Millisecond)
resp, err := client.Get(ts.URL)
if err != nil {
t.Error(err)
return
}
defer resp.Body.Close()
bs, err := io.ReadAll(resp.Body)
if err != nil {
t.Error(err)
return
}
if string(bs) != "1" {
t.Errorf("want 1, got %s", bs)
return
}
})
}