server.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package basic
  2. import (
  3. "fmt"
  4. "net"
  5. "strconv"
  6. "github.com/onsi/ginkgo/v2"
  7. "github.com/fatedier/frp/test/e2e/framework"
  8. "github.com/fatedier/frp/test/e2e/framework/consts"
  9. "github.com/fatedier/frp/test/e2e/pkg/port"
  10. "github.com/fatedier/frp/test/e2e/pkg/request"
  11. )
  12. var _ = ginkgo.Describe("[Feature: Server Manager]", func() {
  13. f := framework.NewDefaultFramework()
  14. ginkgo.It("Ports Whitelist", func() {
  15. serverConf := consts.LegacyDefaultServerConfig
  16. clientConf := consts.LegacyDefaultClientConfig
  17. serverConf += `
  18. allow_ports = 10000-11000,11002,12000-13000
  19. `
  20. tcpPortName := port.GenName("TCP", port.WithRangePorts(10000, 11000))
  21. udpPortName := port.GenName("UDP", port.WithRangePorts(12000, 13000))
  22. clientConf += fmt.Sprintf(`
  23. [tcp-allowded-in-range]
  24. type = tcp
  25. local_port = {{ .%s }}
  26. remote_port = {{ .%s }}
  27. `, framework.TCPEchoServerPort, tcpPortName)
  28. clientConf += fmt.Sprintf(`
  29. [tcp-port-not-allowed]
  30. type = tcp
  31. local_port = {{ .%s }}
  32. remote_port = 11001
  33. `, framework.TCPEchoServerPort)
  34. clientConf += fmt.Sprintf(`
  35. [tcp-port-unavailable]
  36. type = tcp
  37. local_port = {{ .%s }}
  38. remote_port = {{ .%s }}
  39. `, framework.TCPEchoServerPort, consts.PortServerName)
  40. clientConf += fmt.Sprintf(`
  41. [udp-allowed-in-range]
  42. type = udp
  43. local_port = {{ .%s }}
  44. remote_port = {{ .%s }}
  45. `, framework.UDPEchoServerPort, udpPortName)
  46. clientConf += fmt.Sprintf(`
  47. [udp-port-not-allowed]
  48. type = udp
  49. local_port = {{ .%s }}
  50. remote_port = 11003
  51. `, framework.UDPEchoServerPort)
  52. f.RunProcesses([]string{serverConf}, []string{clientConf})
  53. // TCP
  54. // Allowed in range
  55. framework.NewRequestExpect(f).PortName(tcpPortName).Ensure()
  56. // Not Allowed
  57. framework.NewRequestExpect(f).Port(11001).ExpectError(true).Ensure()
  58. // Unavailable, already bind by frps
  59. framework.NewRequestExpect(f).PortName(consts.PortServerName).ExpectError(true).Ensure()
  60. // UDP
  61. // Allowed in range
  62. framework.NewRequestExpect(f).Protocol("udp").PortName(udpPortName).Ensure()
  63. // Not Allowed
  64. framework.NewRequestExpect(f).RequestModify(func(r *request.Request) {
  65. r.UDP().Port(11003)
  66. }).ExpectError(true).Ensure()
  67. })
  68. ginkgo.It("Alloc Random Port", func() {
  69. serverConf := consts.LegacyDefaultServerConfig
  70. clientConf := consts.LegacyDefaultClientConfig
  71. adminPort := f.AllocPort()
  72. clientConf += fmt.Sprintf(`
  73. admin_port = %d
  74. [tcp]
  75. type = tcp
  76. local_port = {{ .%s }}
  77. [udp]
  78. type = udp
  79. local_port = {{ .%s }}
  80. `, adminPort, framework.TCPEchoServerPort, framework.UDPEchoServerPort)
  81. f.RunProcesses([]string{serverConf}, []string{clientConf})
  82. client := f.APIClientForFrpc(adminPort)
  83. // tcp random port
  84. status, err := client.GetProxyStatus("tcp")
  85. framework.ExpectNoError(err)
  86. _, portStr, err := net.SplitHostPort(status.RemoteAddr)
  87. framework.ExpectNoError(err)
  88. port, err := strconv.Atoi(portStr)
  89. framework.ExpectNoError(err)
  90. framework.NewRequestExpect(f).Port(port).Ensure()
  91. // udp random port
  92. status, err = client.GetProxyStatus("udp")
  93. framework.ExpectNoError(err)
  94. _, portStr, err = net.SplitHostPort(status.RemoteAddr)
  95. framework.ExpectNoError(err)
  96. port, err = strconv.Atoi(portStr)
  97. framework.ExpectNoError(err)
  98. framework.NewRequestExpect(f).Protocol("udp").Port(port).Ensure()
  99. })
  100. ginkgo.It("Port Reuse", func() {
  101. serverConf := consts.LegacyDefaultServerConfig
  102. // Use same port as PortServer
  103. serverConf += fmt.Sprintf(`
  104. vhost_http_port = {{ .%s }}
  105. `, consts.PortServerName)
  106. clientConf := consts.LegacyDefaultClientConfig + fmt.Sprintf(`
  107. [http]
  108. type = http
  109. local_port = {{ .%s }}
  110. custom_domains = example.com
  111. `, framework.HTTPSimpleServerPort)
  112. f.RunProcesses([]string{serverConf}, []string{clientConf})
  113. framework.NewRequestExpect(f).RequestModify(func(r *request.Request) {
  114. r.HTTP().HTTPHost("example.com")
  115. }).PortName(consts.PortServerName).Ensure()
  116. })
  117. ginkgo.It("healthz", func() {
  118. serverConf := consts.LegacyDefaultServerConfig
  119. dashboardPort := f.AllocPort()
  120. // Use same port as PortServer
  121. serverConf += fmt.Sprintf(`
  122. vhost_http_port = {{ .%s }}
  123. dashboard_addr = 0.0.0.0
  124. dashboard_port = %d
  125. dashboard_user = admin
  126. dashboard_pwd = admin
  127. `, consts.PortServerName, dashboardPort)
  128. clientConf := consts.LegacyDefaultClientConfig + fmt.Sprintf(`
  129. [http]
  130. type = http
  131. local_port = {{ .%s }}
  132. custom_domains = example.com
  133. `, framework.HTTPSimpleServerPort)
  134. f.RunProcesses([]string{serverConf}, []string{clientConf})
  135. framework.NewRequestExpect(f).RequestModify(func(r *request.Request) {
  136. r.HTTP().HTTPPath("/healthz")
  137. }).Port(dashboardPort).ExpectResp([]byte("")).Ensure()
  138. framework.NewRequestExpect(f).RequestModify(func(r *request.Request) {
  139. r.HTTP().HTTPPath("/")
  140. }).Port(dashboardPort).
  141. Ensure(framework.ExpectResponseCode(401))
  142. })
  143. })