| 9 | "github.com/onsi/ginkgo/v2" |
| 10 | pp "github.com/pires/go-proxyproto" |
| 11 |
| 12 | "github.com/fatedier/frp/pkg/util/log" |
| 13 | "github.com/fatedier/frp/test/e2e/framework" |
| 14 | "github.com/fatedier/frp/test/e2e/framework/consts" |
| 15 | "github.com/fatedier/frp/test/e2e/mock/server/httpserver" |
| 16 | "github.com/fatedier/frp/test/e2e/mock/server/streamserver" |
| 17 | "github.com/fatedier/frp/test/e2e/pkg/request" |
| 18 | "github.com/fatedier/frp/test/e2e/pkg/rpc" |
| 19 | ) |
| 20 |
| 21 | var _ = ginkgo.Describe("[Feature: Real IP]", func() { |
| 22 | f := framework.NewDefaultFramework() |
| 23 |
| 24 | ginkgo.It("HTTP X-Forwarded-For", func() { |
| 25 | vhostHTTPPort := f.AllocPort() |
| 26 | serverConf := consts.LegacyDefaultServerConfig + fmt.Sprintf(` |
| 27 | vhost_http_port = %d |
| 28 | `, vhostHTTPPort) |
| 29 |
| 30 | localPort := f.AllocPort() |
| 31 | localServer := httpserver.New( |
| 32 | httpserver.WithBindPort(localPort), |
| 33 | httpserver.WithHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 34 | _, _ = w.Write([]byte(req.Header.Get("X-Forwarded-For"))) |
| 35 | })), |
| 36 | ) |
| 37 | f.RunServer("", localServer) |
| 38 |
| 39 | clientConf := consts.LegacyDefaultClientConfig |
| 40 | clientConf += fmt.Sprintf(` |
| 41 | [test] |
| 42 | type = http |
| 43 | local_port = %d |
| 44 | custom_domains = normal.example.com |
| 45 | `, localPort) |
| 46 |
| 47 | f.RunProcesses(serverConf, []string{clientConf}) |
| 48 |
| 49 | framework.NewRequestExpect(f).Port(vhostHTTPPort). |
| 50 | RequestModify(func(r *request.Request) { |
| 51 | r.HTTP().HTTPHost("normal.example.com") |
| 52 | }). |
| 53 | ExpectResp([]byte("127.0.0.1")). |
| 54 | Ensure() |
| 55 | }) |
| 56 |
| 57 | ginkgo.Describe("Proxy Protocol", func() { |
| 58 | ginkgo.It("TCP", func() { |
| 59 | serverConf := consts.LegacyDefaultServerConfig |
| 60 | clientConf := consts.LegacyDefaultClientConfig |
| 61 |
| 62 | localPort := f.AllocPort() |
| 63 | localServer := streamserver.New(streamserver.TCP, streamserver.WithBindPort(localPort), |
| 64 | streamserver.WithCustomHandler(func(c net.Conn) { |
| 65 | defer c.Close() |
| 66 | rd := bufio.NewReader(c) |
| 67 | ppHeader, err := pp.Read(rd) |
| 68 | if err != nil { |
| 69 | log.Errorf("read proxy protocol error: %v", err) |
| 70 | return |
| 71 | } |
| 72 |
| 73 | for { |
| 74 | if _, err := rpc.ReadBytes(rd); err != nil { |
| 75 | return |
| 76 | } |
| 77 |
| 78 | buf := []byte(ppHeader.SourceAddr.String()) |
| 79 | _, _ = rpc.WriteBytes(c, buf) |
| 80 | } |
| 81 | })) |
| 82 | f.RunServer("", localServer) |
| 83 |
| 84 | remotePort := f.AllocPort() |
| 85 | clientConf += fmt.Sprintf(` |
| 86 | [tcp] |
| 87 | type = tcp |
| 88 | local_port = %d |
| 89 | remote_port = %d |
| 90 | proxy_protocol_version = v2 |
| 91 | `, localPort, remotePort) |
| 92 |
| 93 | f.RunProcesses(serverConf, []string{clientConf}) |
| 94 |
| 95 | framework.NewRequestExpect(f).Port(remotePort).Ensure(func(resp *request.Response) bool { |
| 96 | log.Tracef("proxy protocol get SourceAddr: %s", string(resp.Content)) |
| 97 | addr, err := net.ResolveTCPAddr("tcp", string(resp.Content)) |
| 98 | if err != nil { |
| 99 | return false |
| 100 | } |
| 101 | if addr.IP.String() != "127.0.0.1" { |
| 102 | return false |
| 103 | } |
| 104 | return true |
| 105 | }) |
| 106 | }) |
| 107 |
| 108 | ginkgo.It("HTTP", func() { |
| 109 | vhostHTTPPort := f.AllocPort() |
| 110 | serverConf := consts.LegacyDefaultServerConfig + fmt.Sprintf(` |
| 111 | vhost_http_port = %d |
| 112 | `, vhostHTTPPort) |
| 113 |
| 114 | clientConf := consts.LegacyDefaultClientConfig |
| 115 |
| 116 | localPort := f.AllocPort() |
| 117 | var srcAddrRecord string |
| 118 | localServer := streamserver.New(streamserver.TCP, streamserver.WithBindPort(localPort), |
| 119 | streamserver.WithCustomHandler(func(c net.Conn) { |
| 120 | defer c.Close() |
| 121 | rd := bufio.NewReader(c) |
| 122 | ppHeader, err := pp.Read(rd) |
| 123 | if err != nil { |
| 124 | log.Errorf("read proxy protocol error: %v", err) |
| 125 | return |
| 126 | } |
| 127 | srcAddrRecord = ppHeader.SourceAddr.String() |
| 128 | })) |
| 129 | f.RunServer("", localServer) |
| 130 |
| 131 | clientConf += fmt.Sprintf(` |
| 132 | [test] |
| 133 | type = http |
| 134 | local_port = %d |
| 135 | custom_domains = normal.example.com |
| 136 | proxy_protocol_version = v2 |
| 137 | `, localPort) |
| 138 |
| 139 | f.RunProcesses(serverConf, []string{clientConf}) |
| 140 |
| 141 | framework.NewRequestExpect(f).Port(vhostHTTPPort).RequestModify(func(r *request.Request) { |
| 142 | r.HTTP().HTTPHost("normal.example.com") |
| 143 | }).Ensure(framework.ExpectResponseCode(404)) |
| 144 |
| 145 | log.Tracef("proxy protocol get SourceAddr: %s", srcAddrRecord) |
| 146 | addr, err := net.ResolveTCPAddr("tcp", srcAddrRecord) |
| 147 | framework.ExpectNoError(err, srcAddrRecord) |
| 148 | framework.ExpectEqualValues("127.0.0.1", addr.IP.String()) |
| 149 | }) |
| 150 | }) |
| 151 | }) |