PAC support
proxylib.pac
PAC (Proxy Auto-Config) support: the standard Netscape utility functions a
PAC script's FindProxyForURL relies on, plus the common Microsoft
*Ex/IPv6-aware extensions, and loading PAC scripts (as plain Python via
subclassing, or as real JS via the optional dukpy backend).
__all__ = ('PAC', 'load', 'clear_download_cache', 'clear_dns_cache')
module-attribute
JSProxyAutoConfig(js, overrides=None)
A PAC whose FindProxyForURL (and the utility functions above) run as real JS.
Source code in src/proxylib/pac/javascript.py
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 | |
PAC
Bases: object
Base implementation of the PAC utility-function namespace.
FindProxyForURL here always returns "DIRECT"; subclass and
override it (or use :class:JSProxyAutoConfig to run a real PAC script)
to actually select proxies.
FindProxyForURL(url, host)
staticmethod
Source code in src/proxylib/pac/__init__.py
312 313 314 | |
__contains__(key)
Source code in src/proxylib/pac/__init__.py
332 333 334 335 336 337 | |
__getitem__(url)
Source code in src/proxylib/pac/__init__.py
316 317 318 319 320 321 322 323 324 | |
convert_addr(ipaddr)
staticmethod
Source code in src/proxylib/pac/__init__.py
121 122 123 | |
dateRange(*args)
staticmethod
Best-effort implementation of the PAC dateRange overload set.
Supports the documented call shapes: a single day/month/year, a
day/month/year range, (day1, month1, day2, month2),
(month1, year1, month2, year2) and
(day1, month1, year1, day2, month2, year2), each optionally
followed by "GMT".
Source code in src/proxylib/pac/__init__.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
dnsDomainIs(host, domain)
staticmethod
Source code in src/proxylib/pac/__init__.py
241 242 243 | |
dnsDomainLevels(host)
staticmethod
Number of dots in host (the PAC spec's definition), e.g. 2 for sub.example.com.
Source code in src/proxylib/pac/__init__.py
116 117 118 119 | |
dnsResolve(host, /, cache_ttl=30.0)
staticmethod
Resolve host to a single IPv4/IPv6 address string, or None.
Cached per host for cache_ttl seconds (default 30); pass
cache_ttl=0/None to force a fresh resolution.
Source code in src/proxylib/pac/__init__.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
dnsResolveEx(host)
staticmethod
Resolve host to all of its addresses (IPv4 and IPv6), '; '-separated.
Source code in src/proxylib/pac/__init__.py
90 91 92 93 94 95 96 97 98 99 100 101 102 | |
get(uri, default=None)
Source code in src/proxylib/pac/__init__.py
326 327 328 329 330 | |
getClientVersion()
staticmethod
Microsoft extension: PAC engine version string.
Source code in src/proxylib/pac/__init__.py
307 308 309 310 | |
isInNet(host, pattern, mask)
staticmethod
IPv4 (spec) net-membership check, tolerant of IPv6 host/pattern too.
Source code in src/proxylib/pac/__init__.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |
isInNetEx(ip_address, ip_prefix)
staticmethod
Microsoft extension: CIDR-notation net check, IPv4 or IPv6.
Source code in src/proxylib/pac/__init__.py
287 288 289 290 291 292 293 294 295 | |
isPlainHostName(host)
staticmethod
Source code in src/proxylib/pac/__init__.py
237 238 239 | |
isResolvable(host)
staticmethod
Source code in src/proxylib/pac/__init__.py
256 257 258 259 260 261 262 | |
isResolvableEx(host)
staticmethod
Source code in src/proxylib/pac/__init__.py
264 265 266 | |
localHostOrDomainIs(host, hostdom)
staticmethod
True if host is either the bare hostname or the full host+domain of hostdom.
Compares the host part of hostdom exactly (not a prefix check)
so e.g. "ww" does not wrongly match "www.example.com".
Source code in src/proxylib/pac/__init__.py
245 246 247 248 249 250 251 252 253 254 | |
myIpAddress()
staticmethod
Source code in src/proxylib/pac/__init__.py
104 105 106 107 108 109 | |
myIpAddressEx()
staticmethod
All local addresses (IPv4 and IPv6), '; '-separated.
Source code in src/proxylib/pac/__init__.py
111 112 113 114 | |
shExpMatch(test, shexp)
staticmethod
Source code in src/proxylib/pac/__init__.py
125 126 127 | |
sortIpAddressList(ip_address_list)
staticmethod
Microsoft extension: sort a '; '-separated address list numerically.
Source code in src/proxylib/pac/__init__.py
297 298 299 300 301 302 303 304 305 | |
timeRange(*args)
staticmethod
PAC timeRange: hour, hour-hour, hour:min-hour:min, or hour:min:sec range, +GMT.
Source code in src/proxylib/pac/__init__.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
weekdayRange(wd1, /, *args)
staticmethod
weekdayRange(wd1: _WEEKDAY, gmt: 'None|_Literal["GMT"]' = None) -> bool
weekdayRange(wd1: _WEEKDAY, wd2: _WEEKDAY, gmt: 'None|_Literal["GMT"]' = None) -> bool
Source code in src/proxylib/pac/__init__.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
clear_dns_cache()
Source code in src/proxylib/pac/__init__.py
60 61 | |
clear_download_cache()
Source code in src/proxylib/pac/__init__.py
369 370 | |
load(url, cache_ttl=300.0, **urllib_kwds)
Load a PAC script from a URL, a file: path, or inline JS source.
Requires the dukpy extra (proxylib[jspac]) to actually execute
the script; without it, a warning is issued and an always-DIRECT
:class:PAC is returned instead.
Genuine network downloads (not file: paths or inline JS) are cached
per URL for cache_ttl seconds (default 5 minutes); pass
cache_ttl=0/None to force a fresh fetch.
Source code in src/proxylib/pac/__init__.py
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 | |
proxylib.pac.wpad
DNS + HTTP WPAD (Web Proxy Auto-Discovery) fallback, used by auto_proxy()
when no explicit OS/env proxy configuration is found.
DHCP option 252 discovery is intentionally not implemented: it needs raw access to the OS's DHCP lease data, which has no portable stdlib-only path.
__all__ = ('discover',)
module-attribute
discover(fqdn=None, timeout=3.0, cache_ttl=300.0, **urllib_kwds)
Try each http://wpad.<domain>/wpad.dat from most to least specific.
Returns the first successfully loaded PAC, or None if discovery fails.
Results (including failures) are cached per fqdn for cache_ttl
seconds; pass cache_ttl=0 (or None) to force a fresh probe.
Source code in src/proxylib/pac/wpad.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | |
proxylib.pac.javascript
Runs a PAC script as real JavaScript via a pluggable engine (dukpy or
quickjs -- see :mod:proxylib.pac.engines), exposing every PAC
static/class method (and any subclass adds) into the JS global scope so
FindProxyForURL can call them.
__all__ = ['JSContext']
module-attribute
JSContext(js, overrides=None)
Base class that boots a JS engine with _JSCONTEXT exported, then evals js.
The engine itself is pluggable (see :mod:proxylib.pac.engines) --
this class only depends on the small :class:~proxylib.pac.engines.base.JSEngine
interface (export_function/eval/call), not on any specific
engine's API.
Source code in src/proxylib/pac/javascript.py
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 | |
__getattribute__(name)
Source code in src/proxylib/pac/javascript.py
135 136 137 138 139 140 141 | |
JSContextMeta
Bases: ABCMeta
Collects every alpha-leading attribute (methods) of a class and its
bases into _JSCONTEXT, the set of functions exported into the JS engine.
__new__(metaclass, cls_name, base_classes, cls_builder)
Source code in src/proxylib/pac/javascript.py
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 | |
proxylib.pac.engines.base
Common interface a PAC JS execution engine must implement.
:mod:proxylib.pac.javascript's JSContext is written against this
protocol, not any specific engine -- dukpy.py/quickjs.py each wrap a
concrete engine behind it.
__all__ = ('JSEngine',)
module-attribute
JSEngine
Bases: Protocol
One JS execution context.
call(name, *args)
Call the JS global function name with args.
Must resolve name fresh in the engine's global scope on every
call, not once at bind time -- this is what implements "a PAC
script that redefines an exported name overrides it": the name is
looked up again after the script has had a chance to reassign it.
Source code in src/proxylib/pac/engines/base.py
27 28 29 30 31 32 33 34 35 | |
eval(code)
Evaluate a script (used once, to load the PAC source).
Source code in src/proxylib/pac/engines/base.py
23 24 25 | |
export_function(name, func)
Make func callable from JS as the global function name.
Source code in src/proxylib/pac/engines/base.py
19 20 21 | |