github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/common/settings/proxy_windows.go (about)

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