| 9 | "github.com/onsi/ginkgo/v2" |
| 10 |
| 11 | "github.com/fatedier/frp/pkg/transport" |
| 12 | "github.com/fatedier/frp/test/e2e/framework" |
| 13 | "github.com/fatedier/frp/test/e2e/framework/consts" |
| 14 | "github.com/fatedier/frp/test/e2e/mock/server/httpserver" |
| 15 | "github.com/fatedier/frp/test/e2e/mock/server/streamserver" |
| 16 | "github.com/fatedier/frp/test/e2e/pkg/port" |
| 17 | "github.com/fatedier/frp/test/e2e/pkg/request" |
| 18 | ) |
| 19 |
| 20 | var _ = ginkgo.Describe("[Feature: Basic]", func() { |
| 21 | f := framework.NewDefaultFramework() |
| 22 |
| 23 | ginkgo.Describe("TCP && UDP", func() { |
| 24 | types := []string{"tcp", "udp"} |
| 25 | for _, t := range types { |
| 26 | proxyType := t |
| 27 | ginkgo.It(fmt.Sprintf("Expose a %s echo server", strings.ToUpper(proxyType)), func() { |
| 28 | serverConf := consts.LegacyDefaultServerConfig |
| 29 | var clientConf strings.Builder |
| 30 | clientConf.WriteString(consts.LegacyDefaultClientConfig) |
| 31 |
| 32 | localPortName := "" |
| 33 | protocol := "tcp" |
| 34 | switch proxyType { |
| 35 | case "tcp": |
| 36 | localPortName = framework.TCPEchoServerPort |
| 37 | protocol = "tcp" |
| 38 | case "udp": |
| 39 | localPortName = framework.UDPEchoServerPort |
| 40 | protocol = "udp" |
| 41 | } |
| 42 | getProxyConf := func(proxyName string, portName string, extra string) string { |
| 43 | return fmt.Sprintf(` |
| 44 | [%s] |
| 45 | type = %s |
| 46 | local_port = {{ .%s }} |
| 47 | remote_port = {{ .%s }} |
| 48 | `+extra, proxyName, proxyType, localPortName, portName) |
| 49 | } |
| 50 |
| 51 | tests := []struct { |
| 52 | proxyName string |
| 53 | portName string |
| 54 | extraConfig string |
| 55 | }{ |
| 56 | { |
| 57 | proxyName: "normal", |
| 58 | portName: port.GenName("Normal"), |
| 59 | }, |
| 60 | { |
| 61 | proxyName: "with-encryption", |
| 62 | portName: port.GenName("WithEncryption"), |
| 63 | extraConfig: "use_encryption = true", |
| 64 | }, |
| 65 | { |
| 66 | proxyName: "with-compression", |
| 67 | portName: port.GenName("WithCompression"), |
| 68 | extraConfig: "use_compression = true", |
| 69 | }, |
| 70 | { |
| 71 | proxyName: "with-encryption-and-compression", |
| 72 | portName: port.GenName("WithEncryptionAndCompression"), |
| 73 | extraConfig: ` |
| 74 | use_encryption = true |
| 75 | use_compression = true |
| 76 | `, |
| 77 | }, |
| 78 | } |
| 79 |
| 80 | // build all client config |
| 81 | for _, test := range tests { |
| 82 | clientConf.WriteString(getProxyConf(test.proxyName, test.portName, test.extraConfig) + "\n") |
| 83 | } |
| 84 | // run frps and frpc |
| 85 | f.RunProcesses(serverConf, []string{clientConf.String()}) |
| 86 |
| 87 | for _, test := range tests { |
| 88 | framework.NewRequestExpect(f). |
| 89 | Protocol(protocol). |
| 90 | PortName(test.portName). |
| 91 | Explain(test.proxyName). |
| 92 | Ensure() |
| 93 | } |
| 94 | }) |
| 95 | } |
| 96 | }) |
| 97 |
| 98 | ginkgo.Describe("HTTP", func() { |
| 99 | ginkgo.It("proxy to HTTP server", func() { |
| 100 | serverConf := consts.LegacyDefaultServerConfig |
| 101 | vhostHTTPPort := f.AllocPort() |
| 102 | serverConf += fmt.Sprintf(` |
| 103 | vhost_http_port = %d |
| 104 | `, vhostHTTPPort) |
| 105 |
| 106 | var clientConf strings.Builder |
| 107 | clientConf.WriteString(consts.LegacyDefaultClientConfig) |
| 108 |
| 109 | getProxyConf := func(proxyName string, customDomains string, extra string) string { |
| 110 | return fmt.Sprintf(` |
| 111 | [%s] |
| 112 | type = http |
| 113 | local_port = {{ .%s }} |
| 114 | custom_domains = %s |
| 115 | `+extra, proxyName, framework.HTTPSimpleServerPort, customDomains) |
| 116 | } |
| 117 |
| 118 | tests := []struct { |
| 119 | proxyName string |
| 120 | customDomains string |
| 121 | extraConfig string |
| 122 | }{ |
| 123 | { |
| 124 | proxyName: "normal", |
| 125 | }, |
| 126 | { |
| 127 | proxyName: "with-encryption", |
| 128 | extraConfig: "use_encryption = true", |
| 129 | }, |
| 130 | { |
| 131 | proxyName: "with-compression", |
| 132 | extraConfig: "use_compression = true", |
| 133 | }, |
| 134 | { |
| 135 | proxyName: "with-encryption-and-compression", |
| 136 | extraConfig: ` |
| 137 | use_encryption = true |
| 138 | use_compression = true |
| 139 | `, |
| 140 | }, |
| 141 | { |
| 142 | proxyName: "multiple-custom-domains", |
| 143 | customDomains: "a.example.com, b.example.com", |
| 144 | }, |
| 145 | } |
| 146 |
| 147 | // build all client config |
| 148 | for i, test := range tests { |
| 149 | if tests[i].customDomains == "" { |
| 150 | tests[i].customDomains = test.proxyName + ".example.com" |
| 151 | } |
| 152 | clientConf.WriteString(getProxyConf(test.proxyName, tests[i].customDomains, test.extraConfig) + "\n") |
| 153 | } |
| 154 | // run frps and frpc |
| 155 | f.RunProcesses(serverConf, []string{clientConf.String()}) |
| 156 |
| 157 | for _, test := range tests { |
| 158 | for domain := range strings.SplitSeq(test.customDomains, ",") { |
| 159 | domain = strings.TrimSpace(domain) |
| 160 | framework.NewRequestExpect(f). |
| 161 | Explain(test.proxyName + "-" + domain). |
| 162 | Port(vhostHTTPPort). |
| 163 | RequestModify(func(r *request.Request) { |
| 164 | r.HTTP().HTTPHost(domain) |
| 165 | }). |
| 166 | Ensure() |
| 167 | } |
| 168 | } |
| 169 |
| 170 | // not exist host |
| 171 | framework.NewRequestExpect(f). |
| 172 | Explain("not exist host"). |
| 173 | Port(vhostHTTPPort). |
| 174 | RequestModify(func(r *request.Request) { |
| 175 | r.HTTP().HTTPHost("not-exist.example.com") |
| 176 | }). |
| 177 | Ensure(framework.ExpectResponseCode(404)) |
| 178 | }) |
| 179 | }) |
| 180 |
| 181 | ginkgo.Describe("HTTPS", func() { |
| 182 | ginkgo.It("proxy to HTTPS server", func() { |
| 183 | serverConf := consts.LegacyDefaultServerConfig |
| 184 | vhostHTTPSPort := f.AllocPort() |
| 185 | serverConf += fmt.Sprintf(` |
| 186 | vhost_https_port = %d |
| 187 | `, vhostHTTPSPort) |
| 188 |
| 189 | localPort := f.AllocPort() |
| 190 | var clientConf strings.Builder |
| 191 | clientConf.WriteString(consts.LegacyDefaultClientConfig) |
| 192 | getProxyConf := func(proxyName string, customDomains string, extra string) string { |
| 193 | return fmt.Sprintf(` |
| 194 | [%s] |
| 195 | type = https |
| 196 | local_port = %d |
| 197 | custom_domains = %s |
| 198 | `+extra, proxyName, localPort, customDomains) |
| 199 | } |
| 200 |
| 201 | tests := []struct { |
| 202 | proxyName string |
| 203 | customDomains string |
| 204 | extraConfig string |
| 205 | }{ |
| 206 | { |
| 207 | proxyName: "normal", |
| 208 | }, |
| 209 | { |
| 210 | proxyName: "with-encryption", |
| 211 | extraConfig: "use_encryption = true", |
| 212 | }, |
| 213 | { |
| 214 | proxyName: "with-compression", |
| 215 | extraConfig: "use_compression = true", |
| 216 | }, |
| 217 | { |
| 218 | proxyName: "with-encryption-and-compression", |
| 219 | extraConfig: ` |
| 220 | use_encryption = true |
| 221 | use_compression = true |
| 222 | `, |
| 223 | }, |
| 224 | { |
| 225 | proxyName: "multiple-custom-domains", |
| 226 | customDomains: "a.example.com, b.example.com", |
| 227 | }, |
| 228 | } |
| 229 |
| 230 | // build all client config |
| 231 | for i, test := range tests { |
| 232 | if tests[i].customDomains == "" { |
| 233 | tests[i].customDomains = test.proxyName + ".example.com" |
| 234 | } |
| 235 | clientConf.WriteString(getProxyConf(test.proxyName, tests[i].customDomains, test.extraConfig) + "\n") |
| 236 | } |
| 237 | // run frps and frpc |
| 238 | f.RunProcesses(serverConf, []string{clientConf.String()}) |
| 239 |
| 240 | tlsConfig, err := transport.NewServerTLSConfig("", "", "") |
| 241 | framework.ExpectNoError(err) |
| 242 | localServer := httpserver.New( |
| 243 | httpserver.WithBindPort(localPort), |
| 244 | httpserver.WithTLSConfig(tlsConfig), |
| 245 | httpserver.WithResponse([]byte("test")), |
| 246 | ) |
| 247 | f.RunServer("", localServer) |
| 248 |
| 249 | for _, test := range tests { |
| 250 | for domain := range strings.SplitSeq(test.customDomains, ",") { |
| 251 | domain = strings.TrimSpace(domain) |
| 252 | framework.NewRequestExpect(f). |
| 253 | Explain(test.proxyName + "-" + domain). |
| 254 | Port(vhostHTTPSPort). |
| 255 | RequestModify(func(r *request.Request) { |
| 256 | r.HTTPS().HTTPHost(domain).TLSConfig(&tls.Config{ |
| 257 | ServerName: domain, |
| 258 | InsecureSkipVerify: true, |
| 259 | }) |
| 260 | }). |
| 261 | ExpectResp([]byte("test")). |
| 262 | Ensure() |
| 263 | } |
| 264 | } |
| 265 |
| 266 | // not exist host |
| 267 | notExistDomain := "not-exist.example.com" |
| 268 | framework.NewRequestExpect(f). |
| 269 | Explain("not exist host"). |
| 270 | Port(vhostHTTPSPort). |
| 271 | RequestModify(func(r *request.Request) { |
| 272 | r.HTTPS().HTTPHost(notExistDomain).TLSConfig(&tls.Config{ |
| 273 | ServerName: notExistDomain, |
| 274 | InsecureSkipVerify: true, |
| 275 | }) |
| 276 | }). |
| 277 | ExpectError(true). |
| 278 | Ensure() |
| 279 | }) |
| 280 | }) |
| 281 |
| 282 | ginkgo.Describe("STCP && SUDP && XTCP", func() { |
| 283 | types := []string{"stcp", "sudp", "xtcp"} |
| 284 | for _, t := range types { |
| 285 | proxyType := t |
| 286 | ginkgo.It(fmt.Sprintf("Expose echo server with %s", strings.ToUpper(proxyType)), func() { |
| 287 | serverConf := consts.LegacyDefaultServerConfig |
| 288 | var clientServerConf strings.Builder |
| 289 | clientServerConf.WriteString(consts.LegacyDefaultClientConfig + "\nuser = user1") |
| 290 | var clientVisitorConf strings.Builder |
| 291 | clientVisitorConf.WriteString(consts.LegacyDefaultClientConfig + "\nuser = user1") |
| 292 | var clientUser2VisitorConf strings.Builder |
| 293 | clientUser2VisitorConf.WriteString(consts.LegacyDefaultClientConfig + "\nuser = user2") |
| 294 |
| 295 | localPortName := "" |
| 296 | protocol := "tcp" |
| 297 | switch proxyType { |
| 298 | case "stcp": |
| 299 | localPortName = framework.TCPEchoServerPort |
| 300 | protocol = "tcp" |
| 301 | case "sudp": |
| 302 | localPortName = framework.UDPEchoServerPort |
| 303 | protocol = "udp" |
| 304 | case "xtcp": |
| 305 | localPortName = framework.TCPEchoServerPort |
| 306 | protocol = "tcp" |
| 307 | ginkgo.Skip("stun server is not stable") |
| 308 | } |
| 309 |
| 310 | correctSK := "abc" |
| 311 | wrongSK := "123" |
| 312 |
| 313 | getProxyServerConf := func(proxyName string, extra string) string { |
| 314 | return fmt.Sprintf(` |
| 315 | [%s] |
| 316 | type = %s |
| 317 | role = server |
| 318 | sk = %s |
| 319 | local_port = {{ .%s }} |
| 320 | `+extra, proxyName, proxyType, correctSK, localPortName) |
| 321 | } |
| 322 | getProxyVisitorConf := func(proxyName string, portName, visitorSK, extra string) string { |
| 323 | return fmt.Sprintf(` |
| 324 | [%s] |
| 325 | type = %s |
| 326 | role = visitor |
| 327 | server_name = %s |
| 328 | sk = %s |
| 329 | bind_port = {{ .%s }} |
| 330 | `+extra, proxyName, proxyType, proxyName, visitorSK, portName) |
| 331 | } |
| 332 |
| 333 | tests := []struct { |
| 334 | proxyName string |
| 335 | bindPortName string |
| 336 | visitorSK string |
| 337 | commonExtraConfig string |
| 338 | proxyExtraConfig string |
| 339 | visitorExtraConfig string |
| 340 | expectError bool |
| 341 | deployUser2Client bool |
| 342 | // skipXTCP is used to skip xtcp test case |
| 343 | skipXTCP bool |
| 344 | }{ |
| 345 | { |
| 346 | proxyName: "normal", |
| 347 | bindPortName: port.GenName("Normal"), |
| 348 | visitorSK: correctSK, |
| 349 | skipXTCP: true, |
| 350 | }, |
| 351 | { |
| 352 | proxyName: "with-encryption", |
| 353 | bindPortName: port.GenName("WithEncryption"), |
| 354 | visitorSK: correctSK, |
| 355 | commonExtraConfig: "use_encryption = true", |
| 356 | skipXTCP: true, |
| 357 | }, |
| 358 | { |
| 359 | proxyName: "with-compression", |
| 360 | bindPortName: port.GenName("WithCompression"), |
| 361 | visitorSK: correctSK, |
| 362 | commonExtraConfig: "use_compression = true", |
| 363 | skipXTCP: true, |
| 364 | }, |
| 365 | { |
| 366 | proxyName: "with-encryption-and-compression", |
| 367 | bindPortName: port.GenName("WithEncryptionAndCompression"), |
| 368 | visitorSK: correctSK, |
| 369 | commonExtraConfig: ` |
| 370 | use_encryption = true |
| 371 | use_compression = true |
| 372 | `, |
| 373 | skipXTCP: true, |
| 374 | }, |
| 375 | { |
| 376 | proxyName: "with-error-sk", |
| 377 | bindPortName: port.GenName("WithErrorSK"), |
| 378 | visitorSK: wrongSK, |
| 379 | expectError: true, |
| 380 | }, |
| 381 | { |
| 382 | proxyName: "allowed-user", |
| 383 | bindPortName: port.GenName("AllowedUser"), |
| 384 | visitorSK: correctSK, |
| 385 | proxyExtraConfig: "allow_users = another, user2", |
| 386 | visitorExtraConfig: "server_user = user1", |
| 387 | deployUser2Client: true, |
| 388 | }, |
| 389 | { |
| 390 | proxyName: "not-allowed-user", |
| 391 | bindPortName: port.GenName("NotAllowedUser"), |
| 392 | visitorSK: correctSK, |
| 393 | proxyExtraConfig: "allow_users = invalid", |
| 394 | visitorExtraConfig: "server_user = user1", |
| 395 | expectError: true, |
| 396 | }, |
| 397 | { |
| 398 | proxyName: "allow-all", |
| 399 | bindPortName: port.GenName("AllowAll"), |
| 400 | visitorSK: correctSK, |
| 401 | proxyExtraConfig: "allow_users = *", |
| 402 | visitorExtraConfig: "server_user = user1", |
| 403 | deployUser2Client: true, |
| 404 | }, |
| 405 | } |
| 406 |
| 407 | // build all client config |
| 408 | for _, test := range tests { |
| 409 | clientServerConf.WriteString(getProxyServerConf(test.proxyName, test.commonExtraConfig+"\n"+test.proxyExtraConfig) + "\n") |
| 410 | } |
| 411 | for _, test := range tests { |
| 412 | config := getProxyVisitorConf( |
| 413 | test.proxyName, test.bindPortName, test.visitorSK, test.commonExtraConfig+"\n"+test.visitorExtraConfig, |
| 414 | ) + "\n" |
| 415 | if test.deployUser2Client { |
| 416 | clientUser2VisitorConf.WriteString(config) |
| 417 | } else { |
| 418 | clientVisitorConf.WriteString(config) |
| 419 | } |
| 420 | } |
| 421 | // run frps and frpc |
| 422 | f.RunProcesses(serverConf, []string{clientServerConf.String(), clientVisitorConf.String(), clientUser2VisitorConf.String()}) |
| 423 |
| 424 | for _, test := range tests { |
| 425 | timeout := time.Second |
| 426 | if t == "xtcp" { |
| 427 | if test.skipXTCP { |
| 428 | continue |
| 429 | } |
| 430 | timeout = 10 * time.Second |
| 431 | } |
| 432 | framework.NewRequestExpect(f). |
| 433 | RequestModify(func(r *request.Request) { |
| 434 | r.Timeout(timeout) |
| 435 | }). |
| 436 | Protocol(protocol). |
| 437 | PortName(test.bindPortName). |
| 438 | Explain(test.proxyName). |
| 439 | ExpectError(test.expectError). |
| 440 | Ensure() |
| 441 | } |
| 442 | }) |
| 443 | } |
| 444 | }) |
| 445 |
| 446 | ginkgo.Describe("TCPMUX", func() { |
| 447 | ginkgo.It("Type tcpmux", func() { |
| 448 | serverConf := consts.LegacyDefaultServerConfig |
| 449 | var clientConf strings.Builder |
| 450 | clientConf.WriteString(consts.LegacyDefaultClientConfig) |
| 451 |
| 452 | tcpmuxHTTPConnectPortName := port.GenName("TCPMUX") |
| 453 | serverConf += fmt.Sprintf(` |
| 454 | tcpmux_httpconnect_port = {{ .%s }} |
| 455 | `, tcpmuxHTTPConnectPortName) |
| 456 |
| 457 | getProxyConf := func(proxyName string, extra string) string { |
| 458 | return fmt.Sprintf(` |
| 459 | [%s] |
| 460 | type = tcpmux |
| 461 | multiplexer = httpconnect |
| 462 | local_port = {{ .%s }} |
| 463 | custom_domains = %s |
| 464 | `+extra, proxyName, port.GenName(proxyName), proxyName) |
| 465 | } |
| 466 |
| 467 | tests := []struct { |
| 468 | proxyName string |
| 469 | extraConfig string |
| 470 | }{ |
| 471 | { |
| 472 | proxyName: "normal", |
| 473 | }, |
| 474 | { |
| 475 | proxyName: "with-encryption", |
| 476 | extraConfig: "use_encryption = true", |
| 477 | }, |
| 478 | { |
| 479 | proxyName: "with-compression", |
| 480 | extraConfig: "use_compression = true", |
| 481 | }, |
| 482 | { |
| 483 | proxyName: "with-encryption-and-compression", |
| 484 | extraConfig: ` |
| 485 | use_encryption = true |
| 486 | use_compression = true |
| 487 | `, |
| 488 | }, |
| 489 | } |
| 490 |
| 491 | // build all client config |
| 492 | for _, test := range tests { |
| 493 | clientConf.WriteString(getProxyConf(test.proxyName, test.extraConfig) + "\n") |
| 494 |
| 495 | localServer := streamserver.New(streamserver.TCP, streamserver.WithBindPort(f.AllocPort()), streamserver.WithRespContent([]byte(test.proxyName))) |
| 496 | f.RunServer(port.GenName(test.proxyName), localServer) |
| 497 | } |
| 498 |
| 499 | // run frps and frpc |
| 500 | f.RunProcesses(serverConf, []string{clientConf.String()}) |
| 501 |
| 502 | // Request without HTTP connect should get error |
| 503 | framework.NewRequestExpect(f). |
| 504 | PortName(tcpmuxHTTPConnectPortName). |
| 505 | ExpectError(true). |
| 506 | Explain("request without HTTP connect expect error"). |
| 507 | Ensure() |
| 508 |
| 509 | proxyURL := fmt.Sprintf("http://127.0.0.1:%d", f.PortByName(tcpmuxHTTPConnectPortName)) |
| 510 | // Request with incorrect connect hostname |
| 511 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 512 | r.Addr("invalid").Proxy(proxyURL) |
| 513 | }).ExpectError(true).Explain("request without HTTP connect expect error").Ensure() |
| 514 |
| 515 | // Request with correct connect hostname |
| 516 | for _, test := range tests { |
| 517 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 518 | r.Addr(test.proxyName).Proxy(proxyURL) |
| 519 | }).ExpectResp([]byte(test.proxyName)).Explain(test.proxyName).Ensure() |
| 520 | } |
| 521 | }) |
| 522 | }) |
| 523 | }) |