| 9 | "github.com/onsi/ginkgo/v2" |
| 10 |
| 11 | "github.com/fatedier/frp/pkg/transport" |
| 12 | "github.com/fatedier/frp/test/e2e/framework" |
| 13 | "github.com/fatedier/frp/test/e2e/framework/consts" |
| 14 | "github.com/fatedier/frp/test/e2e/mock/server/httpserver" |
| 15 | "github.com/fatedier/frp/test/e2e/pkg/cert" |
| 16 | "github.com/fatedier/frp/test/e2e/pkg/port" |
| 17 | "github.com/fatedier/frp/test/e2e/pkg/request" |
| 18 | ) |
| 19 |
| 20 | var _ = ginkgo.Describe("[Feature: Client-Plugins]", func() { |
| 21 | f := framework.NewDefaultFramework() |
| 22 |
| 23 | ginkgo.Describe("UnixDomainSocket", func() { |
| 24 | ginkgo.It("Expose a unix domain socket echo server", func() { |
| 25 | serverConf := consts.LegacyDefaultServerConfig |
| 26 | var clientConf strings.Builder |
| 27 | clientConf.WriteString(consts.LegacyDefaultClientConfig) |
| 28 |
| 29 | getProxyConf := func(proxyName string, portName string, extra string) string { |
| 30 | return fmt.Sprintf(` |
| 31 | [%s] |
| 32 | type = tcp |
| 33 | remote_port = {{ .%s }} |
| 34 | plugin = unix_domain_socket |
| 35 | plugin_unix_path = {{ .%s }} |
| 36 | `+extra, proxyName, portName, framework.UDSEchoServerAddr) |
| 37 | } |
| 38 |
| 39 | tests := []struct { |
| 40 | proxyName string |
| 41 | portName string |
| 42 | extraConfig string |
| 43 | }{ |
| 44 | { |
| 45 | proxyName: "normal", |
| 46 | portName: port.GenName("Normal"), |
| 47 | }, |
| 48 | { |
| 49 | proxyName: "with-encryption", |
| 50 | portName: port.GenName("WithEncryption"), |
| 51 | extraConfig: "use_encryption = true", |
| 52 | }, |
| 53 | { |
| 54 | proxyName: "with-compression", |
| 55 | portName: port.GenName("WithCompression"), |
| 56 | extraConfig: "use_compression = true", |
| 57 | }, |
| 58 | { |
| 59 | proxyName: "with-encryption-and-compression", |
| 60 | portName: port.GenName("WithEncryptionAndCompression"), |
| 61 | extraConfig: ` |
| 62 | use_encryption = true |
| 63 | use_compression = true |
| 64 | `, |
| 65 | }, |
| 66 | } |
| 67 |
| 68 | // build all client config |
| 69 | for _, test := range tests { |
| 70 | clientConf.WriteString(getProxyConf(test.proxyName, test.portName, test.extraConfig) + "\n") |
| 71 | } |
| 72 | // run frps and frpc |
| 73 | f.RunProcesses(serverConf, []string{clientConf.String()}) |
| 74 |
| 75 | for _, test := range tests { |
| 76 | framework.NewRequestExpect(f).Port(f.PortByName(test.portName)).Ensure() |
| 77 | } |
| 78 | }) |
| 79 | }) |
| 80 |
| 81 | ginkgo.It("http_proxy", func() { |
| 82 | serverConf := consts.LegacyDefaultServerConfig |
| 83 | clientConf := consts.LegacyDefaultClientConfig |
| 84 |
| 85 | remotePort := f.AllocPort() |
| 86 | clientConf += fmt.Sprintf(` |
| 87 | [tcp] |
| 88 | type = tcp |
| 89 | remote_port = %d |
| 90 | plugin = http_proxy |
| 91 | plugin_http_user = abc |
| 92 | plugin_http_passwd = 123 |
| 93 | `, remotePort) |
| 94 |
| 95 | f.RunProcesses(serverConf, []string{clientConf}) |
| 96 |
| 97 | // http proxy, no auth info |
| 98 | framework.NewRequestExpect(f).PortName(framework.HTTPSimpleServerPort).RequestModify(func(r *request.Request) { |
| 99 | r.HTTP().Proxy("http://127.0.0.1:" + strconv.Itoa(remotePort)) |
| 100 | }).Ensure(framework.ExpectResponseCode(407)) |
| 101 |
| 102 | // http proxy, correct auth |
| 103 | framework.NewRequestExpect(f).PortName(framework.HTTPSimpleServerPort).RequestModify(func(r *request.Request) { |
| 104 | r.HTTP().Proxy("http://abc:123@127.0.0.1:" + strconv.Itoa(remotePort)) |
| 105 | }).Ensure() |
| 106 |
| 107 | // connect TCP server by CONNECT method |
| 108 | framework.NewRequestExpect(f).PortName(framework.TCPEchoServerPort).RequestModify(func(r *request.Request) { |
| 109 | r.TCP().Proxy("http://abc:123@127.0.0.1:" + strconv.Itoa(remotePort)) |
| 110 | }) |
| 111 | }) |
| 112 |
| 113 | ginkgo.It("socks5 proxy", func() { |
| 114 | serverConf := consts.LegacyDefaultServerConfig |
| 115 | clientConf := consts.LegacyDefaultClientConfig |
| 116 |
| 117 | remotePort := f.AllocPort() |
| 118 | clientConf += fmt.Sprintf(` |
| 119 | [tcp] |
| 120 | type = tcp |
| 121 | remote_port = %d |
| 122 | plugin = socks5 |
| 123 | plugin_user = abc |
| 124 | plugin_passwd = 123 |
| 125 | `, remotePort) |
| 126 |
| 127 | f.RunProcesses(serverConf, []string{clientConf}) |
| 128 |
| 129 | // http proxy, no auth info |
| 130 | framework.NewRequestExpect(f).PortName(framework.TCPEchoServerPort).RequestModify(func(r *request.Request) { |
| 131 | r.TCP().Proxy("socks5://127.0.0.1:" + strconv.Itoa(remotePort)) |
| 132 | }).ExpectError(true).Ensure() |
| 133 |
| 134 | // http proxy, correct auth |
| 135 | framework.NewRequestExpect(f).PortName(framework.TCPEchoServerPort).RequestModify(func(r *request.Request) { |
| 136 | r.TCP().Proxy("socks5://abc:123@127.0.0.1:" + strconv.Itoa(remotePort)) |
| 137 | }).Ensure() |
| 138 | }) |
| 139 |
| 140 | ginkgo.It("static_file", func() { |
| 141 | vhostPort := f.AllocPort() |
| 142 | serverConf := consts.LegacyDefaultServerConfig + fmt.Sprintf(` |
| 143 | vhost_http_port = %d |
| 144 | `, vhostPort) |
| 145 | clientConf := consts.LegacyDefaultClientConfig |
| 146 |
| 147 | remotePort := f.AllocPort() |
| 148 | f.WriteTempFile("test_static_file", "foo") |
| 149 | clientConf += fmt.Sprintf(` |
| 150 | [tcp] |
| 151 | type = tcp |
| 152 | remote_port = %d |
| 153 | plugin = static_file |
| 154 | plugin_local_path = %s |
| 155 |
| 156 | [http] |
| 157 | type = http |
| 158 | custom_domains = example.com |
| 159 | plugin = static_file |
| 160 | plugin_local_path = %s |
| 161 |
| 162 | [http-with-auth] |
| 163 | type = http |
| 164 | custom_domains = other.example.com |
| 165 | plugin = static_file |
| 166 | plugin_local_path = %s |
| 167 | plugin_http_user = abc |
| 168 | plugin_http_passwd = 123 |
| 169 | `, remotePort, f.TempDirectory, f.TempDirectory, f.TempDirectory) |
| 170 |
| 171 | f.RunProcesses(serverConf, []string{clientConf}) |
| 172 |
| 173 | // from tcp proxy |
| 174 | framework.NewRequestExpect(f).Request( |
| 175 | framework.NewHTTPRequest().HTTPPath("/test_static_file").Port(remotePort), |
| 176 | ).ExpectResp([]byte("foo")).Ensure() |
| 177 |
| 178 | // from http proxy without auth |
| 179 | framework.NewRequestExpect(f).Request( |
| 180 | framework.NewHTTPRequest().HTTPHost("example.com").HTTPPath("/test_static_file").Port(vhostPort), |
| 181 | ).ExpectResp([]byte("foo")).Ensure() |
| 182 |
| 183 | // from http proxy with auth |
| 184 | framework.NewRequestExpect(f).Request( |
| 185 | framework.NewHTTPRequest().HTTPHost("other.example.com").HTTPPath("/test_static_file").Port(vhostPort).HTTPAuth("abc", "123"), |
| 186 | ).ExpectResp([]byte("foo")).Ensure() |
| 187 | }) |
| 188 |
| 189 | ginkgo.It("http2https", func() { |
| 190 | serverConf := consts.LegacyDefaultServerConfig |
| 191 | vhostHTTPPort := f.AllocPort() |
| 192 | serverConf += fmt.Sprintf(` |
| 193 | vhost_http_port = %d |
| 194 | `, vhostHTTPPort) |
| 195 |
| 196 | localPort := f.AllocPort() |
| 197 | clientConf := consts.LegacyDefaultClientConfig + fmt.Sprintf(` |
| 198 | [http2https] |
| 199 | type = http |
| 200 | custom_domains = example.com |
| 201 | plugin = http2https |
| 202 | plugin_local_addr = 127.0.0.1:%d |
| 203 | `, localPort) |
| 204 |
| 205 | f.RunProcesses(serverConf, []string{clientConf}) |
| 206 |
| 207 | tlsConfig, err := transport.NewServerTLSConfig("", "", "") |
| 208 | framework.ExpectNoError(err) |
| 209 | localServer := httpserver.New( |
| 210 | httpserver.WithBindPort(localPort), |
| 211 | httpserver.WithTLSConfig(tlsConfig), |
| 212 | httpserver.WithResponse([]byte("test")), |
| 213 | ) |
| 214 | f.RunServer("", localServer) |
| 215 |
| 216 | framework.NewRequestExpect(f). |
| 217 | Port(vhostHTTPPort). |
| 218 | RequestModify(func(r *request.Request) { |
| 219 | r.HTTP().HTTPHost("example.com") |
| 220 | }). |
| 221 | ExpectResp([]byte("test")). |
| 222 | Ensure() |
| 223 | }) |
| 224 |
| 225 | ginkgo.It("https2http", func() { |
| 226 | generator := &cert.SelfSignedCertGenerator{} |
| 227 | artifacts, err := generator.Generate("example.com") |
| 228 | framework.ExpectNoError(err) |
| 229 | crtPath := f.WriteTempFile("server.crt", string(artifacts.Cert)) |
| 230 | keyPath := f.WriteTempFile("server.key", string(artifacts.Key)) |
| 231 |
| 232 | serverConf := consts.LegacyDefaultServerConfig |
| 233 | vhostHTTPSPort := f.AllocPort() |
| 234 | serverConf += fmt.Sprintf(` |
| 235 | vhost_https_port = %d |
| 236 | `, vhostHTTPSPort) |
| 237 |
| 238 | localPort := f.AllocPort() |
| 239 | clientConf := consts.LegacyDefaultClientConfig + fmt.Sprintf(` |
| 240 | [https2http] |
| 241 | type = https |
| 242 | custom_domains = example.com |
| 243 | plugin = https2http |
| 244 | plugin_local_addr = 127.0.0.1:%d |
| 245 | plugin_crt_path = %s |
| 246 | plugin_key_path = %s |
| 247 | `, localPort, crtPath, keyPath) |
| 248 |
| 249 | f.RunProcesses(serverConf, []string{clientConf}) |
| 250 |
| 251 | localServer := httpserver.New( |
| 252 | httpserver.WithBindPort(localPort), |
| 253 | httpserver.WithResponse([]byte("test")), |
| 254 | ) |
| 255 | f.RunServer("", localServer) |
| 256 |
| 257 | framework.NewRequestExpect(f). |
| 258 | Port(vhostHTTPSPort). |
| 259 | RequestModify(func(r *request.Request) { |
| 260 | r.HTTPS().HTTPHost("example.com").TLSConfig(&tls.Config{ |
| 261 | ServerName: "example.com", |
| 262 | InsecureSkipVerify: true, |
| 263 | }) |
| 264 | }). |
| 265 | ExpectResp([]byte("test")). |
| 266 | Ensure() |
| 267 | }) |
| 268 |
| 269 | ginkgo.It("https2https", func() { |
| 270 | generator := &cert.SelfSignedCertGenerator{} |
| 271 | artifacts, err := generator.Generate("example.com") |
| 272 | framework.ExpectNoError(err) |
| 273 | crtPath := f.WriteTempFile("server.crt", string(artifacts.Cert)) |
| 274 | keyPath := f.WriteTempFile("server.key", string(artifacts.Key)) |
| 275 |
| 276 | serverConf := consts.LegacyDefaultServerConfig |
| 277 | vhostHTTPSPort := f.AllocPort() |
| 278 | serverConf += fmt.Sprintf(` |
| 279 | vhost_https_port = %d |
| 280 | `, vhostHTTPSPort) |
| 281 |
| 282 | localPort := f.AllocPort() |
| 283 | clientConf := consts.LegacyDefaultClientConfig + fmt.Sprintf(` |
| 284 | [https2https] |
| 285 | type = https |
| 286 | custom_domains = example.com |
| 287 | plugin = https2https |
| 288 | plugin_local_addr = 127.0.0.1:%d |
| 289 | plugin_crt_path = %s |
| 290 | plugin_key_path = %s |
| 291 | `, localPort, crtPath, keyPath) |
| 292 |
| 293 | f.RunProcesses(serverConf, []string{clientConf}) |
| 294 |
| 295 | tlsConfig, err := transport.NewServerTLSConfig("", "", "") |
| 296 | framework.ExpectNoError(err) |
| 297 | localServer := httpserver.New( |
| 298 | httpserver.WithBindPort(localPort), |
| 299 | httpserver.WithResponse([]byte("test")), |
| 300 | httpserver.WithTLSConfig(tlsConfig), |
| 301 | ) |
| 302 | f.RunServer("", localServer) |
| 303 |
| 304 | framework.NewRequestExpect(f). |
| 305 | Port(vhostHTTPSPort). |
| 306 | RequestModify(func(r *request.Request) { |
| 307 | r.HTTPS().HTTPHost("example.com").TLSConfig(&tls.Config{ |
| 308 | ServerName: "example.com", |
| 309 | InsecureSkipVerify: true, |
| 310 | }) |
| 311 | }). |
| 312 | ExpectResp([]byte("test")). |
| 313 | Ensure() |
| 314 | }) |
| 315 | }) |