Fix open cache file

This commit is contained in:
世界 2023-03-05 11:05:30 +08:00
parent 5b3b74bd0f
commit a2d43b3746
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 22 additions and 17 deletions

25
box.go
View file

@ -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
}

View file

@ -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")