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