server.go 4.7 KB

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