frp/test/e2e/v1/plugin/server.go

416 lines
10 KiB
Go
Raw Normal View History

2021-08-02 13:07:28 +08:00
package plugin
import (
"fmt"
"time"
2023-02-27 14:44:16 +08:00
"github.com/onsi/ginkgo/v2"
2022-08-29 01:02:53 +08:00
2021-08-02 13:07:28 +08:00
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"
)
2022-08-29 01:02:53 +08:00
var _ = ginkgo.Describe("[Feature: Server-Plugins]", func() {
2021-08-02 13:07:28 +08:00
f := framework.NewDefaultFramework()
2022-08-29 01:02:53 +08:00
ginkgo.Describe("Login", func() {
2021-08-02 13:07:28 +08:00
newFunc := func() *plugin.Request {
var r plugin.Request
r.Content = &plugin.LoginContent{}
return &r
}
2022-08-29 01:02:53 +08:00
ginkgo.It("Auth for custom meta token", func() {
2021-08-02 13:07:28 +08:00
localPort := f.AllocPort()
clientAddressGot := false
2021-08-02 13:07:28 +08:00
handler := func(req *plugin.Request) *plugin.Response {
var ret plugin.Response
content := req.Content.(*plugin.LoginContent)
if content.ClientAddress != "" {
clientAddressGot = true
}
2021-08-02 13:07:28 +08:00
if content.Metas["token"] == "123" {
ret.Unchange = true
} else {
ret.Reject = true
ret.RejectReason = "invalid token"
}
return &ret
}
pluginServer := NewHTTPPluginServer(localPort, newFunc, handler, nil)
f.RunServer("", pluginServer)
serverConf := consts.DefaultServerConfig + fmt.Sprintf(`
2023-09-13 16:32:39 +08:00
[[httpPlugins]]
name = "user-manager"
addr = "127.0.0.1:%d"
path = "/handler"
ops = ["Login"]
2021-08-02 13:07:28 +08:00
`, localPort)
clientConf := consts.DefaultClientConfig
remotePort := f.AllocPort()
clientConf += fmt.Sprintf(`
2023-09-13 16:32:39 +08:00
metadatas.token = "123"
2021-08-02 13:07:28 +08:00
2023-09-13 16:32:39 +08:00
[[proxies]]
name = "tcp"
type = "tcp"
localPort = {{ .%s }}
remotePort = %d
2021-08-02 13:07:28 +08:00
`, framework.TCPEchoServerPort, remotePort)
remotePort2 := f.AllocPort()
invalidTokenClientConf := consts.DefaultClientConfig + fmt.Sprintf(`
2023-09-13 16:32:39 +08:00
[[proxies]]
name = "tcp2"
type = "tcp"
localPort = {{ .%s }}
remotePort = %d
2021-08-02 13:07:28 +08:00
`, 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)
2021-08-02 13:07:28 +08:00
})
})
2022-08-29 01:02:53 +08:00
ginkgo.Describe("NewProxy", func() {
2021-08-02 13:07:28 +08:00
newFunc := func() *plugin.Request {
var r plugin.Request
r.Content = &plugin.NewProxyContent{}
return &r
}
2022-08-29 01:02:53 +08:00
ginkgo.It("Validate Info", func() {
2021-08-02 13:07:28 +08:00
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 := NewHTTPPluginServer(localPort, newFunc, handler, nil)
f.RunServer("", pluginServer)
serverConf := consts.DefaultServerConfig + fmt.Sprintf(`
2023-09-13 16:32:39 +08:00
[[httpPlugins]]
name = "test"
addr = "127.0.0.1:%d"
path = "/handler"
ops = ["NewProxy"]
2021-08-02 13:07:28 +08:00
`, localPort)
clientConf := consts.DefaultClientConfig
remotePort := f.AllocPort()
clientConf += fmt.Sprintf(`
2023-09-13 16:32:39 +08:00
[[proxies]]
name = "tcp"
type = "tcp"
localPort = {{ .%s }}
remotePort = %d
2021-08-02 13:07:28 +08:00
`, framework.TCPEchoServerPort, remotePort)
f.RunProcesses([]string{serverConf}, []string{clientConf})
framework.NewRequestExpect(f).Port(remotePort).Ensure()
})
2022-08-29 01:02:53 +08:00
ginkgo.It("Mofify RemotePort", func() {
2021-08-02 13:07:28 +08:00
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 := NewHTTPPluginServer(localPort, newFunc, handler, nil)
f.RunServer("", pluginServer)
serverConf := consts.DefaultServerConfig + fmt.Sprintf(`
2023-09-13 16:32:39 +08:00
[[httpPlugins]]
name = "test"
addr = "127.0.0.1:%d"
path = "/handler"
ops = ["NewProxy"]
2021-08-02 13:07:28 +08:00
`, localPort)
clientConf := consts.DefaultClientConfig
clientConf += fmt.Sprintf(`
2023-09-13 16:32:39 +08:00
[[proxies]]
name = "tcp"
type = "tcp"
localPort = {{ .%s }}
remotePort = 0
2022-03-28 12:12:35 +08:00
`, framework.TCPEchoServerPort)
2021-08-02 13:07:28 +08:00
f.RunProcesses([]string{serverConf}, []string{clientConf})
framework.NewRequestExpect(f).Port(remotePort).Ensure()
})
})
2022-08-29 01:02:53 +08:00
ginkgo.Describe("CloseProxy", func() {
newFunc := func() *plugin.Request {
var r plugin.Request
r.Content = &plugin.CloseProxyContent{}
return &r
}
2022-08-29 01:02:53 +08:00
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 := NewHTTPPluginServer(localPort, newFunc, handler, nil)
f.RunServer("", pluginServer)
serverConf := consts.DefaultServerConfig + fmt.Sprintf(`
2023-09-13 16:32:39 +08:00
[[httpPlugins]]
name = "test"
addr = "127.0.0.1:%d"
path = "/handler"
ops = ["CloseProxy"]
`, localPort)
clientConf := consts.DefaultClientConfig
remotePort := f.AllocPort()
clientConf += fmt.Sprintf(`
2023-09-13 16:32:39 +08:00
[[proxies]]
name = "tcp"
type = "tcp"
localPort = {{ .%s }}
remotePort = %d
`, framework.TCPEchoServerPort, remotePort)
_, clients := f.RunProcesses([]string{serverConf}, []string{clientConf})
framework.NewRequestExpect(f).Port(remotePort).Ensure()
for _, c := range clients {
2022-08-29 01:02:53 +08:00
_ = c.Stop()
}
time.Sleep(1 * time.Second)
framework.ExpectEqual(recordProxyName, "tcp")
})
})
2022-08-29 01:02:53 +08:00
ginkgo.Describe("Ping", func() {
2021-08-02 13:07:28 +08:00
newFunc := func() *plugin.Request {
var r plugin.Request
r.Content = &plugin.PingContent{}
return &r
}
2022-08-29 01:02:53 +08:00
ginkgo.It("Validate Info", func() {
2021-08-02 13:07:28 +08:00
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 := NewHTTPPluginServer(localPort, newFunc, handler, nil)
f.RunServer("", pluginServer)
serverConf := consts.DefaultServerConfig + fmt.Sprintf(`
2023-09-13 16:32:39 +08:00
[[httpPlugins]]
name = "test"
addr = "127.0.0.1:%d"
path = "/handler"
ops = ["Ping"]
2021-08-02 13:07:28 +08:00
`, localPort)
remotePort := f.AllocPort()
clientConf := consts.DefaultClientConfig
clientConf += fmt.Sprintf(`
2023-09-13 16:32:39 +08:00
transport.heartbeatInterval = 1
auth.additionalScopes = ["HeartBeats"]
[[proxies]]
name = "tcp"
type = "tcp"
localPort = {{ .%s }}
remotePort = %d
2021-08-02 13:07:28 +08:00
`, framework.TCPEchoServerPort, remotePort)
f.RunProcesses([]string{serverConf}, []string{clientConf})
framework.NewRequestExpect(f).Port(remotePort).Ensure()
time.Sleep(3 * time.Second)
framework.ExpectNotEqual("", record)
})
})
2022-08-29 01:02:53 +08:00
ginkgo.Describe("NewWorkConn", func() {
2021-08-02 13:07:28 +08:00
newFunc := func() *plugin.Request {
var r plugin.Request
r.Content = &plugin.NewWorkConnContent{}
return &r
}
2022-08-29 01:02:53 +08:00
ginkgo.It("Validate Info", func() {
2021-08-02 13:07:28 +08:00
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 := NewHTTPPluginServer(localPort, newFunc, handler, nil)
f.RunServer("", pluginServer)
serverConf := consts.DefaultServerConfig + fmt.Sprintf(`
2023-09-13 16:32:39 +08:00
[[httpPlugins]]
name = "test"
addr = "127.0.0.1:%d"
path = "/handler"
ops = ["NewWorkConn"]
2021-08-02 13:07:28 +08:00
`, localPort)
remotePort := f.AllocPort()
clientConf := consts.DefaultClientConfig
clientConf += fmt.Sprintf(`
2023-09-13 16:32:39 +08:00
[[proxies]]
name = "tcp"
type = "tcp"
localPort = {{ .%s }}
remotePort = %d
2021-08-02 13:07:28 +08:00
`, framework.TCPEchoServerPort, remotePort)
f.RunProcesses([]string{serverConf}, []string{clientConf})
framework.NewRequestExpect(f).Port(remotePort).Ensure()
framework.ExpectNotEqual("", record)
})
})
2022-08-29 01:02:53 +08:00
ginkgo.Describe("NewUserConn", func() {
2021-08-02 13:07:28 +08:00
newFunc := func() *plugin.Request {
var r plugin.Request
r.Content = &plugin.NewUserConnContent{}
return &r
}
2022-08-29 01:02:53 +08:00
ginkgo.It("Validate Info", func() {
2021-08-02 13:07:28 +08:00
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 := NewHTTPPluginServer(localPort, newFunc, handler, nil)
f.RunServer("", pluginServer)
serverConf := consts.DefaultServerConfig + fmt.Sprintf(`
2023-09-13 16:32:39 +08:00
[[httpPlugins]]
name = "test"
addr = "127.0.0.1:%d"
path = "/handler"
ops = ["NewUserConn"]
2021-08-02 13:07:28 +08:00
`, localPort)
remotePort := f.AllocPort()
clientConf := consts.DefaultClientConfig
clientConf += fmt.Sprintf(`
2023-09-13 16:32:39 +08:00
[[proxies]]
name = "tcp"
type = "tcp"
localPort = {{ .%s }}
remotePort = %d
2021-08-02 13:07:28 +08:00
`, framework.TCPEchoServerPort, remotePort)
f.RunProcesses([]string{serverConf}, []string{clientConf})
framework.NewRequestExpect(f).Port(remotePort).Ensure()
framework.ExpectNotEqual("", record)
})
})
2022-08-29 01:02:53 +08:00
ginkgo.Describe("HTTPS Protocol", func() {
2021-08-02 13:07:28 +08:00
newFunc := func() *plugin.Request {
var r plugin.Request
r.Content = &plugin.NewUserConnContent{}
return &r
}
2022-08-29 01:02:53 +08:00
ginkgo.It("Validate Login Info, disable tls verify", func() {
2021-08-02 13:07:28 +08:00
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 := NewHTTPPluginServer(localPort, newFunc, handler, tlsConfig)
f.RunServer("", pluginServer)
serverConf := consts.DefaultServerConfig + fmt.Sprintf(`
2023-09-13 16:32:39 +08:00
[[httpPlugins]]
name = "test"
addr = "https://127.0.0.1:%d"
path = "/handler"
ops = ["NewUserConn"]
2021-08-02 13:07:28 +08:00
`, localPort)
remotePort := f.AllocPort()
clientConf := consts.DefaultClientConfig
clientConf += fmt.Sprintf(`
2023-09-13 16:32:39 +08:00
[[proxies]]
name = "tcp"
type = "tcp"
localPort = {{ .%s }}
remotePort = %d
2021-08-02 13:07:28 +08:00
`, framework.TCPEchoServerPort, remotePort)
f.RunProcesses([]string{serverConf}, []string{clientConf})
framework.NewRequestExpect(f).Port(remotePort).Ensure()
framework.ExpectNotEqual("", record)
})
})
})