| 9 | "github.com/gorilla/websocket" |
| 10 | "github.com/onsi/ginkgo/v2" |
| 11 |
| 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/request" |
| 16 | ) |
| 17 |
| 18 | var _ = ginkgo.Describe("[Feature: HTTP]", func() { |
| 19 | f := framework.NewDefaultFramework() |
| 20 |
| 21 | getDefaultServerConf := func(vhostHTTPPort int) string { |
| 22 | conf := consts.LegacyDefaultServerConfig + ` |
| 23 | vhost_http_port = %d |
| 24 | ` |
| 25 | return fmt.Sprintf(conf, vhostHTTPPort) |
| 26 | } |
| 27 | newHTTPServer := func(port int, respContent string) *httpserver.Server { |
| 28 | return httpserver.New( |
| 29 | httpserver.WithBindPort(port), |
| 30 | httpserver.WithHandler(framework.SpecifiedHTTPBodyHandler([]byte(respContent))), |
| 31 | ) |
| 32 | } |
| 33 |
| 34 | ginkgo.It("HTTP route by locations", func() { |
| 35 | vhostHTTPPort := f.AllocPort() |
| 36 | serverConf := getDefaultServerConf(vhostHTTPPort) |
| 37 |
| 38 | fooPort := f.AllocPort() |
| 39 | f.RunServer("", newHTTPServer(fooPort, "foo")) |
| 40 |
| 41 | barPort := f.AllocPort() |
| 42 | f.RunServer("", newHTTPServer(barPort, "bar")) |
| 43 |
| 44 | clientConf := consts.LegacyDefaultClientConfig |
| 45 | clientConf += fmt.Sprintf(` |
| 46 | [foo] |
| 47 | type = http |
| 48 | local_port = %d |
| 49 | custom_domains = normal.example.com |
| 50 | locations = /,/foo |
| 51 |
| 52 | [bar] |
| 53 | type = http |
| 54 | local_port = %d |
| 55 | custom_domains = normal.example.com |
| 56 | locations = /bar |
| 57 | `, fooPort, barPort) |
| 58 |
| 59 | f.RunProcesses(serverConf, []string{clientConf}) |
| 60 |
| 61 | tests := []struct { |
| 62 | path string |
| 63 | expectResp string |
| 64 | desc string |
| 65 | }{ |
| 66 | {path: "/foo", expectResp: "foo", desc: "foo path"}, |
| 67 | {path: "/bar", expectResp: "bar", desc: "bar path"}, |
| 68 | {path: "/other", expectResp: "foo", desc: "other path"}, |
| 69 | } |
| 70 |
| 71 | for _, test := range tests { |
| 72 | framework.NewRequestExpect(f).Explain(test.desc).Port(vhostHTTPPort). |
| 73 | RequestModify(func(r *request.Request) { |
| 74 | r.HTTP().HTTPHost("normal.example.com").HTTPPath(test.path) |
| 75 | }). |
| 76 | ExpectResp([]byte(test.expectResp)). |
| 77 | Ensure() |
| 78 | } |
| 79 | }) |
| 80 |
| 81 | ginkgo.It("HTTP route by HTTP user", func() { |
| 82 | vhostHTTPPort := f.AllocPort() |
| 83 | serverConf := getDefaultServerConf(vhostHTTPPort) |
| 84 |
| 85 | fooPort := f.AllocPort() |
| 86 | f.RunServer("", newHTTPServer(fooPort, "foo")) |
| 87 |
| 88 | barPort := f.AllocPort() |
| 89 | f.RunServer("", newHTTPServer(barPort, "bar")) |
| 90 |
| 91 | otherPort := f.AllocPort() |
| 92 | f.RunServer("", newHTTPServer(otherPort, "other")) |
| 93 |
| 94 | clientConf := consts.LegacyDefaultClientConfig |
| 95 | clientConf += fmt.Sprintf(` |
| 96 | [foo] |
| 97 | type = http |
| 98 | local_port = %d |
| 99 | custom_domains = normal.example.com |
| 100 | route_by_http_user = user1 |
| 101 |
| 102 | [bar] |
| 103 | type = http |
| 104 | local_port = %d |
| 105 | custom_domains = normal.example.com |
| 106 | route_by_http_user = user2 |
| 107 |
| 108 | [catchAll] |
| 109 | type = http |
| 110 | local_port = %d |
| 111 | custom_domains = normal.example.com |
| 112 | `, fooPort, barPort, otherPort) |
| 113 |
| 114 | f.RunProcesses(serverConf, []string{clientConf}) |
| 115 |
| 116 | // user1 |
| 117 | framework.NewRequestExpect(f).Explain("user1").Port(vhostHTTPPort). |
| 118 | RequestModify(func(r *request.Request) { |
| 119 | r.HTTP().HTTPHost("normal.example.com").HTTPAuth("user1", "") |
| 120 | }). |
| 121 | ExpectResp([]byte("foo")). |
| 122 | Ensure() |
| 123 |
| 124 | // user2 |
| 125 | framework.NewRequestExpect(f).Explain("user2").Port(vhostHTTPPort). |
| 126 | RequestModify(func(r *request.Request) { |
| 127 | r.HTTP().HTTPHost("normal.example.com").HTTPAuth("user2", "") |
| 128 | }). |
| 129 | ExpectResp([]byte("bar")). |
| 130 | Ensure() |
| 131 |
| 132 | // other user |
| 133 | framework.NewRequestExpect(f).Explain("other user").Port(vhostHTTPPort). |
| 134 | RequestModify(func(r *request.Request) { |
| 135 | r.HTTP().HTTPHost("normal.example.com").HTTPAuth("user3", "") |
| 136 | }). |
| 137 | ExpectResp([]byte("other")). |
| 138 | Ensure() |
| 139 | }) |
| 140 |
| 141 | ginkgo.It("HTTP Basic Auth", func() { |
| 142 | vhostHTTPPort := f.AllocPort() |
| 143 | serverConf := getDefaultServerConf(vhostHTTPPort) |
| 144 |
| 145 | clientConf := consts.LegacyDefaultClientConfig |
| 146 | clientConf += fmt.Sprintf(` |
| 147 | [test] |
| 148 | type = http |
| 149 | local_port = {{ .%s }} |
| 150 | custom_domains = normal.example.com |
| 151 | http_user = test |
| 152 | http_pwd = test |
| 153 | `, framework.HTTPSimpleServerPort) |
| 154 |
| 155 | f.RunProcesses(serverConf, []string{clientConf}) |
| 156 |
| 157 | // not set auth header |
| 158 | framework.NewRequestExpect(f).Port(vhostHTTPPort). |
| 159 | RequestModify(func(r *request.Request) { |
| 160 | r.HTTP().HTTPHost("normal.example.com") |
| 161 | }). |
| 162 | Ensure(framework.ExpectResponseCode(401)) |
| 163 |
| 164 | // set incorrect auth header |
| 165 | framework.NewRequestExpect(f).Port(vhostHTTPPort). |
| 166 | RequestModify(func(r *request.Request) { |
| 167 | r.HTTP().HTTPHost("normal.example.com").HTTPAuth("test", "invalid") |
| 168 | }). |
| 169 | Ensure(framework.ExpectResponseCode(401)) |
| 170 |
| 171 | // set correct auth header |
| 172 | framework.NewRequestExpect(f).Port(vhostHTTPPort). |
| 173 | RequestModify(func(r *request.Request) { |
| 174 | r.HTTP().HTTPHost("normal.example.com").HTTPAuth("test", "test") |
| 175 | }). |
| 176 | Ensure() |
| 177 | }) |
| 178 |
| 179 | ginkgo.It("Wildcard domain", func() { |
| 180 | vhostHTTPPort := f.AllocPort() |
| 181 | serverConf := getDefaultServerConf(vhostHTTPPort) |
| 182 |
| 183 | clientConf := consts.LegacyDefaultClientConfig |
| 184 | clientConf += fmt.Sprintf(` |
| 185 | [test] |
| 186 | type = http |
| 187 | local_port = {{ .%s }} |
| 188 | custom_domains = *.example.com |
| 189 | `, framework.HTTPSimpleServerPort) |
| 190 |
| 191 | f.RunProcesses(serverConf, []string{clientConf}) |
| 192 |
| 193 | // not match host |
| 194 | framework.NewRequestExpect(f).Port(vhostHTTPPort). |
| 195 | RequestModify(func(r *request.Request) { |
| 196 | r.HTTP().HTTPHost("not-match.test.com") |
| 197 | }). |
| 198 | Ensure(framework.ExpectResponseCode(404)) |
| 199 |
| 200 | // test.example.com match *.example.com |
| 201 | framework.NewRequestExpect(f).Port(vhostHTTPPort). |
| 202 | RequestModify(func(r *request.Request) { |
| 203 | r.HTTP().HTTPHost("test.example.com") |
| 204 | }). |
| 205 | Ensure() |
| 206 |
| 207 | // sub.test.example.com match *.example.com |
| 208 | framework.NewRequestExpect(f).Port(vhostHTTPPort). |
| 209 | RequestModify(func(r *request.Request) { |
| 210 | r.HTTP().HTTPHost("sub.test.example.com") |
| 211 | }). |
| 212 | Ensure() |
| 213 | }) |
| 214 |
| 215 | ginkgo.It("Subdomain", func() { |
| 216 | vhostHTTPPort := f.AllocPort() |
| 217 | serverConf := getDefaultServerConf(vhostHTTPPort) |
| 218 | serverConf += ` |
| 219 | subdomain_host = example.com |
| 220 | ` |
| 221 |
| 222 | fooPort := f.AllocPort() |
| 223 | f.RunServer("", newHTTPServer(fooPort, "foo")) |
| 224 |
| 225 | barPort := f.AllocPort() |
| 226 | f.RunServer("", newHTTPServer(barPort, "bar")) |
| 227 |
| 228 | clientConf := consts.LegacyDefaultClientConfig |
| 229 | clientConf += fmt.Sprintf(` |
| 230 | [foo] |
| 231 | type = http |
| 232 | local_port = %d |
| 233 | subdomain = foo |
| 234 |
| 235 | [bar] |
| 236 | type = http |
| 237 | local_port = %d |
| 238 | subdomain = bar |
| 239 | `, fooPort, barPort) |
| 240 |
| 241 | f.RunProcesses(serverConf, []string{clientConf}) |
| 242 |
| 243 | // foo |
| 244 | framework.NewRequestExpect(f).Explain("foo subdomain").Port(vhostHTTPPort). |
| 245 | RequestModify(func(r *request.Request) { |
| 246 | r.HTTP().HTTPHost("foo.example.com") |
| 247 | }). |
| 248 | ExpectResp([]byte("foo")). |
| 249 | Ensure() |
| 250 |
| 251 | // bar |
| 252 | framework.NewRequestExpect(f).Explain("bar subdomain").Port(vhostHTTPPort). |
| 253 | RequestModify(func(r *request.Request) { |
| 254 | r.HTTP().HTTPHost("bar.example.com") |
| 255 | }). |
| 256 | ExpectResp([]byte("bar")). |
| 257 | Ensure() |
| 258 | }) |
| 259 |
| 260 | ginkgo.It("Modify headers", func() { |
| 261 | vhostHTTPPort := f.AllocPort() |
| 262 | serverConf := getDefaultServerConf(vhostHTTPPort) |
| 263 |
| 264 | localPort := f.AllocPort() |
| 265 | localServer := httpserver.New( |
| 266 | httpserver.WithBindPort(localPort), |
| 267 | httpserver.WithHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 268 | _, _ = w.Write([]byte(req.Header.Get("X-From-Where"))) |
| 269 | })), |
| 270 | ) |
| 271 | f.RunServer("", localServer) |
| 272 |
| 273 | clientConf := consts.LegacyDefaultClientConfig |
| 274 | clientConf += fmt.Sprintf(` |
| 275 | [test] |
| 276 | type = http |
| 277 | local_port = %d |
| 278 | custom_domains = normal.example.com |
| 279 | header_X-From-Where = frp |
| 280 | `, localPort) |
| 281 |
| 282 | f.RunProcesses(serverConf, []string{clientConf}) |
| 283 |
| 284 | // not set auth header |
| 285 | framework.NewRequestExpect(f).Port(vhostHTTPPort). |
| 286 | RequestModify(func(r *request.Request) { |
| 287 | r.HTTP().HTTPHost("normal.example.com") |
| 288 | }). |
| 289 | ExpectResp([]byte("frp")). // local http server will write this X-From-Where header to response body |
| 290 | Ensure() |
| 291 | }) |
| 292 |
| 293 | ginkgo.It("Host Header Rewrite", func() { |
| 294 | vhostHTTPPort := f.AllocPort() |
| 295 | serverConf := getDefaultServerConf(vhostHTTPPort) |
| 296 |
| 297 | localPort := f.AllocPort() |
| 298 | localServer := httpserver.New( |
| 299 | httpserver.WithBindPort(localPort), |
| 300 | httpserver.WithHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 301 | _, _ = w.Write([]byte(req.Host)) |
| 302 | })), |
| 303 | ) |
| 304 | f.RunServer("", localServer) |
| 305 |
| 306 | clientConf := consts.LegacyDefaultClientConfig |
| 307 | clientConf += fmt.Sprintf(` |
| 308 | [test] |
| 309 | type = http |
| 310 | local_port = %d |
| 311 | custom_domains = normal.example.com |
| 312 | host_header_rewrite = rewrite.example.com |
| 313 | `, localPort) |
| 314 |
| 315 | f.RunProcesses(serverConf, []string{clientConf}) |
| 316 |
| 317 | framework.NewRequestExpect(f).Port(vhostHTTPPort). |
| 318 | RequestModify(func(r *request.Request) { |
| 319 | r.HTTP().HTTPHost("normal.example.com") |
| 320 | }). |
| 321 | ExpectResp([]byte("rewrite.example.com")). // local http server will write host header to response body |
| 322 | Ensure() |
| 323 | }) |
| 324 |
| 325 | ginkgo.It("Websocket protocol", func() { |
| 326 | vhostHTTPPort := f.AllocPort() |
| 327 | serverConf := getDefaultServerConf(vhostHTTPPort) |
| 328 |
| 329 | upgrader := websocket.Upgrader{} |
| 330 |
| 331 | localPort := f.AllocPort() |
| 332 | localServer := httpserver.New( |
| 333 | httpserver.WithBindPort(localPort), |
| 334 | httpserver.WithHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 335 | c, err := upgrader.Upgrade(w, req, nil) |
| 336 | if err != nil { |
| 337 | return |
| 338 | } |
| 339 | defer c.Close() |
| 340 | for { |
| 341 | mt, message, err := c.ReadMessage() |
| 342 | if err != nil { |
| 343 | break |
| 344 | } |
| 345 | err = c.WriteMessage(mt, message) |
| 346 | if err != nil { |
| 347 | break |
| 348 | } |
| 349 | } |
| 350 | })), |
| 351 | ) |
| 352 |
| 353 | f.RunServer("", localServer) |
| 354 |
| 355 | clientConf := consts.LegacyDefaultClientConfig |
| 356 | clientConf += fmt.Sprintf(` |
| 357 | [test] |
| 358 | type = http |
| 359 | local_port = %d |
| 360 | custom_domains = 127.0.0.1 |
| 361 | `, localPort) |
| 362 |
| 363 | f.RunProcesses(serverConf, []string{clientConf}) |
| 364 |
| 365 | u := url.URL{Scheme: "ws", Host: "127.0.0.1:" + strconv.Itoa(vhostHTTPPort)} |
| 366 | c, _, err := websocket.DefaultDialer.Dial(u.String(), nil) |
| 367 | framework.ExpectNoError(err) |
| 368 |
| 369 | err = c.WriteMessage(websocket.TextMessage, []byte(consts.TestString)) |
| 370 | framework.ExpectNoError(err) |
| 371 |
| 372 | _, msg, err := c.ReadMessage() |
| 373 | framework.ExpectNoError(err) |
| 374 | framework.ExpectEqualValues(consts.TestString, string(msg)) |
| 375 | }) |
| 376 | }) |