server.go 5.0 KB

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