github.com/sagernet/sing-box@v1.2.7/common/settings/proxy_android.go (about)

     1  package settings
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  
     7  	"github.com/sagernet/sing-box/adapter"
     8  	C "github.com/sagernet/sing-box/constant"
     9  	F "github.com/sagernet/sing/common/format"
    10  	"github.com/sagernet/sing/common/shell"
    11  )
    12  
    13  var (
    14  	useRish  bool
    15  	rishPath string
    16  )
    17  
    18  func init() {
    19  	userId := os.Getuid()
    20  	if userId == 0 || userId == 1000 || userId == 2000 {
    21  		useRish = false
    22  	} else {
    23  		rishPath, useRish = C.FindPath("rish")
    24  	}
    25  }
    26  
    27  func runAndroidShell(name string, args ...string) error {
    28  	if !useRish {
    29  		return shell.Exec(name, args...).Attach().Run()
    30  	} else {
    31  		return shell.Exec("sh", rishPath, "-c", F.ToString(name, " ", strings.Join(args, " "))).Attach().Run()
    32  	}
    33  }
    34  
    35  func SetSystemProxy(router adapter.Router, port uint16, isMixed bool) (func() error, error) {
    36  	err := runAndroidShell("settings", "put", "global", "http_proxy", F.ToString("127.0.0.1:", port))
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	return func() error {
    41  		return runAndroidShell("settings", "put", "global", "http_proxy", ":0")
    42  	}, nil
    43  }