Skip to content

Commit

Permalink
Add filemanager api
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Jun 7, 2023
1 parent b6068ce commit 98c2c43
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 54 deletions.
3 changes: 2 additions & 1 deletion box.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func New(options Options) (*Box, error) {
defaultLogWriter = io.Discard
}
logFactory, err := log.New(log.Options{
Context: ctx,
Options: common.PtrValueOrDefault(options.Log),
Observable: needClashAPI,
DefaultWriter: defaultLogWriter,
Expand Down Expand Up @@ -142,7 +143,7 @@ func New(options Options) (*Box, error) {
preServices := make(map[string]adapter.Service)
postServices := make(map[string]adapter.Service)
if needClashAPI {
clashServer, err := experimental.NewClashServer(router, logFactory.(log.ObservableFactory), common.PtrValueOrDefault(options.Experimental.ClashAPI))
clashServer, err := experimental.NewClashServer(ctx, router, logFactory.(log.ObservableFactory), common.PtrValueOrDefault(options.Experimental.ClashAPI))
if err != nil {
return nil, E.Cause(err, "create clash api server")
}
Expand Down
29 changes: 1 addition & 28 deletions constant/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,13 @@ package constant
import (
"os"
"path/filepath"
"strings"

"github.com/sagernet/sing/common/rw"
)

const dirName = "sing-box"

var (
basePath string
tempPath string
resourcePaths []string
)

func BasePath(name string) string {
if basePath == "" || strings.HasPrefix(name, "/") {
return name
}
return filepath.Join(basePath, name)
}

func CreateTemp(pattern string) (*os.File, error) {
if tempPath == "" {
tempPath = os.TempDir()
}
return os.CreateTemp(tempPath, pattern)
}

func SetBasePath(path string) {
basePath = path
}

func SetTempPath(path string) {
tempPath = path
}
var resourcePaths []string

func FindPath(name string) (string, bool) {
name = os.ExpandEnv(name)
Expand Down
7 changes: 4 additions & 3 deletions experimental/clashapi.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package experimental

import (
"context"
"os"

"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
)

type ClashServerConstructor = func(router adapter.Router, logFactory log.ObservableFactory, options option.ClashAPIOptions) (adapter.ClashServer, error)
type ClashServerConstructor = func(ctx context.Context, router adapter.Router, logFactory log.ObservableFactory, options option.ClashAPIOptions) (adapter.ClashServer, error)

var clashServerConstructor ClashServerConstructor

func RegisterClashServerConstructor(constructor ClashServerConstructor) {
clashServerConstructor = constructor
}

func NewClashServer(router adapter.Router, logFactory log.ObservableFactory, options option.ClashAPIOptions) (adapter.ClashServer, error) {
func NewClashServer(ctx context.Context, router adapter.Router, logFactory log.ObservableFactory, options option.ClashAPIOptions) (adapter.ClashServer, error) {
if clashServerConstructor == nil {
return nil, os.ErrInvalid
}
return clashServerConstructor(router, logFactory, options)
return clashServerConstructor(ctx, router, logFactory, options)
}
9 changes: 6 additions & 3 deletions experimental/clashapi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
E "github.com/sagernet/sing/common/exceptions"
F "github.com/sagernet/sing/common/format"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/service/filemanager"
"github.com/sagernet/websocket"

"github.com/go-chi/chi/v5"
Expand All @@ -37,6 +38,7 @@ func init() {
var _ adapter.ClashServer = (*Server)(nil)

type Server struct {
ctx context.Context
router adapter.Router
logger log.Logger
httpServer *http.Server
Expand All @@ -53,10 +55,11 @@ type Server struct {
externalUIDownloadDetour string
}

func NewServer(router adapter.Router, logFactory log.ObservableFactory, options option.ClashAPIOptions) (adapter.ClashServer, error) {
func NewServer(ctx context.Context, router adapter.Router, logFactory log.ObservableFactory, options option.ClashAPIOptions) (adapter.ClashServer, error) {
trafficManager := trafficontrol.NewManager()
chiRouter := chi.NewRouter()
server := &Server{
ctx: ctx,
router: router,
logger: logFactory.NewLogger("clash-api"),
httpServer: &http.Server{
Expand All @@ -82,7 +85,7 @@ func NewServer(router adapter.Router, logFactory log.ObservableFactory, options
if foundPath, loaded := C.FindPath(cachePath); loaded {
cachePath = foundPath
} else {
cachePath = C.BasePath(cachePath)
cachePath = filemanager.BasePath(ctx, cachePath)
}
server.cacheFilePath = cachePath
}
Expand Down Expand Up @@ -113,7 +116,7 @@ func NewServer(router adapter.Router, logFactory log.ObservableFactory, options
server.setupMetaAPI(r)
})
if options.ExternalUI != "" {
server.externalUI = C.BasePath(os.ExpandEnv(options.ExternalUI))
server.externalUI = filemanager.BasePath(ctx, os.ExpandEnv(options.ExternalUI))
chiRouter.Group(func(r chi.Router) {
fs := http.StripPrefix("/ui", http.FileServer(http.Dir(server.externalUI)))
r.Get("/ui", http.RedirectHandler("/ui/", http.StatusTemporaryRedirect).ServeHTTP)
Expand Down
10 changes: 5 additions & 5 deletions experimental/clashapi/server_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
"time"

"github.com/sagernet/sing-box/adapter"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/service/filemanager"
)

func (s *Server) checkAndDownloadExternalUI() {
Expand Down Expand Up @@ -79,7 +79,7 @@ func (s *Server) downloadExternalUI() error {
}

func (s *Server) downloadZIP(name string, body io.Reader, output string) error {
tempFile, err := C.CreateTemp(name)
tempFile, err := filemanager.CreateTemp(s.ctx, name)
if err != nil {
return err
}
Expand Down Expand Up @@ -112,16 +112,16 @@ func (s *Server) downloadZIP(name string, body io.Reader, output string) error {
return err
}
savePath := filepath.Join(saveDirectory, pathElements[len(pathElements)-1])
err = downloadZIPEntry(file, savePath)
err = downloadZIPEntry(s.ctx, file, savePath)
if err != nil {
return err
}
}
return nil
}

func downloadZIPEntry(zipFile *zip.File, savePath string) error {
saveFile, err := os.Create(savePath)
func downloadZIPEntry(ctx context.Context, zipFile *zip.File, savePath string) error {
saveFile, err := filemanager.Create(ctx, savePath)
if err != nil {
return err
}
Expand Down
24 changes: 24 additions & 0 deletions experimental/libbox/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,27 @@ func RedirectStderr(path string) error {
stderrFile = outputFile
return nil
}

func RedirectStderrAsUser(path string, uid, gid int) error {
if stats, err := os.Stat(path); err == nil && stats.Size() > 0 {
_ = os.Rename(path, path+".old")
}
outputFile, err := os.Create(path)
if err != nil {
return err
}
err = outputFile.Chown(uid, gid)
if err != nil {
outputFile.Close()
os.Remove(outputFile.Name())
return err
}
err = unix.Dup2(int(outputFile.Fd()), int(os.Stderr.Fd()))
if err != nil {
outputFile.Close()
os.Remove(outputFile.Name())
return err
}
stderrFile = outputFile
return nil
}
2 changes: 2 additions & 0 deletions experimental/libbox/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/sagernet/sing/common/control"
E "github.com/sagernet/sing/common/exceptions"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/service/filemanager"
)

type BoxService struct {
Expand All @@ -30,6 +31,7 @@ func NewService(configContent string, platformInterface PlatformInterface) (*Box
return nil, err
}
ctx, cancel := context.WithCancel(context.Background())
ctx = filemanager.WithDefault(ctx, sBasePath, sTempPath, sUserID, sGroupID)
instance, err := box.New(box.Options{
Context: ctx,
Options: options,
Expand Down
24 changes: 19 additions & 5 deletions experimental/libbox/setup.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
package libbox

import (
"os"

C "github.com/sagernet/sing-box/constant"

"github.com/dustin/go-humanize"
)

func SetBasePath(path string) {
C.SetBasePath(path)
}
var (
sBasePath string
sTempPath string
sUserID int
sGroupID int
)

func SetTempPath(path string) {
C.SetTempPath(path)
func Setup(basePath string, tempPath string, userID int, groupID int) {
sBasePath = basePath
sTempPath = tempPath
sUserID = userID
sGroupID = groupID
if sUserID == -1 {
sUserID = os.Getuid()
}
if sGroupID == -1 {
sGroupID = os.Getgid()
}
}

func Version() string {
Expand Down
4 changes: 3 additions & 1 deletion include/clashapi_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package include

import (
"context"

"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/experimental"
"github.com/sagernet/sing-box/log"
Expand All @@ -11,7 +13,7 @@ import (
)

func init() {
experimental.RegisterClashServerConstructor(func(router adapter.Router, logFactory log.ObservableFactory, options option.ClashAPIOptions) (adapter.ClashServer, error) {
experimental.RegisterClashServerConstructor(func(ctx context.Context, router adapter.Router, logFactory log.ObservableFactory, options option.ClashAPIOptions) (adapter.ClashServer, error) {
return nil, E.New(`clash api is not included in this build, rebuild with -tags with_clash_api`)
})
}
6 changes: 4 additions & 2 deletions log/log.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package log

import (
"context"
"io"
"os"
"time"

C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/service/filemanager"
)

type factoryWithFile struct {
Expand Down Expand Up @@ -36,6 +37,7 @@ func (f *observableFactoryWithFile) Close() error {
}

type Options struct {
Context context.Context
Options option.LogOptions
Observable bool
DefaultWriter io.Writer
Expand Down Expand Up @@ -65,7 +67,7 @@ func New(options Options) (Factory, error) {
logWriter = os.Stdout
default:
var err error
logFile, err = os.OpenFile(C.BasePath(logOptions.Output), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
logFile, err = filemanager.OpenFile(options.Context, logOptions.Output, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return nil, err
}
Expand Down
13 changes: 7 additions & 6 deletions route/router_geo_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
"github.com/sagernet/sing/common/rw"
"github.com/sagernet/sing/service/filemanager"
)

func (r *Router) GeoIPReader() *geoip.Reader {
Expand Down Expand Up @@ -51,7 +52,7 @@ func (r *Router) prepareGeoIPDatabase() error {
geoPath = foundPath
}
}
geoPath = C.BasePath(geoPath)
geoPath = filemanager.BasePath(r.ctx, geoPath)
if rw.FileExists(geoPath) {
geoReader, codes, err := geoip.Open(geoPath)
if err == nil {
Expand Down Expand Up @@ -95,7 +96,7 @@ func (r *Router) prepareGeositeDatabase() error {
geoPath = foundPath
}
}
geoPath = C.BasePath(geoPath)
geoPath = filemanager.BasePath(r.ctx, geoPath)
if !rw.FileExists(geoPath) {
r.logger.Warn("geosite database not exists: ", geoPath)
var err error
Expand Down Expand Up @@ -142,10 +143,10 @@ func (r *Router) downloadGeoIPDatabase(savePath string) error {
}

if parentDir := filepath.Dir(savePath); parentDir != "" {
os.MkdirAll(parentDir, 0o755)
filemanager.MkdirAll(r.ctx, parentDir, 0o755)
}

saveFile, err := os.OpenFile(savePath, os.O_CREATE|os.O_WRONLY, 0o644)
saveFile, err := filemanager.Create(r.ctx, savePath)
if err != nil {
return E.Cause(err, "open output file: ", downloadURL)
}
Expand Down Expand Up @@ -190,10 +191,10 @@ func (r *Router) downloadGeositeDatabase(savePath string) error {
}

if parentDir := filepath.Dir(savePath); parentDir != "" {
os.MkdirAll(parentDir, 0o755)
filemanager.MkdirAll(r.ctx, parentDir, 0o755)
}

saveFile, err := os.OpenFile(savePath, os.O_CREATE|os.O_WRONLY, 0o644)
saveFile, err := filemanager.Create(r.ctx, savePath)
if err != nil {
return E.Cause(err, "open output file: ", downloadURL)
}
Expand Down

0 comments on commit 98c2c43

Please sign in to comment.