Skip to content

Commit

Permalink
sysproxy: add probe for state
Browse files Browse the repository at this point in the history
  • Loading branch information
5aaee9 committed Apr 16, 2024
1 parent f9d5a46 commit e812fbd
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pkgs/proxy/sysproxy/sysproxy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package sysproxy

type SystemProxyStatus struct {
State bool `json:"state"`
}

type SystemProxy interface {
TurnOn(addrPort string) error
TurnOff() error

Status() (*SystemProxyStatus, error)
}
4 changes: 4 additions & 0 deletions pkgs/proxy/sysproxy/sysproxy_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ func (p *DarwinSystemProxy) TurnOff() error {
return nil
}

func (p *DarwinSystemProxy) Status() (*SystemProxyStatus, error) {
return nil, nil
}

func (p *DarwinSystemProxy) TurnOn(addrport string) error {
host, port, err := net.SplitHostPort(addrport)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkgs/proxy/sysproxy/sysproxy_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ func (p *OtherSystemProxy) TurnOn(addrport string) error {
func NewSystemProxy() SystemProxy {
return &OtherSystemProxy{}
}

func (p *OtherSystemProxy) Status() (*SystemProxyStatus, error) {
return nil, errors.New("not implemented")
}
15 changes: 15 additions & 0 deletions pkgs/proxy/sysproxy/sysproxy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package sysproxy

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_SystemProxyStatus(t *testing.T) {
proxy := NewSystemProxy()

status, err := proxy.Status()
assert.NoError(t, err)
assert.NotNil(t, status)
}
32 changes: 30 additions & 2 deletions pkgs/proxy/sysproxy/sysproxy_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ func NewSystemProxy() SystemProxy {
var _ SystemProxy = (*WindowsSystemProxy)(nil)

var (
modwininet = windows.NewLazySystemDLL("wininet.dll")
procInternetSetOptionW = modwininet.NewProc("InternetSetOptionW")
modwininet = windows.NewLazySystemDLL("wininet.dll")
procInternetSetOptionW = modwininet.NewProc("InternetSetOptionW")
procInternetQueryOptionA = modwininet.NewProc("InternetQueryOptionA")
)

const (
internetOptionPerConnectionOption = 75
internetOptionSettingsChanged = 39
internetOptionRefresh = 37
internetOptionProxySettingsChanged = 95
internetOptionProxy = 38
)

const (
Expand Down Expand Up @@ -63,6 +65,12 @@ type internetPerConnOption struct {
value uint64
}

type internetProxyInfo struct {
dwAccessType uint32
lpszProxy *uint16
lpszProxyBypass *uint16
}

func internetSetOption(option uintptr, lpBuffer uintptr, dwBufferSize uintptr) error {
r0, _, err := syscall.SyscallN(procInternetSetOptionW.Addr(), 0, option, lpBuffer, dwBufferSize)
if r0 != 1 {
Expand Down Expand Up @@ -126,3 +134,23 @@ func (p *WindowsSystemProxy) TurnOn(addrport string) error {
func (p *WindowsSystemProxy) TurnOff() error {
return ClearSystemProxy()
}

func (p *WindowsSystemProxy) Status() (*SystemProxyStatus, error) {
var bufferLength uint32 = 1024 * 10
buffer := make([]byte, bufferLength)

r0, _, err := syscall.SyscallN(procInternetQueryOptionA.Addr(), 0, internetOptionProxy,
uintptr(unsafe.Pointer(&buffer[0])), uintptr(unsafe.Pointer(&bufferLength)), 0, 0)

if r0 != 1 {
return nil, err
}

proxyInfo := (*internetProxyInfo)(unsafe.Pointer(&buffer[0]))

res := &SystemProxyStatus{}

res.State = proxyInfo.dwAccessType != 1

return res, nil
}

0 comments on commit e812fbd

Please sign in to comment.