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