| 9 | plugin "github.com/fatedier/frp/pkg/plugin/server" |
| 10 | "github.com/fatedier/frp/pkg/transport" |
| 11 | "github.com/fatedier/frp/test/e2e/framework" |
| 12 | "github.com/fatedier/frp/test/e2e/framework/consts" |
| 13 | pluginpkg "github.com/fatedier/frp/test/e2e/pkg/plugin" |
| 14 | ) |
| 15 |
| 16 | var _ = ginkgo.Describe("[Feature: Server-Plugins]", func() { |
| 17 | f := framework.NewDefaultFramework() |
| 18 |
| 19 | ginkgo.Describe("Login", func() { |
| 20 | newFunc := func() *plugin.Request { |
| 21 | var r plugin.Request |
| 22 | r.Content = &plugin.LoginContent{} |
| 23 | return &r |
| 24 | } |
| 25 |
| 26 | ginkgo.It("Auth for custom meta token", func() { |
| 27 | localPort := f.AllocPort() |
| 28 |
| 29 | clientAddressGot := false |
| 30 | handler := func(req *plugin.Request) *plugin.Response { |
| 31 | var ret plugin.Response |
| 32 | content := req.Content.(*plugin.LoginContent) |
| 33 | if content.ClientAddress != "" { |
| 34 | clientAddressGot = true |
| 35 | } |
| 36 | if content.Metas["token"] == "123" { |
| 37 | ret.Unchange = true |
| 38 | } else { |
| 39 | ret.Reject = true |
| 40 | ret.RejectReason = "invalid token" |
| 41 | } |
| 42 | return &ret |
| 43 | } |
| 44 | pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil) |
| 45 |
| 46 | f.RunServer("", pluginServer) |
| 47 |
| 48 | serverConf := consts.LegacyDefaultServerConfig + fmt.Sprintf(` |
| 49 | [plugin.user-manager] |
| 50 | addr = 127.0.0.1:%d |
| 51 | path = /handler |
| 52 | ops = Login |
| 53 | `, localPort) |
| 54 | clientConf := consts.LegacyDefaultClientConfig |
| 55 |
| 56 | remotePort := f.AllocPort() |
| 57 | clientConf += fmt.Sprintf(` |
| 58 | meta_token = 123 |
| 59 |
| 60 | [tcp] |
| 61 | type = tcp |
| 62 | local_port = {{ .%s }} |
| 63 | remote_port = %d |
| 64 | `, framework.TCPEchoServerPort, remotePort) |
| 65 |
| 66 | remotePort2 := f.AllocPort() |
| 67 | invalidTokenClientConf := consts.LegacyDefaultClientConfig + fmt.Sprintf(` |
| 68 | [tcp2] |
| 69 | type = tcp |
| 70 | local_port = {{ .%s }} |
| 71 | remote_port = %d |
| 72 | `, framework.TCPEchoServerPort, remotePort2) |
| 73 |
| 74 | f.RunProcesses(serverConf, []string{clientConf, invalidTokenClientConf}) |
| 75 |
| 76 | framework.NewRequestExpect(f).Port(remotePort).Ensure() |
| 77 | framework.NewRequestExpect(f).Port(remotePort2).ExpectError(true).Ensure() |
| 78 |
| 79 | framework.ExpectTrue(clientAddressGot) |
| 80 | }) |
| 81 | }) |
| 82 |
| 83 | ginkgo.Describe("NewProxy", func() { |
| 84 | newFunc := func() *plugin.Request { |
| 85 | var r plugin.Request |
| 86 | r.Content = &plugin.NewProxyContent{} |
| 87 | return &r |
| 88 | } |
| 89 |
| 90 | ginkgo.It("Validate Info", func() { |
| 91 | localPort := f.AllocPort() |
| 92 | handler := func(req *plugin.Request) *plugin.Response { |
| 93 | var ret plugin.Response |
| 94 | content := req.Content.(*plugin.NewProxyContent) |
| 95 | if content.ProxyName == "tcp" { |
| 96 | ret.Unchange = true |
| 97 | } else { |
| 98 | ret.Reject = true |
| 99 | } |
| 100 | return &ret |
| 101 | } |
| 102 | pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil) |
| 103 |
| 104 | f.RunServer("", pluginServer) |
| 105 |
| 106 | serverConf := consts.LegacyDefaultServerConfig + fmt.Sprintf(` |
| 107 | [plugin.test] |
| 108 | addr = 127.0.0.1:%d |
| 109 | path = /handler |
| 110 | ops = NewProxy |
| 111 | `, localPort) |
| 112 | clientConf := consts.LegacyDefaultClientConfig |
| 113 |
| 114 | remotePort := f.AllocPort() |
| 115 | clientConf += fmt.Sprintf(` |
| 116 | [tcp] |
| 117 | type = tcp |
| 118 | local_port = {{ .%s }} |
| 119 | remote_port = %d |
| 120 | `, framework.TCPEchoServerPort, remotePort) |
| 121 |
| 122 | f.RunProcesses(serverConf, []string{clientConf}) |
| 123 |
| 124 | framework.NewRequestExpect(f).Port(remotePort).Ensure() |
| 125 | }) |
| 126 |
| 127 | ginkgo.It("Modify RemotePort", func() { |
| 128 | localPort := f.AllocPort() |
| 129 | remotePort := f.AllocPort() |
| 130 | handler := func(req *plugin.Request) *plugin.Response { |
| 131 | var ret plugin.Response |
| 132 | content := req.Content.(*plugin.NewProxyContent) |
| 133 | content.RemotePort = remotePort |
| 134 | ret.Content = content |
| 135 | return &ret |
| 136 | } |
| 137 | pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil) |
| 138 |
| 139 | f.RunServer("", pluginServer) |
| 140 |
| 141 | serverConf := consts.LegacyDefaultServerConfig + fmt.Sprintf(` |
| 142 | [plugin.test] |
| 143 | addr = 127.0.0.1:%d |
| 144 | path = /handler |
| 145 | ops = NewProxy |
| 146 | `, localPort) |
| 147 | clientConf := consts.LegacyDefaultClientConfig |
| 148 |
| 149 | clientConf += fmt.Sprintf(` |
| 150 | [tcp] |
| 151 | type = tcp |
| 152 | local_port = {{ .%s }} |
| 153 | remote_port = 0 |
| 154 | `, framework.TCPEchoServerPort) |
| 155 |
| 156 | f.RunProcesses(serverConf, []string{clientConf}) |
| 157 |
| 158 | framework.NewRequestExpect(f).Port(remotePort).Ensure() |
| 159 | }) |
| 160 | }) |
| 161 |
| 162 | ginkgo.Describe("CloseProxy", func() { |
| 163 | newFunc := func() *plugin.Request { |
| 164 | var r plugin.Request |
| 165 | r.Content = &plugin.CloseProxyContent{} |
| 166 | return &r |
| 167 | } |
| 168 |
| 169 | ginkgo.It("Validate Info", func() { |
| 170 | localPort := f.AllocPort() |
| 171 | var recordProxyName string |
| 172 | handler := func(req *plugin.Request) *plugin.Response { |
| 173 | var ret plugin.Response |
| 174 | content := req.Content.(*plugin.CloseProxyContent) |
| 175 | recordProxyName = content.ProxyName |
| 176 | return &ret |
| 177 | } |
| 178 | pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil) |
| 179 |
| 180 | f.RunServer("", pluginServer) |
| 181 |
| 182 | serverConf := consts.LegacyDefaultServerConfig + fmt.Sprintf(` |
| 183 | [plugin.test] |
| 184 | addr = 127.0.0.1:%d |
| 185 | path = /handler |
| 186 | ops = CloseProxy |
| 187 | `, localPort) |
| 188 | clientConf := consts.LegacyDefaultClientConfig |
| 189 |
| 190 | remotePort := f.AllocPort() |
| 191 | clientConf += fmt.Sprintf(` |
| 192 | [tcp] |
| 193 | type = tcp |
| 194 | local_port = {{ .%s }} |
| 195 | remote_port = %d |
| 196 | `, framework.TCPEchoServerPort, remotePort) |
| 197 |
| 198 | _, clients := f.RunProcesses(serverConf, []string{clientConf}) |
| 199 |
| 200 | framework.NewRequestExpect(f).Port(remotePort).Ensure() |
| 201 |
| 202 | for _, c := range clients { |
| 203 | _ = c.Stop() |
| 204 | } |
| 205 |
| 206 | time.Sleep(1 * time.Second) |
| 207 |
| 208 | framework.ExpectEqual(recordProxyName, "tcp") |
| 209 | }) |
| 210 | }) |
| 211 |
| 212 | ginkgo.Describe("Ping", func() { |
| 213 | newFunc := func() *plugin.Request { |
| 214 | var r plugin.Request |
| 215 | r.Content = &plugin.PingContent{} |
| 216 | return &r |
| 217 | } |
| 218 |
| 219 | ginkgo.It("Validate Info", func() { |
| 220 | localPort := f.AllocPort() |
| 221 |
| 222 | var record string |
| 223 | handler := func(req *plugin.Request) *plugin.Response { |
| 224 | var ret plugin.Response |
| 225 | content := req.Content.(*plugin.PingContent) |
| 226 | record = content.PrivilegeKey |
| 227 | ret.Unchange = true |
| 228 | return &ret |
| 229 | } |
| 230 | pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil) |
| 231 |
| 232 | f.RunServer("", pluginServer) |
| 233 |
| 234 | serverConf := consts.LegacyDefaultServerConfig + fmt.Sprintf(` |
| 235 | [plugin.test] |
| 236 | addr = 127.0.0.1:%d |
| 237 | path = /handler |
| 238 | ops = Ping |
| 239 | `, localPort) |
| 240 |
| 241 | remotePort := f.AllocPort() |
| 242 | clientConf := consts.LegacyDefaultClientConfig |
| 243 | clientConf += fmt.Sprintf(` |
| 244 | heartbeat_interval = 1 |
| 245 | authenticate_heartbeats = true |
| 246 |
| 247 | [tcp] |
| 248 | type = tcp |
| 249 | local_port = {{ .%s }} |
| 250 | remote_port = %d |
| 251 | `, framework.TCPEchoServerPort, remotePort) |
| 252 |
| 253 | f.RunProcesses(serverConf, []string{clientConf}) |
| 254 |
| 255 | framework.NewRequestExpect(f).Port(remotePort).Ensure() |
| 256 |
| 257 | time.Sleep(3 * time.Second) |
| 258 | framework.ExpectNotEqual("", record) |
| 259 | }) |
| 260 | }) |
| 261 |
| 262 | ginkgo.Describe("NewWorkConn", func() { |
| 263 | newFunc := func() *plugin.Request { |
| 264 | var r plugin.Request |
| 265 | r.Content = &plugin.NewWorkConnContent{} |
| 266 | return &r |
| 267 | } |
| 268 |
| 269 | ginkgo.It("Validate Info", func() { |
| 270 | localPort := f.AllocPort() |
| 271 |
| 272 | var record string |
| 273 | handler := func(req *plugin.Request) *plugin.Response { |
| 274 | var ret plugin.Response |
| 275 | content := req.Content.(*plugin.NewWorkConnContent) |
| 276 | record = content.RunID |
| 277 | ret.Unchange = true |
| 278 | return &ret |
| 279 | } |
| 280 | pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil) |
| 281 |
| 282 | f.RunServer("", pluginServer) |
| 283 |
| 284 | serverConf := consts.LegacyDefaultServerConfig + fmt.Sprintf(` |
| 285 | [plugin.test] |
| 286 | addr = 127.0.0.1:%d |
| 287 | path = /handler |
| 288 | ops = NewWorkConn |
| 289 | `, localPort) |
| 290 |
| 291 | remotePort := f.AllocPort() |
| 292 | clientConf := consts.LegacyDefaultClientConfig |
| 293 | clientConf += fmt.Sprintf(` |
| 294 | [tcp] |
| 295 | type = tcp |
| 296 | local_port = {{ .%s }} |
| 297 | remote_port = %d |
| 298 | `, framework.TCPEchoServerPort, remotePort) |
| 299 |
| 300 | f.RunProcesses(serverConf, []string{clientConf}) |
| 301 |
| 302 | framework.NewRequestExpect(f).Port(remotePort).Ensure() |
| 303 |
| 304 | framework.ExpectNotEqual("", record) |
| 305 | }) |
| 306 | }) |
| 307 |
| 308 | ginkgo.Describe("NewUserConn", func() { |
| 309 | newFunc := func() *plugin.Request { |
| 310 | var r plugin.Request |
| 311 | r.Content = &plugin.NewUserConnContent{} |
| 312 | return &r |
| 313 | } |
| 314 | ginkgo.It("Validate Info", func() { |
| 315 | localPort := f.AllocPort() |
| 316 |
| 317 | var record string |
| 318 | handler := func(req *plugin.Request) *plugin.Response { |
| 319 | var ret plugin.Response |
| 320 | content := req.Content.(*plugin.NewUserConnContent) |
| 321 | record = content.RemoteAddr |
| 322 | ret.Unchange = true |
| 323 | return &ret |
| 324 | } |
| 325 | pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil) |
| 326 |
| 327 | f.RunServer("", pluginServer) |
| 328 |
| 329 | serverConf := consts.LegacyDefaultServerConfig + fmt.Sprintf(` |
| 330 | [plugin.test] |
| 331 | addr = 127.0.0.1:%d |
| 332 | path = /handler |
| 333 | ops = NewUserConn |
| 334 | `, localPort) |
| 335 |
| 336 | remotePort := f.AllocPort() |
| 337 | clientConf := consts.LegacyDefaultClientConfig |
| 338 | clientConf += fmt.Sprintf(` |
| 339 | [tcp] |
| 340 | type = tcp |
| 341 | local_port = {{ .%s }} |
| 342 | remote_port = %d |
| 343 | `, framework.TCPEchoServerPort, remotePort) |
| 344 |
| 345 | f.RunProcesses(serverConf, []string{clientConf}) |
| 346 |
| 347 | framework.NewRequestExpect(f).Port(remotePort).Ensure() |
| 348 |
| 349 | framework.ExpectNotEqual("", record) |
| 350 | }) |
| 351 | }) |
| 352 |
| 353 | ginkgo.Describe("HTTPS Protocol", func() { |
| 354 | newFunc := func() *plugin.Request { |
| 355 | var r plugin.Request |
| 356 | r.Content = &plugin.NewUserConnContent{} |
| 357 | return &r |
| 358 | } |
| 359 | ginkgo.It("Validate Login Info, disable tls verify", func() { |
| 360 | localPort := f.AllocPort() |
| 361 |
| 362 | var record string |
| 363 | handler := func(req *plugin.Request) *plugin.Response { |
| 364 | var ret plugin.Response |
| 365 | content := req.Content.(*plugin.NewUserConnContent) |
| 366 | record = content.RemoteAddr |
| 367 | ret.Unchange = true |
| 368 | return &ret |
| 369 | } |
| 370 | tlsConfig, err := transport.NewServerTLSConfig("", "", "") |
| 371 | framework.ExpectNoError(err) |
| 372 | pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, tlsConfig) |
| 373 |
| 374 | f.RunServer("", pluginServer) |
| 375 |
| 376 | serverConf := consts.LegacyDefaultServerConfig + fmt.Sprintf(` |
| 377 | [plugin.test] |
| 378 | addr = https://127.0.0.1:%d |
| 379 | path = /handler |
| 380 | ops = NewUserConn |
| 381 | `, localPort) |
| 382 |
| 383 | remotePort := f.AllocPort() |
| 384 | clientConf := consts.LegacyDefaultClientConfig |
| 385 | clientConf += fmt.Sprintf(` |
| 386 | [tcp] |
| 387 | type = tcp |
| 388 | local_port = {{ .%s }} |
| 389 | remote_port = %d |
| 390 | `, framework.TCPEchoServerPort, remotePort) |
| 391 |
| 392 | f.RunProcesses(serverConf, []string{clientConf}) |
| 393 |
| 394 | framework.NewRequestExpect(f).Port(remotePort).Ensure() |
| 395 |
| 396 | framework.ExpectNotEqual("", record) |
| 397 | }) |
| 398 | }) |
| 399 | }) |