proxy_android.go 1001 B

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