| 9 |
| 10 | "github.com/onsi/ginkgo/v2" |
| 11 |
| 12 | "github.com/fatedier/frp/pkg/transport" |
| 13 | "github.com/fatedier/frp/test/e2e/framework" |
| 14 | "github.com/fatedier/frp/test/e2e/framework/consts" |
| 15 | "github.com/fatedier/frp/test/e2e/mock/server/httpserver" |
| 16 | "github.com/fatedier/frp/test/e2e/mock/server/streamserver" |
| 17 | "github.com/fatedier/frp/test/e2e/pkg/request" |
| 18 | "github.com/fatedier/frp/test/e2e/pkg/ssh" |
| 19 | ) |
| 20 |
| 21 | var _ = ginkgo.Describe("[Feature: SSH Tunnel]", func() { |
| 22 | f := framework.NewDefaultFramework() |
| 23 |
| 24 | ginkgo.It("tcp", func() { |
| 25 | sshPort := f.AllocPort() |
| 26 | serverConf := consts.DefaultServerConfig + fmt.Sprintf(` |
| 27 | sshTunnelGateway.bindPort = %d |
| 28 | `, sshPort) |
| 29 |
| 30 | f.RunProcesses(serverConf, nil) |
| 31 | framework.ExpectNoError(framework.WaitForTCPReady(net.JoinHostPort("127.0.0.1", strconv.Itoa(sshPort)), 5*time.Second)) |
| 32 |
| 33 | localPort := f.PortByName(framework.TCPEchoServerPort) |
| 34 | remotePort := f.AllocPort() |
| 35 | tc := ssh.NewTunnelClient( |
| 36 | fmt.Sprintf("127.0.0.1:%d", localPort), |
| 37 | fmt.Sprintf("127.0.0.1:%d", sshPort), |
| 38 | fmt.Sprintf("tcp --remote-port %d", remotePort), |
| 39 | ) |
| 40 | framework.ExpectNoError(tc.Start()) |
| 41 | defer tc.Close() |
| 42 |
| 43 | time.Sleep(time.Second) |
| 44 | framework.NewRequestExpect(f).Port(remotePort).Ensure() |
| 45 | }) |
| 46 |
| 47 | ginkgo.It("http", func() { |
| 48 | sshPort := f.AllocPort() |
| 49 | vhostPort := f.AllocPort() |
| 50 | serverConf := consts.DefaultServerConfig + fmt.Sprintf(` |
| 51 | vhostHTTPPort = %d |
| 52 | sshTunnelGateway.bindPort = %d |
| 53 | `, vhostPort, sshPort) |
| 54 |
| 55 | f.RunProcesses(serverConf, nil) |
| 56 | framework.ExpectNoError(framework.WaitForTCPReady(net.JoinHostPort("127.0.0.1", strconv.Itoa(sshPort)), 5*time.Second)) |
| 57 |
| 58 | localPort := f.PortByName(framework.HTTPSimpleServerPort) |
| 59 | tc := ssh.NewTunnelClient( |
| 60 | fmt.Sprintf("127.0.0.1:%d", localPort), |
| 61 | fmt.Sprintf("127.0.0.1:%d", sshPort), |
| 62 | "http --custom-domain test.example.com", |
| 63 | ) |
| 64 | framework.ExpectNoError(tc.Start()) |
| 65 | defer tc.Close() |
| 66 |
| 67 | time.Sleep(time.Second) |
| 68 | framework.NewRequestExpect(f).Port(vhostPort). |
| 69 | RequestModify(func(r *request.Request) { |
| 70 | r.HTTP().HTTPHost("test.example.com") |
| 71 | }). |
| 72 | Ensure() |
| 73 | }) |
| 74 |
| 75 | ginkgo.It("https", func() { |
| 76 | sshPort := f.AllocPort() |
| 77 | vhostPort := f.AllocPort() |
| 78 | serverConf := consts.DefaultServerConfig + fmt.Sprintf(` |
| 79 | vhostHTTPSPort = %d |
| 80 | sshTunnelGateway.bindPort = %d |
| 81 | `, vhostPort, sshPort) |
| 82 |
| 83 | f.RunProcesses(serverConf, nil) |
| 84 | framework.ExpectNoError(framework.WaitForTCPReady(net.JoinHostPort("127.0.0.1", strconv.Itoa(sshPort)), 5*time.Second)) |
| 85 |
| 86 | localPort := f.AllocPort() |
| 87 | testDomain := "test.example.com" |
| 88 | tc := ssh.NewTunnelClient( |
| 89 | fmt.Sprintf("127.0.0.1:%d", localPort), |
| 90 | fmt.Sprintf("127.0.0.1:%d", sshPort), |
| 91 | fmt.Sprintf("https --custom-domain %s", testDomain), |
| 92 | ) |
| 93 | framework.ExpectNoError(tc.Start()) |
| 94 | defer tc.Close() |
| 95 |
| 96 | tlsConfig, err := transport.NewServerTLSConfig("", "", "") |
| 97 | framework.ExpectNoError(err) |
| 98 | localServer := httpserver.New( |
| 99 | httpserver.WithBindPort(localPort), |
| 100 | httpserver.WithTLSConfig(tlsConfig), |
| 101 | httpserver.WithResponse([]byte("test")), |
| 102 | ) |
| 103 | f.RunServer("", localServer) |
| 104 |
| 105 | time.Sleep(time.Second) |
| 106 | framework.NewRequestExpect(f). |
| 107 | Port(vhostPort). |
| 108 | RequestModify(func(r *request.Request) { |
| 109 | r.HTTPS().HTTPHost(testDomain).TLSConfig(&tls.Config{ |
| 110 | ServerName: testDomain, |
| 111 | InsecureSkipVerify: true, |
| 112 | }) |
| 113 | }). |
| 114 | ExpectResp([]byte("test")). |
| 115 | Ensure() |
| 116 | }) |
| 117 |
| 118 | ginkgo.It("tcpmux", func() { |
| 119 | sshPort := f.AllocPort() |
| 120 | tcpmuxPort := f.AllocPort() |
| 121 | serverConf := consts.DefaultServerConfig + fmt.Sprintf(` |
| 122 | tcpmuxHTTPConnectPort = %d |
| 123 | sshTunnelGateway.bindPort = %d |
| 124 | `, tcpmuxPort, sshPort) |
| 125 |
| 126 | f.RunProcesses(serverConf, nil) |
| 127 | framework.ExpectNoError(framework.WaitForTCPReady(net.JoinHostPort("127.0.0.1", strconv.Itoa(sshPort)), 5*time.Second)) |
| 128 |
| 129 | localPort := f.AllocPort() |
| 130 | testDomain := "test.example.com" |
| 131 | tc := ssh.NewTunnelClient( |
| 132 | fmt.Sprintf("127.0.0.1:%d", localPort), |
| 133 | fmt.Sprintf("127.0.0.1:%d", sshPort), |
| 134 | fmt.Sprintf("tcpmux --mux=httpconnect --custom-domain %s", testDomain), |
| 135 | ) |
| 136 | framework.ExpectNoError(tc.Start()) |
| 137 | defer tc.Close() |
| 138 |
| 139 | localServer := streamserver.New( |
| 140 | streamserver.TCP, |
| 141 | streamserver.WithBindPort(localPort), |
| 142 | streamserver.WithRespContent([]byte("test")), |
| 143 | ) |
| 144 | f.RunServer("", localServer) |
| 145 |
| 146 | time.Sleep(time.Second) |
| 147 | // Request without HTTP connect should get error |
| 148 | framework.NewRequestExpect(f). |
| 149 | Port(tcpmuxPort). |
| 150 | ExpectError(true). |
| 151 | Explain("request without HTTP connect expect error"). |
| 152 | Ensure() |
| 153 |
| 154 | proxyURL := fmt.Sprintf("http://127.0.0.1:%d", tcpmuxPort) |
| 155 | // Request with incorrect connect hostname |
| 156 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 157 | r.Addr("invalid").Proxy(proxyURL) |
| 158 | }).ExpectError(true).Explain("request without HTTP connect expect error").Ensure() |
| 159 |
| 160 | // Request with correct connect hostname |
| 161 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 162 | r.Addr(testDomain).Proxy(proxyURL) |
| 163 | }).ExpectResp([]byte("test")).Ensure() |
| 164 | }) |
| 165 |
| 166 | ginkgo.It("stcp", func() { |
| 167 | sshPort := f.AllocPort() |
| 168 | serverConf := consts.DefaultServerConfig + fmt.Sprintf(` |
| 169 | sshTunnelGateway.bindPort = %d |
| 170 | `, sshPort) |
| 171 |
| 172 | bindPort := f.AllocPort() |
| 173 | visitorConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 174 | [[visitors]] |
| 175 | name = "stcp-test-visitor" |
| 176 | type = "stcp" |
| 177 | serverName = "stcp-test" |
| 178 | secretKey = "abcdefg" |
| 179 | bindPort = %d |
| 180 | `, bindPort) |
| 181 |
| 182 | f.RunProcesses(serverConf, []string{visitorConf}) |
| 183 | framework.ExpectNoError(framework.WaitForTCPReady(net.JoinHostPort("127.0.0.1", strconv.Itoa(sshPort)), 5*time.Second)) |
| 184 |
| 185 | localPort := f.PortByName(framework.TCPEchoServerPort) |
| 186 | tc := ssh.NewTunnelClient( |
| 187 | fmt.Sprintf("127.0.0.1:%d", localPort), |
| 188 | fmt.Sprintf("127.0.0.1:%d", sshPort), |
| 189 | "stcp -n stcp-test --sk=abcdefg --allow-users=\"*\"", |
| 190 | ) |
| 191 | framework.ExpectNoError(tc.Start()) |
| 192 | defer tc.Close() |
| 193 |
| 194 | time.Sleep(time.Second) |
| 195 |
| 196 | framework.NewRequestExpect(f). |
| 197 | Port(bindPort). |
| 198 | Ensure() |
| 199 | }) |
| 200 | }) |