| 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.LegacyDefaultServerConfig + ` |
| 24 | tcpmux_httpconnect_port = %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.LegacyDefaultClientConfig |
| 57 | clientConf += fmt.Sprintf(` |
| 58 | [foo] |
| 59 | type = tcpmux |
| 60 | multiplexer = httpconnect |
| 61 | local_port = %d |
| 62 | custom_domains = normal.example.com |
| 63 | route_by_http_user = user1 |
| 64 |
| 65 | [bar] |
| 66 | type = tcpmux |
| 67 | multiplexer = httpconnect |
| 68 | local_port = %d |
| 69 | custom_domains = normal.example.com |
| 70 | route_by_http_user = user2 |
| 71 |
| 72 | [catchAll] |
| 73 | type = tcpmux |
| 74 | multiplexer = httpconnect |
| 75 | local_port = %d |
| 76 | custom_domains = normal.example.com |
| 77 | `, fooPort, barPort, otherPort) |
| 78 |
| 79 | f.RunProcesses(serverConf, []string{clientConf}) |
| 80 |
| 81 | // user1 |
| 82 | framework.NewRequestExpect(f).Explain("user1"). |
| 83 | RequestModify(func(r *request.Request) { |
| 84 | r.Addr("normal.example.com").Proxy(proxyURLWithAuth("user1", "", vhostPort)) |
| 85 | }). |
| 86 | ExpectResp([]byte("foo")). |
| 87 | Ensure() |
| 88 |
| 89 | // user2 |
| 90 | framework.NewRequestExpect(f).Explain("user2"). |
| 91 | RequestModify(func(r *request.Request) { |
| 92 | r.Addr("normal.example.com").Proxy(proxyURLWithAuth("user2", "", vhostPort)) |
| 93 | }). |
| 94 | ExpectResp([]byte("bar")). |
| 95 | Ensure() |
| 96 |
| 97 | // other user |
| 98 | framework.NewRequestExpect(f).Explain("other user"). |
| 99 | RequestModify(func(r *request.Request) { |
| 100 | r.Addr("normal.example.com").Proxy(proxyURLWithAuth("user3", "", vhostPort)) |
| 101 | }). |
| 102 | ExpectResp([]byte("other")). |
| 103 | Ensure() |
| 104 | }) |
| 105 |
| 106 | ginkgo.It("Proxy auth", func() { |
| 107 | vhostPort := f.AllocPort() |
| 108 | serverConf := getDefaultServerConf(vhostPort) |
| 109 |
| 110 | fooPort := f.AllocPort() |
| 111 | f.RunServer("", newServer(fooPort, "foo")) |
| 112 |
| 113 | clientConf := consts.LegacyDefaultClientConfig |
| 114 | clientConf += fmt.Sprintf(` |
| 115 | [test] |
| 116 | type = tcpmux |
| 117 | multiplexer = httpconnect |
| 118 | local_port = %d |
| 119 | custom_domains = normal.example.com |
| 120 | http_user = test |
| 121 | http_pwd = test |
| 122 | `, fooPort) |
| 123 |
| 124 | f.RunProcesses(serverConf, []string{clientConf}) |
| 125 |
| 126 | // not set auth header |
| 127 | framework.NewRequestExpect(f).Explain("no auth"). |
| 128 | RequestModify(func(r *request.Request) { |
| 129 | r.Addr("normal.example.com").Proxy(proxyURLWithAuth("", "", vhostPort)) |
| 130 | }). |
| 131 | ExpectError(true). |
| 132 | Ensure() |
| 133 |
| 134 | // set incorrect auth header |
| 135 | framework.NewRequestExpect(f).Explain("incorrect auth"). |
| 136 | RequestModify(func(r *request.Request) { |
| 137 | r.Addr("normal.example.com").Proxy(proxyURLWithAuth("test", "invalid", vhostPort)) |
| 138 | }). |
| 139 | ExpectError(true). |
| 140 | Ensure() |
| 141 |
| 142 | // set correct auth header |
| 143 | framework.NewRequestExpect(f).Explain("correct auth"). |
| 144 | RequestModify(func(r *request.Request) { |
| 145 | r.Addr("normal.example.com").Proxy(proxyURLWithAuth("test", "test", vhostPort)) |
| 146 | }). |
| 147 | ExpectResp([]byte("foo")). |
| 148 | Ensure() |
| 149 | }) |
| 150 |
| 151 | ginkgo.It("TCPMux Passthrough", func() { |
| 152 | vhostPort := f.AllocPort() |
| 153 | serverConf := getDefaultServerConf(vhostPort) |
| 154 | serverConf += ` |
| 155 | tcpmux_passthrough = true |
| 156 | ` |
| 157 |
| 158 | var ( |
| 159 | respErr error |
| 160 | connectRequestHost string |
| 161 | ) |
| 162 | newServer := func(port int) *streamserver.Server { |
| 163 | return streamserver.New( |
| 164 | streamserver.TCP, |
| 165 | streamserver.WithBindPort(port), |
| 166 | streamserver.WithCustomHandler(func(conn net.Conn) { |
| 167 | defer conn.Close() |
| 168 |
| 169 | // read HTTP CONNECT request |
| 170 | bufioReader := bufio.NewReader(conn) |
| 171 | req, err := http.ReadRequest(bufioReader) |
| 172 | if err != nil { |
| 173 | respErr = err |
| 174 | return |
| 175 | } |
| 176 | connectRequestHost = req.Host |
| 177 |
| 178 | // return ok response |
| 179 | res := httppkg.OkResponse() |
| 180 | if res.Body != nil { |
| 181 | defer res.Body.Close() |
| 182 | } |
| 183 | _ = res.Write(conn) |
| 184 |
| 185 | buf, err := rpc.ReadBytes(conn) |
| 186 | if err != nil { |
| 187 | respErr = err |
| 188 | return |
| 189 | } |
| 190 | _, _ = rpc.WriteBytes(conn, buf) |
| 191 | }), |
| 192 | ) |
| 193 | } |
| 194 |
| 195 | localPort := f.AllocPort() |
| 196 | f.RunServer("", newServer(localPort)) |
| 197 |
| 198 | clientConf := consts.LegacyDefaultClientConfig |
| 199 | clientConf += fmt.Sprintf(` |
| 200 | [test] |
| 201 | type = tcpmux |
| 202 | multiplexer = httpconnect |
| 203 | local_port = %d |
| 204 | custom_domains = normal.example.com |
| 205 | `, localPort) |
| 206 |
| 207 | f.RunProcesses(serverConf, []string{clientConf}) |
| 208 |
| 209 | framework.NewRequestExpect(f). |
| 210 | RequestModify(func(r *request.Request) { |
| 211 | r.Addr("normal.example.com").Proxy(proxyURLWithAuth("", "", vhostPort)).Body([]byte("frp")) |
| 212 | }). |
| 213 | ExpectResp([]byte("frp")). |
| 214 | Ensure() |
| 215 | framework.ExpectNoError(respErr) |
| 216 | framework.ExpectEqualValues(connectRequestHost, "normal.example.com") |
| 217 | }) |
| 218 | }) |