basic.go 7.2 KB

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