From d8140ddf083425014f92ae525b36442141ab92bb Mon Sep 17 00:00:00 2001 From: Daniel Ding Date: Tue, 23 Sep 2025 19:38:29 +0800 Subject: [PATCH] fix: display expire time. --- cmd/api/v5/version.go | 5 +++-- pkg/api/version.go | 7 +++++-- pkg/cache/user.go | 23 +++++++++++++++++++++++ pkg/schema/version.go | 1 + 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/cmd/api/v5/version.go b/cmd/api/v5/version.go index 73e9f06..d88a84f 100755 --- a/cmd/api/v5/version.go +++ b/cmd/api/v5/version.go @@ -15,8 +15,9 @@ func (v Version) Url(prefix, name string) string { } func (v Version) Tmpl() string { - return `Version : {{ .Version }} -Build at: {{ .Date}} + return `Version : {{ .Version }} +Build at : {{ .Date}} +Expire at: {{ .Expire }} ` } diff --git a/pkg/api/version.go b/pkg/api/version.go index e025156..d28bc7a 100755 --- a/pkg/api/version.go +++ b/pkg/api/version.go @@ -1,9 +1,11 @@ package api import ( - "github.com/gorilla/mux" - "github.com/luscis/openlan/pkg/schema" "net/http" + + "github.com/gorilla/mux" + "github.com/luscis/openlan/pkg/cache" + "github.com/luscis/openlan/pkg/schema" ) type Version struct { @@ -15,5 +17,6 @@ func (l Version) Router(router *mux.Router) { func (l Version) List(w http.ResponseWriter, r *http.Request) { ver := schema.NewVersionSchema() + ver.Expire = cache.User.ExpireTime() ResponseJson(w, ver) } diff --git a/pkg/cache/user.go b/pkg/cache/user.go index 3e0fdc8..4089d0c 100755 --- a/pkg/cache/user.go +++ b/pkg/cache/user.go @@ -246,6 +246,29 @@ func (w *user) SetCert(cfg *libol.CertConfig) { w.Cert = cfg.Crt } +func (w *user) ExpireTime() string { + if w.Cert == "" { + return "" + } + + pemData, err := os.ReadFile(w.Cert) + if err != nil { + return "" + } + + block, rest := pem.Decode(pemData) + if block == nil || len(rest) > 0 { + return "" + } + + cert, err := x509.ParseCertificate(block.Bytes) + if err != nil { + return "" + } + + return cert.NotAfter.Format(time.RFC3339) +} + var User = user{ Users: libol.NewSafeStrMap(1024), } diff --git a/pkg/schema/version.go b/pkg/schema/version.go index 2887353..c404a27 100755 --- a/pkg/schema/version.go +++ b/pkg/schema/version.go @@ -5,6 +5,7 @@ import "github.com/luscis/openlan/pkg/libol" type Version struct { Version string `json:"version"` Date string `json:"date"` + Expire string `json:"expire"` } func NewVersionSchema() Version {