From 873b532f381d4dddabe5370dcffef49816f9f808 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 23 Jun 2020 22:52:21 +0800 Subject: add global proxy settings --- xmake/modules/devel/git/clone.lua | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'xmake/modules/devel/git/clone.lua') diff --git a/xmake/modules/devel/git/clone.lua b/xmake/modules/devel/git/clone.lua index 4285b392d..a2fb554fb 100644 --- a/xmake/modules/devel/git/clone.lua +++ b/xmake/modules/devel/git/clone.lua @@ -20,6 +20,7 @@ -- imports import("core.base.option") +import("core.base.global") import("lib.detect.find_tool") -- clone url @@ -75,6 +76,13 @@ function main(url, opt) table.insert(argv, path.translate(opt.outputdir)) end + -- use proxy? + local envs + local proxy = global.get("proxy") + if proxy then + envs = {ALL_PROXY = proxy} + end + -- clone it - os.vrunv(git.program, argv) + os.vrunv(git.program, argv, {envs = envs}) end -- cgit v1.3.1 From 18bded45c229f5580c13fc8b4afc5edb6f18819a Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 24 Jun 2020 00:40:19 +0800 Subject: improve proxy --- xmake/actions/global/xmake.lua | 9 ++++- xmake/modules/devel/git/clone.lua | 8 ++--- xmake/modules/devel/git/ls_remote.lua | 8 ++--- xmake/modules/devel/git/pull.lua | 22 ++++++++++--- xmake/modules/net/http/download.lua | 20 +++++------ xmake/modules/net/proxy.lua | 62 +++++++++++++++++++++++++++++++++++ 6 files changed, 106 insertions(+), 23 deletions(-) create mode 100644 xmake/modules/net/proxy.lua (limited to 'xmake/modules/devel/git/clone.lua') diff --git a/xmake/actions/global/xmake.lua b/xmake/actions/global/xmake.lua index 720b51066..c70571dd2 100644 --- a/xmake/actions/global/xmake.lua +++ b/xmake/actions/global/xmake.lua @@ -49,11 +49,18 @@ task("global") return import("core.theme.theme.names")() end} , {nil, "debugger", "kv", "auto" , "The debugger program path." } - , {'x', "proxy", "kv", nil , "Use proxy on given port. [PROTOCOL://]HOST[:PORT]" + + -- proxy configuration + , {category = "Proxy Configuration"} + , {'x', "proxy", "kv", nil , "Use proxy on given port. [protocol://]host[:port]" , " e.g." , " - xmake g --proxy='http://host:port'" , " - xmake g --proxy='https://host:port'" , " - xmake g --proxy='socks5://host:port'" } + , {nil, "proxy_hosts", "kv", nil , "Only enable proxy for the given hosts list, it will enable all if be unset," + , "and we can pass match pattern to list:" + , " e.g." + , " - xmake g --proxy_hosts='github.com,gitlab.*,*.xmake.io'"} -- package configuration , {category = "Package Configuration"} diff --git a/xmake/modules/devel/git/clone.lua b/xmake/modules/devel/git/clone.lua index a2fb554fb..ef3aa769b 100644 --- a/xmake/modules/devel/git/clone.lua +++ b/xmake/modules/devel/git/clone.lua @@ -20,8 +20,8 @@ -- imports import("core.base.option") -import("core.base.global") import("lib.detect.find_tool") +import("net.proxy") -- clone url -- @@ -78,9 +78,9 @@ function main(url, opt) -- use proxy? local envs - local proxy = global.get("proxy") - if proxy then - envs = {ALL_PROXY = proxy} + local proxy_conf = proxy.get(url) + if proxy_conf then + envs = {ALL_PROXY = proxy_conf} end -- clone it diff --git a/xmake/modules/devel/git/ls_remote.lua b/xmake/modules/devel/git/ls_remote.lua index ebbb99d98..1775567db 100644 --- a/xmake/modules/devel/git/ls_remote.lua +++ b/xmake/modules/devel/git/ls_remote.lua @@ -20,8 +20,8 @@ -- imports import("core.base.option") -import("core.base.global") import("lib.detect.find_tool") +import("net.proxy") -- ls_remote to given branch, tag or commit -- @@ -58,9 +58,9 @@ function main(reftype, url) -- use proxy? local envs - local proxy = global.get("proxy") - if proxy then - envs = {ALL_PROXY = proxy} + local proxy_conf = proxy.get(url) + if proxy_conf then + envs = {ALL_PROXY = proxy_conf} end -- get refs diff --git a/xmake/modules/devel/git/pull.lua b/xmake/modules/devel/git/pull.lua index 6e0f0a735..2320c51bd 100644 --- a/xmake/modules/devel/git/pull.lua +++ b/xmake/modules/devel/git/pull.lua @@ -20,8 +20,8 @@ -- imports import("core.base.option") -import("core.base.global") import("lib.detect.find_tool") +import("net.proxy") -- pull remote commits -- @@ -66,9 +66,23 @@ function main(opt) -- use proxy? local envs - local proxy = global.get("proxy") - if proxy then - envs = {ALL_PROXY = proxy} + local proxy_conf = proxy.get() + if proxy_conf then + -- get proxy configuration from the current remote url + local remoteinfo = try { function() return os.iorunv(git.program, {"remote", "-v"}) end } + if remoteinfo then + for _, line in ipairs(remoteinfo:split('\n', {plain = true})) do + local splitinfo = line:split("%s+") + if #splitinfo > 1 and splitinfo[1] == (opt.remote or "origin") then + local url = splitinfo[2] + if url then + proxy_conf = proxy.get(url) + end + break + end + end + end + envs = {ALL_PROXY = proxy_conf} end -- pull it diff --git a/xmake/modules/net/http/download.lua b/xmake/modules/net/http/download.lua index f85561d49..1267e24b8 100644 --- a/xmake/modules/net/http/download.lua +++ b/xmake/modules/net/http/download.lua @@ -20,8 +20,8 @@ -- imports import("core.base.option") -import("core.base.global") import("lib.detect.find_tool") +import("net.proxy") -- get user agent function _get_user_agent() @@ -64,10 +64,10 @@ function _curl_download(tool, url, outputfile) end -- use proxy? - local proxy = global.get("proxy") - if proxy then + local proxy_conf = proxy.get(url) + if proxy_conf then table.insert(argv, "-x") - table.insert(argv, proxy) + table.insert(argv, proxy_conf) end -- set user-agent @@ -108,19 +108,19 @@ function _wget_download(tool, url, outputfile) end -- use proxy? - local proxy = global.get("proxy") - if proxy then + local proxy_conf = proxy.get(url) + if proxy_conf then table.insert(argv, "-e") table.insert(argv, "use_proxy=yes") table.insert(argv, "-e") if url:startswith("http://") then - table.insert(argv, "http_proxy=" .. proxy) + table.insert(argv, "http_proxy=" .. proxy_conf) elseif url:startswith("https://") then - table.insert(argv, "https_proxy=" .. proxy) + table.insert(argv, "https_proxy=" .. proxy_conf) elseif url:startswith("ftp://") then - table.insert(argv, "ftp_proxy=" .. proxy) + table.insert(argv, "ftp_proxy=" .. proxy_conf) else - table.insert(argv, "http_proxy=" .. proxy) + table.insert(argv, "http_proxy=" .. proxy_conf) end end 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 -- cgit v1.3.1