proxy_android.go 818 B

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