summaryrefslogtreecommitdiff
path: root/xmake/modules/net/proxy.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2020-06-24 00:40:19 +0800
committerruki <[email protected]>2020-06-23 22:01:57 +0800
commit18bded45c229f5580c13fc8b4afc5edb6f18819a (patch)
tree52b63db3274349f9a41872414208c7c30058cfd7 /xmake/modules/net/proxy.lua
parent39e0626b0879f882812a178519b0a0998411866e (diff)
improve proxy
Diffstat (limited to 'xmake/modules/net/proxy.lua')
-rw-r--r--xmake/modules/net/proxy.lua62
1 files changed, 62 insertions, 0 deletions
diff --git a/xmake/modules/net/proxy.lua b/xmake/modules/net/proxy.lua
new file mode 100644
index 000000000..0c7d308cb
--- /dev/null
+++ b/xmake/modules/net/proxy.lua
@@ -0,0 +1,62 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015-2020, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file proxy.lua
+--
+
+-- imports
+import("core.base.global")
+
+-- get proxy hosts
+function _proxy_hosts()
+ local proxy_hosts = _g._PROXY_HOSTS
+ if proxy_hosts == nil then
+ proxy_hosts = global.get("proxy_hosts")
+ if proxy_hosts then
+ proxy_hosts = proxy_hosts:split(',', {plain = true})
+ end
+ _g._PROXY_HOSTS = proxy_hosts or false
+ end
+ return proxy_hosts or nil
+end
+
+-- convert host pattern to a lua pattern
+function _host_pattern(pattern)
+ pattern = pattern:gsub("([%+%.%-%^%$%(%)%%])", "%%%1")
+ pattern = pattern:gsub("%*", "\001")
+ pattern = pattern:gsub("\001", ".*")
+ return pattern
+end
+
+-- get proxy configuration from the given url, [protocol://]host[:port]
+function get(url)
+ if url then
+ local host = url:match("://(.-)/") or url:match("@(.-):")
+ local proxy_hosts = _proxy_hosts()
+ if host and proxy_hosts then
+ host = host:lower()
+ 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")
+ end
+ end
+ end
+ return
+ end
+ return global.get("proxy")
+end