| 9 |
| 10 | "github.com/gorilla/websocket" |
| 11 | "github.com/onsi/ginkgo/v2" |
| 12 |
| 13 | "github.com/fatedier/frp/test/e2e/framework" |
| 14 | "github.com/fatedier/frp/test/e2e/framework/consts" |
| 15 | "github.com/fatedier/frp/test/e2e/mock/server/httpserver" |
| 16 | "github.com/fatedier/frp/test/e2e/pkg/request" |
| 17 | ) |
| 18 |
| 19 | var _ = ginkgo.Describe("[Feature: HTTP]", func() { |
| 20 | f := framework.NewDefaultFramework() |
| 21 |
| 22 | getDefaultServerConf := func(vhostHTTPPort int) string { |
| 23 | conf := consts.DefaultServerConfig + ` |
| 24 | vhostHTTPPort = %d |
| 25 | ` |
| 26 | return fmt.Sprintf(conf, vhostHTTPPort) |
| 27 | } |
| 28 | newHTTPServer := func(port int, respContent string) *httpserver.Server { |
| 29 | return httpserver.New( |
| 30 | httpserver.WithBindPort(port), |
| 31 | httpserver.WithHandler(framework.SpecifiedHTTPBodyHandler([]byte(respContent))), |
| 32 | ) |
| 33 | } |
| 34 |
| 35 | ginkgo.It("HTTP route by locations", func() { |
| 36 | vhostHTTPPort := f.AllocPort() |
| 37 | serverConf := getDefaultServerConf(vhostHTTPPort) |
| 38 |
| 39 | fooPort := f.AllocPort() |
| 40 | f.RunServer("", newHTTPServer(fooPort, "foo")) |
| 41 |
| 42 | barPort := f.AllocPort() |
| 43 | f.RunServer("", newHTTPServer(barPort, "bar")) |
| 44 |
| 45 | clientConf := consts.DefaultClientConfig |
| 46 | clientConf += fmt.Sprintf(` |
| 47 | [[proxies]] |
| 48 | name = "foo" |
| 49 | type = "http" |
| 50 | localPort = %d |
| 51 | customDomains = ["normal.example.com"] |
| 52 | locations = ["/","/foo"] |
| 53 |
| 54 | [[proxies]] |
| 55 | name = "bar" |
| 56 | type = "http" |
| 57 | localPort = %d |
| 58 | customDomains = ["normal.example.com"] |
| 59 | locations = ["/bar"] |
| 60 | `, fooPort, barPort) |
| 61 |
| 62 | f.RunProcesses(serverConf, []string{clientConf}) |
| 63 |
| 64 | tests := []struct { |
| 65 | path string |
| 66 | expectResp string |
| 67 | desc string |
| 68 | }{ |
| 69 | {path: "/foo", expectResp: "foo", desc: "foo path"}, |
| 70 | {path: "/bar", expectResp: "bar", desc: "bar path"}, |
| 71 | {path: "/other", expectResp: "foo", desc: "other path"}, |
| 72 | } |
| 73 |
| 74 | for _, test := range tests { |
| 75 | framework.NewRequestExpect(f).Explain(test.desc).Port(vhostHTTPPort). |
| 76 | RequestModify(func(r *request.Request) { |
| 77 | r.HTTP().HTTPHost("normal.example.com").HTTPPath(test.path) |
| 78 | }). |
| 79 | ExpectResp([]byte(test.expectResp)). |
| 80 | Ensure() |
| 81 | } |
| 82 | }) |
| 83 |
| 84 | ginkgo.It("HTTP route by HTTP user", func() { |
| 85 | vhostHTTPPort := f.AllocPort() |
| 86 | serverConf := getDefaultServerConf(vhostHTTPPort) |
| 87 |
| 88 | fooPort := f.AllocPort() |
| 89 | f.RunServer("", newHTTPServer(fooPort, "foo")) |
| 90 |
| 91 | barPort := f.AllocPort() |
| 92 | f.RunServer("", newHTTPServer(barPort, "bar")) |
| 93 |
| 94 | otherPort := f.AllocPort() |
| 95 | f.RunServer("", newHTTPServer(otherPort, "other")) |
| 96 |
| 97 | clientConf := consts.DefaultClientConfig |
| 98 | clientConf += fmt.Sprintf(` |
| 99 | [[proxies]] |
| 100 | name = "foo" |
| 101 | type = "http" |
| 102 | localPort = %d |
| 103 | customDomains = ["normal.example.com"] |
| 104 | routeByHTTPUser = "user1" |
| 105 |
| 106 | [[proxies]] |
| 107 | name = "bar" |
| 108 | type = "http" |
| 109 | localPort = %d |
| 110 | customDomains = ["normal.example.com"] |
| 111 | routeByHTTPUser = "user2" |
| 112 |
| 113 | [[proxies]] |
| 114 | name = "catchAll" |
| 115 | type = "http" |
| 116 | localPort = %d |
| 117 | customDomains = ["normal.example.com"] |
| 118 | `, fooPort, barPort, otherPort) |
| 119 |
| 120 | f.RunProcesses(serverConf, []string{clientConf}) |
| 121 |
| 122 | // user1 |
| 123 | framework.NewRequestExpect(f).Explain("user1").Port(vhostHTTPPort). |
| 124 | RequestModify(func(r *request.Request) { |
| 125 | r.HTTP().HTTPHost("normal.example.com").HTTPAuth("user1", "") |
| 126 | }). |
| 127 | ExpectResp([]byte("foo")). |
| 128 | Ensure() |
| 129 |
| 130 | // user2 |
| 131 | framework.NewRequestExpect(f).Explain("user2").Port(vhostHTTPPort). |
| 132 | RequestModify(func(r *request.Request) { |
| 133 | r.HTTP().HTTPHost("normal.example.com").HTTPAuth("user2", "") |
| 134 | }). |
| 135 | ExpectResp([]byte("bar")). |
| 136 | Ensure() |
| 137 |
| 138 | // other user |
| 139 | framework.NewRequestExpect(f).Explain("other user").Port(vhostHTTPPort). |
| 140 | RequestModify(func(r *request.Request) { |
| 141 | r.HTTP().HTTPHost("normal.example.com").HTTPAuth("user3", "") |
| 142 | }). |
| 143 | ExpectResp([]byte("other")). |
| 144 | Ensure() |
| 145 | }) |
| 146 |
| 147 | ginkgo.It("HTTP proxy mode uses proxy auth consistently", func() { |
| 148 | vhostHTTPPort := f.AllocPort() |
| 149 | serverConf := getDefaultServerConf(vhostHTTPPort) |
| 150 |
| 151 | backendPort := f.AllocPort() |
| 152 | f.RunServer("", newHTTPServer(backendPort, "PRIVATE")) |
| 153 |
| 154 | clientConf := consts.DefaultClientConfig |
| 155 | clientConf += fmt.Sprintf(` |
| 156 | [[proxies]] |
| 157 | name = "protected" |
| 158 | type = "http" |
| 159 | localPort = %d |
| 160 | customDomains = ["normal.example.com"] |
| 161 | routeByHTTPUser = "alice" |
| 162 | httpUser = "alice" |
| 163 | httpPassword = "secret" |
| 164 | `, backendPort) |
| 165 |
| 166 | f.RunProcesses(serverConf, []string{clientConf}) |
| 167 |
| 168 | proxyURLWithAuth := func(username, password string) string { |
| 169 | if username == "" { |
| 170 | return fmt.Sprintf("http://127.0.0.1:%d", vhostHTTPPort) |
| 171 | } |
| 172 | return fmt.Sprintf("http://%s:%s@127.0.0.1:%d", username, password, vhostHTTPPort) |
| 173 | } |
| 174 |
| 175 | framework.NewRequestExpect(f).Explain("direct no auth").Port(vhostHTTPPort). |
| 176 | RequestModify(func(r *request.Request) { |
| 177 | r.HTTP().HTTPHost("normal.example.com") |
| 178 | }). |
| 179 | Ensure(framework.ExpectResponseCode(http.StatusNotFound)) |
| 180 |
| 181 | framework.NewRequestExpect(f).Explain("direct correct auth").Port(vhostHTTPPort). |
| 182 | RequestModify(func(r *request.Request) { |
| 183 | r.HTTP().HTTPHost("normal.example.com").HTTPAuth("alice", "secret") |
| 184 | }). |
| 185 | ExpectResp([]byte("PRIVATE")). |
| 186 | Ensure() |
| 187 |
| 188 | framework.NewRequestExpect(f).Explain("direct wrong auth").Port(vhostHTTPPort). |
| 189 | RequestModify(func(r *request.Request) { |
| 190 | r.HTTP().HTTPHost("normal.example.com").HTTPAuth("alice", "wrong") |
| 191 | }). |
| 192 | Ensure(framework.ExpectResponseCode(http.StatusUnauthorized)) |
| 193 |
| 194 | framework.NewRequestExpect(f).Explain("proxy correct proxy auth"). |
| 195 | RequestModify(func(r *request.Request) { |
| 196 | r.HTTP().Addr("normal.example.com").Proxy(proxyURLWithAuth("alice", "secret")) |
| 197 | }). |
| 198 | ExpectResp([]byte("PRIVATE")). |
| 199 | Ensure() |
| 200 |
| 201 | framework.NewRequestExpect(f).Explain("proxy wrong proxy auth"). |
| 202 | RequestModify(func(r *request.Request) { |
| 203 | r.HTTP().Addr("normal.example.com").Proxy(proxyURLWithAuth("alice", "wrong")) |
| 204 | }). |
| 205 | Ensure(framework.ExpectResponseCode(http.StatusProxyAuthRequired)) |
| 206 |
| 207 | framework.NewRequestExpect(f).Explain("proxy request ignores authorization header"). |
| 208 | RequestModify(func(r *request.Request) { |
| 209 | r.HTTP().Addr("normal.example.com").Proxy(proxyURLWithAuth("", "")).HTTPAuth("alice", "secret") |
| 210 | }). |
| 211 | Ensure(framework.ExpectResponseCode(http.StatusProxyAuthRequired)) |
| 212 |
| 213 | framework.NewRequestExpect(f).Explain("proxy wrong proxy auth with correct authorization"). |
| 214 | RequestModify(func(r *request.Request) { |
| 215 | r.HTTP().Addr("normal.example.com").Proxy(proxyURLWithAuth("alice", "wrong")).HTTPAuth("alice", "secret") |
| 216 | }). |
| 217 | Ensure(framework.ExpectResponseCode(http.StatusProxyAuthRequired)) |
| 218 | }) |
| 219 |
| 220 | ginkgo.It("HTTP Basic Auth", func() { |
| 221 | vhostHTTPPort := f.AllocPort() |
| 222 | serverConf := getDefaultServerConf(vhostHTTPPort) |
| 223 |
| 224 | clientConf := consts.DefaultClientConfig |
| 225 | clientConf += fmt.Sprintf(` |
| 226 | [[proxies]] |
| 227 | name = "test" |
| 228 | type = "http" |
| 229 | localPort = {{ .%s }} |
| 230 | customDomains = ["normal.example.com"] |
| 231 | httpUser = "test" |
| 232 | httpPassword = "test" |
| 233 | `, framework.HTTPSimpleServerPort) |
| 234 |
| 235 | f.RunProcesses(serverConf, []string{clientConf}) |
| 236 |
| 237 | // not set auth header |
| 238 | framework.NewRequestExpect(f).Port(vhostHTTPPort). |
| 239 | RequestModify(func(r *request.Request) { |
| 240 | r.HTTP().HTTPHost("normal.example.com") |
| 241 | }). |
| 242 | Ensure(framework.ExpectResponseCode(401)) |
| 243 |
| 244 | // set incorrect auth header |
| 245 | framework.NewRequestExpect(f).Port(vhostHTTPPort). |
| 246 | RequestModify(func(r *request.Request) { |
| 247 | r.HTTP().HTTPHost("normal.example.com").HTTPAuth("test", "invalid") |
| 248 | }). |
| 249 | Ensure(framework.ExpectResponseCode(401)) |
| 250 |
| 251 | // set correct auth header |
| 252 | framework.NewRequestExpect(f).Port(vhostHTTPPort). |
| 253 | RequestModify(func(r *request.Request) { |
| 254 | r.HTTP().HTTPHost("normal.example.com").HTTPAuth("test", "test") |
| 255 | }). |
| 256 | Ensure() |
| 257 | }) |
| 258 |
| 259 | ginkgo.It("Wildcard domain", func() { |
| 260 | vhostHTTPPort := f.AllocPort() |
| 261 | serverConf := getDefaultServerConf(vhostHTTPPort) |
| 262 |
| 263 | clientConf := consts.DefaultClientConfig |
| 264 | clientConf += fmt.Sprintf(` |
| 265 | [[proxies]] |
| 266 | name = "test" |
| 267 | type = "http" |
| 268 | localPort = {{ .%s }} |
| 269 | customDomains = ["*.example.com"] |
| 270 | `, framework.HTTPSimpleServerPort) |
| 271 |
| 272 | f.RunProcesses(serverConf, []string{clientConf}) |
| 273 |
| 274 | // not match host |
| 275 | framework.NewRequestExpect(f).Port(vhostHTTPPort). |
| 276 | RequestModify(func(r *request.Request) { |
| 277 | r.HTTP().HTTPHost("not-match.test.com") |
| 278 | }). |
| 279 | Ensure(framework.ExpectResponseCode(404)) |
| 280 |
| 281 | // test.example.com match *.example.com |
| 282 | framework.NewRequestExpect(f).Port(vhostHTTPPort). |
| 283 | RequestModify(func(r *request.Request) { |
| 284 | r.HTTP().HTTPHost("test.example.com") |
| 285 | }). |
| 286 | Ensure() |
| 287 |
| 288 | // sub.test.example.com match *.example.com |
| 289 | framework.NewRequestExpect(f).Port(vhostHTTPPort). |
| 290 | RequestModify(func(r *request.Request) { |
| 291 | r.HTTP().HTTPHost("sub.test.example.com") |
| 292 | }). |
| 293 | Ensure() |
| 294 | }) |
| 295 |
| 296 | ginkgo.It("Subdomain", func() { |
| 297 | vhostHTTPPort := f.AllocPort() |
| 298 | serverConf := getDefaultServerConf(vhostHTTPPort) |
| 299 | serverConf += ` |
| 300 | subdomainHost = "example.com" |
| 301 | ` |
| 302 |
| 303 | fooPort := f.AllocPort() |
| 304 | f.RunServer("", newHTTPServer(fooPort, "foo")) |
| 305 |
| 306 | barPort := f.AllocPort() |
| 307 | f.RunServer("", newHTTPServer(barPort, "bar")) |
| 308 |
| 309 | clientConf := consts.DefaultClientConfig |
| 310 | clientConf += fmt.Sprintf(` |
| 311 | [[proxies]] |
| 312 | name = "foo" |
| 313 | type = "http" |
| 314 | localPort = %d |
| 315 | subdomain = "foo" |
| 316 |
| 317 | [[proxies]] |
| 318 | name = "bar" |
| 319 | type = "http" |
| 320 | localPort = %d |
| 321 | subdomain = "bar" |
| 322 | `, fooPort, barPort) |
| 323 |
| 324 | f.RunProcesses(serverConf, []string{clientConf}) |
| 325 |
| 326 | // foo |
| 327 | framework.NewRequestExpect(f).Explain("foo subdomain").Port(vhostHTTPPort). |
| 328 | RequestModify(func(r *request.Request) { |
| 329 | r.HTTP().HTTPHost("foo.example.com") |
| 330 | }). |
| 331 | ExpectResp([]byte("foo")). |
| 332 | Ensure() |
| 333 |
| 334 | // bar |
| 335 | framework.NewRequestExpect(f).Explain("bar subdomain").Port(vhostHTTPPort). |
| 336 | RequestModify(func(r *request.Request) { |
| 337 | r.HTTP().HTTPHost("bar.example.com") |
| 338 | }). |
| 339 | ExpectResp([]byte("bar")). |
| 340 | Ensure() |
| 341 | }) |
| 342 |
| 343 | ginkgo.It("Modify request headers", func() { |
| 344 | vhostHTTPPort := f.AllocPort() |
| 345 | serverConf := getDefaultServerConf(vhostHTTPPort) |
| 346 |
| 347 | localPort := f.AllocPort() |
| 348 | localServer := httpserver.New( |
| 349 | httpserver.WithBindPort(localPort), |
| 350 | httpserver.WithHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 351 | _, _ = w.Write([]byte(req.Header.Get("X-From-Where"))) |
| 352 | })), |
| 353 | ) |
| 354 | f.RunServer("", localServer) |
| 355 |
| 356 | clientConf := consts.DefaultClientConfig |
| 357 | clientConf += fmt.Sprintf(` |
| 358 | [[proxies]] |
| 359 | name = "test" |
| 360 | type = "http" |
| 361 | localPort = %d |
| 362 | customDomains = ["normal.example.com"] |
| 363 | requestHeaders.set.x-from-where = "frp" |
| 364 | `, localPort) |
| 365 |
| 366 | f.RunProcesses(serverConf, []string{clientConf}) |
| 367 |
| 368 | framework.NewRequestExpect(f).Port(vhostHTTPPort). |
| 369 | RequestModify(func(r *request.Request) { |
| 370 | r.HTTP().HTTPHost("normal.example.com") |
| 371 | }). |
| 372 | ExpectResp([]byte("frp")). // local http server will write this X-From-Where header to response body |
| 373 | Ensure() |
| 374 | }) |
| 375 |
| 376 | ginkgo.It("Modify response headers", func() { |
| 377 | vhostHTTPPort := f.AllocPort() |
| 378 | serverConf := getDefaultServerConf(vhostHTTPPort) |
| 379 |
| 380 | localPort := f.AllocPort() |
| 381 | localServer := httpserver.New( |
| 382 | httpserver.WithBindPort(localPort), |
| 383 | httpserver.WithHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 384 | w.WriteHeader(200) |
| 385 | })), |
| 386 | ) |
| 387 | f.RunServer("", localServer) |
| 388 |
| 389 | clientConf := consts.DefaultClientConfig |
| 390 | clientConf += fmt.Sprintf(` |
| 391 | [[proxies]] |
| 392 | name = "test" |
| 393 | type = "http" |
| 394 | localPort = %d |
| 395 | customDomains = ["normal.example.com"] |
| 396 | responseHeaders.set.x-from-where = "frp" |
| 397 | `, localPort) |
| 398 |
| 399 | f.RunProcesses(serverConf, []string{clientConf}) |
| 400 |
| 401 | framework.NewRequestExpect(f).Port(vhostHTTPPort). |
| 402 | RequestModify(func(r *request.Request) { |
| 403 | r.HTTP().HTTPHost("normal.example.com") |
| 404 | }). |
| 405 | Ensure(func(res *request.Response) bool { |
| 406 | return res.Header.Get("X-From-Where") == "frp" |
| 407 | }) |
| 408 | }) |
| 409 |
| 410 | ginkgo.It("Host Header Rewrite", func() { |
| 411 | vhostHTTPPort := f.AllocPort() |
| 412 | serverConf := getDefaultServerConf(vhostHTTPPort) |
| 413 |
| 414 | localPort := f.AllocPort() |
| 415 | localServer := httpserver.New( |
| 416 | httpserver.WithBindPort(localPort), |
| 417 | httpserver.WithHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 418 | _, _ = w.Write([]byte(req.Host)) |
| 419 | })), |
| 420 | ) |
| 421 | f.RunServer("", localServer) |
| 422 |
| 423 | clientConf := consts.DefaultClientConfig |
| 424 | clientConf += fmt.Sprintf(` |
| 425 | [[proxies]] |
| 426 | name = "test" |
| 427 | type = "http" |
| 428 | localPort = %d |
| 429 | customDomains = ["normal.example.com"] |
| 430 | hostHeaderRewrite = "rewrite.example.com" |
| 431 | `, localPort) |
| 432 |
| 433 | f.RunProcesses(serverConf, []string{clientConf}) |
| 434 |
| 435 | framework.NewRequestExpect(f).Port(vhostHTTPPort). |
| 436 | RequestModify(func(r *request.Request) { |
| 437 | r.HTTP().HTTPHost("normal.example.com") |
| 438 | }). |
| 439 | ExpectResp([]byte("rewrite.example.com")). // local http server will write host header to response body |
| 440 | Ensure() |
| 441 | }) |
| 442 |
| 443 | ginkgo.It("Websocket protocol", func() { |
| 444 | vhostHTTPPort := f.AllocPort() |
| 445 | serverConf := getDefaultServerConf(vhostHTTPPort) |
| 446 |
| 447 | upgrader := websocket.Upgrader{} |
| 448 |
| 449 | localPort := f.AllocPort() |
| 450 | localServer := httpserver.New( |
| 451 | httpserver.WithBindPort(localPort), |
| 452 | httpserver.WithHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 453 | c, err := upgrader.Upgrade(w, req, nil) |
| 454 | if err != nil { |
| 455 | return |
| 456 | } |
| 457 | defer c.Close() |
| 458 | for { |
| 459 | mt, message, err := c.ReadMessage() |
| 460 | if err != nil { |
| 461 | break |
| 462 | } |
| 463 | err = c.WriteMessage(mt, message) |
| 464 | if err != nil { |
| 465 | break |
| 466 | } |
| 467 | } |
| 468 | })), |
| 469 | ) |
| 470 |
| 471 | f.RunServer("", localServer) |
| 472 |
| 473 | clientConf := consts.DefaultClientConfig |
| 474 | clientConf += fmt.Sprintf(` |
| 475 | [[proxies]] |
| 476 | name = "test" |
| 477 | type = "http" |
| 478 | localPort = %d |
| 479 | customDomains = ["127.0.0.1"] |
| 480 | `, localPort) |
| 481 |
| 482 | f.RunProcesses(serverConf, []string{clientConf}) |
| 483 |
| 484 | u := url.URL{Scheme: "ws", Host: "127.0.0.1:" + strconv.Itoa(vhostHTTPPort)} |
| 485 | c, _, err := websocket.DefaultDialer.Dial(u.String(), nil) |
| 486 | framework.ExpectNoError(err) |
| 487 |
| 488 | err = c.WriteMessage(websocket.TextMessage, []byte(consts.TestString)) |
| 489 | framework.ExpectNoError(err) |
| 490 |
| 491 | _, msg, err := c.ReadMessage() |
| 492 | framework.ExpectNoError(err) |
| 493 | framework.ExpectEqualValues(consts.TestString, string(msg)) |
| 494 | }) |
| 495 |
| 496 | ginkgo.It("vhostHTTPTimeout", func() { |
| 497 | vhostHTTPPort := f.AllocPort() |
| 498 | serverConf := getDefaultServerConf(vhostHTTPPort) |
| 499 | serverConf += ` |
| 500 | vhostHTTPTimeout = 2 |
| 501 | ` |
| 502 |
| 503 | delayDuration := 0 * time.Second |
| 504 | localPort := f.AllocPort() |
| 505 | localServer := httpserver.New( |
| 506 | httpserver.WithBindPort(localPort), |
| 507 | httpserver.WithHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 508 | time.Sleep(delayDuration) |
| 509 | _, _ = w.Write([]byte(req.Host)) |
| 510 | })), |
| 511 | ) |
| 512 | f.RunServer("", localServer) |
| 513 |
| 514 | clientConf := consts.DefaultClientConfig |
| 515 | clientConf += fmt.Sprintf(` |
| 516 | [[proxies]] |
| 517 | name = "test" |
| 518 | type = "http" |
| 519 | localPort = %d |
| 520 | customDomains = ["normal.example.com"] |
| 521 | `, localPort) |
| 522 |
| 523 | f.RunProcesses(serverConf, []string{clientConf}) |
| 524 |
| 525 | framework.NewRequestExpect(f).Port(vhostHTTPPort). |
| 526 | RequestModify(func(r *request.Request) { |
| 527 | r.HTTP().HTTPHost("normal.example.com").HTTP().Timeout(time.Second) |
| 528 | }). |
| 529 | ExpectResp([]byte("normal.example.com")). |
| 530 | Ensure() |
| 531 |
| 532 | delayDuration = 3 * time.Second |
| 533 | framework.NewRequestExpect(f).Port(vhostHTTPPort). |
| 534 | RequestModify(func(r *request.Request) { |
| 535 | r.HTTP().HTTPHost("normal.example.com").HTTP().Timeout(5 * time.Second) |
| 536 | }). |
| 537 | Ensure(framework.ExpectResponseCode(504)) |
| 538 | }) |
| 539 | }) |