| 9 | "net/http" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 |
| 13 | "github.com/onsi/ginkgo/v2" |
| 14 | pp "github.com/pires/go-proxyproto" |
| 15 |
| 16 | "github.com/fatedier/frp/pkg/transport" |
| 17 | "github.com/fatedier/frp/pkg/util/log" |
| 18 | "github.com/fatedier/frp/test/e2e/framework" |
| 19 | "github.com/fatedier/frp/test/e2e/framework/consts" |
| 20 | "github.com/fatedier/frp/test/e2e/mock/server/httpserver" |
| 21 | "github.com/fatedier/frp/test/e2e/mock/server/streamserver" |
| 22 | "github.com/fatedier/frp/test/e2e/pkg/cert" |
| 23 | "github.com/fatedier/frp/test/e2e/pkg/port" |
| 24 | "github.com/fatedier/frp/test/e2e/pkg/request" |
| 25 | ) |
| 26 |
| 27 | var _ = ginkgo.Describe("[Feature: Client-Plugins]", func() { |
| 28 | f := framework.NewDefaultFramework() |
| 29 |
| 30 | ginkgo.Describe("UnixDomainSocket", func() { |
| 31 | ginkgo.It("Expose a unix domain socket echo server", func() { |
| 32 | serverConf := consts.DefaultServerConfig |
| 33 | var clientConf strings.Builder |
| 34 | clientConf.WriteString(consts.DefaultClientConfig) |
| 35 |
| 36 | getProxyConf := func(proxyName string, portName string, extra string) string { |
| 37 | return fmt.Sprintf(` |
| 38 | [[proxies]] |
| 39 | name = "%s" |
| 40 | type = "tcp" |
| 41 | remotePort = {{ .%s }} |
| 42 | `+extra, proxyName, portName) + fmt.Sprintf(` |
| 43 | [proxies.plugin] |
| 44 | type = "unix_domain_socket" |
| 45 | unixPath = "{{ .%s }}" |
| 46 | `, framework.UDSEchoServerAddr) |
| 47 | } |
| 48 |
| 49 | tests := []struct { |
| 50 | proxyName string |
| 51 | portName string |
| 52 | extraConfig string |
| 53 | }{ |
| 54 | { |
| 55 | proxyName: "normal", |
| 56 | portName: port.GenName("Normal"), |
| 57 | }, |
| 58 | { |
| 59 | proxyName: "with-encryption", |
| 60 | portName: port.GenName("WithEncryption"), |
| 61 | extraConfig: "transport.useEncryption = true", |
| 62 | }, |
| 63 | { |
| 64 | proxyName: "with-compression", |
| 65 | portName: port.GenName("WithCompression"), |
| 66 | extraConfig: "transport.useCompression = true", |
| 67 | }, |
| 68 | { |
| 69 | proxyName: "with-encryption-and-compression", |
| 70 | portName: port.GenName("WithEncryptionAndCompression"), |
| 71 | extraConfig: ` |
| 72 | transport.useEncryption = true |
| 73 | transport.useCompression = true |
| 74 | `, |
| 75 | }, |
| 76 | } |
| 77 |
| 78 | // build all client config |
| 79 | for _, test := range tests { |
| 80 | clientConf.WriteString(getProxyConf(test.proxyName, test.portName, test.extraConfig) + "\n") |
| 81 | } |
| 82 | // run frps and frpc |
| 83 | f.RunProcesses(serverConf, []string{clientConf.String()}) |
| 84 |
| 85 | for _, test := range tests { |
| 86 | framework.NewRequestExpect(f).Port(f.PortByName(test.portName)).Ensure() |
| 87 | } |
| 88 | }) |
| 89 | }) |
| 90 |
| 91 | ginkgo.It("http_proxy", func() { |
| 92 | serverConf := consts.DefaultServerConfig |
| 93 | clientConf := consts.DefaultClientConfig |
| 94 |
| 95 | remotePort := f.AllocPort() |
| 96 | clientConf += fmt.Sprintf(` |
| 97 | [[proxies]] |
| 98 | name = "tcp" |
| 99 | type = "tcp" |
| 100 | remotePort = %d |
| 101 | [proxies.plugin] |
| 102 | type = "http_proxy" |
| 103 | httpUser = "abc" |
| 104 | httpPassword = "123" |
| 105 | `, remotePort) |
| 106 |
| 107 | f.RunProcesses(serverConf, []string{clientConf}) |
| 108 |
| 109 | // http proxy, no auth info |
| 110 | framework.NewRequestExpect(f).PortName(framework.HTTPSimpleServerPort).RequestModify(func(r *request.Request) { |
| 111 | r.HTTP().Proxy("http://127.0.0.1:" + strconv.Itoa(remotePort)) |
| 112 | }).Ensure(framework.ExpectResponseCode(407)) |
| 113 |
| 114 | // http proxy, correct auth |
| 115 | framework.NewRequestExpect(f).PortName(framework.HTTPSimpleServerPort).RequestModify(func(r *request.Request) { |
| 116 | r.HTTP().Proxy("http://abc:123@127.0.0.1:" + strconv.Itoa(remotePort)) |
| 117 | }).Ensure() |
| 118 |
| 119 | // connect TCP server by CONNECT method |
| 120 | framework.NewRequestExpect(f).PortName(framework.TCPEchoServerPort).RequestModify(func(r *request.Request) { |
| 121 | r.TCP().Proxy("http://abc:123@127.0.0.1:" + strconv.Itoa(remotePort)) |
| 122 | }) |
| 123 | }) |
| 124 |
| 125 | ginkgo.It("socks5 proxy", func() { |
| 126 | serverConf := consts.DefaultServerConfig |
| 127 | clientConf := consts.DefaultClientConfig |
| 128 |
| 129 | remotePort := f.AllocPort() |
| 130 | clientConf += fmt.Sprintf(` |
| 131 | [[proxies]] |
| 132 | name = "tcp" |
| 133 | type = "tcp" |
| 134 | remotePort = %d |
| 135 | [proxies.plugin] |
| 136 | type = "socks5" |
| 137 | username = "abc" |
| 138 | password = "123" |
| 139 | `, remotePort) |
| 140 |
| 141 | f.RunProcesses(serverConf, []string{clientConf}) |
| 142 |
| 143 | // http proxy, no auth info |
| 144 | framework.NewRequestExpect(f).PortName(framework.TCPEchoServerPort).RequestModify(func(r *request.Request) { |
| 145 | r.TCP().Proxy("socks5://127.0.0.1:" + strconv.Itoa(remotePort)) |
| 146 | }).ExpectError(true).Ensure() |
| 147 |
| 148 | // http proxy, correct auth |
| 149 | framework.NewRequestExpect(f).PortName(framework.TCPEchoServerPort).RequestModify(func(r *request.Request) { |
| 150 | r.TCP().Proxy("socks5://abc:123@127.0.0.1:" + strconv.Itoa(remotePort)) |
| 151 | }).Ensure() |
| 152 | }) |
| 153 |
| 154 | ginkgo.It("static_file", func() { |
| 155 | vhostPort := f.AllocPort() |
| 156 | serverConf := consts.DefaultServerConfig + fmt.Sprintf(` |
| 157 | vhostHTTPPort = %d |
| 158 | `, vhostPort) |
| 159 | clientConf := consts.DefaultClientConfig |
| 160 |
| 161 | remotePort := f.AllocPort() |
| 162 | f.WriteTempFile("test_static_file", "foo") |
| 163 | clientConf += fmt.Sprintf(` |
| 164 | [[proxies]] |
| 165 | name = "tcp" |
| 166 | type = "tcp" |
| 167 | remotePort = %d |
| 168 | [proxies.plugin] |
| 169 | type = "static_file" |
| 170 | localPath = "%s" |
| 171 |
| 172 | [[proxies]] |
| 173 | name = "http" |
| 174 | type = "http" |
| 175 | customDomains = ["example.com"] |
| 176 | [proxies.plugin] |
| 177 | type = "static_file" |
| 178 | localPath = "%s" |
| 179 |
| 180 | [[proxies]] |
| 181 | name = "http-with-auth" |
| 182 | type = "http" |
| 183 | customDomains = ["other.example.com"] |
| 184 | [proxies.plugin] |
| 185 | type = "static_file" |
| 186 | localPath = "%s" |
| 187 | httpUser = "abc" |
| 188 | httpPassword = "123" |
| 189 | `, remotePort, f.TempDirectory, f.TempDirectory, f.TempDirectory) |
| 190 |
| 191 | f.RunProcesses(serverConf, []string{clientConf}) |
| 192 |
| 193 | // from tcp proxy |
| 194 | framework.NewRequestExpect(f).Request( |
| 195 | framework.NewHTTPRequest().HTTPPath("/test_static_file").Port(remotePort), |
| 196 | ).ExpectResp([]byte("foo")).Ensure() |
| 197 |
| 198 | // from http proxy without auth |
| 199 | framework.NewRequestExpect(f).Request( |
| 200 | framework.NewHTTPRequest().HTTPHost("example.com").HTTPPath("/test_static_file").Port(vhostPort), |
| 201 | ).ExpectResp([]byte("foo")).Ensure() |
| 202 |
| 203 | // from http proxy with auth |
| 204 | framework.NewRequestExpect(f).Request( |
| 205 | framework.NewHTTPRequest().HTTPHost("other.example.com").HTTPPath("/test_static_file").Port(vhostPort).HTTPAuth("abc", "123"), |
| 206 | ).ExpectResp([]byte("foo")).Ensure() |
| 207 | }) |
| 208 |
| 209 | ginkgo.It("http2https", func() { |
| 210 | serverConf := consts.DefaultServerConfig |
| 211 | vhostHTTPPort := f.AllocPort() |
| 212 | serverConf += fmt.Sprintf(` |
| 213 | vhostHTTPPort = %d |
| 214 | `, vhostHTTPPort) |
| 215 |
| 216 | localPort := f.AllocPort() |
| 217 | clientConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 218 | [[proxies]] |
| 219 | name = "http2https" |
| 220 | type = "http" |
| 221 | customDomains = ["example.com"] |
| 222 | [proxies.plugin] |
| 223 | type = "http2https" |
| 224 | localAddr = "127.0.0.1:%d" |
| 225 | `, localPort) |
| 226 |
| 227 | f.RunProcesses(serverConf, []string{clientConf}) |
| 228 |
| 229 | tlsConfig, err := transport.NewServerTLSConfig("", "", "") |
| 230 | framework.ExpectNoError(err) |
| 231 | localServer := httpserver.New( |
| 232 | httpserver.WithBindPort(localPort), |
| 233 | httpserver.WithTLSConfig(tlsConfig), |
| 234 | httpserver.WithResponse([]byte("test")), |
| 235 | ) |
| 236 | f.RunServer("", localServer) |
| 237 |
| 238 | framework.NewRequestExpect(f). |
| 239 | Port(vhostHTTPPort). |
| 240 | RequestModify(func(r *request.Request) { |
| 241 | r.HTTP().HTTPHost("example.com") |
| 242 | }). |
| 243 | ExpectResp([]byte("test")). |
| 244 | Ensure() |
| 245 | }) |
| 246 |
| 247 | ginkgo.It("https2http", func() { |
| 248 | generator := &cert.SelfSignedCertGenerator{} |
| 249 | artifacts, err := generator.Generate("example.com") |
| 250 | framework.ExpectNoError(err) |
| 251 | crtPath := f.WriteTempFile("server.crt", string(artifacts.Cert)) |
| 252 | keyPath := f.WriteTempFile("server.key", string(artifacts.Key)) |
| 253 |
| 254 | serverConf := consts.DefaultServerConfig |
| 255 | vhostHTTPSPort := f.AllocPort() |
| 256 | serverConf += fmt.Sprintf(` |
| 257 | vhostHTTPSPort = %d |
| 258 | `, vhostHTTPSPort) |
| 259 |
| 260 | localPort := f.AllocPort() |
| 261 | clientConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 262 | [[proxies]] |
| 263 | name = "https2http" |
| 264 | type = "https" |
| 265 | customDomains = ["example.com"] |
| 266 | [proxies.plugin] |
| 267 | type = "https2http" |
| 268 | localAddr = "127.0.0.1:%d" |
| 269 | crtPath = "%s" |
| 270 | keyPath = "%s" |
| 271 | `, localPort, crtPath, keyPath) |
| 272 |
| 273 | f.RunProcesses(serverConf, []string{clientConf}) |
| 274 |
| 275 | localServer := httpserver.New( |
| 276 | httpserver.WithBindPort(localPort), |
| 277 | httpserver.WithResponse([]byte("test")), |
| 278 | ) |
| 279 | f.RunServer("", localServer) |
| 280 |
| 281 | framework.NewRequestExpect(f). |
| 282 | Port(vhostHTTPSPort). |
| 283 | RequestModify(func(r *request.Request) { |
| 284 | r.HTTPS().HTTPHost("example.com").TLSConfig(&tls.Config{ |
| 285 | ServerName: "example.com", |
| 286 | InsecureSkipVerify: true, |
| 287 | }) |
| 288 | }). |
| 289 | ExpectResp([]byte("test")). |
| 290 | Ensure() |
| 291 | }) |
| 292 |
| 293 | ginkgo.It("https2https", func() { |
| 294 | generator := &cert.SelfSignedCertGenerator{} |
| 295 | artifacts, err := generator.Generate("example.com") |
| 296 | framework.ExpectNoError(err) |
| 297 | crtPath := f.WriteTempFile("server.crt", string(artifacts.Cert)) |
| 298 | keyPath := f.WriteTempFile("server.key", string(artifacts.Key)) |
| 299 |
| 300 | serverConf := consts.DefaultServerConfig |
| 301 | vhostHTTPSPort := f.AllocPort() |
| 302 | serverConf += fmt.Sprintf(` |
| 303 | vhostHTTPSPort = %d |
| 304 | `, vhostHTTPSPort) |
| 305 |
| 306 | localPort := f.AllocPort() |
| 307 | clientConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 308 | [[proxies]] |
| 309 | name = "https2https" |
| 310 | type = "https" |
| 311 | customDomains = ["example.com"] |
| 312 | [proxies.plugin] |
| 313 | type = "https2https" |
| 314 | localAddr = "127.0.0.1:%d" |
| 315 | crtPath = "%s" |
| 316 | keyPath = "%s" |
| 317 | `, localPort, crtPath, keyPath) |
| 318 |
| 319 | f.RunProcesses(serverConf, []string{clientConf}) |
| 320 |
| 321 | tlsConfig, err := transport.NewServerTLSConfig("", "", "") |
| 322 | framework.ExpectNoError(err) |
| 323 | localServer := httpserver.New( |
| 324 | httpserver.WithBindPort(localPort), |
| 325 | httpserver.WithResponse([]byte("test")), |
| 326 | httpserver.WithTLSConfig(tlsConfig), |
| 327 | ) |
| 328 | f.RunServer("", localServer) |
| 329 |
| 330 | framework.NewRequestExpect(f). |
| 331 | Port(vhostHTTPSPort). |
| 332 | RequestModify(func(r *request.Request) { |
| 333 | r.HTTPS().HTTPHost("example.com").TLSConfig(&tls.Config{ |
| 334 | ServerName: "example.com", |
| 335 | InsecureSkipVerify: true, |
| 336 | }) |
| 337 | }). |
| 338 | ExpectResp([]byte("test")). |
| 339 | Ensure() |
| 340 | }) |
| 341 |
| 342 | ginkgo.Describe("http2http", func() { |
| 343 | ginkgo.It("host header rewrite", func() { |
| 344 | serverConf := consts.DefaultServerConfig |
| 345 |
| 346 | localPort := f.AllocPort() |
| 347 | remotePort := f.AllocPort() |
| 348 | clientConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 349 | [[proxies]] |
| 350 | name = "http2http" |
| 351 | type = "tcp" |
| 352 | remotePort = %d |
| 353 | [proxies.plugin] |
| 354 | type = "http2http" |
| 355 | localAddr = "127.0.0.1:%d" |
| 356 | hostHeaderRewrite = "rewrite.test.com" |
| 357 | `, remotePort, localPort) |
| 358 |
| 359 | f.RunProcesses(serverConf, []string{clientConf}) |
| 360 |
| 361 | localServer := httpserver.New( |
| 362 | httpserver.WithBindPort(localPort), |
| 363 | httpserver.WithHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 364 | _, _ = w.Write([]byte(req.Host)) |
| 365 | })), |
| 366 | ) |
| 367 | f.RunServer("", localServer) |
| 368 |
| 369 | framework.NewRequestExpect(f). |
| 370 | Port(remotePort). |
| 371 | RequestModify(func(r *request.Request) { |
| 372 | r.HTTP().HTTPHost("example.com") |
| 373 | }). |
| 374 | ExpectResp([]byte("rewrite.test.com")). |
| 375 | Ensure() |
| 376 | }) |
| 377 |
| 378 | ginkgo.It("set request header", func() { |
| 379 | serverConf := consts.DefaultServerConfig |
| 380 |
| 381 | localPort := f.AllocPort() |
| 382 | remotePort := f.AllocPort() |
| 383 | clientConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 384 | [[proxies]] |
| 385 | name = "http2http" |
| 386 | type = "tcp" |
| 387 | remotePort = %d |
| 388 | [proxies.plugin] |
| 389 | type = "http2http" |
| 390 | localAddr = "127.0.0.1:%d" |
| 391 | requestHeaders.set.x-from-where = "frp" |
| 392 | `, remotePort, localPort) |
| 393 |
| 394 | f.RunProcesses(serverConf, []string{clientConf}) |
| 395 |
| 396 | localServer := httpserver.New( |
| 397 | httpserver.WithBindPort(localPort), |
| 398 | httpserver.WithHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 399 | _, _ = w.Write([]byte(req.Header.Get("x-from-where"))) |
| 400 | })), |
| 401 | ) |
| 402 | f.RunServer("", localServer) |
| 403 |
| 404 | framework.NewRequestExpect(f). |
| 405 | Port(remotePort). |
| 406 | RequestModify(func(r *request.Request) { |
| 407 | r.HTTP().HTTPHost("example.com") |
| 408 | }). |
| 409 | ExpectResp([]byte("frp")). |
| 410 | Ensure() |
| 411 | }) |
| 412 | }) |
| 413 |
| 414 | ginkgo.It("tls2raw", func() { |
| 415 | generator := &cert.SelfSignedCertGenerator{} |
| 416 | artifacts, err := generator.Generate("example.com") |
| 417 | framework.ExpectNoError(err) |
| 418 | crtPath := f.WriteTempFile("tls2raw_server.crt", string(artifacts.Cert)) |
| 419 | keyPath := f.WriteTempFile("tls2raw_server.key", string(artifacts.Key)) |
| 420 |
| 421 | serverConf := consts.DefaultServerConfig |
| 422 | vhostHTTPSPort := f.AllocPort() |
| 423 | serverConf += fmt.Sprintf(` |
| 424 | vhostHTTPSPort = %d |
| 425 | `, vhostHTTPSPort) |
| 426 |
| 427 | localPort := f.AllocPort() |
| 428 | clientConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 429 | [[proxies]] |
| 430 | name = "tls2raw-test" |
| 431 | type = "https" |
| 432 | customDomains = ["example.com"] |
| 433 | [proxies.plugin] |
| 434 | type = "tls2raw" |
| 435 | localAddr = "127.0.0.1:%d" |
| 436 | crtPath = "%s" |
| 437 | keyPath = "%s" |
| 438 | `, localPort, crtPath, keyPath) |
| 439 |
| 440 | f.RunProcesses(serverConf, []string{clientConf}) |
| 441 |
| 442 | localServer := httpserver.New( |
| 443 | httpserver.WithBindPort(localPort), |
| 444 | httpserver.WithResponse([]byte("test")), |
| 445 | ) |
| 446 | f.RunServer("", localServer) |
| 447 |
| 448 | framework.NewRequestExpect(f). |
| 449 | Port(vhostHTTPSPort). |
| 450 | RequestModify(func(r *request.Request) { |
| 451 | r.HTTPS().HTTPHost("example.com").TLSConfig(&tls.Config{ |
| 452 | ServerName: "example.com", |
| 453 | InsecureSkipVerify: true, |
| 454 | }) |
| 455 | }). |
| 456 | ExpectResp([]byte("test")). |
| 457 | Ensure() |
| 458 | }) |
| 459 |
| 460 | ginkgo.It("tls2raw with proxy protocol v2", func() { |
| 461 | generator := &cert.SelfSignedCertGenerator{} |
| 462 | artifacts, err := generator.Generate("example.com") |
| 463 | framework.ExpectNoError(err) |
| 464 | crtPath := f.WriteTempFile("tls2raw_proxy_protocol_server.crt", string(artifacts.Cert)) |
| 465 | keyPath := f.WriteTempFile("tls2raw_proxy_protocol_server.key", string(artifacts.Key)) |
| 466 |
| 467 | serverConf := consts.DefaultServerConfig |
| 468 | vhostHTTPSPort := f.AllocPort() |
| 469 | serverConf += fmt.Sprintf(` |
| 470 | vhostHTTPSPort = %d |
| 471 | `, vhostHTTPSPort) |
| 472 |
| 473 | localPort := f.AllocPort() |
| 474 | clientConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 475 | [[proxies]] |
| 476 | name = "tls2raw-proxy-protocol-test" |
| 477 | type = "https" |
| 478 | customDomains = ["example.com"] |
| 479 | transport.proxyProtocolVersion = "v2" |
| 480 | [proxies.plugin] |
| 481 | type = "tls2raw" |
| 482 | localAddr = "127.0.0.1:%d" |
| 483 | crtPath = "%s" |
| 484 | keyPath = "%s" |
| 485 | `, localPort, crtPath, keyPath) |
| 486 |
| 487 | f.RunProcesses(serverConf, []string{clientConf}) |
| 488 |
| 489 | localServer := streamserver.New(streamserver.TCP, streamserver.WithBindPort(localPort), |
| 490 | streamserver.WithCustomHandler(func(c net.Conn) { |
| 491 | defer c.Close() |
| 492 |
| 493 | writeResp := func(body string) { |
| 494 | _, _ = fmt.Fprintf(c, "HTTP/1.1 200 OK\r\nContent-Length: %d\r\nConnection: close\r\n\r\n%s", len(body), body) |
| 495 | } |
| 496 |
| 497 | rd := bufio.NewReader(c) |
| 498 | ppHeader, err := pp.Read(rd) |
| 499 | if err != nil { |
| 500 | log.Errorf("read proxy protocol error: %v", err) |
| 501 | writeResp("missing proxy protocol") |
| 502 | return |
| 503 | } |
| 504 | if ppHeader.Version != 2 { |
| 505 | log.Errorf("unexpected proxy protocol version: %d", ppHeader.Version) |
| 506 | writeResp("unexpected proxy protocol version") |
| 507 | return |
| 508 | } |
| 509 | srcAddr, ok := ppHeader.SourceAddr.(*net.TCPAddr) |
| 510 | if !ok || srcAddr.IP.String() != "127.0.0.1" { |
| 511 | log.Errorf("unexpected proxy protocol source address: %v", ppHeader.SourceAddr) |
| 512 | writeResp("unexpected proxy protocol source address") |
| 513 | return |
| 514 | } |
| 515 |
| 516 | req, err := http.ReadRequest(rd) |
| 517 | if err != nil { |
| 518 | log.Errorf("read http request after proxy protocol error: %v", err) |
| 519 | writeResp("missing http request") |
| 520 | return |
| 521 | } |
| 522 | _, _ = io.Copy(io.Discard, req.Body) |
| 523 | _ = req.Body.Close() |
| 524 |
| 525 | writeResp("test") |
| 526 | })) |
| 527 | f.RunServer("", localServer) |
| 528 |
| 529 | framework.NewRequestExpect(f). |
| 530 | Port(vhostHTTPSPort). |
| 531 | RequestModify(func(r *request.Request) { |
| 532 | r.HTTPS().HTTPHost("example.com").TLSConfig(&tls.Config{ |
| 533 | ServerName: "example.com", |
| 534 | InsecureSkipVerify: true, |
| 535 | }) |
| 536 | }). |
| 537 | ExpectResp([]byte("test")). |
| 538 | Ensure() |
| 539 | }) |
| 540 | }) |