github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/sysproxy/sysproxy_windows.go (about) 1 package sysproxy 2 3 import ( 4 _ "embed" 5 "fmt" 6 "net" 7 8 "github.com/Asutorufa/yuhaiin/pkg/log" 9 "github.com/Asutorufa/yuhaiin/pkg/utils/memmod" 10 ) 11 12 // use github.com/Asutorufa/winproxy/c to generate dll 13 /* 14 gcc -c proxy.c -o proxy.o 15 gcc proxy.o -o proxy.dll -shared -lwininet 16 ar cr libproxy.a proxy.o 17 */ 18 19 //go:embed proxy.dll 20 var proxyDLL []byte 21 22 func SetSysProxy(hh, hp, _, _ string) { 23 if hh == "" && hp == "" { 24 return 25 } 26 27 if err := setSysProxy(net.JoinHostPort(hh, hp), ""); err != nil { 28 log.Error("set system proxy failed:", "err", err) 29 } 30 } 31 32 var ( 33 dll = memmod.NewLazyDLL("proxy.dll", proxyDLL) 34 switchSystemProxy = dll.NewProc("switch_system_proxy") 35 setSystemProxy = dll.NewProc("set_system_proxy_server") 36 setSystemBypass = dll.NewProc("set_system_proxy_bypass_list") 37 ) 38 39 func setSysProxy(http, _ string) error { 40 if http == "" { 41 return nil 42 } 43 44 r1, _, errno := switchSystemProxy.Call(1) 45 log.Debug("switch_system_proxy:", "r1", r1, "err", errno) 46 47 host, err := memmod.StrPtr(http) 48 if err != nil { 49 return fmt.Errorf("can't convert host: %w", err) 50 } 51 r1, _, err = setSystemProxy.Call(host) 52 log.Debug("set_system_proxy_server:", "r1", r1, "err", err) 53 54 bypass, err := memmod.StrPtr("localhost;127.*;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;172.31.*;172.32.*;192.168.*") 55 if err != nil { 56 return fmt.Errorf("can't convert bypasslist to ptr: %w", err) 57 } 58 r1, _, err = setSystemBypass.Call(bypass) 59 log.Debug("set_system_proxy_bypass_list:", "r1", r1, "err", err) 60 61 return nil 62 } 63 64 func UnsetSysProxy() { 65 if err := unsetSysProxy(); err != nil { 66 log.Error("unset wystem proxy failed:", "err", err) 67 } 68 } 69 70 func unsetSysProxy() error { 71 r1, _, err := switchSystemProxy.Call(0) 72 log.Debug("switch_system_proxy:", "r1", r1, "err", err) 73 return nil 74 } 75 76 /* 77 * check error from https://github.com/golang/sys/blob/master/windows/zsyscall_windows.go#L1073 78 */