client_server.go 7.7 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/cert"
  8. "github.com/fatedier/frp/test/e2e/pkg/port"
  9. . "github.com/onsi/ginkgo"
  10. )
  11. type generalTestConfigures struct {
  12. server string
  13. client string
  14. expectError bool
  15. }
  16. func runClientServerTest(f *framework.Framework, configures *generalTestConfigures) {
  17. serverConf := consts.DefaultServerConfig
  18. clientConf := consts.DefaultClientConfig
  19. serverConf += fmt.Sprintf(`
  20. %s
  21. `, configures.server)
  22. tcpPortName := port.GenName("TCP")
  23. udpPortName := port.GenName("UDP")
  24. clientConf += fmt.Sprintf(`
  25. %s
  26. [tcp]
  27. type = tcp
  28. local_port = {{ .%s }}
  29. remote_port = {{ .%s }}
  30. [udp]
  31. type = udp
  32. local_port = {{ .%s }}
  33. remote_port = {{ .%s }}
  34. `, configures.client,
  35. framework.TCPEchoServerPort, tcpPortName,
  36. framework.UDPEchoServerPort, udpPortName,
  37. )
  38. f.RunProcesses([]string{serverConf}, []string{clientConf})
  39. framework.NewRequestExpect(f).PortName(tcpPortName).ExpectError(configures.expectError).Explain("tcp proxy").Ensure()
  40. framework.NewRequestExpect(f).Protocol("udp").
  41. PortName(udpPortName).ExpectError(configures.expectError).Explain("udp proxy").Ensure()
  42. }
  43. // defineClientServerTest test a normal tcp and udp proxy with specified TestConfigures.
  44. func defineClientServerTest(desc string, f *framework.Framework, configures *generalTestConfigures) {
  45. It(desc, func() {
  46. runClientServerTest(f, configures)
  47. })
  48. }
  49. var _ = Describe("[Feature: Client-Server]", func() {
  50. f := framework.NewDefaultFramework()
  51. Describe("Protocol", func() {
  52. supportProtocols := []string{"tcp", "kcp", "websocket"}
  53. for _, protocol := range supportProtocols {
  54. configures := &generalTestConfigures{
  55. server: fmt.Sprintf(`
  56. kcp_bind_port = {{ .%s }}
  57. protocol = %s"
  58. `, consts.PortServerName, protocol),
  59. client: "protocol = " + protocol,
  60. }
  61. defineClientServerTest(protocol, f, configures)
  62. }
  63. })
  64. Describe("Authentication", func() {
  65. defineClientServerTest("Token Correct", f, &generalTestConfigures{
  66. server: "token = 123456",
  67. client: "token = 123456",
  68. })
  69. defineClientServerTest("Token Incorrect", f, &generalTestConfigures{
  70. server: "token = 123456",
  71. client: "token = invalid",
  72. expectError: true,
  73. })
  74. })
  75. Describe("TLS", func() {
  76. supportProtocols := []string{"tcp", "kcp", "websocket"}
  77. for _, protocol := range supportProtocols {
  78. tmp := protocol
  79. defineClientServerTest("TLS over "+strings.ToUpper(tmp), f, &generalTestConfigures{
  80. server: fmt.Sprintf(`
  81. kcp_bind_port = {{ .%s }}
  82. protocol = %s
  83. `, consts.PortServerName, protocol),
  84. client: fmt.Sprintf(`tls_enable = true
  85. protocol = %s
  86. `, protocol),
  87. })
  88. }
  89. defineClientServerTest("enable tls_only, client with TLS", f, &generalTestConfigures{
  90. server: "tls_only = true",
  91. client: "tls_enable = true",
  92. })
  93. defineClientServerTest("enable tls_only, client without TLS", f, &generalTestConfigures{
  94. server: "tls_only = true",
  95. expectError: true,
  96. })
  97. })
  98. Describe("TLS with custom certificate", func() {
  99. supportProtocols := []string{"tcp", "kcp", "websocket"}
  100. var (
  101. caCrtPath string
  102. serverCrtPath, serverKeyPath string
  103. clientCrtPath, clientKeyPath string
  104. )
  105. JustBeforeEach(func() {
  106. generator := &cert.SelfSignedCertGenerator{}
  107. artifacts, err := generator.Generate("0.0.0.0")
  108. framework.ExpectNoError(err)
  109. caCrtPath = f.WriteTempFile("ca.crt", string(artifacts.CACert))
  110. serverCrtPath = f.WriteTempFile("server.crt", string(artifacts.Cert))
  111. serverKeyPath = f.WriteTempFile("server.key", string(artifacts.Key))
  112. generator.SetCA(artifacts.CACert, artifacts.CAKey)
  113. generator.Generate("0.0.0.0")
  114. clientCrtPath = f.WriteTempFile("client.crt", string(artifacts.Cert))
  115. clientKeyPath = f.WriteTempFile("client.key", string(artifacts.Key))
  116. })
  117. for _, protocol := range supportProtocols {
  118. tmp := protocol
  119. It("one-way authentication: "+tmp, func() {
  120. runClientServerTest(f, &generalTestConfigures{
  121. server: fmt.Sprintf(`
  122. protocol = %s
  123. kcp_bind_port = {{ .%s }}
  124. tls_trusted_ca_file = %s
  125. `, tmp, consts.PortServerName, caCrtPath),
  126. client: fmt.Sprintf(`
  127. protocol = %s
  128. tls_enable = true
  129. tls_cert_file = %s
  130. tls_key_file = %s
  131. `, tmp, clientCrtPath, clientKeyPath),
  132. })
  133. })
  134. It("mutual authentication: "+tmp, func() {
  135. runClientServerTest(f, &generalTestConfigures{
  136. server: fmt.Sprintf(`
  137. protocol = %s
  138. kcp_bind_port = {{ .%s }}
  139. tls_cert_file = %s
  140. tls_key_file = %s
  141. tls_trusted_ca_file = %s
  142. `, tmp, consts.PortServerName, serverCrtPath, serverKeyPath, caCrtPath),
  143. client: fmt.Sprintf(`
  144. protocol = %s
  145. tls_enable = true
  146. tls_cert_file = %s
  147. tls_key_file = %s
  148. tls_trusted_ca_file = %s
  149. `, tmp, clientCrtPath, clientKeyPath, caCrtPath),
  150. })
  151. })
  152. }
  153. })
  154. Describe("TLS with custom certificate and specified server name", func() {
  155. var (
  156. caCrtPath string
  157. serverCrtPath, serverKeyPath string
  158. clientCrtPath, clientKeyPath string
  159. )
  160. JustBeforeEach(func() {
  161. generator := &cert.SelfSignedCertGenerator{}
  162. artifacts, err := generator.Generate("example.com")
  163. framework.ExpectNoError(err)
  164. caCrtPath = f.WriteTempFile("ca.crt", string(artifacts.CACert))
  165. serverCrtPath = f.WriteTempFile("server.crt", string(artifacts.Cert))
  166. serverKeyPath = f.WriteTempFile("server.key", string(artifacts.Key))
  167. generator.SetCA(artifacts.CACert, artifacts.CAKey)
  168. generator.Generate("example.com")
  169. clientCrtPath = f.WriteTempFile("client.crt", string(artifacts.Cert))
  170. clientKeyPath = f.WriteTempFile("client.key", string(artifacts.Key))
  171. })
  172. It("mutual authentication", func() {
  173. runClientServerTest(f, &generalTestConfigures{
  174. server: fmt.Sprintf(`
  175. tls_cert_file = %s
  176. tls_key_file = %s
  177. tls_trusted_ca_file = %s
  178. `, serverCrtPath, serverKeyPath, caCrtPath),
  179. client: fmt.Sprintf(`
  180. tls_enable = true
  181. tls_server_name = example.com
  182. tls_cert_file = %s
  183. tls_key_file = %s
  184. tls_trusted_ca_file = %s
  185. `, clientCrtPath, clientKeyPath, caCrtPath),
  186. })
  187. })
  188. It("mutual authentication with incorrect server name", func() {
  189. runClientServerTest(f, &generalTestConfigures{
  190. server: fmt.Sprintf(`
  191. tls_cert_file = %s
  192. tls_key_file = %s
  193. tls_trusted_ca_file = %s
  194. `, serverCrtPath, serverKeyPath, caCrtPath),
  195. client: fmt.Sprintf(`
  196. tls_enable = true
  197. tls_server_name = invalid.com
  198. tls_cert_file = %s
  199. tls_key_file = %s
  200. tls_trusted_ca_file = %s
  201. `, clientCrtPath, clientKeyPath, caCrtPath),
  202. expectError: true,
  203. })
  204. })
  205. })
  206. Describe("TLS with disable_custom_tls_first_byte", func() {
  207. supportProtocols := []string{"tcp", "kcp", "websocket"}
  208. for _, protocol := range supportProtocols {
  209. tmp := protocol
  210. defineClientServerTest("TLS over "+strings.ToUpper(tmp), f, &generalTestConfigures{
  211. server: fmt.Sprintf(`
  212. kcp_bind_port = {{ .%s }}
  213. protocol = %s
  214. `, consts.PortServerName, protocol),
  215. client: fmt.Sprintf(`
  216. tls_enable = true
  217. protocol = %s
  218. disable_custom_tls_first_byte = true
  219. `, protocol),
  220. })
  221. }
  222. })
  223. Describe("IPv6 bind address", func() {
  224. supportProtocols := []string{"tcp", "kcp", "websocket"}
  225. for _, protocol := range supportProtocols {
  226. tmp := protocol
  227. defineClientServerTest("IPv6 bind address: "+strings.ToUpper(tmp), f, &generalTestConfigures{
  228. server: fmt.Sprintf(`
  229. bind_addr = ::
  230. kcp_bind_port = {{ .%s }}
  231. protocol = %s
  232. `, consts.PortServerName, protocol),
  233. client: fmt.Sprintf(`
  234. tls_enable = true
  235. protocol = %s
  236. disable_custom_tls_first_byte = true
  237. `, protocol),
  238. })
  239. }
  240. })
  241. })