1
0

server.go 5.0 KB

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