From 158b3b4f47a040430d42055cd23ba5289c77ece2 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 24 Jun 2020 22:51:00 +0800 Subject: support proxy pac file --- xmake/modules/net/proxy.lua | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'xmake/modules/net/proxy.lua') diff --git a/xmake/modules/net/proxy.lua b/xmake/modules/net/proxy.lua index 0c7d308cb..e0bd7d246 100644 --- a/xmake/modules/net/proxy.lua +++ b/xmake/modules/net/proxy.lua @@ -34,6 +34,28 @@ function _proxy_hosts() return proxy_hosts or nil end +-- get proxy pac file +function _proxy_pac() + local proxy_pac = _g._PROXY_PAC + if proxy_pac == nil then + local pac = global.get("proxy_pac") + local pacfile + if pac and pac:endswith(".lua") then + if os.isfile(pac) then + pacfile = pac + end + if not pacfile and not path.is_absolute(pac) then + pacfile = path.join(global.directory(), pac) + end + end + if pacfile and os.isfile(pacfile) then + proxy_pac = import(path.basename(pacfile), {rootdir = path.directory(pacfile), try = true, anonymous = true}) + end + _g._PROXY_PAC = proxy_pac or false + end + return proxy_pac or nil +end + -- convert host pattern to a lua pattern function _host_pattern(pattern) pattern = pattern:gsub("([%+%.%-%^%$%(%)%%])", "%%%1") @@ -44,7 +66,11 @@ end -- get proxy configuration from the given url, [protocol://]host[:port] function get(url) + + -- enable proxy for the given url and configuration pattern if url then + + -- get proxy from the given hosts pattern local host = url:match("://(.-)/") or url:match("@(.-):") local proxy_hosts = _proxy_hosts() if host and proxy_hosts then @@ -56,7 +82,15 @@ function get(url) end end end + + -- get proxy from the pac file + local proxy_pac = _proxy_pac() + if proxy_pac and proxy_pac(url, host) then + return global.get("proxy") + end return end + + -- enable global proxy return global.get("proxy") end -- cgit v1.3.1