bootstrap
This commit is contained in:
79
custom/custom.go
Normal file
79
custom/custom.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package main
|
||||
|
||||
/*
|
||||
#include "stdint.h"
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"github.com/hiddify/libcore/shared"
|
||||
B "github.com/sagernet/sing-box/experimental/libbox"
|
||||
)
|
||||
|
||||
var box *BoxService
|
||||
|
||||
var templateOptions = shared.ConfigTemplateOptions{IncludeTunInbound: false, IncludeMixedInbound: true, IncludeLogOutput: true}
|
||||
|
||||
//export setup
|
||||
func setup(baseDir *C.char, workingDir *C.char, tempDir *C.char) {
|
||||
Setup(C.GoString(baseDir), C.GoString(workingDir), C.GoString(tempDir))
|
||||
}
|
||||
|
||||
//export checkConfig
|
||||
func checkConfig(configPath *C.char) *C.char {
|
||||
configContent, err := shared.ConvertToSingbox(C.GoString(configPath), templateOptions)
|
||||
if err != nil {
|
||||
return C.CString(err.Error())
|
||||
}
|
||||
|
||||
err = B.CheckConfig(string(configContent))
|
||||
if err != nil {
|
||||
return C.CString(err.Error())
|
||||
}
|
||||
return C.CString("")
|
||||
}
|
||||
|
||||
//export create
|
||||
func create(configPath *C.char) *C.char {
|
||||
path := C.GoString(configPath)
|
||||
|
||||
configContent, err := shared.ConvertToSingbox(path, templateOptions)
|
||||
if err != nil {
|
||||
return C.CString(err.Error())
|
||||
}
|
||||
|
||||
instance, err := NewService(string(configContent))
|
||||
if err != nil {
|
||||
return C.CString(err.Error())
|
||||
}
|
||||
box = instance
|
||||
|
||||
return C.CString("")
|
||||
}
|
||||
|
||||
//export start
|
||||
func start() *C.char {
|
||||
if box == nil {
|
||||
return C.CString("instance not found")
|
||||
}
|
||||
err := box.Start()
|
||||
if err != nil {
|
||||
return C.CString(err.Error())
|
||||
}
|
||||
|
||||
return C.CString("")
|
||||
}
|
||||
|
||||
//export stop
|
||||
func stop() *C.char {
|
||||
if box == nil {
|
||||
return C.CString("instance not found")
|
||||
}
|
||||
|
||||
err := box.Close()
|
||||
if err != nil {
|
||||
return C.CString(err.Error())
|
||||
}
|
||||
return C.CString("")
|
||||
}
|
||||
|
||||
func main() {}
|
||||
76
custom/service.go
Normal file
76
custom/service.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
B "github.com/sagernet/sing-box"
|
||||
"github.com/sagernet/sing-box/common/urltest"
|
||||
"github.com/sagernet/sing-box/option"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
"github.com/sagernet/sing/service"
|
||||
"github.com/sagernet/sing/service/filemanager"
|
||||
)
|
||||
|
||||
var (
|
||||
sBasePath string
|
||||
sWorkingPath string
|
||||
sTempPath string
|
||||
sUserID int
|
||||
sGroupID int
|
||||
)
|
||||
|
||||
type BoxService struct {
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
instance *B.Box
|
||||
}
|
||||
|
||||
func Setup(basePath string, workingPath string, tempPath string) {
|
||||
sBasePath = basePath
|
||||
sWorkingPath = workingPath
|
||||
sTempPath = tempPath
|
||||
sUserID = os.Getuid()
|
||||
sGroupID = os.Getgid()
|
||||
}
|
||||
|
||||
func NewService(configContent string) (*BoxService, error) {
|
||||
options, err := parseConfig(configContent)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
ctx = filemanager.WithDefault(ctx, sWorkingPath, sTempPath, sUserID, sGroupID)
|
||||
ctx = service.ContextWithPtr(ctx, urltest.NewHistoryStorage())
|
||||
instance, err := B.New(B.Options{
|
||||
Context: ctx,
|
||||
Options: options,
|
||||
})
|
||||
if err != nil {
|
||||
cancel()
|
||||
return nil, E.Cause(err, "create service")
|
||||
}
|
||||
return &BoxService{
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
instance: instance,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *BoxService) Start() error {
|
||||
return s.instance.Start()
|
||||
}
|
||||
|
||||
func (s *BoxService) Close() error {
|
||||
s.cancel()
|
||||
return s.instance.Close()
|
||||
}
|
||||
|
||||
func parseConfig(configContent string) (option.Options, error) {
|
||||
var options option.Options
|
||||
err := options.UnmarshalJSON([]byte(configContent))
|
||||
if err != nil {
|
||||
return option.Options{}, E.Cause(err, "decode config")
|
||||
}
|
||||
return options, nil
|
||||
}
|
||||
Reference in New Issue
Block a user