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