| 9 | "github.com/onsi/ginkgo/v2" |
| 10 |
| 11 | "github.com/fatedier/frp/test/e2e/framework" |
| 12 | "github.com/fatedier/frp/test/e2e/framework/consts" |
| 13 | "github.com/fatedier/frp/test/e2e/pkg/port" |
| 14 | ) |
| 15 |
| 16 | var _ = ginkgo.Describe("[Feature: WireProtocol]", func() { |
| 17 | f := framework.NewDefaultFramework() |
| 18 |
| 19 | ginkgo.It("v1 tcp and udp proxies", func() { |
| 20 | serverConf := consts.DefaultServerConfig |
| 21 | tcpPortName := port.GenName("WireV1TCP") |
| 22 | udpPortName := port.GenName("WireV1UDP") |
| 23 | clientConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 24 | transport.wireProtocol = "v1" |
| 25 |
| 26 | [[proxies]] |
| 27 | name = "tcp" |
| 28 | type = "tcp" |
| 29 | localPort = {{ .%s }} |
| 30 | remotePort = {{ .%s }} |
| 31 |
| 32 | [[proxies]] |
| 33 | name = "udp" |
| 34 | type = "udp" |
| 35 | localPort = {{ .%s }} |
| 36 | remotePort = {{ .%s }} |
| 37 | `, framework.TCPEchoServerPort, tcpPortName, framework.UDPEchoServerPort, udpPortName) |
| 38 |
| 39 | f.RunProcesses(serverConf, []string{clientConf}) |
| 40 |
| 41 | framework.NewRequestExpect(f).PortName(tcpPortName).Ensure() |
| 42 | framework.NewRequestExpect(f).Protocol("udp").PortName(udpPortName).Ensure() |
| 43 | }) |
| 44 |
| 45 | ginkgo.It("v2 tcp and udp proxies", func() { |
| 46 | serverConf := consts.DefaultServerConfig |
| 47 | tcpPortName := port.GenName("WireV2TCP") |
| 48 | udpPortName := port.GenName("WireV2UDP") |
| 49 | clientConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 50 | transport.wireProtocol = "v2" |
| 51 |
| 52 | [[proxies]] |
| 53 | name = "tcp" |
| 54 | type = "tcp" |
| 55 | localPort = {{ .%s }} |
| 56 | remotePort = {{ .%s }} |
| 57 |
| 58 | [[proxies]] |
| 59 | name = "udp" |
| 60 | type = "udp" |
| 61 | localPort = {{ .%s }} |
| 62 | remotePort = {{ .%s }} |
| 63 | `, framework.TCPEchoServerPort, tcpPortName, framework.UDPEchoServerPort, udpPortName) |
| 64 |
| 65 | f.RunProcesses(serverConf, []string{clientConf}) |
| 66 |
| 67 | framework.NewRequestExpect(f).PortName(tcpPortName).Ensure() |
| 68 | framework.NewRequestExpect(f).Protocol("udp").PortName(udpPortName).Ensure() |
| 69 | }) |
| 70 |
| 71 | ginkgo.It("v2 stcp visitor", func() { |
| 72 | serverConf := consts.DefaultServerConfig |
| 73 | bindPortName := port.GenName("WireV2STCP") |
| 74 | clientServerConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 75 | user = "user1" |
| 76 | transport.wireProtocol = "v2" |
| 77 |
| 78 | [[proxies]] |
| 79 | name = "stcp" |
| 80 | type = "stcp" |
| 81 | secretKey = "abc" |
| 82 | localPort = {{ .%s }} |
| 83 | `, framework.TCPEchoServerPort) |
| 84 | clientVisitorConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 85 | user = "user1" |
| 86 | transport.wireProtocol = "v2" |
| 87 |
| 88 | [[visitors]] |
| 89 | name = "stcp-visitor" |
| 90 | type = "stcp" |
| 91 | serverName = "stcp" |
| 92 | secretKey = "abc" |
| 93 | bindPort = {{ .%s }} |
| 94 | `, bindPortName) |
| 95 |
| 96 | f.RunProcesses(serverConf, []string{clientServerConf, clientVisitorConf}) |
| 97 |
| 98 | framework.NewRequestExpect(f).PortName(bindPortName).Ensure() |
| 99 | }) |
| 100 |
| 101 | for _, tc := range []struct { |
| 102 | name string |
| 103 | proxyWireConfig string |
| 104 | visitorWireConfig string |
| 105 | extraProxyConfig string |
| 106 | extraVisitorConfig string |
| 107 | }{ |
| 108 | { |
| 109 | name: "default sudp visitor", |
| 110 | }, |
| 111 | { |
| 112 | name: "v2 sudp visitor", |
| 113 | proxyWireConfig: `transport.wireProtocol = "v2"`, |
| 114 | visitorWireConfig: `transport.wireProtocol = "v2"`, |
| 115 | }, |
| 116 | { |
| 117 | name: "mixed sudp proxy v1 visitor v2", |
| 118 | proxyWireConfig: `transport.wireProtocol = "v1"`, |
| 119 | visitorWireConfig: `transport.wireProtocol = "v2"`, |
| 120 | extraProxyConfig: ` |
| 121 | transport.useEncryption = true |
| 122 | transport.useCompression = true |
| 123 | `, |
| 124 | extraVisitorConfig: ` |
| 125 | transport.useEncryption = true |
| 126 | transport.useCompression = true |
| 127 | `, |
| 128 | }, |
| 129 | { |
| 130 | name: "mixed sudp proxy v2 visitor v1", |
| 131 | proxyWireConfig: `transport.wireProtocol = "v2"`, |
| 132 | visitorWireConfig: `transport.wireProtocol = "v1"`, |
| 133 | }, |
| 134 | } { |
| 135 | ginkgo.It(tc.name, func() { |
| 136 | serverConf := consts.DefaultServerConfig |
| 137 | bindPortName := port.GenName("WireSUDP") |
| 138 | clientServerConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 139 | user = "user1" |
| 140 | %s |
| 141 |
| 142 | [[proxies]] |
| 143 | name = "sudp" |
| 144 | type = "sudp" |
| 145 | secretKey = "abc" |
| 146 | localPort = {{ .%s }} |
| 147 | %s |
| 148 | `, tc.proxyWireConfig, framework.UDPEchoServerPort, tc.extraProxyConfig) |
| 149 | clientVisitorConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 150 | user = "user1" |
| 151 | %s |
| 152 |
| 153 | [[visitors]] |
| 154 | name = "sudp-visitor" |
| 155 | type = "sudp" |
| 156 | serverName = "sudp" |
| 157 | secretKey = "abc" |
| 158 | bindPort = {{ .%s }} |
| 159 | %s |
| 160 | `, tc.visitorWireConfig, bindPortName, tc.extraVisitorConfig) |
| 161 |
| 162 | f.RunProcesses(serverConf, []string{clientServerConf, clientVisitorConf}) |
| 163 |
| 164 | framework.NewRequestExpect(f).Protocol("udp").PortName(bindPortName).Ensure() |
| 165 | }) |
| 166 | } |
| 167 |
| 168 | ginkgo.It("reports client wire protocol", func() { |
| 169 | webPort := f.AllocPort() |
| 170 | serverConf := consts.DefaultServerConfig + fmt.Sprintf(` |
| 171 | webServer.port = %d |
| 172 | `, webPort) |
| 173 |
| 174 | v1PortName := port.GenName("WireReportV1") |
| 175 | v1ClientConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 176 | clientID = "wire-v1" |
| 177 | transport.wireProtocol = "v1" |
| 178 |
| 179 | [[proxies]] |
| 180 | name = "v1" |
| 181 | type = "tcp" |
| 182 | localPort = {{ .%s }} |
| 183 | remotePort = {{ .%s }} |
| 184 | `, framework.TCPEchoServerPort, v1PortName) |
| 185 |
| 186 | v2PortName := port.GenName("WireReportV2") |
| 187 | v2ClientConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 188 | clientID = "wire-v2" |
| 189 | transport.wireProtocol = "v2" |
| 190 |
| 191 | [[proxies]] |
| 192 | name = "v2" |
| 193 | type = "tcp" |
| 194 | localPort = {{ .%s }} |
| 195 | remotePort = {{ .%s }} |
| 196 | `, framework.TCPEchoServerPort, v2PortName) |
| 197 |
| 198 | f.RunProcesses(serverConf, []string{v1ClientConf, v2ClientConf}) |
| 199 |
| 200 | framework.NewRequestExpect(f).PortName(v1PortName).Ensure() |
| 201 | framework.NewRequestExpect(f).PortName(v2PortName).Ensure() |
| 202 | expectClientWireProtocol(webPort, "wire-v1", "v1") |
| 203 | expectClientWireProtocol(webPort, "wire-v2", "v2") |
| 204 | }) |
| 205 | }) |
| 206 |
| 207 | type wireClientInfo struct { |
| 208 | ClientID string `json:"clientID"` |
| 209 | WireProtocol string `json:"wireProtocol"` |
| 210 | } |
| 211 |
| 212 | func expectClientWireProtocol(webPort int, clientID string, wireProtocol string) { |
| 213 | resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d/api/clients", webPort)) |
| 214 | framework.ExpectNoError(err) |
| 215 | defer resp.Body.Close() |
| 216 | framework.ExpectEqual(resp.StatusCode, 200) |
| 217 |
| 218 | content, err := io.ReadAll(resp.Body) |
| 219 | framework.ExpectNoError(err) |
| 220 |
| 221 | var clients []wireClientInfo |
| 222 | framework.ExpectNoError(json.Unmarshal(content, &clients)) |
| 223 | for _, client := range clients { |
| 224 | if client.ClientID == clientID { |
| 225 | framework.ExpectEqual(client.WireProtocol, wireProtocol) |
| 226 | return |
| 227 | } |
| 228 | } |
| 229 | framework.Failf("client %q not found in /api/clients response: %s", clientID, string(content)) |
| 230 | } |