2021-06-18 16:48:36 +08:00
|
|
|
package basic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2024-04-11 20:19:08 +08:00
|
|
|
"time"
|
2021-06-18 16:48:36 +08:00
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
"github.com/gorilla/websocket"
|
2023-02-27 14:44:16 +08:00
|
|
|
"github.com/onsi/ginkgo/v2"
|
2022-08-29 01:02:53 +08:00
|
|
|
|
2021-06-18 16:48:36 +08:00
|
|
|
"github.com/fatedier/frp/test/e2e/framework"
|
|
|
|
"github.com/fatedier/frp/test/e2e/framework/consts"
|
|
|
|
"github.com/fatedier/frp/test/e2e/mock/server/httpserver"
|
|
|
|
"github.com/fatedier/frp/test/e2e/pkg/request"
|
|
|
|
)
|
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
var _ = ginkgo.Describe("[Feature: HTTP]", func() {
|
2021-06-18 16:48:36 +08:00
|
|
|
f := framework.NewDefaultFramework()
|
|
|
|
|
|
|
|
getDefaultServerConf := func(vhostHTTPPort int) string {
|
|
|
|
conf := consts.DefaultServerConfig + `
|
2023-09-13 16:32:39 +08:00
|
|
|
vhostHTTPPort = %d
|
2021-06-18 16:48:36 +08:00
|
|
|
`
|
|
|
|
return fmt.Sprintf(conf, vhostHTTPPort)
|
|
|
|
}
|
|
|
|
newHTTPServer := func(port int, respContent string) *httpserver.Server {
|
|
|
|
return httpserver.New(
|
|
|
|
httpserver.WithBindPort(port),
|
|
|
|
httpserver.WithHandler(framework.SpecifiedHTTPBodyHandler([]byte(respContent))),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
ginkgo.It("HTTP route by locations", func() {
|
2021-06-18 16:48:36 +08:00
|
|
|
vhostHTTPPort := f.AllocPort()
|
|
|
|
serverConf := getDefaultServerConf(vhostHTTPPort)
|
|
|
|
|
|
|
|
fooPort := f.AllocPort()
|
|
|
|
f.RunServer("", newHTTPServer(fooPort, "foo"))
|
|
|
|
|
|
|
|
barPort := f.AllocPort()
|
|
|
|
f.RunServer("", newHTTPServer(barPort, "bar"))
|
|
|
|
|
|
|
|
clientConf := consts.DefaultClientConfig
|
|
|
|
clientConf += fmt.Sprintf(`
|
2023-09-13 16:32:39 +08:00
|
|
|
[[proxies]]
|
|
|
|
name = "foo"
|
|
|
|
type = "http"
|
|
|
|
localPort = %d
|
|
|
|
customDomains = ["normal.example.com"]
|
|
|
|
locations = ["/","/foo"]
|
|
|
|
|
|
|
|
[[proxies]]
|
|
|
|
name = "bar"
|
|
|
|
type = "http"
|
|
|
|
localPort = %d
|
|
|
|
customDomains = ["normal.example.com"]
|
|
|
|
locations = ["/bar"]
|
2021-06-18 16:48:36 +08:00
|
|
|
`, fooPort, barPort)
|
|
|
|
|
|
|
|
f.RunProcesses([]string{serverConf}, []string{clientConf})
|
|
|
|
|
2022-05-26 23:57:30 +08:00
|
|
|
tests := []struct {
|
|
|
|
path string
|
|
|
|
expectResp string
|
|
|
|
desc string
|
|
|
|
}{
|
|
|
|
{path: "/foo", expectResp: "foo", desc: "foo path"},
|
|
|
|
{path: "/bar", expectResp: "bar", desc: "bar path"},
|
|
|
|
{path: "/other", expectResp: "foo", desc: "other path"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
framework.NewRequestExpect(f).Explain(test.desc).Port(vhostHTTPPort).
|
|
|
|
RequestModify(func(r *request.Request) {
|
|
|
|
r.HTTP().HTTPHost("normal.example.com").HTTPPath(test.path)
|
|
|
|
}).
|
|
|
|
ExpectResp([]byte(test.expectResp)).
|
|
|
|
Ensure()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
ginkgo.It("HTTP route by HTTP user", func() {
|
2022-05-26 23:57:30 +08:00
|
|
|
vhostHTTPPort := f.AllocPort()
|
|
|
|
serverConf := getDefaultServerConf(vhostHTTPPort)
|
|
|
|
|
|
|
|
fooPort := f.AllocPort()
|
|
|
|
f.RunServer("", newHTTPServer(fooPort, "foo"))
|
|
|
|
|
|
|
|
barPort := f.AllocPort()
|
|
|
|
f.RunServer("", newHTTPServer(barPort, "bar"))
|
|
|
|
|
|
|
|
otherPort := f.AllocPort()
|
|
|
|
f.RunServer("", newHTTPServer(otherPort, "other"))
|
|
|
|
|
|
|
|
clientConf := consts.DefaultClientConfig
|
|
|
|
clientConf += fmt.Sprintf(`
|
2023-09-13 16:32:39 +08:00
|
|
|
[[proxies]]
|
|
|
|
name = "foo"
|
|
|
|
type = "http"
|
|
|
|
localPort = %d
|
|
|
|
customDomains = ["normal.example.com"]
|
|
|
|
routeByHTTPUser = "user1"
|
|
|
|
|
|
|
|
[[proxies]]
|
|
|
|
name = "bar"
|
|
|
|
type = "http"
|
|
|
|
localPort = %d
|
|
|
|
customDomains = ["normal.example.com"]
|
|
|
|
routeByHTTPUser = "user2"
|
|
|
|
|
|
|
|
[[proxies]]
|
|
|
|
name = "catchAll"
|
|
|
|
type = "http"
|
|
|
|
localPort = %d
|
|
|
|
customDomains = ["normal.example.com"]
|
2022-05-26 23:57:30 +08:00
|
|
|
`, fooPort, barPort, otherPort)
|
|
|
|
|
|
|
|
f.RunProcesses([]string{serverConf}, []string{clientConf})
|
|
|
|
|
|
|
|
// user1
|
|
|
|
framework.NewRequestExpect(f).Explain("user1").Port(vhostHTTPPort).
|
2021-06-18 16:48:36 +08:00
|
|
|
RequestModify(func(r *request.Request) {
|
2022-05-26 23:57:30 +08:00
|
|
|
r.HTTP().HTTPHost("normal.example.com").HTTPAuth("user1", "")
|
2021-06-18 16:48:36 +08:00
|
|
|
}).
|
|
|
|
ExpectResp([]byte("foo")).
|
|
|
|
Ensure()
|
|
|
|
|
2022-05-26 23:57:30 +08:00
|
|
|
// user2
|
|
|
|
framework.NewRequestExpect(f).Explain("user2").Port(vhostHTTPPort).
|
2021-06-18 16:48:36 +08:00
|
|
|
RequestModify(func(r *request.Request) {
|
2022-05-26 23:57:30 +08:00
|
|
|
r.HTTP().HTTPHost("normal.example.com").HTTPAuth("user2", "")
|
2021-06-18 16:48:36 +08:00
|
|
|
}).
|
|
|
|
ExpectResp([]byte("bar")).
|
|
|
|
Ensure()
|
|
|
|
|
2022-05-26 23:57:30 +08:00
|
|
|
// other user
|
|
|
|
framework.NewRequestExpect(f).Explain("other user").Port(vhostHTTPPort).
|
2021-06-18 16:48:36 +08:00
|
|
|
RequestModify(func(r *request.Request) {
|
2022-05-26 23:57:30 +08:00
|
|
|
r.HTTP().HTTPHost("normal.example.com").HTTPAuth("user3", "")
|
2021-06-18 16:48:36 +08:00
|
|
|
}).
|
2022-05-26 23:57:30 +08:00
|
|
|
ExpectResp([]byte("other")).
|
2021-06-18 16:48:36 +08:00
|
|
|
Ensure()
|
|
|
|
})
|
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
ginkgo.It("HTTP Basic Auth", func() {
|
2021-06-18 16:48:36 +08:00
|
|
|
vhostHTTPPort := f.AllocPort()
|
|
|
|
serverConf := getDefaultServerConf(vhostHTTPPort)
|
|
|
|
|
|
|
|
clientConf := consts.DefaultClientConfig
|
|
|
|
clientConf += fmt.Sprintf(`
|
2023-09-13 16:32:39 +08:00
|
|
|
[[proxies]]
|
|
|
|
name = "test"
|
|
|
|
type = "http"
|
|
|
|
localPort = {{ .%s }}
|
|
|
|
customDomains = ["normal.example.com"]
|
|
|
|
httpUser = "test"
|
|
|
|
httpPassword = "test"
|
2021-06-18 16:48:36 +08:00
|
|
|
`, framework.HTTPSimpleServerPort)
|
|
|
|
|
|
|
|
f.RunProcesses([]string{serverConf}, []string{clientConf})
|
|
|
|
|
|
|
|
// not set auth header
|
|
|
|
framework.NewRequestExpect(f).Port(vhostHTTPPort).
|
|
|
|
RequestModify(func(r *request.Request) {
|
|
|
|
r.HTTP().HTTPHost("normal.example.com")
|
|
|
|
}).
|
|
|
|
Ensure(framework.ExpectResponseCode(401))
|
|
|
|
|
|
|
|
// set incorrect auth header
|
|
|
|
framework.NewRequestExpect(f).Port(vhostHTTPPort).
|
|
|
|
RequestModify(func(r *request.Request) {
|
2022-05-26 23:57:30 +08:00
|
|
|
r.HTTP().HTTPHost("normal.example.com").HTTPAuth("test", "invalid")
|
2021-06-18 16:48:36 +08:00
|
|
|
}).
|
|
|
|
Ensure(framework.ExpectResponseCode(401))
|
|
|
|
|
|
|
|
// set correct auth header
|
|
|
|
framework.NewRequestExpect(f).Port(vhostHTTPPort).
|
|
|
|
RequestModify(func(r *request.Request) {
|
2022-05-26 23:57:30 +08:00
|
|
|
r.HTTP().HTTPHost("normal.example.com").HTTPAuth("test", "test")
|
2021-06-18 16:48:36 +08:00
|
|
|
}).
|
|
|
|
Ensure()
|
|
|
|
})
|
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
ginkgo.It("Wildcard domain", func() {
|
2021-06-18 16:48:36 +08:00
|
|
|
vhostHTTPPort := f.AllocPort()
|
|
|
|
serverConf := getDefaultServerConf(vhostHTTPPort)
|
|
|
|
|
|
|
|
clientConf := consts.DefaultClientConfig
|
|
|
|
clientConf += fmt.Sprintf(`
|
2023-09-13 16:32:39 +08:00
|
|
|
[[proxies]]
|
|
|
|
name = "test"
|
|
|
|
type = "http"
|
|
|
|
localPort = {{ .%s }}
|
|
|
|
customDomains = ["*.example.com"]
|
2021-06-18 16:48:36 +08:00
|
|
|
`, framework.HTTPSimpleServerPort)
|
|
|
|
|
|
|
|
f.RunProcesses([]string{serverConf}, []string{clientConf})
|
|
|
|
|
|
|
|
// not match host
|
|
|
|
framework.NewRequestExpect(f).Port(vhostHTTPPort).
|
|
|
|
RequestModify(func(r *request.Request) {
|
|
|
|
r.HTTP().HTTPHost("not-match.test.com")
|
|
|
|
}).
|
|
|
|
Ensure(framework.ExpectResponseCode(404))
|
|
|
|
|
|
|
|
// test.example.com match *.example.com
|
|
|
|
framework.NewRequestExpect(f).Port(vhostHTTPPort).
|
|
|
|
RequestModify(func(r *request.Request) {
|
|
|
|
r.HTTP().HTTPHost("test.example.com")
|
|
|
|
}).
|
|
|
|
Ensure()
|
|
|
|
|
|
|
|
// sub.test.example.com match *.example.com
|
|
|
|
framework.NewRequestExpect(f).Port(vhostHTTPPort).
|
|
|
|
RequestModify(func(r *request.Request) {
|
|
|
|
r.HTTP().HTTPHost("sub.test.example.com")
|
|
|
|
}).
|
|
|
|
Ensure()
|
|
|
|
})
|
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
ginkgo.It("Subdomain", func() {
|
2021-06-18 16:48:36 +08:00
|
|
|
vhostHTTPPort := f.AllocPort()
|
|
|
|
serverConf := getDefaultServerConf(vhostHTTPPort)
|
|
|
|
serverConf += `
|
2023-09-13 16:32:39 +08:00
|
|
|
subdomainHost = "example.com"
|
2021-06-18 16:48:36 +08:00
|
|
|
`
|
|
|
|
|
|
|
|
fooPort := f.AllocPort()
|
|
|
|
f.RunServer("", newHTTPServer(fooPort, "foo"))
|
|
|
|
|
|
|
|
barPort := f.AllocPort()
|
|
|
|
f.RunServer("", newHTTPServer(barPort, "bar"))
|
|
|
|
|
|
|
|
clientConf := consts.DefaultClientConfig
|
|
|
|
clientConf += fmt.Sprintf(`
|
2023-09-13 16:32:39 +08:00
|
|
|
[[proxies]]
|
|
|
|
name = "foo"
|
|
|
|
type = "http"
|
|
|
|
localPort = %d
|
|
|
|
subdomain = "foo"
|
|
|
|
|
|
|
|
[[proxies]]
|
|
|
|
name = "bar"
|
|
|
|
type = "http"
|
|
|
|
localPort = %d
|
|
|
|
subdomain = "bar"
|
2021-06-18 16:48:36 +08:00
|
|
|
`, fooPort, barPort)
|
|
|
|
|
|
|
|
f.RunProcesses([]string{serverConf}, []string{clientConf})
|
|
|
|
|
|
|
|
// foo
|
|
|
|
framework.NewRequestExpect(f).Explain("foo subdomain").Port(vhostHTTPPort).
|
|
|
|
RequestModify(func(r *request.Request) {
|
|
|
|
r.HTTP().HTTPHost("foo.example.com")
|
|
|
|
}).
|
|
|
|
ExpectResp([]byte("foo")).
|
|
|
|
Ensure()
|
|
|
|
|
|
|
|
// bar
|
|
|
|
framework.NewRequestExpect(f).Explain("bar subdomain").Port(vhostHTTPPort).
|
|
|
|
RequestModify(func(r *request.Request) {
|
|
|
|
r.HTTP().HTTPHost("bar.example.com")
|
|
|
|
}).
|
|
|
|
ExpectResp([]byte("bar")).
|
|
|
|
Ensure()
|
|
|
|
})
|
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
ginkgo.It("Modify headers", func() {
|
2021-06-18 16:48:36 +08:00
|
|
|
vhostHTTPPort := f.AllocPort()
|
|
|
|
serverConf := getDefaultServerConf(vhostHTTPPort)
|
|
|
|
|
|
|
|
localPort := f.AllocPort()
|
|
|
|
localServer := httpserver.New(
|
|
|
|
httpserver.WithBindPort(localPort),
|
|
|
|
httpserver.WithHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
2022-08-29 01:02:53 +08:00
|
|
|
_, _ = w.Write([]byte(req.Header.Get("X-From-Where")))
|
2021-06-18 16:48:36 +08:00
|
|
|
})),
|
|
|
|
)
|
|
|
|
f.RunServer("", localServer)
|
|
|
|
|
|
|
|
clientConf := consts.DefaultClientConfig
|
|
|
|
clientConf += fmt.Sprintf(`
|
2023-09-13 16:32:39 +08:00
|
|
|
[[proxies]]
|
|
|
|
name = "test"
|
|
|
|
type = "http"
|
|
|
|
localPort = %d
|
|
|
|
customDomains = ["normal.example.com"]
|
|
|
|
requestHeaders.set.x-from-where = "frp"
|
2021-06-18 16:48:36 +08:00
|
|
|
`, localPort)
|
|
|
|
|
|
|
|
f.RunProcesses([]string{serverConf}, []string{clientConf})
|
|
|
|
|
|
|
|
// not set auth header
|
|
|
|
framework.NewRequestExpect(f).Port(vhostHTTPPort).
|
|
|
|
RequestModify(func(r *request.Request) {
|
|
|
|
r.HTTP().HTTPHost("normal.example.com")
|
|
|
|
}).
|
|
|
|
ExpectResp([]byte("frp")). // local http server will write this X-From-Where header to response body
|
|
|
|
Ensure()
|
|
|
|
})
|
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
ginkgo.It("Host Header Rewrite", func() {
|
2021-06-18 16:48:36 +08:00
|
|
|
vhostHTTPPort := f.AllocPort()
|
|
|
|
serverConf := getDefaultServerConf(vhostHTTPPort)
|
|
|
|
|
|
|
|
localPort := f.AllocPort()
|
|
|
|
localServer := httpserver.New(
|
|
|
|
httpserver.WithBindPort(localPort),
|
|
|
|
httpserver.WithHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
2022-08-29 01:02:53 +08:00
|
|
|
_, _ = w.Write([]byte(req.Host))
|
2021-06-18 16:48:36 +08:00
|
|
|
})),
|
|
|
|
)
|
|
|
|
f.RunServer("", localServer)
|
|
|
|
|
|
|
|
clientConf := consts.DefaultClientConfig
|
|
|
|
clientConf += fmt.Sprintf(`
|
2023-09-13 16:32:39 +08:00
|
|
|
[[proxies]]
|
|
|
|
name = "test"
|
|
|
|
type = "http"
|
|
|
|
localPort = %d
|
|
|
|
customDomains = ["normal.example.com"]
|
|
|
|
hostHeaderRewrite = "rewrite.example.com"
|
2021-06-18 16:48:36 +08:00
|
|
|
`, localPort)
|
|
|
|
|
|
|
|
f.RunProcesses([]string{serverConf}, []string{clientConf})
|
|
|
|
|
|
|
|
framework.NewRequestExpect(f).Port(vhostHTTPPort).
|
|
|
|
RequestModify(func(r *request.Request) {
|
|
|
|
r.HTTP().HTTPHost("normal.example.com")
|
|
|
|
}).
|
|
|
|
ExpectResp([]byte("rewrite.example.com")). // local http server will write host header to response body
|
|
|
|
Ensure()
|
|
|
|
})
|
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
ginkgo.It("Websocket protocol", func() {
|
2021-06-18 16:48:36 +08:00
|
|
|
vhostHTTPPort := f.AllocPort()
|
|
|
|
serverConf := getDefaultServerConf(vhostHTTPPort)
|
|
|
|
|
|
|
|
upgrader := websocket.Upgrader{}
|
|
|
|
|
|
|
|
localPort := f.AllocPort()
|
|
|
|
localServer := httpserver.New(
|
|
|
|
httpserver.WithBindPort(localPort),
|
|
|
|
httpserver.WithHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
c, err := upgrader.Upgrade(w, req, nil)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
for {
|
|
|
|
mt, message, err := c.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = c.WriteMessage(mt, message)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})),
|
|
|
|
)
|
|
|
|
|
|
|
|
f.RunServer("", localServer)
|
|
|
|
|
|
|
|
clientConf := consts.DefaultClientConfig
|
|
|
|
clientConf += fmt.Sprintf(`
|
2023-09-13 16:32:39 +08:00
|
|
|
[[proxies]]
|
|
|
|
name = "test"
|
|
|
|
type = "http"
|
|
|
|
localPort = %d
|
|
|
|
customDomains = ["127.0.0.1"]
|
2021-06-18 16:48:36 +08:00
|
|
|
`, localPort)
|
|
|
|
|
|
|
|
f.RunProcesses([]string{serverConf}, []string{clientConf})
|
|
|
|
|
|
|
|
u := url.URL{Scheme: "ws", Host: "127.0.0.1:" + strconv.Itoa(vhostHTTPPort)}
|
|
|
|
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
|
|
|
|
framework.ExpectNoError(err)
|
|
|
|
|
|
|
|
err = c.WriteMessage(websocket.TextMessage, []byte(consts.TestString))
|
|
|
|
framework.ExpectNoError(err)
|
|
|
|
|
|
|
|
_, msg, err := c.ReadMessage()
|
|
|
|
framework.ExpectNoError(err)
|
|
|
|
framework.ExpectEqualValues(consts.TestString, string(msg))
|
|
|
|
})
|
2024-04-11 20:19:08 +08:00
|
|
|
|
|
|
|
ginkgo.It("vhostHTTPTimeout", func() {
|
|
|
|
vhostHTTPPort := f.AllocPort()
|
|
|
|
serverConf := getDefaultServerConf(vhostHTTPPort)
|
|
|
|
serverConf += `
|
|
|
|
vhostHTTPTimeout = 2
|
|
|
|
`
|
|
|
|
|
|
|
|
delayDuration := 0 * time.Second
|
|
|
|
localPort := f.AllocPort()
|
|
|
|
localServer := httpserver.New(
|
|
|
|
httpserver.WithBindPort(localPort),
|
|
|
|
httpserver.WithHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
time.Sleep(delayDuration)
|
|
|
|
_, _ = w.Write([]byte(req.Host))
|
|
|
|
})),
|
|
|
|
)
|
|
|
|
f.RunServer("", localServer)
|
|
|
|
|
|
|
|
clientConf := consts.DefaultClientConfig
|
|
|
|
clientConf += fmt.Sprintf(`
|
|
|
|
[[proxies]]
|
|
|
|
name = "test"
|
|
|
|
type = "http"
|
|
|
|
localPort = %d
|
|
|
|
customDomains = ["normal.example.com"]
|
|
|
|
`, localPort)
|
|
|
|
|
|
|
|
f.RunProcesses([]string{serverConf}, []string{clientConf})
|
|
|
|
|
|
|
|
framework.NewRequestExpect(f).Port(vhostHTTPPort).
|
|
|
|
RequestModify(func(r *request.Request) {
|
|
|
|
r.HTTP().HTTPHost("normal.example.com").HTTP().Timeout(time.Second)
|
|
|
|
}).
|
|
|
|
ExpectResp([]byte("normal.example.com")).
|
|
|
|
Ensure()
|
|
|
|
|
|
|
|
delayDuration = 3 * time.Second
|
|
|
|
framework.NewRequestExpect(f).Port(vhostHTTPPort).
|
|
|
|
RequestModify(func(r *request.Request) {
|
|
|
|
r.HTTP().HTTPHost("normal.example.com").HTTP().Timeout(5 * time.Second)
|
|
|
|
}).
|
|
|
|
Ensure(framework.ExpectResponseCode(504))
|
|
|
|
})
|
2021-06-18 16:48:36 +08:00
|
|
|
})
|