summaryrefslogtreecommitdiff
path: root/xmake/modules/net/proxy.lua
diff options
context:
space:
mode:
authorSineStriker <[email protected]>2025-04-16 14:44:41 +0800
committerSineStriker <[email protected]>2025-04-16 14:44:41 +0800
commitc9367898274ef8805ce79ebb4e24ab69a8a01133 (patch)
tree4c03d7c348c3fee5b254124f6c634b05867afe6b /xmake/modules/net/proxy.lua
parentcd260b41e4f4f2003d46ea40e865fcab73644dec (diff)
Add support for automatically using windows system proxy
Diffstat (limited to 'xmake/modules/net/proxy.lua')
-rw-r--r--xmake/modules/net/proxy.lua48
1 files changed, 44 insertions, 4 deletions
diff --git a/xmake/modules/net/proxy.lua b/xmake/modules/net/proxy.lua
index 809b28352..bc00ce655 100644
--- a/xmake/modules/net/proxy.lua
+++ b/xmake/modules/net/proxy.lua
@@ -102,6 +102,46 @@ function mirror(url)
return url
end
+-- get system proxy
+function _system_proxy()
+ local proxy = os.getenv("ALL_PROXY") or os.getenv("HTTP_PROXY") or os.getenv("HTTPS_PROXY")
+ if proxy then
+ return proxy
+ end
+
+ if os.host() == "windows" then
+ -- Check whether system proxy is enabled
+ local proxy_enable = winos.registry_query('HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings;ProxyEnable')
+ -- Verify that the proxy is enabled (0x1 means enabled)
+ if not proxy_enable or proxy_enable ~= "1" then
+ return nil
+ end
+
+ -- Extract the proxy server address
+ local proxy_server = winos.registry_query("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings;ProxyServer")
+ if not proxy_server then
+ return nil
+ end
+
+ -- Prepend the protocol if not present
+ if not proxy_server:match("://(.-)") then
+ proxy_server = "http://" .. proxy_server
+ end
+ return proxy_server
+ end
+
+ return nil
+end
+
+-- translate global proxy
+function _global_proxy()
+ local proxy = global.get("proxy")
+ if proxy and string.lower(proxy) == "system" then
+ return _system_proxy()
+ end
+ return proxy
+end
+
-- get proxy configuration from the given url, [protocol://]host[:port]
--
-- @see https://github.com/xmake-io/xmake/issues/854
@@ -119,7 +159,7 @@ function config(url)
for _, proxy_host in ipairs(proxy_hosts) do
proxy_host = proxy_host:lower()
if host == proxy_hosts or host:match(_host_pattern(proxy_host)) then
- return global.get("proxy")
+ return _global_proxy()
end
end
end
@@ -127,16 +167,16 @@ function config(url)
-- filter proxy host from the pac file
local proxy_pac = _proxy_pac()
if proxy_pac and host and _is_callable(proxy_pac) and proxy_pac(url, host) then
- return global.get("proxy")
+ return _global_proxy()
end
-- use global proxy
if not proxy_pac and not proxy_hosts then
- return global.get("proxy")
+ return _global_proxy()
end
return
end
-- enable global proxy
- return global.get("proxy")
+ return _global_proxy()
end