basic.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. package basic
  2. import (
  3. "crypto/tls"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "github.com/onsi/ginkgo/v2"
  8. "github.com/fatedier/frp/pkg/transport"
  9. "github.com/fatedier/frp/test/e2e/framework"
  10. "github.com/fatedier/frp/test/e2e/framework/consts"
  11. "github.com/fatedier/frp/test/e2e/mock/server/httpserver"
  12. "github.com/fatedier/frp/test/e2e/mock/server/streamserver"
  13. "github.com/fatedier/frp/test/e2e/pkg/port"
  14. "github.com/fatedier/frp/test/e2e/pkg/request"
  15. )
  16. var _ = ginkgo.Describe("[Feature: Basic]", func() {
  17. f := framework.NewDefaultFramework()
  18. ginkgo.Describe("TCP && UDP", func() {
  19. types := []string{"tcp", "udp"}
  20. for _, t := range types {
  21. proxyType := t
  22. ginkgo.It(fmt.Sprintf("Expose a %s echo server", strings.ToUpper(proxyType)), func() {
  23. serverConf := consts.DefaultServerConfig
  24. clientConf := consts.DefaultClientConfig
  25. localPortName := ""
  26. protocol := "tcp"
  27. switch proxyType {
  28. case "tcp":
  29. localPortName = framework.TCPEchoServerPort
  30. protocol = "tcp"
  31. case "udp":
  32. localPortName = framework.UDPEchoServerPort
  33. protocol = "udp"
  34. }
  35. getProxyConf := func(proxyName string, portName string, extra string) string {
  36. return fmt.Sprintf(`
  37. [%s]
  38. type = %s
  39. local_port = {{ .%s }}
  40. remote_port = {{ .%s }}
  41. `+extra, proxyName, proxyType, localPortName, portName)
  42. }
  43. tests := []struct {
  44. proxyName string
  45. portName string
  46. extraConfig string
  47. }{
  48. {
  49. proxyName: "normal",
  50. portName: port.GenName("Normal"),
  51. },
  52. {
  53. proxyName: "with-encryption",
  54. portName: port.GenName("WithEncryption"),
  55. extraConfig: "use_encryption = true",
  56. },
  57. {
  58. proxyName: "with-compression",
  59. portName: port.GenName("WithCompression"),
  60. extraConfig: "use_compression = true",
  61. },
  62. {
  63. proxyName: "with-encryption-and-compression",
  64. portName: port.GenName("WithEncryptionAndCompression"),
  65. extraConfig: `
  66. use_encryption = true
  67. use_compression = true
  68. `,
  69. },
  70. }
  71. // build all client config
  72. for _, test := range tests {
  73. clientConf += getProxyConf(test.proxyName, test.portName, test.extraConfig) + "\n"
  74. }
  75. // run frps and frpc
  76. f.RunProcesses([]string{serverConf}, []string{clientConf})
  77. for _, test := range tests {
  78. framework.NewRequestExpect(f).
  79. Protocol(protocol).
  80. PortName(test.portName).
  81. Explain(test.proxyName).
  82. Ensure()
  83. }
  84. })
  85. }
  86. })
  87. ginkgo.Describe("HTTP", func() {
  88. ginkgo.It("proxy to HTTP server", func() {
  89. serverConf := consts.DefaultServerConfig
  90. vhostHTTPPort := f.AllocPort()
  91. serverConf += fmt.Sprintf(`
  92. vhost_http_port = %d
  93. `, vhostHTTPPort)
  94. clientConf := consts.DefaultClientConfig
  95. getProxyConf := func(proxyName string, customDomains string, extra string) string {
  96. return fmt.Sprintf(`
  97. [%s]
  98. type = http
  99. local_port = {{ .%s }}
  100. custom_domains = %s
  101. `+extra, proxyName, framework.HTTPSimpleServerPort, customDomains)
  102. }
  103. tests := []struct {
  104. proxyName string
  105. customDomains string
  106. extraConfig string
  107. }{
  108. {
  109. proxyName: "normal",
  110. },
  111. {
  112. proxyName: "with-encryption",
  113. extraConfig: "use_encryption = true",
  114. },
  115. {
  116. proxyName: "with-compression",
  117. extraConfig: "use_compression = true",
  118. },
  119. {
  120. proxyName: "with-encryption-and-compression",
  121. extraConfig: `
  122. use_encryption = true
  123. use_compression = true
  124. `,
  125. },
  126. {
  127. proxyName: "multiple-custom-domains",
  128. customDomains: "a.example.com, b.example.com",
  129. },
  130. }
  131. // build all client config
  132. for i, test := range tests {
  133. if tests[i].customDomains == "" {
  134. tests[i].customDomains = test.proxyName + ".example.com"
  135. }
  136. clientConf += getProxyConf(test.proxyName, tests[i].customDomains, test.extraConfig) + "\n"
  137. }
  138. // run frps and frpc
  139. f.RunProcesses([]string{serverConf}, []string{clientConf})
  140. for _, test := range tests {
  141. for _, domain := range strings.Split(test.customDomains, ",") {
  142. domain = strings.TrimSpace(domain)
  143. framework.NewRequestExpect(f).
  144. Explain(test.proxyName + "-" + domain).
  145. Port(vhostHTTPPort).
  146. RequestModify(func(r *request.Request) {
  147. r.HTTP().HTTPHost(domain)
  148. }).
  149. Ensure()
  150. }
  151. }
  152. // not exist host
  153. framework.NewRequestExpect(f).
  154. Explain("not exist host").
  155. Port(vhostHTTPPort).
  156. RequestModify(func(r *request.Request) {
  157. r.HTTP().HTTPHost("not-exist.example.com")
  158. }).
  159. Ensure(framework.ExpectResponseCode(404))
  160. })
  161. })
  162. ginkgo.Describe("HTTPS", func() {
  163. ginkgo.It("proxy to HTTPS server", func() {
  164. serverConf := consts.DefaultServerConfig
  165. vhostHTTPSPort := f.AllocPort()
  166. serverConf += fmt.Sprintf(`
  167. vhost_https_port = %d
  168. `, vhostHTTPSPort)
  169. localPort := f.AllocPort()
  170. clientConf := consts.DefaultClientConfig
  171. getProxyConf := func(proxyName string, customDomains string, extra string) string {
  172. return fmt.Sprintf(`
  173. [%s]
  174. type = https
  175. local_port = %d
  176. custom_domains = %s
  177. `+extra, proxyName, localPort, customDomains)
  178. }
  179. tests := []struct {
  180. proxyName string
  181. customDomains string
  182. extraConfig string
  183. }{
  184. {
  185. proxyName: "normal",
  186. },
  187. {
  188. proxyName: "with-encryption",
  189. extraConfig: "use_encryption = true",
  190. },
  191. {
  192. proxyName: "with-compression",
  193. extraConfig: "use_compression = true",
  194. },
  195. {
  196. proxyName: "with-encryption-and-compression",
  197. extraConfig: `
  198. use_encryption = true
  199. use_compression = true
  200. `,
  201. },
  202. {
  203. proxyName: "multiple-custom-domains",
  204. customDomains: "a.example.com, b.example.com",
  205. },
  206. }
  207. // build all client config
  208. for i, test := range tests {
  209. if tests[i].customDomains == "" {
  210. tests[i].customDomains = test.proxyName + ".example.com"
  211. }
  212. clientConf += getProxyConf(test.proxyName, tests[i].customDomains, test.extraConfig) + "\n"
  213. }
  214. // run frps and frpc
  215. f.RunProcesses([]string{serverConf}, []string{clientConf})
  216. tlsConfig, err := transport.NewServerTLSConfig("", "", "")
  217. framework.ExpectNoError(err)
  218. localServer := httpserver.New(
  219. httpserver.WithBindPort(localPort),
  220. httpserver.WithTLSConfig(tlsConfig),
  221. httpserver.WithResponse([]byte("test")),
  222. )
  223. f.RunServer("", localServer)
  224. for _, test := range tests {
  225. for _, domain := range strings.Split(test.customDomains, ",") {
  226. domain = strings.TrimSpace(domain)
  227. framework.NewRequestExpect(f).
  228. Explain(test.proxyName + "-" + domain).
  229. Port(vhostHTTPSPort).
  230. RequestModify(func(r *request.Request) {
  231. r.HTTPS().HTTPHost(domain).TLSConfig(&tls.Config{
  232. ServerName: domain,
  233. InsecureSkipVerify: true,
  234. })
  235. }).
  236. ExpectResp([]byte("test")).
  237. Ensure()
  238. }
  239. }
  240. // not exist host
  241. notExistDomain := "not-exist.example.com"
  242. framework.NewRequestExpect(f).
  243. Explain("not exist host").
  244. Port(vhostHTTPSPort).
  245. RequestModify(func(r *request.Request) {
  246. r.HTTPS().HTTPHost(notExistDomain).TLSConfig(&tls.Config{
  247. ServerName: notExistDomain,
  248. InsecureSkipVerify: true,
  249. })
  250. }).
  251. ExpectError(true).
  252. Ensure()
  253. })
  254. })
  255. ginkgo.Describe("STCP && SUDP && XTCP", func() {
  256. types := []string{"stcp", "sudp", "xtcp"}
  257. for _, t := range types {
  258. proxyType := t
  259. ginkgo.It(fmt.Sprintf("Expose echo server with %s", strings.ToUpper(proxyType)), func() {
  260. serverConf := consts.DefaultServerConfig
  261. clientServerConf := consts.DefaultClientConfig
  262. clientVisitorConf := consts.DefaultClientConfig
  263. localPortName := ""
  264. protocol := "tcp"
  265. switch proxyType {
  266. case "stcp":
  267. localPortName = framework.TCPEchoServerPort
  268. protocol = "tcp"
  269. case "sudp":
  270. localPortName = framework.UDPEchoServerPort
  271. protocol = "udp"
  272. case "xtcp":
  273. localPortName = framework.TCPEchoServerPort
  274. protocol = "tcp"
  275. }
  276. correctSK := "abc"
  277. wrongSK := "123"
  278. getProxyServerConf := func(proxyName string, extra string) string {
  279. return fmt.Sprintf(`
  280. [%s]
  281. type = %s
  282. role = server
  283. sk = %s
  284. local_port = {{ .%s }}
  285. `+extra, proxyName, proxyType, correctSK, localPortName)
  286. }
  287. getProxyVisitorConf := func(proxyName string, portName, visitorSK, extra string) string {
  288. return fmt.Sprintf(`
  289. [%s]
  290. type = %s
  291. role = visitor
  292. server_name = %s
  293. sk = %s
  294. bind_port = {{ .%s }}
  295. `+extra, proxyName, proxyType, proxyName, visitorSK, portName)
  296. }
  297. tests := []struct {
  298. proxyName string
  299. bindPortName string
  300. visitorSK string
  301. extraConfig string
  302. expectError bool
  303. }{
  304. {
  305. proxyName: "normal",
  306. bindPortName: port.GenName("Normal"),
  307. visitorSK: correctSK,
  308. },
  309. {
  310. proxyName: "with-encryption",
  311. bindPortName: port.GenName("WithEncryption"),
  312. visitorSK: correctSK,
  313. extraConfig: "use_encryption = true",
  314. },
  315. {
  316. proxyName: "with-compression",
  317. bindPortName: port.GenName("WithCompression"),
  318. visitorSK: correctSK,
  319. extraConfig: "use_compression = true",
  320. },
  321. {
  322. proxyName: "with-encryption-and-compression",
  323. bindPortName: port.GenName("WithEncryptionAndCompression"),
  324. visitorSK: correctSK,
  325. extraConfig: `
  326. use_encryption = true
  327. use_compression = true
  328. `,
  329. },
  330. {
  331. proxyName: "with-error-sk",
  332. bindPortName: port.GenName("WithErrorSK"),
  333. visitorSK: wrongSK,
  334. expectError: true,
  335. },
  336. }
  337. // build all client config
  338. for _, test := range tests {
  339. clientServerConf += getProxyServerConf(test.proxyName, test.extraConfig) + "\n"
  340. }
  341. for _, test := range tests {
  342. clientVisitorConf += getProxyVisitorConf(test.proxyName, test.bindPortName, test.visitorSK, test.extraConfig) + "\n"
  343. }
  344. // run frps and frpc
  345. f.RunProcesses([]string{serverConf}, []string{clientServerConf, clientVisitorConf})
  346. for _, test := range tests {
  347. framework.NewRequestExpect(f).
  348. RequestModify(func(r *request.Request) {
  349. r.Timeout(5 * time.Second)
  350. }).
  351. Protocol(protocol).
  352. PortName(test.bindPortName).
  353. Explain(test.proxyName).
  354. ExpectError(test.expectError).
  355. Ensure()
  356. }
  357. })
  358. }
  359. })
  360. ginkgo.Describe("TCPMUX", func() {
  361. ginkgo.It("Type tcpmux", func() {
  362. serverConf := consts.DefaultServerConfig
  363. clientConf := consts.DefaultClientConfig
  364. tcpmuxHTTPConnectPortName := port.GenName("TCPMUX")
  365. serverConf += fmt.Sprintf(`
  366. tcpmux_httpconnect_port = {{ .%s }}
  367. `, tcpmuxHTTPConnectPortName)
  368. getProxyConf := func(proxyName string, extra string) string {
  369. return fmt.Sprintf(`
  370. [%s]
  371. type = tcpmux
  372. multiplexer = httpconnect
  373. local_port = {{ .%s }}
  374. custom_domains = %s
  375. `+extra, proxyName, port.GenName(proxyName), proxyName)
  376. }
  377. tests := []struct {
  378. proxyName string
  379. extraConfig string
  380. }{
  381. {
  382. proxyName: "normal",
  383. },
  384. {
  385. proxyName: "with-encryption",
  386. extraConfig: "use_encryption = true",
  387. },
  388. {
  389. proxyName: "with-compression",
  390. extraConfig: "use_compression = true",
  391. },
  392. {
  393. proxyName: "with-encryption-and-compression",
  394. extraConfig: `
  395. use_encryption = true
  396. use_compression = true
  397. `,
  398. },
  399. }
  400. // build all client config
  401. for _, test := range tests {
  402. clientConf += getProxyConf(test.proxyName, test.extraConfig) + "\n"
  403. localServer := streamserver.New(streamserver.TCP, streamserver.WithBindPort(f.AllocPort()), streamserver.WithRespContent([]byte(test.proxyName)))
  404. f.RunServer(port.GenName(test.proxyName), localServer)
  405. }
  406. // run frps and frpc
  407. f.RunProcesses([]string{serverConf}, []string{clientConf})
  408. // Request without HTTP connect should get error
  409. framework.NewRequestExpect(f).
  410. PortName(tcpmuxHTTPConnectPortName).
  411. ExpectError(true).
  412. Explain("request without HTTP connect expect error").
  413. Ensure()
  414. proxyURL := fmt.Sprintf("http://127.0.0.1:%d", f.PortByName(tcpmuxHTTPConnectPortName))
  415. // Request with incorrect connect hostname
  416. framework.NewRequestExpect(f).RequestModify(func(r *request.Request) {
  417. r.Addr("invalid").Proxy(proxyURL)
  418. }).ExpectError(true).Explain("request without HTTP connect expect error").Ensure()
  419. // Request with correct connect hostname
  420. for _, test := range tests {
  421. framework.NewRequestExpect(f).RequestModify(func(r *request.Request) {
  422. r.Addr(test.proxyName).Proxy(proxyURL)
  423. }).ExpectResp([]byte(test.proxyName)).Explain(test.proxyName).Ensure()
  424. }
  425. })
  426. })
  427. })