This repository was archived by the owner on Jun 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy_darwin.go
More file actions
159 lines (128 loc) · 3.61 KB
/
Copy pathproxy_darwin.go
File metadata and controls
159 lines (128 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package sysproxy
/*
#cgo CFLAGS: -mmacosx-version-min=10.10
#cgo LDFLAGS: -framework CoreFoundation -framework SystemConfiguration
#include <CoreFoundation/CoreFoundation.h>
#include <SystemConfiguration/SystemConfiguration.h>
#include <stdlib.h>
typedef struct {
int enabled;
char host[256];
int port;
} ProxyInfo;
ProxyInfo getHTTP(CFDictionaryRef settings) {
ProxyInfo info = {0};
if (!settings) return info;
CFNumberRef enabledVal = CFDictionaryGetValue(settings, kSCPropNetProxiesHTTPEnable);
if (enabledVal && CFGetTypeID(enabledVal) == CFNumberGetTypeID()) {
CFNumberGetValue(enabledVal, kCFNumberIntType, &info.enabled);
}
if (info.enabled) {
CFStringRef hostVal = CFDictionaryGetValue(settings, kSCPropNetProxiesHTTPProxy);
CFNumberRef portVal = CFDictionaryGetValue(settings, kSCPropNetProxiesHTTPPort);
if (hostVal && CFGetTypeID(hostVal) == CFStringGetTypeID()) {
CFStringGetCString(hostVal, info.host, sizeof(info.host), kCFStringEncodingUTF8);
}
if (portVal && CFGetTypeID(portVal) == CFNumberGetTypeID()) {
CFNumberGetValue(portVal, kCFNumberIntType, &info.port);
}
}
return info;
}
ProxyInfo getHTTPS(CFDictionaryRef settings) {
ProxyInfo info = {0};
if (!settings) return info;
CFNumberRef enabledVal = CFDictionaryGetValue(settings, kSCPropNetProxiesHTTPSEnable);
if (enabledVal && CFGetTypeID(enabledVal) == CFNumberGetTypeID()) {
CFNumberGetValue(enabledVal, kCFNumberIntType, &info.enabled);
}
if (info.enabled) {
CFStringRef hostVal = CFDictionaryGetValue(settings, kSCPropNetProxiesHTTPSProxy);
CFNumberRef portVal = CFDictionaryGetValue(settings, kSCPropNetProxiesHTTPSPort);
if (hostVal && CFGetTypeID(hostVal) == CFStringGetTypeID()) {
CFStringGetCString(hostVal, info.host, sizeof(info.host), kCFStringEncodingUTF8);
}
if (portVal && CFGetTypeID(portVal) == CFNumberGetTypeID()) {
CFNumberGetValue(portVal, kCFNumberIntType, &info.port);
}
}
return info;
}
*/
import "C"
import (
"fmt"
"io"
"unsafe"
)
type setting struct {
ref C.CFDictionaryRef
io.Closer
}
func (s *setting) Close() error {
if unsafe.Pointer(s.ref) != nil {
C.CFRelease(C.CFTypeRef(s.ref))
}
return nil
}
func createSetting() (*setting, error) {
setRef := C.SCDynamicStoreCopyProxies(C.SCDynamicStoreRef(unsafe.Pointer(nil)))
if unsafe.Pointer(setRef) == nil {
return nil, fmt.Errorf("failed to get system proxy settings")
}
return &setting{
ref: C.CFDictionaryRef(setRef),
}, nil
}
func GetAll() (*Info, *Info, error) {
s, err := createSetting()
if err != nil {
return nil, nil, err
}
defer s.Close()
httpProxy, err := getHTTP(s.ref)
if err != nil {
return nil, nil, err
}
httpsProxy, err := getHTTPS(s.ref)
if err != nil {
return nil, nil, err
}
return httpProxy, httpsProxy, nil
}
func getHTTP(settings C.CFDictionaryRef) (*Info, error) {
raw := C.getHTTP(settings)
if raw.enabled == 0 {
return nil, nil
}
return &Info{
Host: C.GoString(&raw.host[0]),
Port: uint16(raw.port),
}, nil
}
func GetHTTP() (*Info, error) {
s, err := createSetting()
if err != nil {
return nil, err
}
defer s.Close()
return getHTTP(s.ref)
}
func getHTTPS(settings C.CFDictionaryRef) (*Info, error) {
raw := C.getHTTPS(settings)
if raw.enabled == 0 {
return nil, nil
}
return &Info{
Host: C.GoString(&raw.host[0]),
Port: uint16(raw.port),
}, nil
}
func GetHTTPS() (*Info, error) {
s, err := createSetting()
if err != nil {
return nil, err
}
defer s.Close()
return getHTTPS(s.ref)
}