basic.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. package basic
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/fatedier/frp/test/e2e/framework"
  6. "github.com/fatedier/frp/test/e2e/framework/consts"
  7. "github.com/fatedier/frp/test/e2e/mock/server"
  8. "github.com/fatedier/frp/test/e2e/pkg/port"
  9. "github.com/fatedier/frp/test/e2e/pkg/request"
  10. . "github.com/onsi/ginkgo"
  11. )
  12. var _ = Describe("[Feature: Basic]", func() {
  13. f := framework.NewDefaultFramework()
  14. Describe("TCP && UDP", func() {
  15. types := []string{"tcp", "udp"}
  16. for _, t := range types {
  17. proxyType := t
  18. It(fmt.Sprintf("Expose a %s echo server", strings.ToUpper(proxyType)), func() {
  19. serverConf := consts.DefaultServerConfig
  20. clientConf := consts.DefaultClientConfig
  21. localPortName := ""
  22. protocol := "tcp"
  23. switch proxyType {
  24. case "tcp":
  25. localPortName = framework.TCPEchoServerPort
  26. protocol = "tcp"
  27. case "udp":
  28. localPortName = framework.UDPEchoServerPort
  29. protocol = "udp"
  30. }
  31. getProxyConf := func(proxyName string, portName string, extra string) string {
  32. return fmt.Sprintf(`
  33. [%s]
  34. type = %s
  35. local_port = {{ .%s }}
  36. remote_port = {{ .%s }}
  37. `+extra, proxyName, proxyType, localPortName, portName)
  38. }
  39. tests := []struct {
  40. proxyName string
  41. portName string
  42. extraConfig string
  43. }{
  44. {
  45. proxyName: "normal",
  46. portName: port.GenName("Normal"),
  47. },
  48. {
  49. proxyName: "with-encryption",
  50. portName: port.GenName("WithEncryption"),
  51. extraConfig: "use_encryption = true",
  52. },
  53. {
  54. proxyName: "with-compression",
  55. portName: port.GenName("WithCompression"),
  56. extraConfig: "use_compression = true",
  57. },
  58. {
  59. proxyName: "with-encryption-and-compression",
  60. portName: port.GenName("WithEncryptionAndCompression"),
  61. extraConfig: `
  62. use_encryption = true
  63. use_compression = true
  64. `,
  65. },
  66. }
  67. // build all client config
  68. for _, test := range tests {
  69. clientConf += getProxyConf(test.proxyName, test.portName, test.extraConfig) + "\n"
  70. }
  71. // run frps and frpc
  72. f.RunProcesses([]string{serverConf}, []string{clientConf})
  73. for _, test := range tests {
  74. framework.NewRequestExpect(f).
  75. RequestModify(framework.SetRequestProtocol(protocol)).
  76. PortName(test.portName).
  77. Explain(test.proxyName).
  78. Ensure()
  79. }
  80. })
  81. }
  82. })
  83. Describe("STCP && SUDP", func() {
  84. types := []string{"stcp", "sudp"}
  85. for _, t := range types {
  86. proxyType := t
  87. It(fmt.Sprintf("Expose echo server with %s", strings.ToUpper(proxyType)), func() {
  88. serverConf := consts.DefaultServerConfig
  89. clientServerConf := consts.DefaultClientConfig
  90. clientVisitorConf := consts.DefaultClientConfig
  91. localPortName := ""
  92. protocol := "tcp"
  93. switch proxyType {
  94. case "stcp":
  95. localPortName = framework.TCPEchoServerPort
  96. protocol = "tcp"
  97. case "sudp":
  98. localPortName = framework.UDPEchoServerPort
  99. protocol = "udp"
  100. }
  101. correctSK := "abc"
  102. wrongSK := "123"
  103. getProxyServerConf := func(proxyName string, extra string) string {
  104. return fmt.Sprintf(`
  105. [%s]
  106. type = %s
  107. role = server
  108. sk = %s
  109. local_port = {{ .%s }}
  110. `+extra, proxyName, proxyType, correctSK, localPortName)
  111. }
  112. getProxyVisitorConf := func(proxyName string, portName, visitorSK, extra string) string {
  113. return fmt.Sprintf(`
  114. [%s]
  115. type = %s
  116. role = visitor
  117. server_name = %s
  118. sk = %s
  119. bind_port = {{ .%s }}
  120. `+extra, proxyName, proxyType, proxyName, visitorSK, portName)
  121. }
  122. tests := []struct {
  123. proxyName string
  124. bindPortName string
  125. visitorSK string
  126. extraConfig string
  127. expectError bool
  128. }{
  129. {
  130. proxyName: "normal",
  131. bindPortName: port.GenName("Normal"),
  132. visitorSK: correctSK,
  133. },
  134. {
  135. proxyName: "with-encryption",
  136. bindPortName: port.GenName("WithEncryption"),
  137. visitorSK: correctSK,
  138. extraConfig: "use_encryption = true",
  139. },
  140. {
  141. proxyName: "with-compression",
  142. bindPortName: port.GenName("WithCompression"),
  143. visitorSK: correctSK,
  144. extraConfig: "use_compression = true",
  145. },
  146. {
  147. proxyName: "with-encryption-and-compression",
  148. bindPortName: port.GenName("WithEncryptionAndCompression"),
  149. visitorSK: correctSK,
  150. extraConfig: `
  151. use_encryption = true
  152. use_compression = true
  153. `,
  154. },
  155. {
  156. proxyName: "with-error-sk",
  157. bindPortName: port.GenName("WithErrorSK"),
  158. visitorSK: wrongSK,
  159. expectError: true,
  160. },
  161. }
  162. // build all client config
  163. for _, test := range tests {
  164. clientServerConf += getProxyServerConf(test.proxyName, test.extraConfig) + "\n"
  165. }
  166. for _, test := range tests {
  167. clientVisitorConf += getProxyVisitorConf(test.proxyName, test.bindPortName, test.visitorSK, test.extraConfig) + "\n"
  168. }
  169. // run frps and frpc
  170. f.RunProcesses([]string{serverConf}, []string{clientServerConf, clientVisitorConf})
  171. for _, test := range tests {
  172. framework.NewRequestExpect(f).
  173. RequestModify(framework.SetRequestProtocol(protocol)).
  174. PortName(test.bindPortName).
  175. Explain(test.proxyName).
  176. ExpectError(test.expectError).
  177. Ensure()
  178. }
  179. })
  180. }
  181. })
  182. Describe("TCPMUX", func() {
  183. It("Type tcpmux", func() {
  184. serverConf := consts.DefaultServerConfig
  185. clientConf := consts.DefaultClientConfig
  186. tcpmuxHTTPConnectPortName := port.GenName("TCPMUX")
  187. serverConf += fmt.Sprintf(`
  188. tcpmux_httpconnect_port = {{ .%s }}
  189. `, tcpmuxHTTPConnectPortName)
  190. getProxyConf := func(proxyName string, extra string) string {
  191. return fmt.Sprintf(`
  192. [%s]
  193. type = tcpmux
  194. multiplexer = httpconnect
  195. local_port = {{ .%s }}
  196. custom_domains = %s
  197. `+extra, proxyName, port.GenName(proxyName), proxyName)
  198. }
  199. tests := []struct {
  200. proxyName string
  201. extraConfig string
  202. }{
  203. {
  204. proxyName: "normal",
  205. },
  206. {
  207. proxyName: "with-encryption",
  208. extraConfig: "use_encryption = true",
  209. },
  210. {
  211. proxyName: "with-compression",
  212. extraConfig: "use_compression = true",
  213. },
  214. {
  215. proxyName: "with-encryption-and-compression",
  216. extraConfig: `
  217. use_encryption = true
  218. use_compression = true
  219. `,
  220. },
  221. }
  222. // build all client config
  223. for _, test := range tests {
  224. clientConf += getProxyConf(test.proxyName, test.extraConfig) + "\n"
  225. localServer := server.New(server.TCP, server.WithBindPort(f.AllocPort()), server.WithRespContent([]byte(test.proxyName)))
  226. f.RunServer(port.GenName(test.proxyName), localServer)
  227. }
  228. // run frps and frpc
  229. f.RunProcesses([]string{serverConf}, []string{clientConf})
  230. // Request without HTTP connect should get error
  231. framework.NewRequestExpect(f).
  232. PortName(tcpmuxHTTPConnectPortName).
  233. ExpectError(true).
  234. Explain("request without HTTP connect expect error").
  235. Ensure()
  236. proxyURL := fmt.Sprintf("http://127.0.0.1:%d", f.PortByName(tcpmuxHTTPConnectPortName))
  237. // Request with incorrect connect hostname
  238. framework.NewRequestExpect(f).RequestModify(func(r *request.Request) {
  239. r.Proxy(proxyURL, "invalid")
  240. }).ExpectError(true).Explain("request without HTTP connect expect error").Ensure()
  241. // Request with correct connect hostname
  242. for _, test := range tests {
  243. framework.NewRequestExpect(f).RequestModify(func(r *request.Request) {
  244. r.Proxy(proxyURL, test.proxyName)
  245. }).ExpectResp([]byte(test.proxyName)).Explain(test.proxyName).Ensure()
  246. }
  247. })
  248. })
  249. })