diff --git a/common/task/task.go b/common/task/task.go index b04eeb7..f5a3234 100644 --- a/common/task/task.go +++ b/common/task/task.go @@ -24,20 +24,24 @@ func (t *Task) hasClosed() bool { return !t.running } -func (t *Task) checkedExecute() error { +func (t *Task) checkedExecute(first bool) error { if t.hasClosed() { return nil } t.access.Lock() defer t.access.Unlock() - + if first { + if err := t.Execute(); err != nil { + t.running = false + return err + } + } if !t.running { return nil } - t.timer = time.AfterFunc(t.Interval, func() { - t.checkedExecute() + t.checkedExecute(true) }) return nil @@ -52,21 +56,12 @@ func (t *Task) Start(first bool) error { } t.running = true t.access.Unlock() - if first { - if err := t.Execute(); err != nil { - t.access.Lock() - t.running = false - t.access.Unlock() - return err - } - } - if err := t.checkedExecute(); err != nil { + if err := t.checkedExecute(first); err != nil { t.access.Lock() t.running = false t.access.Unlock() return err } - return nil } diff --git a/common/task/task_test.go b/common/task/task_test.go new file mode 100644 index 0000000..dec33b1 --- /dev/null +++ b/common/task/task_test.go @@ -0,0 +1,15 @@ +package task + +import ( + "log" + "testing" + "time" +) + +func TestTask(t *testing.T) { + ts := Task{Execute: func() error { + log.Println("q") + return nil + }, Interval: time.Second} + ts.Start(false) +} diff --git a/go.mod b/go.mod index 8aaaffb..5e83c7f 100644 --- a/go.mod +++ b/go.mod @@ -159,7 +159,6 @@ require ( github.com/spf13/pflag v1.0.5 // indirect github.com/stretchr/objx v0.5.0 // indirect github.com/stretchr/testify v1.8.4 // indirect - github.com/subosito/gotenv v1.4.2 // indirect github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.490 // indirect github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.490 // indirect github.com/transip/gotransip/v6 v6.17.0 // indirect diff --git a/go.sum b/go.sum index b50b901..82cf811 100644 --- a/go.sum +++ b/go.sum @@ -947,7 +947,6 @@ golang.org/x/term v0.4.0/go.mod h1:9P2UbLfCdcvo3p/nzKvsmas4TnlujnuoV9hGgYzW1lQ= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols= golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= -golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= diff --git a/node/controller.go b/node/controller.go index 2753d4c..cd64eaf 100644 --- a/node/controller.go +++ b/node/controller.go @@ -63,7 +63,7 @@ func (c *Controller) Start() error { return fmt.Errorf("update rule error: %s", err) } } - if c.nodeInfo.Tls { + if c.nodeInfo.Tls || c.nodeInfo.Type == "hysteria" { err = c.requestCert() if err != nil { return fmt.Errorf("request cert error: %s", err) diff --git a/node/lego/lego.go b/node/lego/lego.go index 374acd4..4a29c9c 100644 --- a/node/lego/lego.go +++ b/node/lego/lego.go @@ -16,7 +16,8 @@ type Lego struct { } func New(config *conf.CertConfig) (*Lego, error) { - user, err := NewUser(path.Join("/etc/V2bX/cert/user/", + user, err := NewUser(path.Join(path.Dir(config.CertFile), + "user", fmt.Sprintf("user-%s.json", config.Email)), config.Email) if err != nil { diff --git a/node/task.go b/node/task.go index 768b7e2..59d3ee2 100644 --- a/node/task.go +++ b/node/task.go @@ -67,7 +67,7 @@ func (c *Controller) nodeInfoMonitor() (err error) { log.Print(err) return nil } - if newNodeInfo.Tls { + if c.nodeInfo.Tls || c.nodeInfo.Type == "hysteria" { err = c.requestCert() if err != nil { return fmt.Errorf("request cert error: %s", err)