File
Blob: tun/client/proxy_test.go
| 1 | package client |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "net" |
| 8 | "net/http" |
| 9 | "net/http/httptest" |
| 10 | "net/url" |
| 11 | "os" |
| 12 | "path/filepath" |
| 13 | "runtime" |
| 14 | "testing" |
| 15 | |
| 16 | "go.miragespace.co/specter/spec/chord" |
| 17 | "go.miragespace.co/specter/spec/mocks" |
| 18 | "go.miragespace.co/specter/spec/protocol" |
| 19 | "go.miragespace.co/specter/spec/rpc" |
| 20 | "go.miragespace.co/specter/util/pipe" |
| 21 | |
| 22 | "github.com/stretchr/testify/mock" |
| 23 | "github.com/stretchr/testify/require" |
| 24 | "github.com/zhangyunhao116/skipmap" |
| 25 | "go.uber.org/atomic" |
| 26 | "go.uber.org/zap/zaptest" |
| 27 | ) |
| 28 | |
| 29 | func randomPipeTarget(t *testing.T, pattern string) (string, string) { |
| 30 | t.Helper() |
| 31 | |
| 32 | file, err := os.CreateTemp("", pattern) |
| 33 | require.NoError(t, err) |
| 34 | |
| 35 | name := file.Name() |
| 36 | require.NoError(t, file.Close()) |
| 37 | require.NoError(t, os.Remove(name)) |
| 38 | |
| 39 | if runtime.GOOS == "windows" { |
| 40 | path := `\\.\pipe\` + filepath.Base(name) |
| 41 | return path, path |
| 42 | } |
| 43 | |
| 44 | t.Cleanup(func() { |
| 45 | _ = os.Remove(name) |
| 46 | }) |
| 47 | |
| 48 | return name, "unix://" + name |
| 49 | } |
| 50 | |
| 51 | func TestJustHTTPProxy(t *testing.T) { |
| 52 | as := require.New(t) |
| 53 | logger := zaptest.NewLogger(t) |
| 54 | |
| 55 | file, err := os.CreateTemp("", "client") |
| 56 | as.NoError(err) |
| 57 | defer os.Remove(file.Name()) |
| 58 | |
| 59 | ctx := t.Context() |
| 60 | |
| 61 | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 62 | fmt.Fprintf(w, "ok") |
| 63 | })) |
| 64 | defer ts.Close() |
| 65 | |
| 66 | token := &protocol.ClientToken{ |
| 67 | Token: []byte("test"), |
| 68 | } |
| 69 | cl := &protocol.Node{ |
| 70 | Id: chord.Random(), |
| 71 | } |
| 72 | |
| 73 | der, cert, key := makeCertificate(as, logger, cl, token, nil) |
| 74 | cfg := &Config{ |
| 75 | path: file.Name(), |
| 76 | Apex: testApex, |
| 77 | Certificate: cert, |
| 78 | PrivKey: key, |
| 79 | Tunnels: []Tunnel{ |
| 80 | { |
| 81 | Target: ts.URL, |
| 82 | }, |
| 83 | }, |
| 84 | } |
| 85 | as.NoError(cfg.validate()) |
| 86 | |
| 87 | m := func(s *mocks.TunnelService, t1 *mocks.MemoryTransport, publishCall *mock.Call) { |
| 88 | defaultNoHostnames(s) |
| 89 | transportHelper(t1, der) |
| 90 | } |
| 91 | |
| 92 | client, t2, assertion := setupClient(t, as, ctx, logger, nil, cfg, nil, m, false, 1) |
| 93 | defer assertion() |
| 94 | defer client.Close() |
| 95 | |
| 96 | client.Start(ctx) |
| 97 | |
| 98 | httpCl := &http.Client{ |
| 99 | Transport: &http.Transport{ |
| 100 | MaxIdleConnsPerHost: -1, |
| 101 | DisableKeepAlives: true, |
| 102 | DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { |
| 103 | c1, err := t2.DialStream(ctx, cl, protocol.Stream_DIRECT) |
| 104 | as.NoError(err) |
| 105 | as.NoError(rpc.Send(c1, &protocol.Link{ |
| 106 | Alpn: protocol.Link_HTTP, |
| 107 | Hostname: testHostname, |
| 108 | })) |
| 109 | return c1, nil |
| 110 | }, |
| 111 | }, |
| 112 | } |
| 113 | resp, err := httpCl.Get("http://test/") |
| 114 | as.NoError(err) |
| 115 | defer resp.Body.Close() |
| 116 | |
| 117 | buf, err := io.ReadAll(resp.Body) |
| 118 | as.NoError(err) |
| 119 | as.Equal("ok", string(buf)) |
| 120 | } |
| 121 | |
| 122 | func TestPipeHTTP(t *testing.T) { |
| 123 | as := require.New(t) |
| 124 | logger := zaptest.NewLogger(t) |
| 125 | |
| 126 | file, err := os.CreateTemp("", "client") |
| 127 | as.NoError(err) |
| 128 | defer os.Remove(file.Name()) |
| 129 | |
| 130 | ctx := t.Context() |
| 131 | |
| 132 | path, target := randomPipeTarget(t, "specterhttp-*") |
| 133 | |
| 134 | pipeListener, err := pipe.ListenPipe(path) |
| 135 | as.NoError(err) |
| 136 | defer pipeListener.Close() |
| 137 | |
| 138 | svc := &http.Server{ |
| 139 | Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 140 | fmt.Fprintf(w, "ok") |
| 141 | }), |
| 142 | } |
| 143 | go svc.Serve(pipeListener) |
| 144 | defer svc.Shutdown(ctx) |
| 145 | |
| 146 | token := &protocol.ClientToken{ |
| 147 | Token: []byte("test"), |
| 148 | } |
| 149 | cl := &protocol.Node{ |
| 150 | Id: chord.Random(), |
| 151 | } |
| 152 | |
| 153 | der, cert, key := makeCertificate(as, logger, cl, token, nil) |
| 154 | cfg := &Config{ |
| 155 | path: file.Name(), |
| 156 | Apex: testApex, |
| 157 | Certificate: cert, |
| 158 | PrivKey: key, |
| 159 | Tunnels: []Tunnel{ |
| 160 | { |
| 161 | Target: target, |
| 162 | }, |
| 163 | }, |
| 164 | } |
| 165 | as.NoError(cfg.validate()) |
| 166 | |
| 167 | m := func(s *mocks.TunnelService, t1 *mocks.MemoryTransport, publishCall *mock.Call) { |
| 168 | defaultNoHostnames(s) |
| 169 | transportHelper(t1, der) |
| 170 | } |
| 171 | |
| 172 | client, t2, assertion := setupClient(t, as, ctx, logger, nil, cfg, nil, m, false, 1) |
| 173 | defer assertion() |
| 174 | defer client.Close() |
| 175 | |
| 176 | client.Start(ctx) |
| 177 | |
| 178 | httpCl := &http.Client{ |
| 179 | Transport: &http.Transport{ |
| 180 | MaxIdleConnsPerHost: -1, |
| 181 | DisableKeepAlives: true, |
| 182 | DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { |
| 183 | c1, err := t2.DialStream(ctx, cl, protocol.Stream_DIRECT) |
| 184 | as.NoError(err) |
| 185 | as.NoError(rpc.Send(c1, &protocol.Link{ |
| 186 | Alpn: protocol.Link_HTTP, |
| 187 | Hostname: testHostname, |
| 188 | })) |
| 189 | return c1, nil |
| 190 | }, |
| 191 | }, |
| 192 | } |
| 193 | resp, err := httpCl.Get("http://test/") |
| 194 | as.NoError(err) |
| 195 | defer resp.Body.Close() |
| 196 | |
| 197 | buf, err := io.ReadAll(resp.Body) |
| 198 | as.NoError(err) |
| 199 | as.Equal("ok", string(buf)) |
| 200 | } |
| 201 | |
| 202 | func TestPipeTCP(t *testing.T) { |
| 203 | as := require.New(t) |
| 204 | logger := zaptest.NewLogger(t) |
| 205 | |
| 206 | file, err := os.CreateTemp("", "client") |
| 207 | as.NoError(err) |
| 208 | defer os.Remove(file.Name()) |
| 209 | |
| 210 | ctx := t.Context() |
| 211 | |
| 212 | path, target := randomPipeTarget(t, "spectertcp-*") |
| 213 | |
| 214 | pipeListener, err := pipe.ListenPipe(path) |
| 215 | as.NoError(err) |
| 216 | defer pipeListener.Close() |
| 217 | |
| 218 | go func() { |
| 219 | conn, err := pipeListener.Accept() |
| 220 | as.NoError(err) |
| 221 | conn.Write([]byte("hi")) |
| 222 | }() |
| 223 | |
| 224 | token := &protocol.ClientToken{ |
| 225 | Token: []byte("test"), |
| 226 | } |
| 227 | cl := &protocol.Node{ |
| 228 | Id: chord.Random(), |
| 229 | } |
| 230 | |
| 231 | der, cert, key := makeCertificate(as, logger, cl, token, nil) |
| 232 | cfg := &Config{ |
| 233 | path: file.Name(), |
| 234 | Apex: testApex, |
| 235 | Certificate: cert, |
| 236 | PrivKey: key, |
| 237 | Tunnels: []Tunnel{ |
| 238 | { |
| 239 | Target: target, |
| 240 | }, |
| 241 | }, |
| 242 | } |
| 243 | as.NoError(cfg.validate()) |
| 244 | |
| 245 | m := func(s *mocks.TunnelService, t1 *mocks.MemoryTransport, publishCall *mock.Call) { |
| 246 | defaultNoHostnames(s) |
| 247 | transportHelper(t1, der) |
| 248 | } |
| 249 | |
| 250 | client, t2, assertion := setupClient(t, as, ctx, logger, nil, cfg, nil, m, false, 1) |
| 251 | defer assertion() |
| 252 | defer client.Close() |
| 253 | |
| 254 | client.Start(ctx) |
| 255 | |
| 256 | conn, err := t2.DialStream(ctx, cl, protocol.Stream_DIRECT) |
| 257 | as.NoError(err) |
| 258 | as.NoError(rpc.Send(conn, &protocol.Link{ |
| 259 | Alpn: protocol.Link_TCP, |
| 260 | Hostname: testHostname, |
| 261 | })) |
| 262 | |
| 263 | status := &protocol.TunnelStatus{} |
| 264 | rpc.BoundedReceive(conn, status, 1024) |
| 265 | as.Equal(protocol.TunnelStatusCode_STATUS_OK, status.GetStatus()) |
| 266 | |
| 267 | buf := make([]byte, 2) |
| 268 | n, err := io.ReadFull(conn, buf) |
| 269 | as.NoError(err) |
| 270 | as.Equal(2, n) |
| 271 | as.Equal("hi", string(buf)) |
| 272 | } |
| 273 | |
| 274 | func TestHTTPProxyHostHeaderDefault(t *testing.T) { |
| 275 | as := require.New(t) |
| 276 | logger := zaptest.NewLogger(t) |
| 277 | |
| 278 | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 279 | fmt.Fprintf(w, "%s", r.Host) |
| 280 | })) |
| 281 | defer ts.Close() |
| 282 | |
| 283 | u, err := url.Parse(ts.URL) |
| 284 | as.NoError(err) |
| 285 | |
| 286 | c := &Client{ |
| 287 | ClientConfig: ClientConfig{ |
| 288 | Logger: logger, |
| 289 | }, |
| 290 | forwarder: &forwarder{ |
| 291 | logger: zaptest.NewLogger(t), |
| 292 | rootDomain: atomic.NewString(testApex), |
| 293 | routes: skipmap.NewString[*route](), |
| 294 | }, |
| 295 | } |
| 296 | |
| 297 | r := Tunnel{ |
| 298 | parsed: u, |
| 299 | } |
| 300 | |
| 301 | hp := c.newHTTPProxy(testHostname, r) |
| 302 | defer hp.close() |
| 303 | |
| 304 | req := httptest.NewRequest("GET", "http://example/", nil) |
| 305 | rr := httptest.NewRecorder() |
| 306 | hp.forwarder.Handler.ServeHTTP(rr, req) |
| 307 | resp := rr.Result() |
| 308 | defer resp.Body.Close() |
| 309 | body, err := io.ReadAll(resp.Body) |
| 310 | as.NoError(err) |
| 311 | as.Equal(u.Host, string(body)) |
| 312 | } |
| 313 | |
| 314 | func TestHTTPProxyHostHeaderHostnameMode(t *testing.T) { |
| 315 | as := require.New(t) |
| 316 | logger := zaptest.NewLogger(t) |
| 317 | |
| 318 | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 319 | fmt.Fprintf(w, "%s", r.Host) |
| 320 | })) |
| 321 | defer ts.Close() |
| 322 | |
| 323 | u, err := url.Parse(ts.URL) |
| 324 | as.NoError(err) |
| 325 | |
| 326 | c := &Client{ |
| 327 | ClientConfig: ClientConfig{ |
| 328 | Logger: logger, |
| 329 | }, |
| 330 | forwarder: &forwarder{ |
| 331 | logger: zaptest.NewLogger(t), |
| 332 | rootDomain: atomic.NewString(testApex), |
| 333 | routes: skipmap.NewString[*route](), |
| 334 | }, |
| 335 | } |
| 336 | |
| 337 | r := Tunnel{ |
| 338 | parsed: u, |
| 339 | ProxyHeaderMode: "hostname", |
| 340 | } |
| 341 | |
| 342 | hp := c.newHTTPProxy(testHostname, r) |
| 343 | defer hp.close() |
| 344 | |
| 345 | req := httptest.NewRequest("GET", "http://example/", nil) |
| 346 | rr := httptest.NewRecorder() |
| 347 | hp.forwarder.Handler.ServeHTTP(rr, req) |
| 348 | resp := rr.Result() |
| 349 | defer resp.Body.Close() |
| 350 | body, err := io.ReadAll(resp.Body) |
| 351 | as.NoError(err) |
| 352 | as.Equal(fmt.Sprintf("%s.%s", testHostname, testApex), string(body)) |
| 353 | } |
| 354 | |
| 355 | func TestHTTPProxyHostHeaderCustomMode(t *testing.T) { |
| 356 | as := require.New(t) |
| 357 | logger := zaptest.NewLogger(t) |
| 358 | |
| 359 | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 360 | fmt.Fprintf(w, "%s", r.Host) |
| 361 | })) |
| 362 | defer ts.Close() |
| 363 | |
| 364 | u, err := url.Parse(ts.URL) |
| 365 | as.NoError(err) |
| 366 | |
| 367 | c := &Client{ |
| 368 | ClientConfig: ClientConfig{ |
| 369 | Logger: logger, |
| 370 | }, |
| 371 | forwarder: &forwarder{ |
| 372 | logger: zaptest.NewLogger(t), |
| 373 | rootDomain: atomic.NewString(testApex), |
| 374 | routes: skipmap.NewString[*route](), |
| 375 | }, |
| 376 | } |
| 377 | |
| 378 | customHost := "custom.example.com" |
| 379 | r := Tunnel{ |
| 380 | parsed: u, |
| 381 | ProxyHeaderMode: "custom", |
| 382 | ProxyHeaderHost: customHost, |
| 383 | } |
| 384 | |
| 385 | hp := c.newHTTPProxy(testHostname, r) |
| 386 | defer hp.close() |
| 387 | |
| 388 | req := httptest.NewRequest("GET", "http://example/", nil) |
| 389 | rr := httptest.NewRecorder() |
| 390 | hp.forwarder.Handler.ServeHTTP(rr, req) |
| 391 | resp := rr.Result() |
| 392 | defer resp.Body.Close() |
| 393 | body, err := io.ReadAll(resp.Body) |
| 394 | as.NoError(err) |
| 395 | as.Equal(customHost, string(body)) |
| 396 | } |