From a2d43b3746e14017f9a48a6065cc271d4c103d57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sun, 5 Mar 2023 11:05:30 +0800 Subject: [PATCH] Fix open cache file --- box.go | 25 +++++++++++++------------ experimental/clashapi/server.go | 14 +++++++++----- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/box.go b/box.go index 2f83aa8b..d2d30637 100644 --- a/box.go +++ b/box.go @@ -213,6 +213,18 @@ func (s *Box) Start() error { } func (s *Box) start() error { + if s.clashServer != nil { + err := s.clashServer.Start() + if err != nil { + return E.Cause(err, "start clash api server") + } + } + if s.v2rayServer != nil { + err := s.v2rayServer.Start() + if err != nil { + return E.Cause(err, "start v2ray api server") + } + } for i, out := range s.outbounds { if starter, isStarter := out.(common.Starter); isStarter { err := starter.Start() @@ -243,18 +255,7 @@ func (s *Box) start() error { return E.Cause(err, "initialize inbound/", in.Type(), "[", tag, "]") } } - if s.clashServer != nil { - err = s.clashServer.Start() - if err != nil { - return E.Cause(err, "start clash api server") - } - } - if s.v2rayServer != nil { - err = s.v2rayServer.Start() - if err != nil { - return E.Cause(err, "start v2ray api server") - } - } + s.logger.Info("sing-box started (", F.Seconds(time.Since(s.createdAt).Seconds()), "s)") return nil } diff --git a/experimental/clashapi/server.go b/experimental/clashapi/server.go index c2ac9ca4..a7bd1b95 100644 --- a/experimental/clashapi/server.go +++ b/experimental/clashapi/server.go @@ -44,6 +44,7 @@ type Server struct { urlTestHistory *urltest.HistoryStorage mode string storeSelected bool + cacheFilePath string cacheFile adapter.ClashCacheFile } @@ -75,11 +76,7 @@ func NewServer(router adapter.Router, logFactory log.ObservableFactory, options } else { cachePath = C.BasePath(cachePath) } - cacheFile, err := cachefile.Open(cachePath) - if err != nil { - return nil, E.Cause(err, "open cache file") - } - server.cacheFile = cacheFile + server.cacheFilePath = cachePath } cors := cors.New(cors.Options{ AllowedOrigins: []string{"*"}, @@ -118,6 +115,13 @@ func NewServer(router adapter.Router, logFactory log.ObservableFactory, options } func (s *Server) Start() error { + if s.cacheFilePath != "" { + cacheFile, err := cachefile.Open(s.cacheFilePath) + if err != nil { + return E.Cause(err, "open cache file") + } + s.cacheFile = cacheFile + } listener, err := net.Listen("tcp", s.httpServer.Addr) if err != nil { return E.Cause(err, "external controller listen error")