| 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.LegacyDefaultServerConfig |
| 22 | clientConf := consts.LegacyDefaultClientConfig |
| 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 | allow_ports = 10000-11000,11002,12000-13000 |
| 28 | ` |
| 29 |
| 30 | tcpPortName := port.GenName("TCP", port.WithRangePorts(10000, 11000)) |
| 31 | udpPortName := port.GenName("UDP", port.WithRangePorts(12000, 13000)) |
| 32 | clientConf += fmt.Sprintf(` |
| 33 | [tcp-allowed-in-range] |
| 34 | type = tcp |
| 35 | local_port = {{ .%s }} |
| 36 | remote_port = {{ .%s }} |
| 37 | `, framework.TCPEchoServerPort, tcpPortName) |
| 38 | clientConf += fmt.Sprintf(` |
| 39 | [tcp-port-not-allowed] |
| 40 | type = tcp |
| 41 | local_port = {{ .%s }} |
| 42 | remote_port = %d |
| 43 | `, framework.TCPEchoServerPort, tcpPortNotAllowed) |
| 44 | clientConf += fmt.Sprintf(` |
| 45 | [tcp-port-unavailable] |
| 46 | type = tcp |
| 47 | local_port = {{ .%s }} |
| 48 | remote_port = {{ .%s }} |
| 49 | `, framework.TCPEchoServerPort, consts.PortServerName) |
| 50 | clientConf += fmt.Sprintf(` |
| 51 | [udp-allowed-in-range] |
| 52 | type = udp |
| 53 | local_port = {{ .%s }} |
| 54 | remote_port = {{ .%s }} |
| 55 | `, framework.UDPEchoServerPort, udpPortName) |
| 56 | clientConf += fmt.Sprintf(` |
| 57 | [udp-port-not-allowed] |
| 58 | type = udp |
| 59 | local_port = {{ .%s }} |
| 60 | remote_port = %d |
| 61 | `, framework.UDPEchoServerPort, udpPortNotAllowed) |
| 62 |
| 63 | f.RunProcesses(serverConf, []string{clientConf}) |
| 64 |
| 65 | // TCP |
| 66 | // Allowed in range |
| 67 | framework.NewRequestExpect(f).PortName(tcpPortName).Ensure() |
| 68 |
| 69 | // Not Allowed |
| 70 | framework.NewRequestExpect(f).Port(tcpPortNotAllowed).ExpectError(true).Ensure() |
| 71 |
| 72 | // Unavailable, already bind by frps |
| 73 | framework.NewRequestExpect(f).PortName(consts.PortServerName).ExpectError(true).Ensure() |
| 74 |
| 75 | // UDP |
| 76 | // Allowed in range |
| 77 | framework.NewRequestExpect(f).Protocol("udp").PortName(udpPortName).Ensure() |
| 78 |
| 79 | // Not Allowed |
| 80 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 81 | r.UDP().Port(udpPortNotAllowed) |
| 82 | }).ExpectError(true).Ensure() |
| 83 | }) |
| 84 |
| 85 | ginkgo.It("Alloc Random Port", func() { |
| 86 | serverConf := consts.LegacyDefaultServerConfig |
| 87 | clientConf := consts.LegacyDefaultClientConfig |
| 88 |
| 89 | adminPort := f.AllocPort() |
| 90 | clientConf += fmt.Sprintf(` |
| 91 | admin_port = %d |
| 92 |
| 93 | [tcp] |
| 94 | type = tcp |
| 95 | local_port = {{ .%s }} |
| 96 |
| 97 | [udp] |
| 98 | type = udp |
| 99 | local_port = {{ .%s }} |
| 100 | `, adminPort, framework.TCPEchoServerPort, framework.UDPEchoServerPort) |
| 101 |
| 102 | f.RunProcesses(serverConf, []string{clientConf}) |
| 103 |
| 104 | client := f.APIClientForFrpc(adminPort) |
| 105 |
| 106 | // tcp random port |
| 107 | status, err := client.GetProxyStatus(context.Background(), "tcp") |
| 108 | framework.ExpectNoError(err) |
| 109 |
| 110 | _, portStr, err := net.SplitHostPort(status.RemoteAddr) |
| 111 | framework.ExpectNoError(err) |
| 112 | port, err := strconv.Atoi(portStr) |
| 113 | framework.ExpectNoError(err) |
| 114 |
| 115 | framework.NewRequestExpect(f).Port(port).Ensure() |
| 116 |
| 117 | // udp random port |
| 118 | status, err = client.GetProxyStatus(context.Background(), "udp") |
| 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).Protocol("udp").Port(port).Ensure() |
| 127 | }) |
| 128 |
| 129 | ginkgo.It("Port Reuse", func() { |
| 130 | serverConf := consts.LegacyDefaultServerConfig |
| 131 | // Use same port as PortServer |
| 132 | serverConf += fmt.Sprintf(` |
| 133 | vhost_http_port = {{ .%s }} |
| 134 | `, consts.PortServerName) |
| 135 |
| 136 | clientConf := consts.LegacyDefaultClientConfig + fmt.Sprintf(` |
| 137 | [http] |
| 138 | type = http |
| 139 | local_port = {{ .%s }} |
| 140 | custom_domains = example.com |
| 141 | `, framework.HTTPSimpleServerPort) |
| 142 |
| 143 | f.RunProcesses(serverConf, []string{clientConf}) |
| 144 |
| 145 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 146 | r.HTTP().HTTPHost("example.com") |
| 147 | }).PortName(consts.PortServerName).Ensure() |
| 148 | }) |
| 149 |
| 150 | ginkgo.It("healthz", func() { |
| 151 | serverConf := consts.LegacyDefaultServerConfig |
| 152 | dashboardPort := f.AllocPort() |
| 153 |
| 154 | // Use same port as PortServer |
| 155 | serverConf += fmt.Sprintf(` |
| 156 | vhost_http_port = {{ .%s }} |
| 157 | dashboard_addr = 0.0.0.0 |
| 158 | dashboard_port = %d |
| 159 | dashboard_user = admin |
| 160 | dashboard_pwd = admin |
| 161 | `, consts.PortServerName, dashboardPort) |
| 162 |
| 163 | clientConf := consts.LegacyDefaultClientConfig + fmt.Sprintf(` |
| 164 | [http] |
| 165 | type = http |
| 166 | local_port = {{ .%s }} |
| 167 | custom_domains = example.com |
| 168 | `, framework.HTTPSimpleServerPort) |
| 169 |
| 170 | f.RunProcesses(serverConf, []string{clientConf}) |
| 171 |
| 172 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 173 | r.HTTP().HTTPPath("/healthz") |
| 174 | }).Port(dashboardPort).ExpectResp([]byte("")).Ensure() |
| 175 |
| 176 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 177 | r.HTTP().HTTPPath("/") |
| 178 | }).Port(dashboardPort). |
| 179 | Ensure(framework.ExpectResponseCode(401)) |
| 180 | }) |
| 181 | }) |