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