| 9 | "github.com/onsi/ginkgo/v2" |
| 10 |
| 11 | httppkg "github.com/fatedier/frp/pkg/util/http" |
| 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/streamserver" |
| 15 | "github.com/fatedier/frp/test/e2e/pkg/request" |
| 16 | "github.com/fatedier/frp/test/e2e/pkg/rpc" |
| 17 | ) |
| 18 |
| 19 | var _ = ginkgo.Describe("[Feature: TCPMUX httpconnect]", func() { |
| 20 | f := framework.NewDefaultFramework() |
| 21 |
| 22 | getDefaultServerConf := func(httpconnectPort int) string { |
| 23 | conf := consts.DefaultServerConfig + ` |
| 24 | tcpmuxHTTPConnectPort = %d |
| 25 | ` |
| 26 | return fmt.Sprintf(conf, httpconnectPort) |
| 27 | } |
| 28 | newServer := func(port int, respContent string) *streamserver.Server { |
| 29 | return streamserver.New( |
| 30 | streamserver.TCP, |
| 31 | streamserver.WithBindPort(port), |
| 32 | streamserver.WithRespContent([]byte(respContent)), |
| 33 | ) |
| 34 | } |
| 35 |
| 36 | proxyURLWithAuth := func(username, password string, port int) string { |
| 37 | if username == "" { |
| 38 | return fmt.Sprintf("http://127.0.0.1:%d", port) |
| 39 | } |
| 40 | return fmt.Sprintf("http://%s:%s@127.0.0.1:%d", username, password, port) |
| 41 | } |
| 42 |
| 43 | ginkgo.It("Route by HTTP user", func() { |
| 44 | vhostPort := f.AllocPort() |
| 45 | serverConf := getDefaultServerConf(vhostPort) |
| 46 |
| 47 | fooPort := f.AllocPort() |
| 48 | f.RunServer("", newServer(fooPort, "foo")) |
| 49 |
| 50 | barPort := f.AllocPort() |
| 51 | f.RunServer("", newServer(barPort, "bar")) |
| 52 |
| 53 | otherPort := f.AllocPort() |
| 54 | f.RunServer("", newServer(otherPort, "other")) |
| 55 |
| 56 | clientConf := consts.DefaultClientConfig |
| 57 | clientConf += fmt.Sprintf(` |
| 58 | [[proxies]] |
| 59 | name = "foo" |
| 60 | type = "tcpmux" |
| 61 | multiplexer = "httpconnect" |
| 62 | localPort = %d |
| 63 | customDomains = ["normal.example.com"] |
| 64 | routeByHTTPUser = "user1" |
| 65 |
| 66 | [[proxies]] |
| 67 | name = "bar" |
| 68 | type = "tcpmux" |
| 69 | multiplexer = "httpconnect" |
| 70 | localPort = %d |
| 71 | customDomains = ["normal.example.com"] |
| 72 | routeByHTTPUser = "user2" |
| 73 |
| 74 | [[proxies]] |
| 75 | name = "catchAll" |
| 76 | type = "tcpmux" |
| 77 | multiplexer = "httpconnect" |
| 78 | localPort = %d |
| 79 | customDomains = ["normal.example.com"] |
| 80 | `, fooPort, barPort, otherPort) |
| 81 |
| 82 | f.RunProcesses(serverConf, []string{clientConf}) |
| 83 |
| 84 | // user1 |
| 85 | framework.NewRequestExpect(f).Explain("user1"). |
| 86 | RequestModify(func(r *request.Request) { |
| 87 | r.Addr("normal.example.com").Proxy(proxyURLWithAuth("user1", "", vhostPort)) |
| 88 | }). |
| 89 | ExpectResp([]byte("foo")). |
| 90 | Ensure() |
| 91 |
| 92 | // user2 |
| 93 | framework.NewRequestExpect(f).Explain("user2"). |
| 94 | RequestModify(func(r *request.Request) { |
| 95 | r.Addr("normal.example.com").Proxy(proxyURLWithAuth("user2", "", vhostPort)) |
| 96 | }). |
| 97 | ExpectResp([]byte("bar")). |
| 98 | Ensure() |
| 99 |
| 100 | // other user |
| 101 | framework.NewRequestExpect(f).Explain("other user"). |
| 102 | RequestModify(func(r *request.Request) { |
| 103 | r.Addr("normal.example.com").Proxy(proxyURLWithAuth("user3", "", vhostPort)) |
| 104 | }). |
| 105 | ExpectResp([]byte("other")). |
| 106 | Ensure() |
| 107 | }) |
| 108 |
| 109 | ginkgo.It("Proxy auth", func() { |
| 110 | vhostPort := f.AllocPort() |
| 111 | serverConf := getDefaultServerConf(vhostPort) |
| 112 |
| 113 | fooPort := f.AllocPort() |
| 114 | f.RunServer("", newServer(fooPort, "foo")) |
| 115 |
| 116 | clientConf := consts.DefaultClientConfig |
| 117 | clientConf += fmt.Sprintf(` |
| 118 | [[proxies]] |
| 119 | name = "test" |
| 120 | type = "tcpmux" |
| 121 | multiplexer = "httpconnect" |
| 122 | localPort = %d |
| 123 | customDomains = ["normal.example.com"] |
| 124 | httpUser = "test" |
| 125 | httpPassword = "test" |
| 126 | `, fooPort) |
| 127 |
| 128 | f.RunProcesses(serverConf, []string{clientConf}) |
| 129 |
| 130 | // not set auth header |
| 131 | framework.NewRequestExpect(f).Explain("no auth"). |
| 132 | RequestModify(func(r *request.Request) { |
| 133 | r.Addr("normal.example.com").Proxy(proxyURLWithAuth("", "", vhostPort)) |
| 134 | }). |
| 135 | ExpectError(true). |
| 136 | Ensure() |
| 137 |
| 138 | // set incorrect auth header |
| 139 | framework.NewRequestExpect(f).Explain("incorrect auth"). |
| 140 | RequestModify(func(r *request.Request) { |
| 141 | r.Addr("normal.example.com").Proxy(proxyURLWithAuth("test", "invalid", vhostPort)) |
| 142 | }). |
| 143 | ExpectError(true). |
| 144 | Ensure() |
| 145 |
| 146 | // set correct auth header |
| 147 | framework.NewRequestExpect(f).Explain("correct auth"). |
| 148 | RequestModify(func(r *request.Request) { |
| 149 | r.Addr("normal.example.com").Proxy(proxyURLWithAuth("test", "test", vhostPort)) |
| 150 | }). |
| 151 | ExpectResp([]byte("foo")). |
| 152 | Ensure() |
| 153 | }) |
| 154 |
| 155 | ginkgo.It("TCPMux Passthrough", func() { |
| 156 | vhostPort := f.AllocPort() |
| 157 | serverConf := getDefaultServerConf(vhostPort) |
| 158 | serverConf += ` |
| 159 | tcpmuxPassthrough = true |
| 160 | ` |
| 161 |
| 162 | var ( |
| 163 | respErr error |
| 164 | connectRequestHost string |
| 165 | ) |
| 166 | newServer := func(port int) *streamserver.Server { |
| 167 | return streamserver.New( |
| 168 | streamserver.TCP, |
| 169 | streamserver.WithBindPort(port), |
| 170 | streamserver.WithCustomHandler(func(conn net.Conn) { |
| 171 | defer conn.Close() |
| 172 |
| 173 | // read HTTP CONNECT request |
| 174 | bufioReader := bufio.NewReader(conn) |
| 175 | req, err := http.ReadRequest(bufioReader) |
| 176 | if err != nil { |
| 177 | respErr = err |
| 178 | return |
| 179 | } |
| 180 | connectRequestHost = req.Host |
| 181 |
| 182 | // return ok response |
| 183 | res := httppkg.OkResponse() |
| 184 | if res.Body != nil { |
| 185 | defer res.Body.Close() |
| 186 | } |
| 187 | _ = res.Write(conn) |
| 188 |
| 189 | buf, err := rpc.ReadBytes(conn) |
| 190 | if err != nil { |
| 191 | respErr = err |
| 192 | return |
| 193 | } |
| 194 | _, _ = rpc.WriteBytes(conn, buf) |
| 195 | }), |
| 196 | ) |
| 197 | } |
| 198 |
| 199 | localPort := f.AllocPort() |
| 200 | f.RunServer("", newServer(localPort)) |
| 201 |
| 202 | clientConf := consts.DefaultClientConfig |
| 203 | clientConf += fmt.Sprintf(` |
| 204 | [[proxies]] |
| 205 | name = "test" |
| 206 | type = "tcpmux" |
| 207 | multiplexer = "httpconnect" |
| 208 | localPort = %d |
| 209 | customDomains = ["normal.example.com"] |
| 210 | `, localPort) |
| 211 |
| 212 | f.RunProcesses(serverConf, []string{clientConf}) |
| 213 |
| 214 | framework.NewRequestExpect(f). |
| 215 | RequestModify(func(r *request.Request) { |
| 216 | r.Addr("normal.example.com").Proxy(proxyURLWithAuth("", "", vhostPort)).Body([]byte("frp")) |
| 217 | }). |
| 218 | ExpectResp([]byte("frp")). |
| 219 | Ensure() |
| 220 | framework.ExpectNoError(respErr) |
| 221 | framework.ExpectEqualValues(connectRequestHost, "normal.example.com") |
| 222 | }) |
| 223 | }) |