basic.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 + "\nuser = user1"
  262. clientVisitorConf := consts.DefaultClientConfig + "\nuser = user1"
  263. clientUser2VisitorConf := consts.DefaultClientConfig + "\nuser = user2"
  264. localPortName := ""
  265. protocol := "tcp"
  266. switch proxyType {
  267. case "stcp":
  268. localPortName = framework.TCPEchoServerPort
  269. protocol = "tcp"
  270. case "sudp":
  271. localPortName = framework.UDPEchoServerPort
  272. protocol = "udp"
  273. case "xtcp":
  274. localPortName = framework.TCPEchoServerPort
  275. protocol = "tcp"
  276. }
  277. correctSK := "abc"
  278. wrongSK := "123"
  279. getProxyServerConf := func(proxyName string, extra string) string {
  280. return fmt.Sprintf(`
  281. [%s]
  282. type = %s
  283. role = server
  284. sk = %s
  285. local_port = {{ .%s }}
  286. `+extra, proxyName, proxyType, correctSK, localPortName)
  287. }
  288. getProxyVisitorConf := func(proxyName string, portName, visitorSK, extra string) string {
  289. return fmt.Sprintf(`
  290. [%s]
  291. type = %s
  292. role = visitor
  293. server_name = %s
  294. sk = %s
  295. bind_port = {{ .%s }}
  296. `+extra, proxyName, proxyType, proxyName, visitorSK, portName)
  297. }
  298. tests := []struct {
  299. proxyName string
  300. bindPortName string
  301. visitorSK string
  302. commonExtraConfig string
  303. proxyExtraConfig string
  304. visitorExtraConfig string
  305. expectError bool
  306. user2 bool
  307. }{
  308. {
  309. proxyName: "normal",
  310. bindPortName: port.GenName("Normal"),
  311. visitorSK: correctSK,
  312. },
  313. {
  314. proxyName: "with-encryption",
  315. bindPortName: port.GenName("WithEncryption"),
  316. visitorSK: correctSK,
  317. commonExtraConfig: "use_encryption = true",
  318. },
  319. {
  320. proxyName: "with-compression",
  321. bindPortName: port.GenName("WithCompression"),
  322. visitorSK: correctSK,
  323. commonExtraConfig: "use_compression = true",
  324. },
  325. {
  326. proxyName: "with-encryption-and-compression",
  327. bindPortName: port.GenName("WithEncryptionAndCompression"),
  328. visitorSK: correctSK,
  329. commonExtraConfig: `
  330. use_encryption = true
  331. use_compression = true
  332. `,
  333. },
  334. {
  335. proxyName: "with-error-sk",
  336. bindPortName: port.GenName("WithErrorSK"),
  337. visitorSK: wrongSK,
  338. expectError: true,
  339. },
  340. {
  341. proxyName: "allowed-user",
  342. bindPortName: port.GenName("AllowedUser"),
  343. visitorSK: correctSK,
  344. proxyExtraConfig: "allow_users = another, user2",
  345. visitorExtraConfig: "server_user = user1",
  346. user2: true,
  347. },
  348. {
  349. proxyName: "not-allowed-user",
  350. bindPortName: port.GenName("NotAllowedUser"),
  351. visitorSK: correctSK,
  352. proxyExtraConfig: "allow_users = invalid",
  353. visitorExtraConfig: "server_user = user1",
  354. expectError: true,
  355. },
  356. {
  357. proxyName: "allow-all",
  358. bindPortName: port.GenName("AllowAll"),
  359. visitorSK: correctSK,
  360. proxyExtraConfig: "allow_users = *",
  361. visitorExtraConfig: "server_user = user1",
  362. user2: true,
  363. },
  364. }
  365. // build all client config
  366. for _, test := range tests {
  367. clientServerConf += getProxyServerConf(test.proxyName, test.commonExtraConfig+"\n"+test.proxyExtraConfig) + "\n"
  368. }
  369. for _, test := range tests {
  370. config := getProxyVisitorConf(
  371. test.proxyName, test.bindPortName, test.visitorSK, test.commonExtraConfig+"\n"+test.visitorExtraConfig,
  372. ) + "\n"
  373. if test.user2 {
  374. clientUser2VisitorConf += config
  375. } else {
  376. clientVisitorConf += config
  377. }
  378. }
  379. // run frps and frpc
  380. f.RunProcesses([]string{serverConf}, []string{clientServerConf, clientVisitorConf, clientUser2VisitorConf})
  381. for _, test := range tests {
  382. timeout := time.Second
  383. if t == "xtcp" {
  384. timeout = 4 * time.Second
  385. }
  386. framework.NewRequestExpect(f).
  387. RequestModify(func(r *request.Request) {
  388. r.Timeout(timeout)
  389. }).
  390. Protocol(protocol).
  391. PortName(test.bindPortName).
  392. Explain(test.proxyName).
  393. ExpectError(test.expectError).
  394. Ensure()
  395. }
  396. })
  397. }
  398. })
  399. ginkgo.Describe("TCPMUX", func() {
  400. ginkgo.It("Type tcpmux", func() {
  401. serverConf := consts.DefaultServerConfig
  402. clientConf := consts.DefaultClientConfig
  403. tcpmuxHTTPConnectPortName := port.GenName("TCPMUX")
  404. serverConf += fmt.Sprintf(`
  405. tcpmux_httpconnect_port = {{ .%s }}
  406. `, tcpmuxHTTPConnectPortName)
  407. getProxyConf := func(proxyName string, extra string) string {
  408. return fmt.Sprintf(`
  409. [%s]
  410. type = tcpmux
  411. multiplexer = httpconnect
  412. local_port = {{ .%s }}
  413. custom_domains = %s
  414. `+extra, proxyName, port.GenName(proxyName), proxyName)
  415. }
  416. tests := []struct {
  417. proxyName string
  418. extraConfig string
  419. }{
  420. {
  421. proxyName: "normal",
  422. },
  423. {
  424. proxyName: "with-encryption",
  425. extraConfig: "use_encryption = true",
  426. },
  427. {
  428. proxyName: "with-compression",
  429. extraConfig: "use_compression = true",
  430. },
  431. {
  432. proxyName: "with-encryption-and-compression",
  433. extraConfig: `
  434. use_encryption = true
  435. use_compression = true
  436. `,
  437. },
  438. }
  439. // build all client config
  440. for _, test := range tests {
  441. clientConf += getProxyConf(test.proxyName, test.extraConfig) + "\n"
  442. localServer := streamserver.New(streamserver.TCP, streamserver.WithBindPort(f.AllocPort()), streamserver.WithRespContent([]byte(test.proxyName)))
  443. f.RunServer(port.GenName(test.proxyName), localServer)
  444. }
  445. // run frps and frpc
  446. f.RunProcesses([]string{serverConf}, []string{clientConf})
  447. // Request without HTTP connect should get error
  448. framework.NewRequestExpect(f).
  449. PortName(tcpmuxHTTPConnectPortName).
  450. ExpectError(true).
  451. Explain("request without HTTP connect expect error").
  452. Ensure()
  453. proxyURL := fmt.Sprintf("http://127.0.0.1:%d", f.PortByName(tcpmuxHTTPConnectPortName))
  454. // Request with incorrect connect hostname
  455. framework.NewRequestExpect(f).RequestModify(func(r *request.Request) {
  456. r.Addr("invalid").Proxy(proxyURL)
  457. }).ExpectError(true).Explain("request without HTTP connect expect error").Ensure()
  458. // Request with correct connect hostname
  459. for _, test := range tests {
  460. framework.NewRequestExpect(f).RequestModify(func(r *request.Request) {
  461. r.Addr(test.proxyName).Proxy(proxyURL)
  462. }).ExpectResp([]byte(test.proxyName)).Explain(test.proxyName).Ensure()
  463. }
  464. })
  465. })
  466. })