mirror of
https://github.com/fatedier/frp.git
synced 2025-07-27 15:45:39 +00:00
optimize the code of the command line (#3614)
This commit is contained in:
@@ -8,10 +8,10 @@ import (
|
||||
|
||||
"github.com/onsi/ginkgo/v2"
|
||||
|
||||
clientsdk "github.com/fatedier/frp/pkg/sdk/client"
|
||||
"github.com/fatedier/frp/test/e2e/framework"
|
||||
"github.com/fatedier/frp/test/e2e/framework/consts"
|
||||
"github.com/fatedier/frp/test/e2e/pkg/request"
|
||||
clientsdk "github.com/fatedier/frp/test/e2e/pkg/sdk/client"
|
||||
)
|
||||
|
||||
var _ = ginkgo.Describe("[Feature: ClientManage]", func() {
|
||||
|
@@ -1,7 +1,6 @@
|
||||
package basic
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -70,7 +69,7 @@ var _ = ginkgo.Describe("[Feature: Cmd]", func() {
|
||||
|
||||
localPort := f.PortByName(framework.TCPEchoServerPort)
|
||||
remotePort := f.AllocPort()
|
||||
_, _, err = f.RunFrpc("tcp", "-s", fmt.Sprintf("127.0.0.1:%d", serverPort), "-t", "123", "-u", "test",
|
||||
_, _, err = f.RunFrpc("tcp", "-s", "127.0.0.1", "-P", strconv.Itoa(serverPort), "-t", "123", "-u", "test",
|
||||
"-l", strconv.Itoa(localPort), "-r", strconv.Itoa(remotePort), "-n", "tcp_test")
|
||||
framework.ExpectNoError(err)
|
||||
|
||||
@@ -84,7 +83,7 @@ var _ = ginkgo.Describe("[Feature: Cmd]", func() {
|
||||
|
||||
localPort := f.PortByName(framework.UDPEchoServerPort)
|
||||
remotePort := f.AllocPort()
|
||||
_, _, err = f.RunFrpc("udp", "-s", fmt.Sprintf("127.0.0.1:%d", serverPort), "-t", "123", "-u", "test",
|
||||
_, _, err = f.RunFrpc("udp", "-s", "127.0.0.1", "-P", strconv.Itoa(serverPort), "-t", "123", "-u", "test",
|
||||
"-l", strconv.Itoa(localPort), "-r", strconv.Itoa(remotePort), "-n", "udp_test")
|
||||
framework.ExpectNoError(err)
|
||||
|
||||
@@ -98,7 +97,7 @@ var _ = ginkgo.Describe("[Feature: Cmd]", func() {
|
||||
_, _, err := f.RunFrps("-t", "123", "-p", strconv.Itoa(serverPort), "--vhost_http_port", strconv.Itoa(vhostHTTPPort))
|
||||
framework.ExpectNoError(err)
|
||||
|
||||
_, _, err = f.RunFrpc("http", "-s", "127.0.0.1:"+strconv.Itoa(serverPort), "-t", "123", "-u", "test",
|
||||
_, _, err = f.RunFrpc("http", "-s", "127.0.0.1", "-P", strconv.Itoa(serverPort), "-t", "123", "-u", "test",
|
||||
"-n", "udp_test", "-l", strconv.Itoa(f.PortByName(framework.HTTPSimpleServerPort)),
|
||||
"--custom_domain", "test.example.com")
|
||||
framework.ExpectNoError(err)
|
||||
|
@@ -7,11 +7,11 @@ import (
|
||||
|
||||
"github.com/onsi/ginkgo/v2"
|
||||
|
||||
clientsdk "github.com/fatedier/frp/pkg/sdk/client"
|
||||
"github.com/fatedier/frp/test/e2e/framework"
|
||||
"github.com/fatedier/frp/test/e2e/framework/consts"
|
||||
"github.com/fatedier/frp/test/e2e/pkg/port"
|
||||
"github.com/fatedier/frp/test/e2e/pkg/request"
|
||||
clientsdk "github.com/fatedier/frp/test/e2e/pkg/sdk/client"
|
||||
)
|
||||
|
||||
var _ = ginkgo.Describe("[Feature: Server Manager]", func() {
|
||||
|
@@ -10,8 +10,8 @@ import (
|
||||
plugin "github.com/fatedier/frp/pkg/plugin/server"
|
||||
"github.com/fatedier/frp/test/e2e/framework"
|
||||
"github.com/fatedier/frp/test/e2e/framework/consts"
|
||||
plugintest "github.com/fatedier/frp/test/e2e/legacy/plugin"
|
||||
"github.com/fatedier/frp/test/e2e/mock/server/streamserver"
|
||||
pluginpkg "github.com/fatedier/frp/test/e2e/pkg/plugin"
|
||||
"github.com/fatedier/frp/test/e2e/pkg/request"
|
||||
)
|
||||
|
||||
@@ -65,7 +65,7 @@ var _ = ginkgo.Describe("[Feature: Bandwidth Limit]", func() {
|
||||
ret.Content = content
|
||||
return &ret
|
||||
}
|
||||
pluginServer := plugintest.NewHTTPPluginServer(pluginPort, newFunc, handler, nil)
|
||||
pluginServer := pluginpkg.NewHTTPPluginServer(pluginPort, newFunc, handler, nil)
|
||||
|
||||
f.RunServer("", pluginServer)
|
||||
|
||||
|
@@ -10,6 +10,7 @@ import (
|
||||
"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() {
|
||||
@@ -40,7 +41,7 @@ var _ = ginkgo.Describe("[Feature: Server-Plugins]", func() {
|
||||
}
|
||||
return &ret
|
||||
}
|
||||
pluginServer := NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
|
||||
f.RunServer("", pluginServer)
|
||||
|
||||
@@ -98,7 +99,7 @@ var _ = ginkgo.Describe("[Feature: Server-Plugins]", func() {
|
||||
}
|
||||
return &ret
|
||||
}
|
||||
pluginServer := NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
|
||||
f.RunServer("", pluginServer)
|
||||
|
||||
@@ -133,7 +134,7 @@ var _ = ginkgo.Describe("[Feature: Server-Plugins]", func() {
|
||||
ret.Content = content
|
||||
return &ret
|
||||
}
|
||||
pluginServer := NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
|
||||
f.RunServer("", pluginServer)
|
||||
|
||||
@@ -174,7 +175,7 @@ var _ = ginkgo.Describe("[Feature: Server-Plugins]", func() {
|
||||
recordProxyName = content.ProxyName
|
||||
return &ret
|
||||
}
|
||||
pluginServer := NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
|
||||
f.RunServer("", pluginServer)
|
||||
|
||||
@@ -226,7 +227,7 @@ var _ = ginkgo.Describe("[Feature: Server-Plugins]", func() {
|
||||
ret.Unchange = true
|
||||
return &ret
|
||||
}
|
||||
pluginServer := NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
|
||||
f.RunServer("", pluginServer)
|
||||
|
||||
@@ -276,7 +277,7 @@ var _ = ginkgo.Describe("[Feature: Server-Plugins]", func() {
|
||||
ret.Unchange = true
|
||||
return &ret
|
||||
}
|
||||
pluginServer := NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
|
||||
f.RunServer("", pluginServer)
|
||||
|
||||
@@ -321,7 +322,7 @@ var _ = ginkgo.Describe("[Feature: Server-Plugins]", func() {
|
||||
ret.Unchange = true
|
||||
return &ret
|
||||
}
|
||||
pluginServer := NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
|
||||
f.RunServer("", pluginServer)
|
||||
|
||||
@@ -368,7 +369,7 @@ var _ = ginkgo.Describe("[Feature: Server-Plugins]", func() {
|
||||
}
|
||||
tlsConfig, err := transport.NewServerTLSConfig("", "", "")
|
||||
framework.ExpectNoError(err)
|
||||
pluginServer := NewHTTPPluginServer(localPort, newFunc, handler, tlsConfig)
|
||||
pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, tlsConfig)
|
||||
|
||||
f.RunServer("", pluginServer)
|
||||
|
||||
|
@@ -14,8 +14,8 @@ import (
|
||||
|
||||
libdial "github.com/fatedier/golib/net/dial"
|
||||
|
||||
"github.com/fatedier/frp/pkg/util/util"
|
||||
"github.com/fatedier/frp/test/e2e/pkg/rpc"
|
||||
"github.com/fatedier/frp/test/e2e/pkg/utils"
|
||||
)
|
||||
|
||||
type Request struct {
|
||||
@@ -115,7 +115,7 @@ func (r *Request) HTTPHeaders(headers map[string]string) *Request {
|
||||
}
|
||||
|
||||
func (r *Request) HTTPAuth(user, password string) *Request {
|
||||
r.authValue = utils.BasicAuth(user, password)
|
||||
r.authValue = util.BasicAuth(user, password)
|
||||
return r
|
||||
}
|
||||
|
||||
|
@@ -1,114 +0,0 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/fatedier/frp/client"
|
||||
"github.com/fatedier/frp/test/e2e/pkg/utils"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
address string
|
||||
authUser string
|
||||
authPwd string
|
||||
}
|
||||
|
||||
func New(host string, port int) *Client {
|
||||
return &Client{
|
||||
address: net.JoinHostPort(host, strconv.Itoa(port)),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) SetAuth(user, pwd string) {
|
||||
c.authUser = user
|
||||
c.authPwd = pwd
|
||||
}
|
||||
|
||||
func (c *Client) GetProxyStatus(name string) (*client.ProxyStatusResp, error) {
|
||||
req, err := http.NewRequest("GET", "http://"+c.address+"/api/status", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
content, err := c.do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
allStatus := make(client.StatusResp)
|
||||
if err = json.Unmarshal([]byte(content), &allStatus); err != nil {
|
||||
return nil, fmt.Errorf("unmarshal http response error: %s", strings.TrimSpace(content))
|
||||
}
|
||||
for _, pss := range allStatus {
|
||||
for _, ps := range pss {
|
||||
if ps.Name == name {
|
||||
return &ps, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("no proxy status found")
|
||||
}
|
||||
|
||||
func (c *Client) Reload() error {
|
||||
req, err := http.NewRequest("GET", "http://"+c.address+"/api/reload", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = c.do(req)
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Client) Stop() error {
|
||||
req, err := http.NewRequest("POST", "http://"+c.address+"/api/stop", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = c.do(req)
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Client) GetConfig() (string, error) {
|
||||
req, err := http.NewRequest("GET", "http://"+c.address+"/api/config", nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return c.do(req)
|
||||
}
|
||||
|
||||
func (c *Client) UpdateConfig(content string) error {
|
||||
req, err := http.NewRequest("PUT", "http://"+c.address+"/api/config", strings.NewReader(content))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = c.do(req)
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Client) setAuthHeader(req *http.Request) {
|
||||
if c.authUser != "" || c.authPwd != "" {
|
||||
req.Header.Set("Authorization", utils.BasicAuth(c.authUser, c.authPwd))
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) do(req *http.Request) (string, error) {
|
||||
c.setAuthHeader(req)
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
return "", fmt.Errorf("api status code [%d]", resp.StatusCode)
|
||||
}
|
||||
buf, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(buf), nil
|
||||
}
|
@@ -1,10 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
)
|
||||
|
||||
func BasicAuth(username, passwd string) string {
|
||||
auth := username + ":" + passwd
|
||||
return "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
|
||||
}
|
@@ -8,10 +8,10 @@ import (
|
||||
|
||||
"github.com/onsi/ginkgo/v2"
|
||||
|
||||
clientsdk "github.com/fatedier/frp/pkg/sdk/client"
|
||||
"github.com/fatedier/frp/test/e2e/framework"
|
||||
"github.com/fatedier/frp/test/e2e/framework/consts"
|
||||
"github.com/fatedier/frp/test/e2e/pkg/request"
|
||||
clientsdk "github.com/fatedier/frp/test/e2e/pkg/sdk/client"
|
||||
)
|
||||
|
||||
var _ = ginkgo.Describe("[Feature: ClientManage]", func() {
|
||||
|
@@ -1,7 +1,6 @@
|
||||
package basic
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -66,7 +65,7 @@ var _ = ginkgo.Describe("[Feature: Cmd]", func() {
|
||||
|
||||
localPort := f.PortByName(framework.TCPEchoServerPort)
|
||||
remotePort := f.AllocPort()
|
||||
_, _, err = f.RunFrpc("tcp", "-s", fmt.Sprintf("127.0.0.1:%d", serverPort), "-t", "123", "-u", "test",
|
||||
_, _, err = f.RunFrpc("tcp", "-s", "127.0.0.1", "-P", strconv.Itoa(serverPort), "-t", "123", "-u", "test",
|
||||
"-l", strconv.Itoa(localPort), "-r", strconv.Itoa(remotePort), "-n", "tcp_test")
|
||||
framework.ExpectNoError(err)
|
||||
|
||||
@@ -80,7 +79,7 @@ var _ = ginkgo.Describe("[Feature: Cmd]", func() {
|
||||
|
||||
localPort := f.PortByName(framework.UDPEchoServerPort)
|
||||
remotePort := f.AllocPort()
|
||||
_, _, err = f.RunFrpc("udp", "-s", fmt.Sprintf("127.0.0.1:%d", serverPort), "-t", "123", "-u", "test",
|
||||
_, _, err = f.RunFrpc("udp", "-s", "127.0.0.1", "-P", strconv.Itoa(serverPort), "-t", "123", "-u", "test",
|
||||
"-l", strconv.Itoa(localPort), "-r", strconv.Itoa(remotePort), "-n", "udp_test")
|
||||
framework.ExpectNoError(err)
|
||||
|
||||
@@ -94,7 +93,7 @@ var _ = ginkgo.Describe("[Feature: Cmd]", func() {
|
||||
_, _, err := f.RunFrps("-t", "123", "-p", strconv.Itoa(serverPort), "--vhost_http_port", strconv.Itoa(vhostHTTPPort))
|
||||
framework.ExpectNoError(err)
|
||||
|
||||
_, _, err = f.RunFrpc("http", "-s", "127.0.0.1:"+strconv.Itoa(serverPort), "-t", "123", "-u", "test",
|
||||
_, _, err = f.RunFrpc("http", "-s", "127.0.0.1", "-P", strconv.Itoa(serverPort), "-t", "123", "-u", "test",
|
||||
"-n", "udp_test", "-l", strconv.Itoa(f.PortByName(framework.HTTPSimpleServerPort)),
|
||||
"--custom_domain", "test.example.com")
|
||||
framework.ExpectNoError(err)
|
||||
|
@@ -7,11 +7,11 @@ import (
|
||||
|
||||
"github.com/onsi/ginkgo/v2"
|
||||
|
||||
clientsdk "github.com/fatedier/frp/pkg/sdk/client"
|
||||
"github.com/fatedier/frp/test/e2e/framework"
|
||||
"github.com/fatedier/frp/test/e2e/framework/consts"
|
||||
"github.com/fatedier/frp/test/e2e/pkg/port"
|
||||
"github.com/fatedier/frp/test/e2e/pkg/request"
|
||||
clientsdk "github.com/fatedier/frp/test/e2e/pkg/sdk/client"
|
||||
)
|
||||
|
||||
var _ = ginkgo.Describe("[Feature: Server Manager]", func() {
|
||||
|
@@ -10,8 +10,8 @@ import (
|
||||
plugin "github.com/fatedier/frp/pkg/plugin/server"
|
||||
"github.com/fatedier/frp/test/e2e/framework"
|
||||
"github.com/fatedier/frp/test/e2e/framework/consts"
|
||||
plugintest "github.com/fatedier/frp/test/e2e/legacy/plugin"
|
||||
"github.com/fatedier/frp/test/e2e/mock/server/streamserver"
|
||||
pluginpkg "github.com/fatedier/frp/test/e2e/pkg/plugin"
|
||||
"github.com/fatedier/frp/test/e2e/pkg/request"
|
||||
)
|
||||
|
||||
@@ -66,7 +66,7 @@ var _ = ginkgo.Describe("[Feature: Bandwidth Limit]", func() {
|
||||
ret.Content = content
|
||||
return &ret
|
||||
}
|
||||
pluginServer := plugintest.NewHTTPPluginServer(pluginPort, newFunc, handler, nil)
|
||||
pluginServer := pluginpkg.NewHTTPPluginServer(pluginPort, newFunc, handler, nil)
|
||||
|
||||
f.RunServer("", pluginServer)
|
||||
|
||||
|
@@ -10,6 +10,7 @@ import (
|
||||
"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() {
|
||||
@@ -40,7 +41,7 @@ var _ = ginkgo.Describe("[Feature: Server-Plugins]", func() {
|
||||
}
|
||||
return &ret
|
||||
}
|
||||
pluginServer := NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
|
||||
f.RunServer("", pluginServer)
|
||||
|
||||
@@ -101,7 +102,7 @@ var _ = ginkgo.Describe("[Feature: Server-Plugins]", func() {
|
||||
}
|
||||
return &ret
|
||||
}
|
||||
pluginServer := NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
|
||||
f.RunServer("", pluginServer)
|
||||
|
||||
@@ -138,7 +139,7 @@ var _ = ginkgo.Describe("[Feature: Server-Plugins]", func() {
|
||||
ret.Content = content
|
||||
return &ret
|
||||
}
|
||||
pluginServer := NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
|
||||
f.RunServer("", pluginServer)
|
||||
|
||||
@@ -181,7 +182,7 @@ var _ = ginkgo.Describe("[Feature: Server-Plugins]", func() {
|
||||
recordProxyName = content.ProxyName
|
||||
return &ret
|
||||
}
|
||||
pluginServer := NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
|
||||
f.RunServer("", pluginServer)
|
||||
|
||||
@@ -235,7 +236,7 @@ var _ = ginkgo.Describe("[Feature: Server-Plugins]", func() {
|
||||
ret.Unchange = true
|
||||
return &ret
|
||||
}
|
||||
pluginServer := NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
|
||||
f.RunServer("", pluginServer)
|
||||
|
||||
@@ -287,7 +288,7 @@ var _ = ginkgo.Describe("[Feature: Server-Plugins]", func() {
|
||||
ret.Unchange = true
|
||||
return &ret
|
||||
}
|
||||
pluginServer := NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
|
||||
f.RunServer("", pluginServer)
|
||||
|
||||
@@ -334,7 +335,7 @@ var _ = ginkgo.Describe("[Feature: Server-Plugins]", func() {
|
||||
ret.Unchange = true
|
||||
return &ret
|
||||
}
|
||||
pluginServer := NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, nil)
|
||||
|
||||
f.RunServer("", pluginServer)
|
||||
|
||||
@@ -383,7 +384,7 @@ var _ = ginkgo.Describe("[Feature: Server-Plugins]", func() {
|
||||
}
|
||||
tlsConfig, err := transport.NewServerTLSConfig("", "", "")
|
||||
framework.ExpectNoError(err)
|
||||
pluginServer := NewHTTPPluginServer(localPort, newFunc, handler, tlsConfig)
|
||||
pluginServer := pluginpkg.NewHTTPPluginServer(localPort, newFunc, handler, tlsConfig)
|
||||
|
||||
f.RunServer("", pluginServer)
|
||||
|
||||
|
@@ -1,41 +0,0 @@
|
||||
package plugin
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
plugin "github.com/fatedier/frp/pkg/plugin/server"
|
||||
"github.com/fatedier/frp/pkg/util/log"
|
||||
"github.com/fatedier/frp/test/e2e/mock/server/httpserver"
|
||||
)
|
||||
|
||||
type Handler func(req *plugin.Request) *plugin.Response
|
||||
|
||||
type NewPluginRequest func() *plugin.Request
|
||||
|
||||
func NewHTTPPluginServer(port int, newFunc NewPluginRequest, handler Handler, tlsConfig *tls.Config) *httpserver.Server {
|
||||
return httpserver.New(
|
||||
httpserver.WithBindPort(port),
|
||||
httpserver.WithTLSConfig(tlsConfig),
|
||||
httpserver.WithHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
r := newFunc()
|
||||
buf, err := io.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
return
|
||||
}
|
||||
log.Trace("plugin request: %s", string(buf))
|
||||
err = json.Unmarshal(buf, &r)
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
return
|
||||
}
|
||||
resp := handler(r)
|
||||
buf, _ = json.Marshal(resp)
|
||||
log.Trace("plugin response: %s", string(buf))
|
||||
_, _ = w.Write(buf)
|
||||
})),
|
||||
)
|
||||
}
|
Reference in New Issue
Block a user