2021-06-20 23:57:41 +08:00
|
|
|
package features
|
|
|
|
|
|
|
|
import (
|
2021-06-21 19:27:26 +08:00
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
|
2023-02-27 14:44:16 +08:00
|
|
|
"github.com/onsi/ginkgo/v2"
|
2022-08-29 01:02:53 +08:00
|
|
|
pp "github.com/pires/go-proxyproto"
|
|
|
|
|
2021-06-21 19:27:26 +08:00
|
|
|
"github.com/fatedier/frp/pkg/util/log"
|
2021-06-20 23:57:41 +08:00
|
|
|
"github.com/fatedier/frp/test/e2e/framework"
|
2021-06-21 19:27:26 +08:00
|
|
|
"github.com/fatedier/frp/test/e2e/framework/consts"
|
|
|
|
"github.com/fatedier/frp/test/e2e/mock/server/httpserver"
|
|
|
|
"github.com/fatedier/frp/test/e2e/mock/server/streamserver"
|
|
|
|
"github.com/fatedier/frp/test/e2e/pkg/request"
|
|
|
|
"github.com/fatedier/frp/test/e2e/pkg/rpc"
|
2021-06-20 23:57:41 +08:00
|
|
|
)
|
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
var _ = ginkgo.Describe("[Feature: Real IP]", func() {
|
2021-06-20 23:57:41 +08:00
|
|
|
f := framework.NewDefaultFramework()
|
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
ginkgo.It("HTTP X-Forwarded-For", func() {
|
2021-06-21 19:27:26 +08:00
|
|
|
vhostHTTPPort := f.AllocPort()
|
2023-09-13 16:32:39 +08:00
|
|
|
serverConf := consts.LegacyDefaultServerConfig + fmt.Sprintf(`
|
2021-06-21 19:27:26 +08:00
|
|
|
vhost_http_port = %d
|
|
|
|
`, 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-Forwarded-For")))
|
2021-06-21 19:27:26 +08:00
|
|
|
})),
|
|
|
|
)
|
|
|
|
f.RunServer("", localServer)
|
|
|
|
|
2023-09-13 16:32:39 +08:00
|
|
|
clientConf := consts.LegacyDefaultClientConfig
|
2021-06-21 19:27:26 +08:00
|
|
|
clientConf += fmt.Sprintf(`
|
|
|
|
[test]
|
|
|
|
type = http
|
|
|
|
local_port = %d
|
|
|
|
custom_domains = 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")
|
|
|
|
}).
|
|
|
|
ExpectResp([]byte("127.0.0.1")).
|
|
|
|
Ensure()
|
2021-06-20 23:57:41 +08:00
|
|
|
})
|
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
ginkgo.Describe("Proxy Protocol", func() {
|
|
|
|
ginkgo.It("TCP", func() {
|
2023-09-13 16:32:39 +08:00
|
|
|
serverConf := consts.LegacyDefaultServerConfig
|
|
|
|
clientConf := consts.LegacyDefaultClientConfig
|
2021-06-21 19:27:26 +08:00
|
|
|
|
|
|
|
localPort := f.AllocPort()
|
|
|
|
localServer := streamserver.New(streamserver.TCP, streamserver.WithBindPort(localPort),
|
|
|
|
streamserver.WithCustomHandler(func(c net.Conn) {
|
|
|
|
defer c.Close()
|
|
|
|
rd := bufio.NewReader(c)
|
|
|
|
ppHeader, err := pp.Read(rd)
|
|
|
|
if err != nil {
|
2024-03-12 13:58:53 +08:00
|
|
|
log.Errorf("read proxy protocol error: %v", err)
|
2021-06-21 19:27:26 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
if _, err := rpc.ReadBytes(rd); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := []byte(ppHeader.SourceAddr.String())
|
2022-08-29 01:02:53 +08:00
|
|
|
_, _ = rpc.WriteBytes(c, buf)
|
2021-06-21 19:27:26 +08:00
|
|
|
}
|
|
|
|
}))
|
|
|
|
f.RunServer("", localServer)
|
|
|
|
|
|
|
|
remotePort := f.AllocPort()
|
|
|
|
clientConf += fmt.Sprintf(`
|
|
|
|
[tcp]
|
|
|
|
type = tcp
|
|
|
|
local_port = %d
|
|
|
|
remote_port = %d
|
|
|
|
proxy_protocol_version = v2
|
|
|
|
`, localPort, remotePort)
|
|
|
|
|
|
|
|
f.RunProcesses([]string{serverConf}, []string{clientConf})
|
|
|
|
|
|
|
|
framework.NewRequestExpect(f).Port(remotePort).Ensure(func(resp *request.Response) bool {
|
2024-03-12 13:58:53 +08:00
|
|
|
log.Tracef("ProxyProtocol get SourceAddr: %s", string(resp.Content))
|
2021-06-21 19:27:26 +08:00
|
|
|
addr, err := net.ResolveTCPAddr("tcp", string(resp.Content))
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if addr.IP.String() != "127.0.0.1" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
ginkgo.It("HTTP", func() {
|
2021-06-21 19:27:26 +08:00
|
|
|
vhostHTTPPort := f.AllocPort()
|
2023-09-13 16:32:39 +08:00
|
|
|
serverConf := consts.LegacyDefaultServerConfig + fmt.Sprintf(`
|
2021-06-21 19:27:26 +08:00
|
|
|
vhost_http_port = %d
|
|
|
|
`, vhostHTTPPort)
|
|
|
|
|
2023-09-13 16:32:39 +08:00
|
|
|
clientConf := consts.LegacyDefaultClientConfig
|
2021-06-21 19:27:26 +08:00
|
|
|
|
|
|
|
localPort := f.AllocPort()
|
|
|
|
var srcAddrRecord string
|
|
|
|
localServer := streamserver.New(streamserver.TCP, streamserver.WithBindPort(localPort),
|
|
|
|
streamserver.WithCustomHandler(func(c net.Conn) {
|
|
|
|
defer c.Close()
|
|
|
|
rd := bufio.NewReader(c)
|
|
|
|
ppHeader, err := pp.Read(rd)
|
|
|
|
if err != nil {
|
2024-03-12 13:58:53 +08:00
|
|
|
log.Errorf("read proxy protocol error: %v", err)
|
2021-06-21 19:27:26 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
srcAddrRecord = ppHeader.SourceAddr.String()
|
|
|
|
}))
|
|
|
|
f.RunServer("", localServer)
|
|
|
|
|
|
|
|
clientConf += fmt.Sprintf(`
|
|
|
|
[test]
|
|
|
|
type = http
|
|
|
|
local_port = %d
|
|
|
|
custom_domains = normal.example.com
|
|
|
|
proxy_protocol_version = v2
|
|
|
|
`, localPort)
|
|
|
|
|
|
|
|
f.RunProcesses([]string{serverConf}, []string{clientConf})
|
|
|
|
|
|
|
|
framework.NewRequestExpect(f).Port(vhostHTTPPort).RequestModify(func(r *request.Request) {
|
|
|
|
r.HTTP().HTTPHost("normal.example.com")
|
|
|
|
}).Ensure(framework.ExpectResponseCode(404))
|
|
|
|
|
2024-03-12 13:58:53 +08:00
|
|
|
log.Tracef("ProxyProtocol get SourceAddr: %s", srcAddrRecord)
|
2021-06-21 19:27:26 +08:00
|
|
|
addr, err := net.ResolveTCPAddr("tcp", srcAddrRecord)
|
|
|
|
framework.ExpectNoError(err, srcAddrRecord)
|
|
|
|
framework.ExpectEqualValues("127.0.0.1", addr.IP.String())
|
|
|
|
})
|
2021-06-20 23:57:41 +08:00
|
|
|
})
|
|
|
|
})
|