summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2020-06-24 22:51:00 +0800
committerruki <[email protected]>2020-06-24 11:06:03 +0800
commit158b3b4f47a040430d42055cd23ba5289c77ece2 (patch)
treebdb16854fe0129ba1d07a13f3ab4b77d3479abd8
parentc18d15eb89116a0f3ce96bd56f3ccf861849f6ec (diff)
support proxy pac file
-rw-r--r--xmake/actions/global/xmake.lua8
-rw-r--r--xmake/modules/net/proxy.lua34
2 files changed, 42 insertions, 0 deletions
diff --git a/xmake/actions/global/xmake.lua b/xmake/actions/global/xmake.lua
index c70571dd2..ae8e752c2 100644
--- a/xmake/actions/global/xmake.lua
+++ b/xmake/actions/global/xmake.lua
@@ -61,6 +61,14 @@ task("global")
, "and we can pass match pattern to list:"
, " e.g."
, " - xmake g --proxy_hosts='github.com,gitlab.*,*.xmake.io'"}
+ , {nil, "proxy_pac", "kv", "pac.lua" , "Set the auto proxy configuration file."
+ , " e.g."
+ , " - xmake g --proxy_pac=pac.lua (in $(globaldir) or absolute path)"
+ , " - function main(url, host)"
+ , " if host == 'github.com' then"
+ , " return true"
+ , " end"
+ , " end"}
-- package configuration
, {category = "Package Configuration"}
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