frp/test/e2e/legacy/features/real_ip.go

152 lines
4.1 KiB
Go
Raw Normal View History

package features
import (
2021-06-21 13:27:26 +02:00
"bufio"
"fmt"
"net"
"net/http"
2023-02-27 07:44:16 +01:00
"github.com/onsi/ginkgo/v2"
2022-08-28 19:02:53 +02:00
pp "github.com/pires/go-proxyproto"
2021-06-21 13:27:26 +02:00
"github.com/fatedier/frp/pkg/util/log"
"github.com/fatedier/frp/test/e2e/framework"
2021-06-21 13:27:26 +02: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"
)
2022-08-28 19:02:53 +02:00
var _ = ginkgo.Describe("[Feature: Real IP]", func() {
f := framework.NewDefaultFramework()
2022-08-28 19:02:53 +02:00
ginkgo.It("HTTP X-Forwarded-For", func() {
2021-06-21 13:27:26 +02:00
vhostHTTPPort := f.AllocPort()
2023-09-13 10:32:39 +02:00
serverConf := consts.LegacyDefaultServerConfig + fmt.Sprintf(`
2021-06-21 13:27:26 +02: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-28 19:02:53 +02:00
_, _ = w.Write([]byte(req.Header.Get("X-Forwarded-For")))
2021-06-21 13:27:26 +02:00
})),
)
f.RunServer("", localServer)
2023-09-13 10:32:39 +02:00
clientConf := consts.LegacyDefaultClientConfig
2021-06-21 13:27:26 +02: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()
})
2022-08-28 19:02:53 +02:00
ginkgo.Describe("Proxy Protocol", func() {
ginkgo.It("TCP", func() {
2023-09-13 10:32:39 +02:00
serverConf := consts.LegacyDefaultServerConfig
clientConf := consts.LegacyDefaultClientConfig
2021-06-21 13:27:26 +02: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 06:58:53 +01:00
log.Errorf("read proxy protocol error: %v", err)
2021-06-21 13:27:26 +02:00
return
}
for {
if _, err := rpc.ReadBytes(rd); err != nil {
return
}
buf := []byte(ppHeader.SourceAddr.String())
2022-08-28 19:02:53 +02:00
_, _ = rpc.WriteBytes(c, buf)
2021-06-21 13:27:26 +02: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 06:58:53 +01:00
log.Tracef("ProxyProtocol get SourceAddr: %s", string(resp.Content))
2021-06-21 13:27:26 +02: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-28 19:02:53 +02:00
ginkgo.It("HTTP", func() {
2021-06-21 13:27:26 +02:00
vhostHTTPPort := f.AllocPort()
2023-09-13 10:32:39 +02:00
serverConf := consts.LegacyDefaultServerConfig + fmt.Sprintf(`
2021-06-21 13:27:26 +02:00
vhost_http_port = %d
`, vhostHTTPPort)
2023-09-13 10:32:39 +02:00
clientConf := consts.LegacyDefaultClientConfig
2021-06-21 13:27:26 +02: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 06:58:53 +01:00
log.Errorf("read proxy protocol error: %v", err)
2021-06-21 13:27:26 +02: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 06:58:53 +01:00
log.Tracef("ProxyProtocol get SourceAddr: %s", srcAddrRecord)
2021-06-21 13:27:26 +02:00
addr, err := net.ResolveTCPAddr("tcp", srcAddrRecord)
framework.ExpectNoError(err, srcAddrRecord)
framework.ExpectEqualValues("127.0.0.1", addr.IP.String())
})
})
})