| 9 |
| 10 | "github.com/fatedier/frp/test/e2e/framework" |
| 11 | "github.com/fatedier/frp/test/e2e/framework/consts" |
| 12 | "github.com/fatedier/frp/test/e2e/pkg/cert" |
| 13 | "github.com/fatedier/frp/test/e2e/pkg/port" |
| 14 | ) |
| 15 |
| 16 | type generalTestConfigures struct { |
| 17 | server string |
| 18 | client string |
| 19 | clientPrefix string |
| 20 | client2 string |
| 21 | client2Prefix string |
| 22 | testDelay time.Duration |
| 23 | expectError bool |
| 24 | } |
| 25 |
| 26 | func renderBindPortConfig(protocol string) string { |
| 27 | switch protocol { |
| 28 | case "kcp": |
| 29 | return fmt.Sprintf(`kcpBindPort = {{ .%s }}`, consts.PortServerName) |
| 30 | case "quic": |
| 31 | return fmt.Sprintf(`quicBindPort = {{ .%s }}`, consts.PortServerName) |
| 32 | default: |
| 33 | return "" |
| 34 | } |
| 35 | } |
| 36 |
| 37 | func runClientServerTest(f *framework.Framework, configures *generalTestConfigures) { |
| 38 | serverConf := consts.DefaultServerConfig |
| 39 | clientConf := consts.DefaultClientConfig |
| 40 | if configures.clientPrefix != "" { |
| 41 | clientConf = configures.clientPrefix |
| 42 | } |
| 43 |
| 44 | serverConf += fmt.Sprintf(` |
| 45 | %s |
| 46 | `, configures.server) |
| 47 |
| 48 | tcpPortName := port.GenName("TCP") |
| 49 | udpPortName := port.GenName("UDP") |
| 50 | clientConf += fmt.Sprintf(` |
| 51 | %s |
| 52 |
| 53 | [[proxies]] |
| 54 | name = "tcp" |
| 55 | type = "tcp" |
| 56 | localPort = {{ .%s }} |
| 57 | remotePort = {{ .%s }} |
| 58 |
| 59 | [[proxies]] |
| 60 | name = "udp" |
| 61 | type = "udp" |
| 62 | localPort = {{ .%s }} |
| 63 | remotePort = {{ .%s }} |
| 64 | `, configures.client, |
| 65 | framework.TCPEchoServerPort, tcpPortName, |
| 66 | framework.UDPEchoServerPort, udpPortName, |
| 67 | ) |
| 68 |
| 69 | clientConfs := []string{clientConf} |
| 70 | if configures.client2 != "" { |
| 71 | client2Conf := consts.DefaultClientConfig |
| 72 | if configures.client2Prefix != "" { |
| 73 | client2Conf = configures.client2Prefix |
| 74 | } |
| 75 | client2Conf += fmt.Sprintf(` |
| 76 | %s |
| 77 | `, configures.client2) |
| 78 | clientConfs = append(clientConfs, client2Conf) |
| 79 | } |
| 80 |
| 81 | f.RunProcesses(serverConf, clientConfs) |
| 82 |
| 83 | if configures.testDelay > 0 { |
| 84 | time.Sleep(configures.testDelay) |
| 85 | } |
| 86 |
| 87 | framework.NewRequestExpect(f).PortName(tcpPortName).ExpectError(configures.expectError).Explain("tcp proxy").Ensure() |
| 88 | framework.NewRequestExpect(f).Protocol("udp"). |
| 89 | PortName(udpPortName).ExpectError(configures.expectError).Explain("udp proxy").Ensure() |
| 90 | } |
| 91 |
| 92 | // defineClientServerTest test a normal tcp and udp proxy with specified TestConfigures. |
| 93 | func defineClientServerTest(desc string, f *framework.Framework, configures *generalTestConfigures) { |
| 94 | ginkgo.It(desc, func() { |
| 95 | runClientServerTest(f, configures) |
| 96 | }) |
| 97 | } |
| 98 |
| 99 | var _ = ginkgo.Describe("[Feature: Client-Server]", func() { |
| 100 | f := framework.NewDefaultFramework() |
| 101 |
| 102 | ginkgo.Describe("Protocol", func() { |
| 103 | supportProtocols := []string{"tcp", "kcp", "quic", "websocket"} |
| 104 | for _, protocol := range supportProtocols { |
| 105 | configures := &generalTestConfigures{ |
| 106 | server: fmt.Sprintf(` |
| 107 | %s |
| 108 | `, renderBindPortConfig(protocol)), |
| 109 | client: fmt.Sprintf(`transport.protocol = "%s"`, protocol), |
| 110 | } |
| 111 | defineClientServerTest(protocol, f, configures) |
| 112 | } |
| 113 | }) |
| 114 |
| 115 | // wss is special, it needs to be tested separately. |
| 116 | // frps only supports ws, so there should be a proxy to terminate TLS before frps. |
| 117 | ginkgo.Describe("Protocol wss", func() { |
| 118 | wssPort := f.AllocPort() |
| 119 | configures := &generalTestConfigures{ |
| 120 | clientPrefix: fmt.Sprintf(` |
| 121 | serverAddr = "127.0.0.1" |
| 122 | serverPort = %d |
| 123 | loginFailExit = false |
| 124 | transport.protocol = "wss" |
| 125 | log.level = "trace" |
| 126 | `, wssPort), |
| 127 | // Due to the fact that frps cannot directly accept wss connections, we use the https2http plugin of another frpc to terminate TLS. |
| 128 | client2: fmt.Sprintf(` |
| 129 | [[proxies]] |
| 130 | name = "wss2ws" |
| 131 | type = "tcp" |
| 132 | remotePort = %d |
| 133 | [proxies.plugin] |
| 134 | type = "https2http" |
| 135 | localAddr = "127.0.0.1:{{ .%s }}" |
| 136 | `, wssPort, consts.PortServerName), |
| 137 | testDelay: 10 * time.Second, |
| 138 | } |
| 139 |
| 140 | defineClientServerTest("wss", f, configures) |
| 141 | }) |
| 142 |
| 143 | ginkgo.Describe("Authentication", func() { |
| 144 | defineClientServerTest("Token Correct", f, &generalTestConfigures{ |
| 145 | server: `auth.token = "123456"`, |
| 146 | client: `auth.token = "123456"`, |
| 147 | }) |
| 148 |
| 149 | defineClientServerTest("Token Incorrect", f, &generalTestConfigures{ |
| 150 | server: `auth.token = "123456"`, |
| 151 | client: `auth.token = "invalid"`, |
| 152 | expectError: true, |
| 153 | }) |
| 154 | }) |
| 155 |
| 156 | ginkgo.Describe("TLS", func() { |
| 157 | supportProtocols := []string{"tcp", "kcp", "quic", "websocket"} |
| 158 | for _, protocol := range supportProtocols { |
| 159 | tmp := protocol |
| 160 | // Since v0.50.0, the default value of tls_enable has been changed to true. |
| 161 | // Therefore, here it needs to be set as false to test the scenario of turning it off. |
| 162 | defineClientServerTest("Disable TLS over "+strings.ToUpper(tmp), f, &generalTestConfigures{ |
| 163 | server: fmt.Sprintf(` |
| 164 | %s |
| 165 | `, renderBindPortConfig(protocol)), |
| 166 | client: fmt.Sprintf(`transport.tls.enable = false |
| 167 | transport.protocol = "%s" |
| 168 | `, protocol), |
| 169 | }) |
| 170 | } |
| 171 |
| 172 | defineClientServerTest("enable tls force, client with TLS", f, &generalTestConfigures{ |
| 173 | server: "transport.tls.force = true", |
| 174 | }) |
| 175 | defineClientServerTest("enable tls force, client without TLS", f, &generalTestConfigures{ |
| 176 | server: "transport.tls.force = true", |
| 177 | client: "transport.tls.enable = false", |
| 178 | expectError: true, |
| 179 | }) |
| 180 | }) |
| 181 |
| 182 | ginkgo.Describe("TLS with custom certificate", func() { |
| 183 | supportProtocols := []string{"tcp", "kcp", "quic", "websocket"} |
| 184 |
| 185 | var ( |
| 186 | caCrtPath string |
| 187 | serverCrtPath, serverKeyPath string |
| 188 | clientCrtPath, clientKeyPath string |
| 189 | ) |
| 190 | ginkgo.JustBeforeEach(func() { |
| 191 | generator := &cert.SelfSignedCertGenerator{} |
| 192 | artifacts, err := generator.Generate("127.0.0.1") |
| 193 | framework.ExpectNoError(err) |
| 194 |
| 195 | caCrtPath = f.WriteTempFile("ca.crt", string(artifacts.CACert)) |
| 196 | serverCrtPath = f.WriteTempFile("server.crt", string(artifacts.Cert)) |
| 197 | serverKeyPath = f.WriteTempFile("server.key", string(artifacts.Key)) |
| 198 | generator.SetCA(artifacts.CACert, artifacts.CAKey) |
| 199 | _, err = generator.Generate("127.0.0.1") |
| 200 | framework.ExpectNoError(err) |
| 201 | clientCrtPath = f.WriteTempFile("client.crt", string(artifacts.Cert)) |
| 202 | clientKeyPath = f.WriteTempFile("client.key", string(artifacts.Key)) |
| 203 | }) |
| 204 |
| 205 | for _, protocol := range supportProtocols { |
| 206 | tmp := protocol |
| 207 |
| 208 | ginkgo.It("one-way authentication: "+tmp, func() { |
| 209 | runClientServerTest(f, &generalTestConfigures{ |
| 210 | server: fmt.Sprintf(` |
| 211 | %s |
| 212 | transport.tls.trustedCaFile = "%s" |
| 213 | `, renderBindPortConfig(tmp), caCrtPath), |
| 214 | client: fmt.Sprintf(` |
| 215 | transport.protocol = "%s" |
| 216 | transport.tls.certFile = "%s" |
| 217 | transport.tls.keyFile = "%s" |
| 218 | `, tmp, clientCrtPath, clientKeyPath), |
| 219 | }) |
| 220 | }) |
| 221 |
| 222 | ginkgo.It("mutual authentication: "+tmp, func() { |
| 223 | runClientServerTest(f, &generalTestConfigures{ |
| 224 | server: fmt.Sprintf(` |
| 225 | %s |
| 226 | transport.tls.certFile = "%s" |
| 227 | transport.tls.keyFile = "%s" |
| 228 | transport.tls.trustedCaFile = "%s" |
| 229 | `, renderBindPortConfig(tmp), serverCrtPath, serverKeyPath, caCrtPath), |
| 230 | client: fmt.Sprintf(` |
| 231 | transport.protocol = "%s" |
| 232 | transport.tls.certFile = "%s" |
| 233 | transport.tls.keyFile = "%s" |
| 234 | transport.tls.trustedCaFile = "%s" |
| 235 | `, tmp, clientCrtPath, clientKeyPath, caCrtPath), |
| 236 | }) |
| 237 | }) |
| 238 | } |
| 239 | }) |
| 240 |
| 241 | ginkgo.Describe("TLS with custom certificate and specified server name", func() { |
| 242 | var ( |
| 243 | caCrtPath string |
| 244 | serverCrtPath, serverKeyPath string |
| 245 | clientCrtPath, clientKeyPath string |
| 246 | ) |
| 247 | ginkgo.JustBeforeEach(func() { |
| 248 | generator := &cert.SelfSignedCertGenerator{} |
| 249 | artifacts, err := generator.Generate("example.com") |
| 250 | framework.ExpectNoError(err) |
| 251 |
| 252 | caCrtPath = f.WriteTempFile("ca.crt", string(artifacts.CACert)) |
| 253 | serverCrtPath = f.WriteTempFile("server.crt", string(artifacts.Cert)) |
| 254 | serverKeyPath = f.WriteTempFile("server.key", string(artifacts.Key)) |
| 255 | generator.SetCA(artifacts.CACert, artifacts.CAKey) |
| 256 | _, err = generator.Generate("example.com") |
| 257 | framework.ExpectNoError(err) |
| 258 | clientCrtPath = f.WriteTempFile("client.crt", string(artifacts.Cert)) |
| 259 | clientKeyPath = f.WriteTempFile("client.key", string(artifacts.Key)) |
| 260 | }) |
| 261 |
| 262 | ginkgo.It("mutual authentication", func() { |
| 263 | runClientServerTest(f, &generalTestConfigures{ |
| 264 | server: fmt.Sprintf(` |
| 265 | transport.tls.certFile = "%s" |
| 266 | transport.tls.keyFile = "%s" |
| 267 | transport.tls.trustedCaFile = "%s" |
| 268 | `, serverCrtPath, serverKeyPath, caCrtPath), |
| 269 | client: fmt.Sprintf(` |
| 270 | transport.tls.serverName = "example.com" |
| 271 | transport.tls.certFile = "%s" |
| 272 | transport.tls.keyFile = "%s" |
| 273 | transport.tls.trustedCaFile = "%s" |
| 274 | `, clientCrtPath, clientKeyPath, caCrtPath), |
| 275 | }) |
| 276 | }) |
| 277 |
| 278 | ginkgo.It("mutual authentication with incorrect server name", func() { |
| 279 | runClientServerTest(f, &generalTestConfigures{ |
| 280 | server: fmt.Sprintf(` |
| 281 | transport.tls.certFile = "%s" |
| 282 | transport.tls.keyFile = "%s" |
| 283 | transport.tls.trustedCaFile = "%s" |
| 284 | `, serverCrtPath, serverKeyPath, caCrtPath), |
| 285 | client: fmt.Sprintf(` |
| 286 | transport.tls.serverName = "invalid.com" |
| 287 | transport.tls.certFile = "%s" |
| 288 | transport.tls.keyFile = "%s" |
| 289 | transport.tls.trustedCaFile = "%s" |
| 290 | `, clientCrtPath, clientKeyPath, caCrtPath), |
| 291 | expectError: true, |
| 292 | }) |
| 293 | }) |
| 294 | }) |
| 295 |
| 296 | ginkgo.Describe("TLS with disableCustomTLSFirstByte set to false", func() { |
| 297 | supportProtocols := []string{"tcp", "kcp", "quic", "websocket"} |
| 298 | for _, protocol := range supportProtocols { |
| 299 | tmp := protocol |
| 300 | defineClientServerTest("TLS over "+strings.ToUpper(tmp), f, &generalTestConfigures{ |
| 301 | server: fmt.Sprintf(` |
| 302 | %s |
| 303 | `, renderBindPortConfig(protocol)), |
| 304 | client: fmt.Sprintf(` |
| 305 | transport.protocol = "%s" |
| 306 | transport.tls.disableCustomTLSFirstByte = false |
| 307 | `, protocol), |
| 308 | }) |
| 309 | } |
| 310 | }) |
| 311 |
| 312 | ginkgo.Describe("IPv6 bind address", func() { |
| 313 | supportProtocols := []string{"tcp", "kcp", "quic", "websocket"} |
| 314 | for _, protocol := range supportProtocols { |
| 315 | tmp := protocol |
| 316 | defineClientServerTest("IPv6 bind address: "+strings.ToUpper(tmp), f, &generalTestConfigures{ |
| 317 | server: fmt.Sprintf(` |
| 318 | bindAddr = "::" |
| 319 | %s |
| 320 | `, renderBindPortConfig(protocol)), |
| 321 | client: fmt.Sprintf(` |
| 322 | transport.protocol = "%s" |
| 323 | `, protocol), |
| 324 | }) |
| 325 | } |
| 326 | }) |
| 327 |
| 328 | ginkgo.Describe("Use same port for bindPort and vhostHTTPSPort", func() { |
| 329 | supportProtocols := []string{"tcp", "kcp", "quic", "websocket"} |
| 330 | for _, protocol := range supportProtocols { |
| 331 | tmp := protocol |
| 332 | defineClientServerTest("Use same port for bindPort and vhostHTTPSPort: "+strings.ToUpper(tmp), f, &generalTestConfigures{ |
| 333 | server: fmt.Sprintf(` |
| 334 | vhostHTTPSPort = {{ .%s }} |
| 335 | %s |
| 336 | `, consts.PortServerName, renderBindPortConfig(protocol)), |
| 337 | // transport.tls.disableCustomTLSFirstByte should set to false when vhostHTTPSPort is same as bindPort |
| 338 | client: fmt.Sprintf(` |
| 339 | transport.protocol = "%s" |
| 340 | transport.tls.disableCustomTLSFirstByte = false |
| 341 | `, protocol), |
| 342 | }) |
| 343 | } |
| 344 | }) |
| 345 | }) |