proxy_windows.go 847 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package settings
  2. import (
  3. "context"
  4. M "github.com/sagernet/sing/common/metadata"
  5. "github.com/sagernet/sing/common/wininet"
  6. )
  7. type WindowsSystemProxy struct {
  8. serverAddr M.Socksaddr
  9. supportSOCKS bool
  10. isEnabled bool
  11. }
  12. func NewSystemProxy(ctx context.Context, serverAddr M.Socksaddr, supportSOCKS bool) (*WindowsSystemProxy, error) {
  13. return &WindowsSystemProxy{
  14. serverAddr: serverAddr,
  15. supportSOCKS: supportSOCKS,
  16. }, nil
  17. }
  18. func (p *WindowsSystemProxy) IsEnabled() bool {
  19. return p.isEnabled
  20. }
  21. func (p *WindowsSystemProxy) Enable() error {
  22. err := wininet.SetSystemProxy("http://"+p.serverAddr.String(), "")
  23. if err != nil {
  24. return err
  25. }
  26. p.isEnabled = true
  27. return nil
  28. }
  29. func (p *WindowsSystemProxy) Disable() error {
  30. err := wininet.ClearSystemProxy()
  31. if err != nil {
  32. return err
  33. }
  34. p.isEnabled = false
  35. return nil
  36. }