| 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 | "github.com/fatedier/frp/test/e2e/pkg/request" |
| 15 | ) |
| 16 |
| 17 | var _ = ginkgo.Describe("[Feature: Server Manager]", func() { |
| 18 | f := framework.NewDefaultFramework() |
| 19 |
| 20 | ginkgo.It("Ports Whitelist", func() { |
| 21 | serverConf := consts.DefaultServerConfig |
| 22 | clientConf := consts.DefaultClientConfig |
| 23 | tcpPortNotAllowed := f.AllocPortExcludingRanges([2]int{10000, 11000}, [2]int{11002, 11002}, [2]int{12000, 13000}) |
| 24 | udpPortNotAllowed := f.AllocPortExcludingRanges([2]int{10000, 11000}, [2]int{11002, 11002}, [2]int{12000, 13000}) |
| 25 |
| 26 | serverConf += ` |
| 27 | allowPorts = [ |
| 28 | { start = 10000, end = 11000 }, |
| 29 | { single = 11002 }, |
| 30 | { start = 12000, end = 13000 }, |
| 31 | ] |
| 32 | ` |
| 33 |
| 34 | tcpPortName := port.GenName("TCP", port.WithRangePorts(10000, 11000)) |
| 35 | udpPortName := port.GenName("UDP", port.WithRangePorts(12000, 13000)) |
| 36 | clientConf += fmt.Sprintf(` |
| 37 | [[proxies]] |
| 38 | name = "tcp-allowed-in-range" |
| 39 | type = "tcp" |
| 40 | localPort = {{ .%s }} |
| 41 | remotePort = {{ .%s }} |
| 42 | `, framework.TCPEchoServerPort, tcpPortName) |
| 43 | clientConf += fmt.Sprintf(` |
| 44 | [[proxies]] |
| 45 | name = "tcp-port-not-allowed" |
| 46 | type = "tcp" |
| 47 | localPort = {{ .%s }} |
| 48 | remotePort = %d |
| 49 | `, framework.TCPEchoServerPort, tcpPortNotAllowed) |
| 50 | clientConf += fmt.Sprintf(` |
| 51 | [[proxies]] |
| 52 | name = "tcp-port-unavailable" |
| 53 | type = "tcp" |
| 54 | localPort = {{ .%s }} |
| 55 | remotePort = {{ .%s }} |
| 56 | `, framework.TCPEchoServerPort, consts.PortServerName) |
| 57 | clientConf += fmt.Sprintf(` |
| 58 | [[proxies]] |
| 59 | name = "udp-allowed-in-range" |
| 60 | type = "udp" |
| 61 | localPort = {{ .%s }} |
| 62 | remotePort = {{ .%s }} |
| 63 | `, framework.UDPEchoServerPort, udpPortName) |
| 64 | clientConf += fmt.Sprintf(` |
| 65 | [[proxies]] |
| 66 | name = "udp-port-not-allowed" |
| 67 | type = "udp" |
| 68 | localPort = {{ .%s }} |
| 69 | remotePort = %d |
| 70 | `, framework.UDPEchoServerPort, udpPortNotAllowed) |
| 71 |
| 72 | f.RunProcesses(serverConf, []string{clientConf}) |
| 73 |
| 74 | // TCP |
| 75 | // Allowed in range |
| 76 | framework.NewRequestExpect(f).PortName(tcpPortName).Ensure() |
| 77 |
| 78 | // Not Allowed |
| 79 | framework.NewRequestExpect(f).Port(tcpPortNotAllowed).ExpectError(true).Ensure() |
| 80 |
| 81 | // Unavailable, already bind by frps |
| 82 | framework.NewRequestExpect(f).PortName(consts.PortServerName).ExpectError(true).Ensure() |
| 83 |
| 84 | // UDP |
| 85 | // Allowed in range |
| 86 | framework.NewRequestExpect(f).Protocol("udp").PortName(udpPortName).Ensure() |
| 87 |
| 88 | // Not Allowed |
| 89 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 90 | r.UDP().Port(udpPortNotAllowed) |
| 91 | }).ExpectError(true).Ensure() |
| 92 | }) |
| 93 |
| 94 | ginkgo.It("Alloc Random Port", func() { |
| 95 | serverConf := consts.DefaultServerConfig |
| 96 | clientConf := consts.DefaultClientConfig |
| 97 |
| 98 | adminPort := f.AllocPort() |
| 99 | clientConf += fmt.Sprintf(` |
| 100 | webServer.port = %d |
| 101 |
| 102 | [[proxies]] |
| 103 | name = "tcp" |
| 104 | type = "tcp" |
| 105 | localPort = {{ .%s }} |
| 106 |
| 107 | [[proxies]] |
| 108 | name = "udp" |
| 109 | type = "udp" |
| 110 | localPort = {{ .%s }} |
| 111 | `, adminPort, framework.TCPEchoServerPort, framework.UDPEchoServerPort) |
| 112 |
| 113 | f.RunProcesses(serverConf, []string{clientConf}) |
| 114 |
| 115 | client := f.APIClientForFrpc(adminPort) |
| 116 |
| 117 | // tcp random port |
| 118 | status, err := client.GetProxyStatus(context.Background(), "tcp") |
| 119 | framework.ExpectNoError(err) |
| 120 |
| 121 | _, portStr, err := net.SplitHostPort(status.RemoteAddr) |
| 122 | framework.ExpectNoError(err) |
| 123 | port, err := strconv.Atoi(portStr) |
| 124 | framework.ExpectNoError(err) |
| 125 |
| 126 | framework.NewRequestExpect(f).Port(port).Ensure() |
| 127 |
| 128 | // udp random port |
| 129 | status, err = client.GetProxyStatus(context.Background(), "udp") |
| 130 | framework.ExpectNoError(err) |
| 131 |
| 132 | _, portStr, err = net.SplitHostPort(status.RemoteAddr) |
| 133 | framework.ExpectNoError(err) |
| 134 | port, err = strconv.Atoi(portStr) |
| 135 | framework.ExpectNoError(err) |
| 136 |
| 137 | framework.NewRequestExpect(f).Protocol("udp").Port(port).Ensure() |
| 138 | }) |
| 139 |
| 140 | ginkgo.It("Port Reuse", func() { |
| 141 | serverConf := consts.DefaultServerConfig |
| 142 | // Use same port as PortServer |
| 143 | serverConf += fmt.Sprintf(` |
| 144 | vhostHTTPPort = {{ .%s }} |
| 145 | `, consts.PortServerName) |
| 146 |
| 147 | clientConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 148 | [[proxies]] |
| 149 | name = "http" |
| 150 | type = "http" |
| 151 | localPort = {{ .%s }} |
| 152 | customDomains = ["example.com"] |
| 153 | `, framework.HTTPSimpleServerPort) |
| 154 |
| 155 | f.RunProcesses(serverConf, []string{clientConf}) |
| 156 |
| 157 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 158 | r.HTTP().HTTPHost("example.com") |
| 159 | }).PortName(consts.PortServerName).Ensure() |
| 160 | }) |
| 161 |
| 162 | ginkgo.It("healthz", func() { |
| 163 | serverConf := consts.DefaultServerConfig |
| 164 | dashboardPort := f.AllocPort() |
| 165 |
| 166 | // Use same port as PortServer |
| 167 | serverConf += fmt.Sprintf(` |
| 168 | vhostHTTPPort = {{ .%s }} |
| 169 | webServer.addr = "0.0.0.0" |
| 170 | webServer.port = %d |
| 171 | webServer.user = "admin" |
| 172 | webServer.password = "admin" |
| 173 | `, consts.PortServerName, dashboardPort) |
| 174 |
| 175 | clientConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 176 | [[proxies]] |
| 177 | name = "http" |
| 178 | type = "http" |
| 179 | localPort = {{ .%s }} |
| 180 | customDomains = ["example.com"] |
| 181 | `, framework.HTTPSimpleServerPort) |
| 182 |
| 183 | f.RunProcesses(serverConf, []string{clientConf}) |
| 184 |
| 185 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 186 | r.HTTP().HTTPPath("/healthz") |
| 187 | }).Port(dashboardPort).ExpectResp([]byte("")).Ensure() |
| 188 |
| 189 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 190 | r.HTTP().HTTPPath("/") |
| 191 | }).Port(dashboardPort). |
| 192 | Ensure(framework.ExpectResponseCode(401)) |
| 193 | }) |
| 194 | }) |