From 0b7eb9972e38b67fff61aff8048559b992aa5ceb Mon Sep 17 00:00:00 2001 From: yuzuki999 Date: Sat, 4 Jun 2022 17:12:28 +0800 Subject: [PATCH] fix bug,fix spelling mistakes,del redundant code --- api/node.go | 10 ++--- app/mydispatcher/default.go | 2 +- common/{legocmd => legoCmd}/cmd/account.go | 0 .../cmd/accounts_storage.go | 2 +- .../{legocmd => legoCmd}/cmd/certs_storage.go | 2 +- common/{legocmd => legoCmd}/cmd/cmd.go | 0 common/{legocmd => legoCmd}/cmd/cmd_before.go | 2 +- .../{legocmd => legoCmd}/cmd/cmd_dnshelp.go | 0 common/{legocmd => legoCmd}/cmd/cmd_list.go | 0 common/{legocmd => legoCmd}/cmd/cmd_renew.go | 2 +- .../cmd/cmd_renew_test.go | 0 common/{legocmd => legoCmd}/cmd/cmd_revoke.go | 2 +- common/{legocmd => legoCmd}/cmd/cmd_run.go | 2 +- common/{legocmd => legoCmd}/cmd/flags.go | 0 common/{legocmd => legoCmd}/cmd/hook.go | 0 common/{legocmd => legoCmd}/cmd/setup.go | 2 +- .../cmd/setup_challenges.go | 2 +- .../cmd/zz_gen_cmd_dnshelp.go | 0 common/{legocmd => legoCmd}/lego.go | 30 +++++++------- common/{legocmd => legoCmd}/lego_test.go | 12 +++--- common/rule/rule.go | 14 +++---- common/serverstatus/serverstatus.go | 41 ------------------- service/controller/controller.go | 4 +- service/controller/inboundbuilder.go | 8 ++-- service/controller/outboundbuilder.go | 6 +-- 25 files changed, 51 insertions(+), 92 deletions(-) rename common/{legocmd => legoCmd}/cmd/account.go (100%) rename common/{legocmd => legoCmd}/cmd/accounts_storage.go (99%) rename common/{legocmd => legoCmd}/cmd/certs_storage.go (99%) rename common/{legocmd => legoCmd}/cmd/cmd.go (100%) rename common/{legocmd => legoCmd}/cmd/cmd_before.go (90%) rename common/{legocmd => legoCmd}/cmd/cmd_dnshelp.go (100%) rename common/{legocmd => legoCmd}/cmd/cmd_list.go (100%) rename common/{legocmd => legoCmd}/cmd/cmd_renew.go (99%) rename common/{legocmd => legoCmd}/cmd/cmd_renew_test.go (100%) rename common/{legocmd => legoCmd}/cmd/cmd_revoke.go (96%) rename common/{legocmd => legoCmd}/cmd/cmd_run.go (99%) rename common/{legocmd => legoCmd}/cmd/flags.go (100%) rename common/{legocmd => legoCmd}/cmd/hook.go (100%) rename common/{legocmd => legoCmd}/cmd/setup.go (98%) rename common/{legocmd => legoCmd}/cmd/setup_challenges.go (98%) rename common/{legocmd => legoCmd}/cmd/zz_gen_cmd_dnshelp.go (100%) rename common/{legocmd => legoCmd}/lego.go (87%) rename common/{legocmd => legoCmd}/lego_test.go (89%) delete mode 100644 common/serverstatus/serverstatus.go diff --git a/api/node.go b/api/node.go index a30b47e..6436cb9 100644 --- a/api/node.go +++ b/api/node.go @@ -196,11 +196,11 @@ func (c *Client) ParseSSNodeResponse() (*NodeInfo, error) { return nil, err } node := &NodeInfo{ - SpeedLimit: uint64(c.SpeedLimit * 1000000 / 8), - DeviceLimit: c.DeviceLimit, - EnableSS2022: c.EnableSS2022, - NodeType: c.NodeType, - NodeId: c.NodeID, + SpeedLimit: uint64(c.SpeedLimit * 1000000 / 8), + DeviceLimit: c.DeviceLimit, + //EnableSS2022: c.EnableSS2022, + NodeType: c.NodeType, + NodeId: c.NodeID, SS: &SSConfig{ Port: port, TransportProtocol: "tcp", diff --git a/app/mydispatcher/default.go b/app/mydispatcher/default.go index 5bb5762..9105f1a 100644 --- a/app/mydispatcher/default.go +++ b/app/mydispatcher/default.go @@ -98,7 +98,7 @@ type DefaultDispatcher struct { dns dns.Client fdns dns.FakeDNSEngine Limiter *limiter.Limiter - RuleManager *rule.RuleManager + RuleManager *rule.Rule } func init() { diff --git a/common/legocmd/cmd/account.go b/common/legoCmd/cmd/account.go similarity index 100% rename from common/legocmd/cmd/account.go rename to common/legoCmd/cmd/account.go diff --git a/common/legocmd/cmd/accounts_storage.go b/common/legoCmd/cmd/accounts_storage.go similarity index 99% rename from common/legocmd/cmd/accounts_storage.go rename to common/legoCmd/cmd/accounts_storage.go index 1592dc7..2f26bd9 100644 --- a/common/legocmd/cmd/accounts_storage.go +++ b/common/legoCmd/cmd/accounts_storage.go @@ -13,7 +13,7 @@ import ( "path/filepath" "strings" - "github.com/Yuzuki616/V2bX/common/legocmd/log" + "github.com/Yuzuki616/V2bX/common/legoCmd/log" "github.com/go-acme/lego/v4/certcrypto" "github.com/go-acme/lego/v4/lego" "github.com/go-acme/lego/v4/registration" diff --git a/common/legocmd/cmd/certs_storage.go b/common/legoCmd/cmd/certs_storage.go similarity index 99% rename from common/legocmd/cmd/certs_storage.go rename to common/legoCmd/cmd/certs_storage.go index e54b461..8df5553 100644 --- a/common/legocmd/cmd/certs_storage.go +++ b/common/legoCmd/cmd/certs_storage.go @@ -11,7 +11,7 @@ import ( "strings" "time" - "github.com/Yuzuki616/V2bX/common/legocmd/log" + "github.com/Yuzuki616/V2bX/common/legoCmd/log" "github.com/go-acme/lego/v4/certcrypto" "github.com/go-acme/lego/v4/certificate" "github.com/urfave/cli" diff --git a/common/legocmd/cmd/cmd.go b/common/legoCmd/cmd/cmd.go similarity index 100% rename from common/legocmd/cmd/cmd.go rename to common/legoCmd/cmd/cmd.go diff --git a/common/legocmd/cmd/cmd_before.go b/common/legoCmd/cmd/cmd_before.go similarity index 90% rename from common/legocmd/cmd/cmd_before.go rename to common/legoCmd/cmd/cmd_before.go index 454fcec..0733eb6 100644 --- a/common/legocmd/cmd/cmd_before.go +++ b/common/legoCmd/cmd/cmd_before.go @@ -1,7 +1,7 @@ package cmd import ( - "github.com/Yuzuki616/V2bX/common/legocmd/log" + "github.com/Yuzuki616/V2bX/common/legoCmd/log" "github.com/urfave/cli" ) diff --git a/common/legocmd/cmd/cmd_dnshelp.go b/common/legoCmd/cmd/cmd_dnshelp.go similarity index 100% rename from common/legocmd/cmd/cmd_dnshelp.go rename to common/legoCmd/cmd/cmd_dnshelp.go diff --git a/common/legocmd/cmd/cmd_list.go b/common/legoCmd/cmd/cmd_list.go similarity index 100% rename from common/legocmd/cmd/cmd_list.go rename to common/legoCmd/cmd/cmd_list.go diff --git a/common/legocmd/cmd/cmd_renew.go b/common/legoCmd/cmd/cmd_renew.go similarity index 99% rename from common/legocmd/cmd/cmd_renew.go rename to common/legoCmd/cmd/cmd_renew.go index e4303a7..aa262be 100644 --- a/common/legocmd/cmd/cmd_renew.go +++ b/common/legoCmd/cmd/cmd_renew.go @@ -5,7 +5,7 @@ import ( "crypto/x509" "time" - "github.com/Yuzuki616/V2bX/common/legocmd/log" + "github.com/Yuzuki616/V2bX/common/legoCmd/log" "github.com/go-acme/lego/v4/certcrypto" "github.com/go-acme/lego/v4/certificate" "github.com/go-acme/lego/v4/lego" diff --git a/common/legocmd/cmd/cmd_renew_test.go b/common/legoCmd/cmd/cmd_renew_test.go similarity index 100% rename from common/legocmd/cmd/cmd_renew_test.go rename to common/legoCmd/cmd/cmd_renew_test.go diff --git a/common/legocmd/cmd/cmd_revoke.go b/common/legoCmd/cmd/cmd_revoke.go similarity index 96% rename from common/legocmd/cmd/cmd_revoke.go rename to common/legoCmd/cmd/cmd_revoke.go index 659e166..a7c6d7a 100644 --- a/common/legocmd/cmd/cmd_revoke.go +++ b/common/legoCmd/cmd/cmd_revoke.go @@ -1,7 +1,7 @@ package cmd import ( - "github.com/Yuzuki616/V2bX/common/legocmd/log" + "github.com/Yuzuki616/V2bX/common/legoCmd/log" "github.com/urfave/cli" ) diff --git a/common/legocmd/cmd/cmd_run.go b/common/legoCmd/cmd/cmd_run.go similarity index 99% rename from common/legocmd/cmd/cmd_run.go rename to common/legoCmd/cmd/cmd_run.go index 5d4dc92..dbbb355 100644 --- a/common/legocmd/cmd/cmd_run.go +++ b/common/legoCmd/cmd/cmd_run.go @@ -6,7 +6,7 @@ import ( "os" "strings" - "github.com/Yuzuki616/V2bX/common/legocmd/log" + "github.com/Yuzuki616/V2bX/common/legoCmd/log" "github.com/go-acme/lego/v4/certificate" "github.com/go-acme/lego/v4/lego" "github.com/go-acme/lego/v4/registration" diff --git a/common/legocmd/cmd/flags.go b/common/legoCmd/cmd/flags.go similarity index 100% rename from common/legocmd/cmd/flags.go rename to common/legoCmd/cmd/flags.go diff --git a/common/legocmd/cmd/hook.go b/common/legoCmd/cmd/hook.go similarity index 100% rename from common/legocmd/cmd/hook.go rename to common/legoCmd/cmd/hook.go diff --git a/common/legocmd/cmd/setup.go b/common/legoCmd/cmd/setup.go similarity index 98% rename from common/legocmd/cmd/setup.go rename to common/legoCmd/cmd/setup.go index f4d2adf..52374b4 100644 --- a/common/legocmd/cmd/setup.go +++ b/common/legoCmd/cmd/setup.go @@ -9,7 +9,7 @@ import ( "strings" "time" - "github.com/Yuzuki616/V2bX/common/legocmd/log" + "github.com/Yuzuki616/V2bX/common/legoCmd/log" "github.com/go-acme/lego/v4/certcrypto" "github.com/go-acme/lego/v4/lego" "github.com/go-acme/lego/v4/registration" diff --git a/common/legocmd/cmd/setup_challenges.go b/common/legoCmd/cmd/setup_challenges.go similarity index 98% rename from common/legocmd/cmd/setup_challenges.go rename to common/legoCmd/cmd/setup_challenges.go index f0511df..076bceb 100644 --- a/common/legocmd/cmd/setup_challenges.go +++ b/common/legoCmd/cmd/setup_challenges.go @@ -5,7 +5,7 @@ import ( "strings" "time" - "github.com/Yuzuki616/V2bX/common/legocmd/log" + "github.com/Yuzuki616/V2bX/common/legoCmd/log" "github.com/go-acme/lego/v4/challenge" "github.com/go-acme/lego/v4/challenge/dns01" "github.com/go-acme/lego/v4/challenge/http01" diff --git a/common/legocmd/cmd/zz_gen_cmd_dnshelp.go b/common/legoCmd/cmd/zz_gen_cmd_dnshelp.go similarity index 100% rename from common/legocmd/cmd/zz_gen_cmd_dnshelp.go rename to common/legoCmd/cmd/zz_gen_cmd_dnshelp.go diff --git a/common/legocmd/lego.go b/common/legoCmd/lego.go similarity index 87% rename from common/legocmd/lego.go rename to common/legoCmd/lego.go index c5ec7ce..7d9a779 100644 --- a/common/legocmd/lego.go +++ b/common/legoCmd/lego.go @@ -1,6 +1,6 @@ -// Let's Encrypt client to go! +// Package legocmd Let's Encrypt client to go! // CLI application for generating Let's Encrypt certificates using the ACME package. -package legocmd +package legoCmd import ( "errors" @@ -11,7 +11,7 @@ import ( "runtime" "strings" - "github.com/Yuzuki616/V2bX/common/legocmd/cmd" + "github.com/Yuzuki616/V2bX/common/legoCmd/cmd" "github.com/urfave/cli" ) @@ -34,18 +34,18 @@ func New() (*LegoCMD, error) { fmt.Printf("lego version %s %s/%s\n", c.App.Version, runtime.GOOS, runtime.GOARCH) } - // Set default path to configPath/cert - var path string = "" + // Set default pathTemp to configPath/cert + var pathTemp = "" configPath := os.Getenv("XRAY_LOCATION_CONFIG") if configPath != "" { - path = configPath + pathTemp = configPath } else if cwd, err := os.Getwd(); err == nil { - path = cwd + pathTemp = cwd } else { - path = "." + pathTemp = "." } - defaultPath = filepath.Join(path, "cert") + defaultPath = filepath.Join(pathTemp, "cert") app.Flags = cmd.CreateFlags(defaultPath) @@ -121,8 +121,8 @@ func (l *LegoCMD) HTTPCert(domain, email string) (CertPath string, KeyPath strin if err == nil { return CertPath, KeyPath, err } - argstring := fmt.Sprintf("lego -a -d %s -m %s --http run", domain, email) - err = l.cmdClient.Run(strings.Split(argstring, " ")) + argString := fmt.Sprintf("lego -a -d %s -m %s --http run", domain, email) + err = l.cmdClient.Run(strings.Split(argString, " ")) if err != nil { return "", "", err @@ -146,7 +146,7 @@ func (l *LegoCMD) RenewCert(domain, email, certMode, provider string, DNSEnv map case error: err = x default: - err = errors.New("unknow panic") + err = errors.New("unknown panic") } return "", "", err } @@ -161,7 +161,7 @@ func (l *LegoCMD) RenewCert(domain, email, certMode, provider string, DNSEnv map } argstring = fmt.Sprintf("lego -a -d %s -m %s --dns %s renew --days 30", domain, email, provider) } else { - return "", "", fmt.Errorf("Unsupport cert mode: %s", certMode) + return "", "", fmt.Errorf("unsupport cert mode: %s", certMode) } err = l.cmdClient.Run(strings.Split(argstring, " ")) @@ -178,10 +178,10 @@ func checkCertfile(domain string) (string, string, error) { keyPath := path.Join(defaultPath, "certificates", fmt.Sprintf("%s.key", domain)) certPath := path.Join(defaultPath, "certificates", fmt.Sprintf("%s.crt", domain)) if _, err := os.Stat(keyPath); os.IsNotExist(err) { - return "", "", fmt.Errorf("Cert key failed: %s", domain) + return "", "", fmt.Errorf("cert key failed: %s", domain) } if _, err := os.Stat(certPath); os.IsNotExist(err) { - return "", "", fmt.Errorf("Cert cert failed: %s", domain) + return "", "", fmt.Errorf("cert cert failed: %s", domain) } absKeyPath, _ := filepath.Abs(keyPath) absCertPath, _ := filepath.Abs(certPath) diff --git a/common/legocmd/lego_test.go b/common/legoCmd/lego_test.go similarity index 89% rename from common/legocmd/lego_test.go rename to common/legoCmd/lego_test.go index b753440..5dab9a2 100644 --- a/common/legocmd/lego_test.go +++ b/common/legoCmd/lego_test.go @@ -1,20 +1,20 @@ -package legocmd_test +package legoCmd_test import ( "testing" - "github.com/Yuzuki616/V2bX/common/legocmd" + "github.com/Yuzuki616/V2bX/common/legoCmd" ) func TestLegoClient(t *testing.T) { - _, err := legocmd.New() + _, err := legoCmd.New() if err != nil { t.Error(err) } } func TestLegoDNSCert(t *testing.T) { - lego, err := legocmd.New() + lego, err := legoCmd.New() if err != nil { t.Error(err) } @@ -36,7 +36,7 @@ func TestLegoDNSCert(t *testing.T) { } func TestLegoHTTPCert(t *testing.T) { - lego, err := legocmd.New() + lego, err := legoCmd.New() if err != nil { t.Error(err) } @@ -53,7 +53,7 @@ func TestLegoHTTPCert(t *testing.T) { } func TestLegoRenewCert(t *testing.T) { - lego, err := legocmd.New() + lego, err := legoCmd.New() if err != nil { t.Error(err) } diff --git a/common/rule/rule.go b/common/rule/rule.go index 5ecbb13..ee87c6a 100644 --- a/common/rule/rule.go +++ b/common/rule/rule.go @@ -12,19 +12,19 @@ import ( mapset "github.com/deckarep/golang-set" ) -type RuleManager struct { +type Rule struct { InboundRule *sync.Map // Key: Tag, Value: []api.DetectRule InboundDetectResult *sync.Map // key: Tag, Value: mapset.NewSet []api.DetectResult } -func New() *RuleManager { - return &RuleManager{ +func New() *Rule { + return &Rule{ InboundRule: new(sync.Map), InboundDetectResult: new(sync.Map), } } -func (r *RuleManager) UpdateRule(tag string, newRuleList []api.DetectRule) error { +func (r *Rule) UpdateRule(tag string, newRuleList []api.DetectRule) error { if value, ok := r.InboundRule.LoadOrStore(tag, newRuleList); ok { oldRuleList := value.([]api.DetectRule) if !reflect.DeepEqual(oldRuleList, newRuleList) { @@ -34,7 +34,7 @@ func (r *RuleManager) UpdateRule(tag string, newRuleList []api.DetectRule) error return nil } -func (r *RuleManager) GetDetectResult(tag string) (*[]api.DetectResult, error) { +func (r *Rule) GetDetectResult(tag string) (*[]api.DetectResult, error) { detectResult := make([]api.DetectResult, 0) if value, ok := r.InboundDetectResult.LoadAndDelete(tag); ok { resultSet := value.(mapset.Set) @@ -46,9 +46,9 @@ func (r *RuleManager) GetDetectResult(tag string) (*[]api.DetectResult, error) { return &detectResult, nil } -func (r *RuleManager) Detect(tag string, destination string, email string) (reject bool) { +func (r *Rule) Detect(tag string, destination string, email string) (reject bool) { reject = false - var hitRuleID int = -1 + var hitRuleID = -1 // If we have some rule for this inbound if value, ok := r.InboundRule.Load(tag); ok { ruleList := value.([]api.DetectRule) diff --git a/common/serverstatus/serverstatus.go b/common/serverstatus/serverstatus.go deleted file mode 100644 index f6a2fb8..0000000 --- a/common/serverstatus/serverstatus.go +++ /dev/null @@ -1,41 +0,0 @@ -// Package serverstatus generate the server system status -package serverstatus - -import ( - "fmt" - "time" - - "github.com/shirou/gopsutil/cpu" - "github.com/shirou/gopsutil/disk" - "github.com/shirou/gopsutil/mem" -) - -// GetSystemInfo get the system info of a given periodic -func GetSystemInfo() (Cpu float64, Mem float64, Disk float64, Uptime int, err error) { - - upTime := time.Now() - cpuPercent, err := cpu.Percent(0, false) - // Check if cpuPercent is empty - if len(cpuPercent) > 0 { - Cpu = cpuPercent[0] - } else { - Cpu = 0 - } - - if err != nil { - return 0, 0, 0, 0, fmt.Errorf("get cpu usage failed: %s", err) - } - - memUsage, err := mem.VirtualMemory() - if err != nil { - return 0, 0, 0, 0, fmt.Errorf("get mem usage failed: %s", err) - } - - diskUsage, err := disk.Usage("/") - if err != nil { - return 0, 0, 0, 0, fmt.Errorf("et disk usage failed: %s", err) - } - - Uptime = int(time.Since(upTime).Seconds()) - return Cpu, memUsage.UsedPercent, diskUsage.UsedPercent, Uptime, nil -} diff --git a/service/controller/controller.go b/service/controller/controller.go index ba5be89..44833e9 100644 --- a/service/controller/controller.go +++ b/service/controller/controller.go @@ -8,7 +8,7 @@ import ( "time" "github.com/Yuzuki616/V2bX/api" - "github.com/Yuzuki616/V2bX/common/legocmd" + "github.com/Yuzuki616/V2bX/common/legoCmd" "github.com/xtls/xray-core/common/protocol" "github.com/xtls/xray-core/common/task" "github.com/xtls/xray-core/core" @@ -168,7 +168,7 @@ func (c *Controller) nodeInfoMonitor() (err error) { // Check Cert if c.nodeInfo.EnableTls && c.config.CertConfig.CertMode != "none" && (c.config.CertConfig.CertMode == "dns" || c.config.CertConfig.CertMode == "http") { - lego, err := legocmd.New() + lego, err := legoCmd.New() if err != nil { log.Print(err) } diff --git a/service/controller/inboundbuilder.go b/service/controller/inboundbuilder.go index b031b9f..5232a49 100644 --- a/service/controller/inboundbuilder.go +++ b/service/controller/inboundbuilder.go @@ -1,11 +1,11 @@ -//Package generate the InbounderConfig used by add inbound +// Package controller the InbounderConfig used by add inbound package controller import ( "encoding/json" "fmt" "github.com/Yuzuki616/V2bX/api" - "github.com/Yuzuki616/V2bX/common/legocmd" + "github.com/Yuzuki616/V2bX/common/legoCmd" "github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/uuid" "github.com/xtls/xray-core/core" @@ -178,7 +178,7 @@ func getCertFile(certConfig *CertConfig) (certFile string, keyFile string, err e } return certConfig.CertFile, certConfig.KeyFile, nil } else if certConfig.CertMode == "dns" { - lego, err := legocmd.New() + lego, err := legoCmd.New() if err != nil { return "", "", err } @@ -188,7 +188,7 @@ func getCertFile(certConfig *CertConfig) (certFile string, keyFile string, err e } return certPath, keyPath, err } else if certConfig.CertMode == "http" { - lego, err := legocmd.New() + lego, err := legoCmd.New() if err != nil { return "", "", err } diff --git a/service/controller/outboundbuilder.go b/service/controller/outboundbuilder.go index 49eb12b..4aefbe7 100644 --- a/service/controller/outboundbuilder.go +++ b/service/controller/outboundbuilder.go @@ -19,11 +19,11 @@ func OutboundBuilder(config *Config, nodeInfo *api.NodeInfo, tag string) (*core. // Build Send IP address if config.SendIP != "" { ipAddress := net.ParseAddress(config.SendIP) - outboundDetourConfig.SendThrough = &conf.Address{ipAddress} + outboundDetourConfig.SendThrough = &conf.Address{Address: ipAddress} } // Freedom Protocol setting - var domainStrategy string = "Asis" + var domainStrategy = "Asis" if config.EnableDNS { if config.DNSType != "" { domainStrategy = config.DNSType @@ -41,7 +41,7 @@ func OutboundBuilder(config *Config, nodeInfo *api.NodeInfo, tag string) (*core. var setting json.RawMessage setting, err := json.Marshal(proxySetting) if err != nil { - return nil, fmt.Errorf("Marshal proxy %s config fialed: %s", nodeInfo.NodeType, err) + return nil, fmt.Errorf("marshal proxy %s config fialed: %s", nodeInfo.NodeType, err) } outboundDetourConfig.Settings = &setting return outboundDetourConfig.Build()