diff --git a/cmd/ratte/run.go b/cmd/ratte/run.go index 7b348a2..f246f3c 100644 --- a/cmd/ratte/run.go +++ b/cmd/ratte/run.go @@ -126,7 +126,7 @@ func runHandle(_ *cobra.Command, _ []string) { } runtime.GC() - sig := make(chan os.Signal, 1) + sig := make(chan os.Signal, 0) signal.Notify(sig, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM) <-sig // clear @@ -200,22 +200,24 @@ func startTriggerAndHandler(c []conf.Node) error { handlers = make([]*handler.Handler, len(c)) for _, nd := range c { var co core.Core - var pl panel.Panel - var ac *acme.Acme if c, ok := cores[nd.Options.Core]; ok { co = c } else { return fmt.Errorf("unknown core name: %s", nd.Options.Core) } + var pl panel.Panel if p, ok := panels[nd.Options.Panel]; ok { pl = p } else { return fmt.Errorf("") } - if a, ok := acmes[nd.Options.Acme]; ok { - ac = a - } else { - return fmt.Errorf("unknown acme name: %s", nd.Options.Acme) + var ac *acme.Acme + if len(acmes) != 0 { + if a, ok := acmes[nd.Options.Acme]; ok { + ac = a + } else { + return fmt.Errorf("unknown acme name: %s", nd.Options.Acme) + } } h := handler.New(co, pl, nd.Name, ac, log.WithFields( diff --git a/conf/conf.go b/conf/conf.go index 813f537..7e00e29 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -43,7 +43,7 @@ func New(path string) *Conf { } func (c *Conf) Load(data []byte) error { - if len(data) >= 0 { + if len(data) > 0 { err := json.Unmarshal(data, c) if err != nil { return fmt.Errorf("decode json error: %w", err) diff --git a/conf/core.go b/conf/core.go index 6feb365..31e0ba4 100644 --- a/conf/core.go +++ b/conf/core.go @@ -1,6 +1,9 @@ package conf -import "github.com/goccy/go-json" +import ( + "fmt" + "github.com/goccy/go-json" +) type Core struct { Name string `json:"Name,omitempty"` @@ -13,7 +16,7 @@ type _core Core func (c *Core) UnmarshalJSON(data []byte) error { err := json.Unmarshal(data, (*_core)(c)) if err != nil { - return err + return fmt.Errorf("failed to unmarshal core: %v", err) } if len(c.Config) == 0 { c.Config = data diff --git a/conf/node.go b/conf/node.go index 60a3403..a813d84 100644 --- a/conf/node.go +++ b/conf/node.go @@ -68,7 +68,7 @@ func (b *IntBytes) UnmarshalJSON(data []byte) error { var numS string err = json.Unmarshal(data, &numS) if err != nil { - return err + return fmt.Errorf("failed to unmarshal intBytes: %v", err) } unit := numS[len(numS)-2:] num, err = strconv.ParseUint(numS[:len(numS)-2], 10, 64) @@ -102,7 +102,7 @@ func (n *Node) UnmarshalJSON(data []byte) (err error) { rn := rawNodeConfig{} err = json.Unmarshal(data, &rn) if err != nil { - return err + return fmt.Errorf("failed to unmarshal Node: %v", err) } n.Remote = Remote{ @@ -112,24 +112,24 @@ func (n *Node) UnmarshalJSON(data []byte) (err error) { if len(rn.RemoteRaw) > 0 { err = json.Unmarshal(rn.RemoteRaw, &n.Remote) if err != nil { - return + return fmt.Errorf("failed to unmarshal RemoteRaw: %v", err) } } else { err = json.Unmarshal(data, &n.Remote) if err != nil { - return + return fmt.Errorf("failed to unmarshal Remote: %v", err) } } n.Options = Options{} if len(rn.OptRaw) > 0 { err = json.Unmarshal(rn.OptRaw, &n.Options) if err != nil { - return + return fmt.Errorf("failed to unmarshal OptRaw: %v", err) } } else { err = json.Unmarshal(data, &n.Options) if err != nil { - return + return fmt.Errorf("failed to unmarshal Options: %v", err) } } n.Trigger = Trigger{ @@ -141,12 +141,12 @@ func (n *Node) UnmarshalJSON(data []byte) (err error) { if len(rn.TriggerRaw) > 0 { err = json.Unmarshal(rn.OptRaw, &n.Trigger) if err != nil { - return + return fmt.Errorf("failed to unmarshal TriggerRaw: %v", err) } } else { err = json.Unmarshal(data, &n.Trigger) if err != nil { - return + return fmt.Errorf("failed to unmarshal Trigger: %v", err) } } if len(rn.Name) > 0 { diff --git a/conf/watcher.go b/conf/watcher.go index 132ff77..8a9e66c 100644 --- a/conf/watcher.go +++ b/conf/watcher.go @@ -46,7 +46,7 @@ func (c *Conf) Watch() error { c.configWatcher = w } } else { - if !c.Watcher.WatchLocalConfig { + if c.Watcher.WatchLocalConfig { w := watcher.NewLocalWatcher(path.Dir(c.path), []string{path.Base(c.path)}) c.configWatcher = w } diff --git a/go.mod b/go.mod index 8ba3f6b..5b28597 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.22.0 toolchain go1.23.1 require ( - github.com/InazumaV/Ratte-Interface v0.0.0-20250326062317-c1a05d8e7762 + github.com/InazumaV/Ratte-Interface v0.0.0-20250413082501-770addad57ef github.com/fsnotify/fsnotify v1.7.0 github.com/go-acme/lego/v4 v4.18.0 github.com/goccy/go-json v0.10.3 @@ -37,7 +37,6 @@ require ( github.com/Azure/go-autorest/tracing v0.6.0 // indirect github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 // indirect github.com/OpenDNS/vegadns2client v0.0.0-20180418235048-a3fa4a771d87 // indirect - github.com/Yuzuki616/Ratte-Interface v0.0.0-20241122172739-1b3f94836144 // indirect github.com/akamai/AkamaiOPEN-edgegrid-golang v1.2.2 // indirect github.com/aliyun/alibaba-cloud-sdk-go v1.62.712 // indirect github.com/aws/aws-sdk-go-v2 v1.27.2 // indirect diff --git a/go.sum b/go.sum index bc8ff1a..a1ed8df 100644 --- a/go.sum +++ b/go.sum @@ -61,19 +61,13 @@ github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2/go.mod h1:wP83 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/HdrHistogram/hdrhistogram-go v1.1.2/go.mod h1:yDgFjdqOqDEKOvasDdhWNXYg9BVp4O+o5f6V/ehm6Oo= -github.com/InazumaV/Ratte-Interface v0.0.0-20241209172154-eb1d9356093c h1:Cv3vuJGl7kGoINJe3E0NNSzkWW/aLj9ohr41idFlmQs= -github.com/InazumaV/Ratte-Interface v0.0.0-20241209172154-eb1d9356093c/go.mod h1:UxqxslbpT8Q4qMldSNI6VCX5WFXWDbvE4fXpymp73kA= -github.com/InazumaV/Ratte-Interface v0.0.0-20250218004206-5947494fa692 h1:CZ5+CmQeE3SkCi6yXGqvmip+Tqw6ZHv9ThjlBk2iUns= -github.com/InazumaV/Ratte-Interface v0.0.0-20250218004206-5947494fa692/go.mod h1:UxqxslbpT8Q4qMldSNI6VCX5WFXWDbvE4fXpymp73kA= -github.com/InazumaV/Ratte-Interface v0.0.0-20250218011150-2b79ae58515b h1:OKjc2fZbgTlCmvnHbX8Do4WJLYzmCYxyUVFrE2HQhxw= -github.com/InazumaV/Ratte-Interface v0.0.0-20250218011150-2b79ae58515b/go.mod h1:UxqxslbpT8Q4qMldSNI6VCX5WFXWDbvE4fXpymp73kA= -github.com/InazumaV/Ratte-Interface v0.0.0-20250326062317-c1a05d8e7762 h1:2J/0eyXfCJ9I9SR5MZTENI1rOA522xj1PiQWIl3A8XA= -github.com/InazumaV/Ratte-Interface v0.0.0-20250326062317-c1a05d8e7762/go.mod h1:UxqxslbpT8Q4qMldSNI6VCX5WFXWDbvE4fXpymp73kA= +github.com/InazumaV/Ratte-Interface v0.0.0-20250413075814-8a0ac3a980e8 h1:YMlH0rDTLoVk1uNerDpkMK6Plam9gaixJIe3f35jKKg= +github.com/InazumaV/Ratte-Interface v0.0.0-20250413075814-8a0ac3a980e8/go.mod h1:UxqxslbpT8Q4qMldSNI6VCX5WFXWDbvE4fXpymp73kA= +github.com/InazumaV/Ratte-Interface v0.0.0-20250413082501-770addad57ef h1:0ULNkD9t3RIP15BlYqlx61DPjSHTGXYObWW2mIgD9qI= +github.com/InazumaV/Ratte-Interface v0.0.0-20250413082501-770addad57ef/go.mod h1:UxqxslbpT8Q4qMldSNI6VCX5WFXWDbvE4fXpymp73kA= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/OpenDNS/vegadns2client v0.0.0-20180418235048-a3fa4a771d87 h1:xPMsUicZ3iosVPSIP7bW5EcGUzjiiMl1OYTe14y/R24= github.com/OpenDNS/vegadns2client v0.0.0-20180418235048-a3fa4a771d87/go.mod h1:iGLljf5n9GjT6kc0HBvyI1nOKnGQbNB66VzSNbK5iks= -github.com/Yuzuki616/Ratte-Interface v0.0.0-20241122172739-1b3f94836144 h1:cAwvrrndgL3R68WwMWAFweyrIgy+0HO4VcQG74jVxyI= -github.com/Yuzuki616/Ratte-Interface v0.0.0-20241122172739-1b3f94836144/go.mod h1:oW6wbZfawdudu9VbTy2YIWoMNpfttYX8qcsM19oXCCI= github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw= github.com/akamai/AkamaiOPEN-edgegrid-golang v1.2.2 h1:F1j7z+/DKEsYqZNoxC6wvfmaiDneLsQOFQmuq9NADSY= github.com/akamai/AkamaiOPEN-edgegrid-golang v1.2.2/go.mod h1:QlXr/TrICfQ/ANa76sLeQyhAJyNR9sEcfNuZBkY9jgY= diff --git a/test_data/config.json5 b/test_data/config.json5 new file mode 100644 index 0000000..6b3dab5 --- /dev/null +++ b/test_data/config.json5 @@ -0,0 +1,53 @@ +{ + "Log": { + "Level": "info", + "Path": "./log" + }, + "Watcher": { + "WatchLocalConfig": true, + "WatchRemoteConfig": true + }, + "Core": [ + { + "Name": "Xray", + "Path": "./test_data/Ratte-Core-Xray", + "DataPath": "./test_data" + } + ], + "Panel": [ + { + "Name": "V2board", + "Path": "./test_data/Ratte-Panel-V2b", + "DataPath": "./test_data" + } + ], + "Node": [ + { + "Name": "default-node", + "Remote": { + "ApiHost": "http://127.0.0.1", + "NodeID": 1, + "ApiKey": "your-api-key", + "NodeType": "model_context", + "Timeout": 30 + }, + "Options": { + "Core": "Xray", + "Panel": "V2board", + "Limit": { + "IPLimit": 0, + "SpeedLimit": "0MB" + }, + "Other": { + "SendIp": "127.0.0.1" + } + }, + "Trigger": { + "PullNodeCron": 60, + "PullUserCron": 60, + "ReportUserCron": 60, + "RenewCertCron": "0 2 * * *" + } + } + ] +}