Skip to content

Commit

Permalink
feat: detect ie config pac url for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
5aaee9 committed Jun 1, 2024
1 parent 377faef commit 80fb842
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
18 changes: 18 additions & 0 deletions pkgs/platform/windows/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package windows

import (
"unicode/utf16"
"unsafe"
)

func GoWString(s *uint16) string {
if s == nil {
return ""
}
p := (*[1<<30 - 1]uint16)(unsafe.Pointer(s))
sz := 0
for p[sz] != 0 {
sz++
}
return string(utf16.Decode(p[:sz:sz]))
}
2 changes: 2 additions & 0 deletions pkgs/proxy/sysproxy/sysproxy_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build windows || darwin

package sysproxy

import (
Expand Down
31 changes: 27 additions & 4 deletions pkgs/proxy/sysproxy/sysproxy_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"syscall"
"unsafe"

pWin "github.com/5aaee9/utils/pkgs/platform/windows"
"golang.org/x/sys/windows"
)

Expand All @@ -17,9 +18,11 @@ func NewSystemProxy() SystemProxy {
var _ SystemProxy = (*WindowsSystemProxy)(nil)

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

const (
Expand Down Expand Up @@ -71,6 +74,13 @@ type internetProxyInfo struct {
lpszProxyBypass *uint16
}

type winHttpCurrentUserIEProxyConfig struct {
fAutoDetect bool
lpszAutoConfigUrl *uint16
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 @@ -146,11 +156,24 @@ func (p *WindowsSystemProxy) Status() (*SystemProxyStatus, error) {
return nil, err
}

ieConfig := winHttpCurrentUserIEProxyConfig{}
r0, _, err = syscall.SyscallN(procWinHttpGetIEProxyConfigForCurrentUser.Addr(), uintptr(unsafe.Pointer(&ieConfig)))

if r0 != 1 {
return nil, err
}

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

res := &SystemProxyStatus{}

res.State = proxyInfo.dwAccessType != 1
if proxyInfo.dwAccessType != 1 {
res.State = true
}

if len(pWin.GoWString(ieConfig.lpszAutoConfigUrl)) > 0 {
res.State = true
}

return res, nil
}

0 comments on commit 80fb842

Please sign in to comment.