123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- package plugin
- import (
- "fmt"
- "time"
- "github.com/onsi/ginkgo/v2"
- plugin "github.com/fatedier/frp/pkg/plugin/server"
- "github.com/fatedier/frp/pkg/transport"
- "github.com/fatedier/frp/test/e2e/framework"
- "github.com/fatedier/frp/test/e2e/framework/consts"
- pluginpkg "github.com/fatedier/frp/test/e2e/pkg/plugin"
- )
- var _ = ginkgo.Describe("[Feature: Server-Plugins]", func() {
- f := framework.NewDefaultFramework()
- ginkgo.Describe("Login", func() {
- newFunc := func() *plugin.Request {
- var r plugin.Request
- r.Content = &plugin.LoginContent{}
- return &r
- }
- ginkgo.It("Auth for custom meta token", func() {
- localPort := f.AllocPort()
- clientAddressGot := false
- handler := func(req *plugin.Request) *plugin.Response {
- var ret plugin.Response
- content := req.Content.(*plugin.LoginContent)
- if content.ClientAddress != "" {
- clientAddressGot = true
- }
- if content.Metas["token"] == "123" {
- ret.Unchange = true
- } else {
- ret.Reject = true
- ret.RejectReason = "invalid token"
- }
- return &ret
- }
- pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil)
- f.RunServer("", pluginServer)
- serverConf := consts.LegacyDefaultServerConfig + fmt.Sprintf(`
- [plugin.user-manager]
- addr = 127.0.0.1:%d
- path = /handler
- ops = Login
- `, localPort)
- clientConf := consts.LegacyDefaultClientConfig
- remotePort := f.AllocPort()
- clientConf += fmt.Sprintf(`
- meta_token = 123
- [tcp]
- type = tcp
- local_port = {{ .%s }}
- remote_port = %d
- `, framework.TCPEchoServerPort, remotePort)
- remotePort2 := f.AllocPort()
- invalidTokenClientConf := consts.LegacyDefaultClientConfig + fmt.Sprintf(`
- [tcp2]
- type = tcp
- local_port = {{ .%s }}
- remote_port = %d
- `, framework.TCPEchoServerPort, remotePort2)
- f.RunProcesses([]string{serverConf}, []string{clientConf, invalidTokenClientConf})
- framework.NewRequestExpect(f).Port(remotePort).Ensure()
- framework.NewRequestExpect(f).Port(remotePort2).ExpectError(true).Ensure()
- framework.ExpectTrue(clientAddressGot)
- })
- })
- ginkgo.Describe("NewProxy", func() {
- newFunc := func() *plugin.Request {
- var r plugin.Request
- r.Content = &plugin.NewProxyContent{}
- return &r
- }
- ginkgo.It("Validate Info", func() {
- localPort := f.AllocPort()
- handler := func(req *plugin.Request) *plugin.Response {
- var ret plugin.Response
- content := req.Content.(*plugin.NewProxyContent)
- if content.ProxyName == "tcp" {
- ret.Unchange = true
- } else {
- ret.Reject = true
- }
- return &ret
- }
- pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil)
- f.RunServer("", pluginServer)
- serverConf := consts.LegacyDefaultServerConfig + fmt.Sprintf(`
- [plugin.test]
- addr = 127.0.0.1:%d
- path = /handler
- ops = NewProxy
- `, localPort)
- clientConf := consts.LegacyDefaultClientConfig
- remotePort := f.AllocPort()
- clientConf += fmt.Sprintf(`
- [tcp]
- type = tcp
- local_port = {{ .%s }}
- remote_port = %d
- `, framework.TCPEchoServerPort, remotePort)
- f.RunProcesses([]string{serverConf}, []string{clientConf})
- framework.NewRequestExpect(f).Port(remotePort).Ensure()
- })
- ginkgo.It("Modify RemotePort", func() {
- localPort := f.AllocPort()
- remotePort := f.AllocPort()
- handler := func(req *plugin.Request) *plugin.Response {
- var ret plugin.Response
- content := req.Content.(*plugin.NewProxyContent)
- content.RemotePort = remotePort
- ret.Content = content
- return &ret
- }
- pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil)
- f.RunServer("", pluginServer)
- serverConf := consts.LegacyDefaultServerConfig + fmt.Sprintf(`
- [plugin.test]
- addr = 127.0.0.1:%d
- path = /handler
- ops = NewProxy
- `, localPort)
- clientConf := consts.LegacyDefaultClientConfig
- clientConf += fmt.Sprintf(`
- [tcp]
- type = tcp
- local_port = {{ .%s }}
- remote_port = 0
- `, framework.TCPEchoServerPort)
- f.RunProcesses([]string{serverConf}, []string{clientConf})
- framework.NewRequestExpect(f).Port(remotePort).Ensure()
- })
- })
- ginkgo.Describe("CloseProxy", func() {
- newFunc := func() *plugin.Request {
- var r plugin.Request
- r.Content = &plugin.CloseProxyContent{}
- return &r
- }
- ginkgo.It("Validate Info", func() {
- localPort := f.AllocPort()
- var recordProxyName string
- handler := func(req *plugin.Request) *plugin.Response {
- var ret plugin.Response
- content := req.Content.(*plugin.CloseProxyContent)
- recordProxyName = content.ProxyName
- return &ret
- }
- pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil)
- f.RunServer("", pluginServer)
- serverConf := consts.LegacyDefaultServerConfig + fmt.Sprintf(`
- [plugin.test]
- addr = 127.0.0.1:%d
- path = /handler
- ops = CloseProxy
- `, localPort)
- clientConf := consts.LegacyDefaultClientConfig
- remotePort := f.AllocPort()
- clientConf += fmt.Sprintf(`
- [tcp]
- type = tcp
- local_port = {{ .%s }}
- remote_port = %d
- `, framework.TCPEchoServerPort, remotePort)
- _, clients := f.RunProcesses([]string{serverConf}, []string{clientConf})
- framework.NewRequestExpect(f).Port(remotePort).Ensure()
- for _, c := range clients {
- _ = c.Stop()
- }
- time.Sleep(1 * time.Second)
- framework.ExpectEqual(recordProxyName, "tcp")
- })
- })
- ginkgo.Describe("Ping", func() {
- newFunc := func() *plugin.Request {
- var r plugin.Request
- r.Content = &plugin.PingContent{}
- return &r
- }
- ginkgo.It("Validate Info", func() {
- localPort := f.AllocPort()
- var record string
- handler := func(req *plugin.Request) *plugin.Response {
- var ret plugin.Response
- content := req.Content.(*plugin.PingContent)
- record = content.Ping.PrivilegeKey
- ret.Unchange = true
- return &ret
- }
- pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil)
- f.RunServer("", pluginServer)
- serverConf := consts.LegacyDefaultServerConfig + fmt.Sprintf(`
- [plugin.test]
- addr = 127.0.0.1:%d
- path = /handler
- ops = Ping
- `, localPort)
- remotePort := f.AllocPort()
- clientConf := consts.LegacyDefaultClientConfig
- clientConf += fmt.Sprintf(`
- heartbeat_interval = 1
- authenticate_heartbeats = true
- [tcp]
- type = tcp
- local_port = {{ .%s }}
- remote_port = %d
- `, framework.TCPEchoServerPort, remotePort)
- f.RunProcesses([]string{serverConf}, []string{clientConf})
- framework.NewRequestExpect(f).Port(remotePort).Ensure()
- time.Sleep(3 * time.Second)
- framework.ExpectNotEqual("", record)
- })
- })
- ginkgo.Describe("NewWorkConn", func() {
- newFunc := func() *plugin.Request {
- var r plugin.Request
- r.Content = &plugin.NewWorkConnContent{}
- return &r
- }
- ginkgo.It("Validate Info", func() {
- localPort := f.AllocPort()
- var record string
- handler := func(req *plugin.Request) *plugin.Response {
- var ret plugin.Response
- content := req.Content.(*plugin.NewWorkConnContent)
- record = content.NewWorkConn.RunID
- ret.Unchange = true
- return &ret
- }
- pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil)
- f.RunServer("", pluginServer)
- serverConf := consts.LegacyDefaultServerConfig + fmt.Sprintf(`
- [plugin.test]
- addr = 127.0.0.1:%d
- path = /handler
- ops = NewWorkConn
- `, localPort)
- remotePort := f.AllocPort()
- clientConf := consts.LegacyDefaultClientConfig
- clientConf += fmt.Sprintf(`
- [tcp]
- type = tcp
- local_port = {{ .%s }}
- remote_port = %d
- `, framework.TCPEchoServerPort, remotePort)
- f.RunProcesses([]string{serverConf}, []string{clientConf})
- framework.NewRequestExpect(f).Port(remotePort).Ensure()
- framework.ExpectNotEqual("", record)
- })
- })
- ginkgo.Describe("NewUserConn", func() {
- newFunc := func() *plugin.Request {
- var r plugin.Request
- r.Content = &plugin.NewUserConnContent{}
- return &r
- }
- ginkgo.It("Validate Info", func() {
- localPort := f.AllocPort()
- var record string
- handler := func(req *plugin.Request) *plugin.Response {
- var ret plugin.Response
- content := req.Content.(*plugin.NewUserConnContent)
- record = content.RemoteAddr
- ret.Unchange = true
- return &ret
- }
- pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil)
- f.RunServer("", pluginServer)
- serverConf := consts.LegacyDefaultServerConfig + fmt.Sprintf(`
- [plugin.test]
- addr = 127.0.0.1:%d
- path = /handler
- ops = NewUserConn
- `, localPort)
- remotePort := f.AllocPort()
- clientConf := consts.LegacyDefaultClientConfig
- clientConf += fmt.Sprintf(`
- [tcp]
- type = tcp
- local_port = {{ .%s }}
- remote_port = %d
- `, framework.TCPEchoServerPort, remotePort)
- f.RunProcesses([]string{serverConf}, []string{clientConf})
- framework.NewRequestExpect(f).Port(remotePort).Ensure()
- framework.ExpectNotEqual("", record)
- })
- })
- ginkgo.Describe("HTTPS Protocol", func() {
- newFunc := func() *plugin.Request {
- var r plugin.Request
- r.Content = &plugin.NewUserConnContent{}
- return &r
- }
- ginkgo.It("Validate Login Info, disable tls verify", func() {
- localPort := f.AllocPort()
- var record string
- handler := func(req *plugin.Request) *plugin.Response {
- var ret plugin.Response
- content := req.Content.(*plugin.NewUserConnContent)
- record = content.RemoteAddr
- ret.Unchange = true
- return &ret
- }
- tlsConfig, err := transport.NewServerTLSConfig("", "", "")
- framework.ExpectNoError(err)
- pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, tlsConfig)
- f.RunServer("", pluginServer)
- serverConf := consts.LegacyDefaultServerConfig + fmt.Sprintf(`
- [plugin.test]
- addr = https://127.0.0.1:%d
- path = /handler
- ops = NewUserConn
- `, localPort)
- remotePort := f.AllocPort()
- clientConf := consts.LegacyDefaultClientConfig
- clientConf += fmt.Sprintf(`
- [tcp]
- type = tcp
- local_port = {{ .%s }}
- remote_port = %d
- `, framework.TCPEchoServerPort, remotePort)
- f.RunProcesses([]string{serverConf}, []string{clientConf})
- framework.NewRequestExpect(f).Port(remotePort).Ensure()
- framework.ExpectNotEqual("", record)
- })
- })
- })
|