github.com/TeaOSLab/EdgeNode@v1.3.8/internal/nodes/task_ocsp_update.go (about) 1 // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. 2 3 package nodes 4 5 import ( 6 "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" 7 teaconst "github.com/TeaOSLab/EdgeNode/internal/const" 8 "github.com/TeaOSLab/EdgeNode/internal/events" 9 "github.com/TeaOSLab/EdgeNode/internal/goman" 10 "github.com/TeaOSLab/EdgeNode/internal/remotelogs" 11 "github.com/TeaOSLab/EdgeNode/internal/rpc" 12 "github.com/iwind/TeaGo/Tea" 13 "time" 14 ) 15 16 var sharedOCSPTask = NewOCSPUpdateTask() 17 18 func init() { 19 if !teaconst.IsMain { 20 return 21 } 22 23 events.On(events.EventLoaded, func() { 24 sharedOCSPTask.version = sharedNodeConfig.OCSPVersion 25 26 goman.New(func() { 27 sharedOCSPTask.Start() 28 }) 29 }) 30 events.OnClose(func() { 31 sharedOCSPTask.Stop() 32 }) 33 } 34 35 // OCSPUpdateTask 更新OCSP任务 36 type OCSPUpdateTask struct { 37 version int64 38 39 ticker *time.Ticker 40 } 41 42 func NewOCSPUpdateTask() *OCSPUpdateTask { 43 var ticker = time.NewTicker(1 * time.Minute) 44 if Tea.IsTesting() { 45 ticker = time.NewTicker(10 * time.Second) 46 } 47 return &OCSPUpdateTask{ 48 ticker: ticker, 49 } 50 } 51 52 func (this *OCSPUpdateTask) Start() { 53 for range this.ticker.C { 54 err := this.Loop() 55 if err != nil { 56 if rpc.IsConnError(err) { 57 remotelogs.Debug("OCSPUpdateTask", "update ocsp failed: "+err.Error()) 58 } else { 59 remotelogs.Warn("OCSPUpdateTask", "update ocsp failed: "+err.Error()) 60 } 61 } 62 } 63 } 64 65 func (this *OCSPUpdateTask) Loop() error { 66 rpcClient, err := rpc.SharedRPC() 67 if err != nil { 68 return err 69 } 70 71 resp, err := rpcClient.SSLCertRPC.ListUpdatedSSLCertOCSP(rpcClient.Context(), &pb.ListUpdatedSSLCertOCSPRequest{ 72 Version: this.version, 73 Size: 100, 74 }) 75 if err != nil { 76 return err 77 } 78 79 for _, ocsp := range resp.SslCertOCSP { 80 // 更新OCSP 81 if sharedNodeConfig != nil { 82 sharedNodeConfig.UpdateCertOCSP(ocsp.SslCertId, ocsp.Data, ocsp.ExpiresAt) 83 } 84 85 // 修改版本 86 this.version = ocsp.Version 87 } 88 89 return nil 90 } 91 92 func (this *OCSPUpdateTask) Stop() { 93 this.ticker.Stop() 94 }