optimize the code of the command line (#3614)

This commit is contained in:
fatedier
2023-09-15 10:33:32 +08:00
committed by GitHub
parent 74255f711e
commit bae0b4d7c0
41 changed files with 566 additions and 1363 deletions

View File

@@ -134,6 +134,9 @@ type PortsRange struct {
type PortsRangeSlice []PortsRange
func (p PortsRangeSlice) String() string {
if len(p) == 0 {
return ""
}
strs := []string{}
for _, v := range p {
if v.Single > 0 {

View File

@@ -29,11 +29,11 @@ func ValidateClientCommonConfig(c *v1.ClientCommonConfig) (Warning, error) {
warnings Warning
errs error
)
if !lo.Contains(supportedAuthMethods, c.Auth.Method) {
errs = AppendError(errs, fmt.Errorf("invalid auth method, optional values are %v", supportedAuthMethods))
if !lo.Contains(SupportedAuthMethods, c.Auth.Method) {
errs = AppendError(errs, fmt.Errorf("invalid auth method, optional values are %v", SupportedAuthMethods))
}
if !lo.Every(supportedAuthAdditionalScopes, c.Auth.AdditionalScopes) {
errs = AppendError(errs, fmt.Errorf("invalid auth additional scopes, optional values are %v", supportedAuthAdditionalScopes))
if !lo.Every(SupportedAuthAdditionalScopes, c.Auth.AdditionalScopes) {
errs = AppendError(errs, fmt.Errorf("invalid auth additional scopes, optional values are %v", SupportedAuthAdditionalScopes))
}
if err := validateLogConfig(&c.Log); err != nil {
@@ -63,8 +63,8 @@ func ValidateClientCommonConfig(c *v1.ClientCommonConfig) (Warning, error) {
warnings = AppendError(warnings, checkTLSConfig("transport.tls.trustedCaFile", c.Transport.TLS.TrustedCaFile))
}
if !lo.Contains(supportedTransportProtocols, c.Transport.Protocol) {
errs = AppendError(errs, fmt.Errorf("invalid transport.protocol, optional values are %v", supportedTransportProtocols))
if !lo.Contains(SupportedTransportProtocols, c.Transport.Protocol) {
errs = AppendError(errs, fmt.Errorf("invalid transport.protocol, optional values are %v", SupportedTransportProtocols))
}
for _, f := range c.IncludeConfigFiles {

View File

@@ -44,8 +44,8 @@ func ValidatePort(port int, fieldPath string) error {
}
func validateLogConfig(c *v1.LogConfig) error {
if !lo.Contains(supportedLogLevels, c.Level) {
return fmt.Errorf("invalid log level, optional values are %v", supportedLogLevels)
if !lo.Contains(SupportedLogLevels, c.Level) {
return fmt.Errorf("invalid log level, optional values are %v", SupportedLogLevels)
}
return nil
}

View File

@@ -27,11 +27,11 @@ func ValidateServerConfig(c *v1.ServerConfig) (Warning, error) {
warnings Warning
errs error
)
if !lo.Contains(supportedAuthMethods, c.Auth.Method) {
errs = AppendError(errs, fmt.Errorf("invalid auth method, optional values are %v", supportedAuthMethods))
if !lo.Contains(SupportedAuthMethods, c.Auth.Method) {
errs = AppendError(errs, fmt.Errorf("invalid auth method, optional values are %v", SupportedAuthMethods))
}
if !lo.Every(supportedAuthAdditionalScopes, c.Auth.AdditionalScopes) {
errs = AppendError(errs, fmt.Errorf("invalid auth additional scopes, optional values are %v", supportedAuthAdditionalScopes))
if !lo.Every(SupportedAuthAdditionalScopes, c.Auth.AdditionalScopes) {
errs = AppendError(errs, fmt.Errorf("invalid auth additional scopes, optional values are %v", SupportedAuthAdditionalScopes))
}
if err := validateLogConfig(&c.Log); err != nil {
@@ -50,8 +50,8 @@ func ValidateServerConfig(c *v1.ServerConfig) (Warning, error) {
errs = AppendError(errs, ValidatePort(c.TCPMuxHTTPConnectPort, "tcpMuxHTTPConnectPort"))
for _, p := range c.HTTPPlugins {
if !lo.Every(supportedHTTPPluginOps, p.Ops) {
errs = AppendError(errs, fmt.Errorf("invalid http plugin ops, optional values are %v", supportedHTTPPluginOps))
if !lo.Every(SupportedHTTPPluginOps, p.Ops) {
errs = AppendError(errs, fmt.Errorf("invalid http plugin ops, optional values are %v", SupportedHTTPPluginOps))
}
}
return warnings, errs

View File

@@ -22,7 +22,7 @@ import (
)
var (
supportedTransportProtocols = []string{
SupportedTransportProtocols = []string{
"tcp",
"kcp",
"quic",
@@ -30,17 +30,17 @@ var (
"wss",
}
supportedAuthMethods = []string{
SupportedAuthMethods = []string{
"token",
"oidc",
}
supportedAuthAdditionalScopes = []v1.AuthScope{
SupportedAuthAdditionalScopes = []v1.AuthScope{
"HeartBeats",
"NewWorkConns",
}
supportedLogLevels = []string{
SupportedLogLevels = []string{
"trace",
"debug",
"info",
@@ -48,7 +48,7 @@ var (
"error",
}
supportedHTTPPluginOps = []string{
SupportedHTTPPluginOps = []string{
splugin.OpLogin,
splugin.OpNewProxy,
splugin.OpCloseProxy,

View File

@@ -42,7 +42,11 @@ func ValidateVisitorConfigurer(c v1.VisitorConfigurer) error {
func validateVisitorBaseConfig(c *v1.VisitorBaseConfig) error {
if c.Name == "" {
return errors.New("name should not be empty")
return errors.New("name is required")
}
if c.ServerName == "" {
return errors.New("server name is required")
}
if c.BindPort == 0 {

130
pkg/sdk/client/client.go Normal file
View File

@@ -0,0 +1,130 @@
package client
import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"strconv"
"strings"
"github.com/fatedier/frp/client"
"github.com/fatedier/frp/pkg/util/util"
)
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) GetAllProxyStatus() (client.StatusResp, 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))
}
return allStatus, nil
}
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", util.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
}

View File

@@ -95,3 +95,8 @@ func ParseBasicAuth(auth string) (username, password string, ok bool) {
}
return cs[:s], cs[s+1:], true
}
func BasicAuth(username, passwd string) string {
auth := username + ":" + passwd
return "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
}