mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-12-01 14:18:36 +00:00
Add netns and unshare support
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/sagernet/sing-box/common/netns"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var commandNetnsHolder = &cobra.Command{
|
||||
Use: "netns-holder",
|
||||
Args: cobra.NoArgs,
|
||||
Hidden: true,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
netns.Hold()
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
mainCommand.AddCommand(commandNetnsHolder)
|
||||
}
|
||||
+31
-11
@@ -104,10 +104,17 @@ func readConfigAndMerge() (option.Options, error) {
|
||||
if err != nil {
|
||||
return option.Options{}, err
|
||||
}
|
||||
return mergeOptionsList(optionsList)
|
||||
}
|
||||
|
||||
func mergeOptionsList(optionsList []*OptionsEntry) (option.Options, error) {
|
||||
if len(optionsList) == 1 {
|
||||
return optionsList[0].options, nil
|
||||
}
|
||||
var mergedMessage json.RawMessage
|
||||
var (
|
||||
mergedMessage json.RawMessage
|
||||
err error
|
||||
)
|
||||
for _, options := range optionsList {
|
||||
mergedMessage, err = badjson.MergeJSON(globalCtx, options.options.RawMessage, mergedMessage, false)
|
||||
if err != nil {
|
||||
@@ -122,11 +129,7 @@ func readConfigAndMerge() (option.Options, error) {
|
||||
return mergedOptions, nil
|
||||
}
|
||||
|
||||
func create() (*box.Box, context.CancelFunc, error) {
|
||||
options, err := readConfigAndMerge()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
func create(options option.Options) (*box.Box, context.CancelFunc, error) {
|
||||
if disableColor {
|
||||
if options.Log == nil {
|
||||
options.Log = &option.LogOptions{}
|
||||
@@ -135,8 +138,9 @@ func create() (*box.Box, context.CancelFunc, error) {
|
||||
}
|
||||
ctx, cancel := context.WithCancel(globalCtx)
|
||||
instance, err := box.New(box.Options{
|
||||
Context: ctx,
|
||||
Options: options,
|
||||
Context: ctx,
|
||||
Options: options,
|
||||
NetworkNamespaceHolderArgs: []string{"/proc/self/exe", commandNetnsHolder.Use},
|
||||
})
|
||||
if err != nil {
|
||||
cancel()
|
||||
@@ -167,13 +171,25 @@ func create() (*box.Box, context.CancelFunc, error) {
|
||||
}
|
||||
|
||||
func run() error {
|
||||
optionsList, err := readConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
options, err := mergeOptionsList(optionsList)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = runInUserNamespaceIfNeeded(options, optionsList)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
osSignals := make(chan os.Signal, 1)
|
||||
signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
|
||||
defer signal.Stop(osSignals)
|
||||
for {
|
||||
instance, cancel, err := create()
|
||||
if err != nil {
|
||||
return err
|
||||
instance, cancel, createErr := create(options)
|
||||
if createErr != nil {
|
||||
return createErr
|
||||
}
|
||||
runtimeDebug.FreeOSMemory()
|
||||
for {
|
||||
@@ -198,6 +214,10 @@ func run() error {
|
||||
}
|
||||
break
|
||||
}
|
||||
options, err = readConfigAndMerge()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
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"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func runInUserNamespaceIfNeeded(options option.Options, optionsList []*OptionsEntry) error {
|
||||
if !common.Any(options.NetworkNamespaces, func(namespace option.NetworkNamespace) bool {
|
||||
return namespace.Type == C.NetNsTypeUnshare
|
||||
}) {
|
||||
return nil
|
||||
}
|
||||
var header unix.CapUserHeader
|
||||
header.Version = unix.LINUX_CAPABILITY_VERSION_3
|
||||
var data [2]unix.CapUserData
|
||||
err := unix.Capget(&header, &data[0])
|
||||
if err != nil {
|
||||
return E.Cause(err, "get capabilities")
|
||||
}
|
||||
if data[0].Effective&(1<<unix.CAP_SYS_ADMIN) != 0 {
|
||||
return nil
|
||||
}
|
||||
command := exec.Command("/proc/self/exe", os.Args[1:]...)
|
||||
command.Args = os.Args
|
||||
command.Stdin = os.Stdin
|
||||
for _, entry := range optionsList {
|
||||
if entry.path == "stdin" {
|
||||
command.Stdin = bytes.NewReader(entry.content)
|
||||
}
|
||||
}
|
||||
command.Stdout = os.Stdout
|
||||
command.Stderr = os.Stderr
|
||||
command.SysProcAttr = &syscall.SysProcAttr{
|
||||
Cloneflags: syscall.CLONE_NEWUSER,
|
||||
UidMappings: []syscall.SysProcIDMap{
|
||||
{ContainerID: os.Geteuid(), HostID: os.Geteuid(), Size: 1},
|
||||
},
|
||||
GidMappings: []syscall.SysProcIDMap{
|
||||
{ContainerID: os.Getegid(), HostID: os.Getegid(), Size: 1},
|
||||
},
|
||||
GidMappingsEnableSetgroups: false,
|
||||
AmbientCaps: []uintptr{unix.CAP_SYS_ADMIN, unix.CAP_NET_ADMIN, unix.CAP_NET_RAW},
|
||||
Setpgid: true,
|
||||
Pdeathsig: syscall.SIGKILL,
|
||||
}
|
||||
err = command.Start()
|
||||
if err != nil {
|
||||
return E.Cause(err, "create user namespace for unshare network namespace (is unprivileged user namespace creation allowed by the kernel?)")
|
||||
}
|
||||
signalChannel := make(chan os.Signal, 4)
|
||||
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
|
||||
go func() {
|
||||
for receivedSignal := range signalChannel {
|
||||
command.Process.Signal(receivedSignal)
|
||||
}
|
||||
}()
|
||||
err = command.Wait()
|
||||
exitError, isExitError := E.Cast[*exec.ExitError](err)
|
||||
if isExitError {
|
||||
os.Exit(exitError.ExitCode())
|
||||
}
|
||||
if err != nil {
|
||||
return E.Cause(err, "wait user namespace child")
|
||||
}
|
||||
os.Exit(0)
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
//go:build !linux
|
||||
|
||||
package main
|
||||
|
||||
import "github.com/sagernet/sing-box/option"
|
||||
|
||||
func runInUserNamespaceIfNeeded(options option.Options, optionsList []*OptionsEntry) error {
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user