2022-10-25 04:55:00 +00:00
|
|
|
package libbox
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/netip"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-box"
|
2023-04-18 06:04:09 +00:00
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2022-10-25 04:55:00 +00:00
|
|
|
"github.com/sagernet/sing-box/common/process"
|
2023-08-01 08:40:11 +00:00
|
|
|
"github.com/sagernet/sing-box/common/sleep"
|
2023-07-02 08:45:30 +00:00
|
|
|
"github.com/sagernet/sing-box/common/urltest"
|
2022-10-25 04:55:00 +00:00
|
|
|
"github.com/sagernet/sing-box/experimental/libbox/internal/procfs"
|
|
|
|
"github.com/sagernet/sing-box/experimental/libbox/platform"
|
2023-02-28 11:02:27 +00:00
|
|
|
"github.com/sagernet/sing-box/option"
|
2022-10-25 04:55:00 +00:00
|
|
|
"github.com/sagernet/sing-tun"
|
2023-04-18 06:04:09 +00:00
|
|
|
"github.com/sagernet/sing/common"
|
2022-10-25 04:55:00 +00:00
|
|
|
"github.com/sagernet/sing/common/control"
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2023-07-23 06:42:19 +00:00
|
|
|
"github.com/sagernet/sing/common/logger"
|
2022-10-25 04:55:00 +00:00
|
|
|
N "github.com/sagernet/sing/common/network"
|
2023-07-02 08:45:30 +00:00
|
|
|
"github.com/sagernet/sing/service"
|
2023-04-21 09:29:00 +00:00
|
|
|
"github.com/sagernet/sing/service/filemanager"
|
2022-10-25 04:55:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type BoxService struct {
|
2023-08-01 08:40:11 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
instance *box.Box
|
|
|
|
sleepManager *sleep.Manager
|
2022-10-25 04:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewService(configContent string, platformInterface PlatformInterface) (*BoxService, error) {
|
|
|
|
options, err := parseConfig(configContent)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2023-07-29 00:37:10 +00:00
|
|
|
ctx = filemanager.WithDefault(ctx, sWorkingPath, sTempPath, sUserID, sGroupID)
|
2023-07-02 08:45:30 +00:00
|
|
|
ctx = service.ContextWithPtr(ctx, urltest.NewHistoryStorage())
|
2023-08-01 08:40:11 +00:00
|
|
|
sleepManager := sleep.NewManager()
|
|
|
|
ctx = service.ContextWithPtr(ctx, sleepManager)
|
2023-04-03 10:24:20 +00:00
|
|
|
instance, err := box.New(box.Options{
|
|
|
|
Context: ctx,
|
|
|
|
Options: options,
|
2023-04-18 06:04:09 +00:00
|
|
|
PlatformInterface: &platformInterfaceWrapper{iif: platformInterface, useProcFS: platformInterface.UseProcFS()},
|
2023-04-03 10:24:20 +00:00
|
|
|
})
|
2022-10-25 04:55:00 +00:00
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
return nil, E.Cause(err, "create service")
|
|
|
|
}
|
|
|
|
return &BoxService{
|
2023-08-01 08:40:11 +00:00
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
instance: instance,
|
|
|
|
sleepManager: sleepManager,
|
2022-10-25 04:55:00 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BoxService) Start() error {
|
|
|
|
return s.instance.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BoxService) Close() error {
|
|
|
|
s.cancel()
|
|
|
|
return s.instance.Close()
|
|
|
|
}
|
|
|
|
|
2023-08-01 08:40:11 +00:00
|
|
|
func (s *BoxService) Sleep() {
|
|
|
|
s.sleepManager.Sleep()
|
|
|
|
_ = s.instance.Router().ResetNetwork()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BoxService) Wake() {
|
|
|
|
s.sleepManager.Wake()
|
|
|
|
}
|
|
|
|
|
2022-10-25 04:55:00 +00:00
|
|
|
var _ platform.Interface = (*platformInterfaceWrapper)(nil)
|
|
|
|
|
|
|
|
type platformInterfaceWrapper struct {
|
|
|
|
iif PlatformInterface
|
|
|
|
useProcFS bool
|
2023-04-18 06:04:09 +00:00
|
|
|
router adapter.Router
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *platformInterfaceWrapper) Initialize(ctx context.Context, router adapter.Router) error {
|
|
|
|
w.router = router
|
|
|
|
return nil
|
2022-10-25 04:55:00 +00:00
|
|
|
}
|
|
|
|
|
2023-04-21 09:04:55 +00:00
|
|
|
func (w *platformInterfaceWrapper) UsePlatformAutoDetectInterfaceControl() bool {
|
|
|
|
return w.iif.UsePlatformAutoDetectInterfaceControl()
|
|
|
|
}
|
|
|
|
|
2022-10-25 04:55:00 +00:00
|
|
|
func (w *platformInterfaceWrapper) AutoDetectInterfaceControl() control.Func {
|
|
|
|
return func(network, address string, conn syscall.RawConn) error {
|
|
|
|
return control.Raw(conn, func(fd uintptr) error {
|
|
|
|
return w.iif.AutoDetectInterfaceControl(int32(fd))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-17 11:05:02 +00:00
|
|
|
func (w *platformInterfaceWrapper) OpenTun(options *tun.Options, platformOptions option.TunPlatformOptions) (tun.Tun, error) {
|
2022-10-25 04:55:00 +00:00
|
|
|
if len(options.IncludeUID) > 0 || len(options.ExcludeUID) > 0 {
|
|
|
|
return nil, E.New("android: unsupported uid options")
|
|
|
|
}
|
|
|
|
if len(options.IncludeAndroidUser) > 0 {
|
|
|
|
return nil, E.New("android: unsupported android_user option")
|
|
|
|
}
|
2023-02-28 11:02:27 +00:00
|
|
|
tunFd, err := w.iif.OpenTun(&tunOptions{options, platformOptions})
|
2022-10-25 04:55:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-17 11:05:02 +00:00
|
|
|
options.Name, err = getTunnelName(tunFd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "query tun name")
|
|
|
|
}
|
2023-04-07 13:10:16 +00:00
|
|
|
dupFd, err := dup(int(tunFd))
|
2023-03-13 11:47:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "dup tun file descriptor")
|
|
|
|
}
|
|
|
|
options.FileDescriptor = dupFd
|
2023-04-17 11:05:02 +00:00
|
|
|
return tun.New(*options)
|
2022-10-25 04:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *platformInterfaceWrapper) Write(p []byte) (n int, err error) {
|
|
|
|
w.iif.WriteLog(string(p))
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *platformInterfaceWrapper) FindProcessInfo(ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*process.Info, error) {
|
|
|
|
var uid int32
|
|
|
|
if w.useProcFS {
|
|
|
|
uid = procfs.ResolveSocketByProcSearch(network, source, destination)
|
|
|
|
if uid == -1 {
|
|
|
|
return nil, E.New("procfs: not found")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var ipProtocol int32
|
|
|
|
switch N.NetworkName(network) {
|
|
|
|
case N.NetworkTCP:
|
|
|
|
ipProtocol = syscall.IPPROTO_TCP
|
|
|
|
case N.NetworkUDP:
|
|
|
|
ipProtocol = syscall.IPPROTO_UDP
|
|
|
|
default:
|
|
|
|
return nil, E.New("unknown network: ", network)
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
uid, err = w.iif.FindConnectionOwner(ipProtocol, source.Addr().String(), int32(source.Port()), destination.Addr().String(), int32(destination.Port()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
packageName, _ := w.iif.PackageNameByUid(uid)
|
|
|
|
return &process.Info{UserId: uid, PackageName: packageName}, nil
|
|
|
|
}
|
2023-04-18 06:04:09 +00:00
|
|
|
|
|
|
|
func (w *platformInterfaceWrapper) UsePlatformDefaultInterfaceMonitor() bool {
|
|
|
|
return w.iif.UsePlatformDefaultInterfaceMonitor()
|
|
|
|
}
|
|
|
|
|
2023-07-23 06:42:19 +00:00
|
|
|
func (w *platformInterfaceWrapper) CreateDefaultInterfaceMonitor(logger logger.Logger) tun.DefaultInterfaceMonitor {
|
2023-04-18 06:04:09 +00:00
|
|
|
return &platformDefaultInterfaceMonitor{
|
|
|
|
platformInterfaceWrapper: w,
|
|
|
|
defaultInterfaceIndex: -1,
|
2023-07-23 06:42:19 +00:00
|
|
|
logger: logger,
|
2023-04-18 06:04:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *platformInterfaceWrapper) UsePlatformInterfaceGetter() bool {
|
|
|
|
return w.iif.UsePlatformInterfaceGetter()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *platformInterfaceWrapper) Interfaces() ([]platform.NetworkInterface, error) {
|
|
|
|
interfaceIterator, err := w.iif.GetInterfaces()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var interfaces []platform.NetworkInterface
|
|
|
|
for _, netInterface := range iteratorToArray[*NetworkInterface](interfaceIterator) {
|
|
|
|
interfaces = append(interfaces, platform.NetworkInterface{
|
|
|
|
Index: int(netInterface.Index),
|
|
|
|
MTU: int(netInterface.MTU),
|
|
|
|
Name: netInterface.Name,
|
|
|
|
Addresses: common.Map(iteratorToArray[string](netInterface.Addresses), netip.MustParsePrefix),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return interfaces, nil
|
|
|
|
}
|
2023-06-07 13:01:29 +00:00
|
|
|
|
|
|
|
func (w *platformInterfaceWrapper) UnderNetworkExtension() bool {
|
|
|
|
return w.iif.UnderNetworkExtension()
|
|
|
|
}
|