Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Helper method for urllib to fetch the proxy configuration settings |
| 3 | * using the SystemConfiguration framework. |
| 4 | */ |
| 5 | #include <Python.h> |
| 6 | #include <SystemConfiguration/SystemConfiguration.h> |
| 7 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 8 | static int32_t |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 9 | cfnum_to_int32(CFNumberRef num) |
| 10 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 11 | int32_t result; |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 12 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 13 | CFNumberGetValue(num, kCFNumberSInt32Type, &result); |
| 14 | return result; |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 15 | } |
| 16 | |
| 17 | static PyObject* |
| 18 | cfstring_to_pystring(CFStringRef ref) |
| 19 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 20 | const char* s; |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 21 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 22 | s = CFStringGetCStringPtr(ref, kCFStringEncodingUTF8); |
| 23 | if (s) { |
| 24 | return PyUnicode_DecodeUTF8( |
| 25 | s, strlen(s), NULL); |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 26 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 27 | } else { |
| 28 | CFIndex len = CFStringGetLength(ref); |
| 29 | Boolean ok; |
| 30 | PyObject* result; |
| 31 | char* buf; |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 32 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 33 | buf = PyMem_Malloc(len*4); |
| 34 | if (buf == NULL) { |
| 35 | PyErr_NoMemory(); |
| 36 | return NULL; |
| 37 | } |
| 38 | |
| 39 | ok = CFStringGetCString(ref, |
| 40 | buf, len * 4, |
| 41 | kCFStringEncodingUTF8); |
| 42 | if (!ok) { |
| 43 | PyMem_Free(buf); |
| 44 | return NULL; |
| 45 | } else { |
| 46 | result = PyUnicode_DecodeUTF8( |
| 47 | buf, strlen(buf), NULL); |
| 48 | PyMem_Free(buf); |
| 49 | } |
| 50 | return result; |
| 51 | } |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | |
| 55 | static PyObject* |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 56 | get_proxy_settings(PyObject* Py_UNUSED(mod), PyObject *Py_UNUSED(ignored)) |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 57 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 58 | CFDictionaryRef proxyDict = NULL; |
| 59 | CFNumberRef aNum = NULL; |
| 60 | CFArrayRef anArray = NULL; |
| 61 | PyObject* result = NULL; |
| 62 | PyObject* v; |
| 63 | int r; |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 64 | |
Max Bélanger | 4859ba0 | 2018-09-11 16:14:00 -0700 | [diff] [blame] | 65 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 66 | proxyDict = SCDynamicStoreCopyProxies(NULL); |
Max Bélanger | 4859ba0 | 2018-09-11 16:14:00 -0700 | [diff] [blame] | 67 | Py_END_ALLOW_THREADS |
| 68 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 69 | if (!proxyDict) { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 70 | Py_RETURN_NONE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 71 | } |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 72 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 73 | result = PyDict_New(); |
| 74 | if (result == NULL) goto error; |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 75 | |
Brett Cannon | 56be5f5 | 2016-09-07 14:07:16 -0700 | [diff] [blame] | 76 | aNum = CFDictionaryGetValue(proxyDict, |
| 77 | kSCPropNetProxiesExcludeSimpleHostnames); |
| 78 | if (aNum == NULL) { |
Ronald Oussoren | 01c4289 | 2010-09-28 14:38:31 +0000 | [diff] [blame] | 79 | v = PyBool_FromLong(0); |
Brett Cannon | 56be5f5 | 2016-09-07 14:07:16 -0700 | [diff] [blame] | 80 | } else { |
| 81 | v = PyBool_FromLong(cfnum_to_int32(aNum)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 82 | } |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 83 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | if (v == NULL) goto error; |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 85 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 86 | r = PyDict_SetItemString(result, "exclude_simple", v); |
| 87 | Py_DECREF(v); v = NULL; |
| 88 | if (r == -1) goto error; |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 89 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 90 | anArray = CFDictionaryGetValue(proxyDict, |
| 91 | kSCPropNetProxiesExceptionsList); |
| 92 | if (anArray != NULL) { |
| 93 | CFIndex len = CFArrayGetCount(anArray); |
| 94 | CFIndex i; |
| 95 | v = PyTuple_New(len); |
| 96 | if (v == NULL) goto error; |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 97 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 98 | r = PyDict_SetItemString(result, "exceptions", v); |
| 99 | Py_DECREF(v); |
| 100 | if (r == -1) goto error; |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 101 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 102 | for (i = 0; i < len; i++) { |
| 103 | CFStringRef aString = NULL; |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 104 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 105 | aString = CFArrayGetValueAtIndex(anArray, i); |
| 106 | if (aString == NULL) { |
| 107 | PyTuple_SetItem(v, i, Py_None); |
| 108 | Py_INCREF(Py_None); |
| 109 | } else { |
| 110 | PyObject* t = cfstring_to_pystring(aString); |
| 111 | if (!t) { |
| 112 | PyTuple_SetItem(v, i, Py_None); |
| 113 | Py_INCREF(Py_None); |
| 114 | } else { |
| 115 | PyTuple_SetItem(v, i, t); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | CFRelease(proxyDict); |
| 122 | return result; |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 123 | |
| 124 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 125 | if (proxyDict) CFRelease(proxyDict); |
| 126 | Py_XDECREF(result); |
| 127 | return NULL; |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 131 | set_proxy(PyObject* proxies, const char* proto, CFDictionaryRef proxyDict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 132 | CFStringRef enabledKey, |
| 133 | CFStringRef hostKey, CFStringRef portKey) |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 134 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 135 | CFNumberRef aNum; |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 137 | aNum = CFDictionaryGetValue(proxyDict, enabledKey); |
| 138 | if (aNum && cfnum_to_int32(aNum)) { |
| 139 | CFStringRef hostString; |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 140 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 141 | hostString = CFDictionaryGetValue(proxyDict, hostKey); |
| 142 | aNum = CFDictionaryGetValue(proxyDict, portKey); |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 143 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 144 | if (hostString) { |
| 145 | int r; |
| 146 | PyObject* h = cfstring_to_pystring(hostString); |
| 147 | PyObject* v; |
| 148 | if (h) { |
| 149 | if (aNum) { |
| 150 | int32_t port = cfnum_to_int32(aNum); |
| 151 | v = PyUnicode_FromFormat("http://%U:%ld", |
| 152 | h, (long)port); |
| 153 | } else { |
| 154 | v = PyUnicode_FromFormat("http://%U", h); |
| 155 | } |
| 156 | Py_DECREF(h); |
| 157 | if (!v) return -1; |
| 158 | r = PyDict_SetItemString(proxies, proto, |
| 159 | v); |
| 160 | Py_DECREF(v); |
| 161 | return r; |
| 162 | } |
| 163 | } |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 164 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 165 | } |
| 166 | return 0; |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | |
| 170 | |
| 171 | static PyObject* |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 172 | get_proxies(PyObject* Py_UNUSED(mod), PyObject *Py_UNUSED(ignored)) |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 173 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 174 | PyObject* result = NULL; |
| 175 | int r; |
| 176 | CFDictionaryRef proxyDict = NULL; |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 177 | |
Max Bélanger | 4859ba0 | 2018-09-11 16:14:00 -0700 | [diff] [blame] | 178 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 179 | proxyDict = SCDynamicStoreCopyProxies(NULL); |
Max Bélanger | 4859ba0 | 2018-09-11 16:14:00 -0700 | [diff] [blame] | 180 | Py_END_ALLOW_THREADS |
| 181 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 182 | if (proxyDict == NULL) { |
| 183 | return PyDict_New(); |
| 184 | } |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 185 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 186 | result = PyDict_New(); |
| 187 | if (result == NULL) goto error; |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 188 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | r = set_proxy(result, "http", proxyDict, |
| 190 | kSCPropNetProxiesHTTPEnable, |
| 191 | kSCPropNetProxiesHTTPProxy, |
| 192 | kSCPropNetProxiesHTTPPort); |
| 193 | if (r == -1) goto error; |
| 194 | r = set_proxy(result, "https", proxyDict, |
| 195 | kSCPropNetProxiesHTTPSEnable, |
| 196 | kSCPropNetProxiesHTTPSProxy, |
| 197 | kSCPropNetProxiesHTTPSPort); |
| 198 | if (r == -1) goto error; |
| 199 | r = set_proxy(result, "ftp", proxyDict, |
| 200 | kSCPropNetProxiesFTPEnable, |
| 201 | kSCPropNetProxiesFTPProxy, |
| 202 | kSCPropNetProxiesFTPPort); |
| 203 | if (r == -1) goto error; |
| 204 | r = set_proxy(result, "gopher", proxyDict, |
| 205 | kSCPropNetProxiesGopherEnable, |
| 206 | kSCPropNetProxiesGopherProxy, |
| 207 | kSCPropNetProxiesGopherPort); |
| 208 | if (r == -1) goto error; |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 209 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | CFRelease(proxyDict); |
| 211 | return result; |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 212 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 213 | if (proxyDict) CFRelease(proxyDict); |
| 214 | Py_XDECREF(result); |
| 215 | return NULL; |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | static PyMethodDef mod_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 219 | { |
| 220 | "_get_proxy_settings", |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 221 | get_proxy_settings, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 222 | METH_NOARGS, |
| 223 | NULL, |
| 224 | }, |
| 225 | { |
| 226 | "_get_proxies", |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 227 | get_proxies, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 228 | METH_NOARGS, |
| 229 | NULL, |
| 230 | }, |
| 231 | { 0, 0, 0, 0 } |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 232 | }; |
| 233 | |
Mohamed Koubaa | 3ff6975 | 2020-09-08 22:28:48 -0500 | [diff] [blame] | 234 | static PyModuleDef_Slot _scproxy_slots[] = { |
| 235 | {0, NULL} |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 236 | }; |
| 237 | |
Mohamed Koubaa | 3ff6975 | 2020-09-08 22:28:48 -0500 | [diff] [blame] | 238 | static struct PyModuleDef _scproxy_module = { |
| 239 | PyModuleDef_HEAD_INIT, |
| 240 | .m_name = "_scproxy", |
| 241 | .m_size = 0, |
| 242 | .m_methods = mod_methods, |
| 243 | .m_slots = _scproxy_slots, |
| 244 | }; |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 245 | |
| 246 | #ifdef __cplusplus |
| 247 | extern "C" { |
| 248 | #endif |
| 249 | |
Victor Stinner | f024d26 | 2015-03-17 17:48:27 +0100 | [diff] [blame] | 250 | PyMODINIT_FUNC |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 251 | PyInit__scproxy(void) |
| 252 | { |
Mohamed Koubaa | 3ff6975 | 2020-09-08 22:28:48 -0500 | [diff] [blame] | 253 | return PyModuleDef_Init(&_scproxy_module); |
Ronald Oussoren | 8415120 | 2010-04-18 20:46:11 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | #ifdef __cplusplus |
| 257 | } |
| 258 | #endif |