From ef17fec2bc80c6ec4ad537e74748762d18e92606 Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 23 Apr 2021 22:52:16 +0800 Subject: [PATCH] improve proxy to support mirror url --- CHANGELOG.md | 2 ++ xmake/modules/devel/git/clone.lua | 2 +- xmake/modules/devel/git/ls_remote.lua | 2 +- xmake/modules/devel/git/pull.lua | 4 ++-- xmake/modules/net/http/download.lua | 4 ++-- xmake/modules/net/proxy.lua | 14 +++++++++++++- .../action/require/impl/actions/download.lua | 7 ++++--- .../require/impl/actions/download_resources.lua | 3 ++- 8 files changed, 27 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cce793c577e..2507f832235 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ * [#1338](https://github.com/xmake-io/xmake/issues/1338): Support import and export installed packages * [#1087](https://github.com/xmake-io/xmake/issues/1087): Add `xrepo env shell` and support load envs from `add_requires/xmake.lua` * [#1313](https://github.com/xmake-io/xmake/issues/1313): Support private package for `add_requires/add_deps` +* [#1358](https://github.com/xmake-io/xmake/issues/1358): Support to set mirror url to speedup download package ### Change @@ -986,6 +987,7 @@ * [#1338](https://github.com/xmake-io/xmake/issues/1338): 支持导入导出已安装的包 * [#1087](https://github.com/xmake-io/xmake/issues/1087): 添加 `xrepo env shell` 并且支持从 `add_requires/xmake.lua` 加载包环境 * [#1313](https://github.com/xmake-io/xmake/issues/1313): 为 `add_requires/add_deps` 添加私有包支持 +* [#1358](https://github.com/xmake-io/xmake/issues/1358): 支持设置镜像 url 站点加速包下载 ### 改进 diff --git a/xmake/modules/devel/git/clone.lua b/xmake/modules/devel/git/clone.lua index da42e33e582..534ae8ac3d4 100644 --- a/xmake/modules/devel/git/clone.lua +++ b/xmake/modules/devel/git/clone.lua @@ -84,7 +84,7 @@ function main(url, opt) -- use proxy? local envs - local proxy_conf = proxy.get(url) + local proxy_conf = proxy.config(url) if proxy_conf then envs = {ALL_PROXY = proxy_conf} end diff --git a/xmake/modules/devel/git/ls_remote.lua b/xmake/modules/devel/git/ls_remote.lua index c736c85d90e..78ee361978b 100644 --- a/xmake/modules/devel/git/ls_remote.lua +++ b/xmake/modules/devel/git/ls_remote.lua @@ -58,7 +58,7 @@ function main(reftype, url) -- use proxy? local envs - local proxy_conf = proxy.get(url) + local proxy_conf = proxy.config(url) if proxy_conf then envs = {ALL_PROXY = proxy_conf} end diff --git a/xmake/modules/devel/git/pull.lua b/xmake/modules/devel/git/pull.lua index e40707e4e00..9970f8dbccb 100644 --- a/xmake/modules/devel/git/pull.lua +++ b/xmake/modules/devel/git/pull.lua @@ -66,7 +66,7 @@ function main(opt) -- use proxy? local envs - local proxy_conf = proxy.get() + local proxy_conf = proxy.config() if proxy_conf then -- get proxy configuration from the current remote url local remoteinfo = try { function() return os.iorunv(git.program, {"remote", "-v"}) end } @@ -76,7 +76,7 @@ function main(opt) if #splitinfo > 1 and splitinfo[1] == (opt.remote or "origin") then local url = splitinfo[2] if url then - proxy_conf = proxy.get(url) + proxy_conf = proxy.config(url) end break end diff --git a/xmake/modules/net/http/download.lua b/xmake/modules/net/http/download.lua index aad644416dc..7a90ac8bc8b 100644 --- a/xmake/modules/net/http/download.lua +++ b/xmake/modules/net/http/download.lua @@ -64,7 +64,7 @@ function _curl_download(tool, url, outputfile, opt) end -- use proxy? - local proxy_conf = proxy.get(url) + local proxy_conf = proxy.config(url) if proxy_conf then table.insert(argv, "-x") table.insert(argv, proxy_conf) @@ -114,7 +114,7 @@ function _wget_download(tool, url, outputfile, opt) end -- use proxy? - local proxy_conf = proxy.get(url) + local proxy_conf = proxy.config(url) if proxy_conf then table.insert(argv, "-e") table.insert(argv, "use_proxy=yes") diff --git a/xmake/modules/net/proxy.lua b/xmake/modules/net/proxy.lua index e216931dd28..0d6b6805bde 100644 --- a/xmake/modules/net/proxy.lua +++ b/xmake/modules/net/proxy.lua @@ -39,6 +39,9 @@ end -- pac.lua -- -- @code +-- function mirror(url) +-- return url:gsub("github.com", "hub.fastgit.org") +-- end -- function main(url, host) -- if host:find("bintray.com") then -- return true @@ -75,11 +78,20 @@ function _host_pattern(pattern) return pattern end +-- get proxy mirror url +function mirror(url) + local proxy_pac = _proxy_pac() + if proxy_pac and proxy_pac.mirror then + return proxy_pac.mirror(url) + end + return url +end + -- get proxy configuration from the given url, [protocol://]host[:port] -- -- @see https://github.com/xmake-io/xmake/issues/854 -- -function get(url) +function config(url) -- enable proxy for the given url and configuration pattern if url then diff --git a/xmake/modules/private/action/require/impl/actions/download.lua b/xmake/modules/private/action/require/impl/actions/download.lua index 019b01ae8a1..ba742aeb339 100644 --- a/xmake/modules/private/action/require/impl/actions/download.lua +++ b/xmake/modules/private/action/require/impl/actions/download.lua @@ -29,6 +29,7 @@ import("lib.detect.find_directory") import("private.action.require.impl.utils.filter") import("private.action.require.impl..utils.url_filename") import("net.http") +import("net.proxy") import("devel.git") import("utils.archive") @@ -67,13 +68,13 @@ function _checkout(package, url, sourcedir, url_alias) if package:branch() then -- only shadow clone this branch - git.clone(url, {depth = 1, recursive = true, longpaths = longpaths, branch = package:branch(), outputdir = packagedir}) + git.clone(proxy.mirror(url), {depth = 1, recursive = true, longpaths = longpaths, branch = package:branch(), outputdir = packagedir}) -- download package from revision or tag? else -- clone whole history and tags - git.clone(url, {longpaths = longpaths, outputdir = packagedir}) + git.clone(proxy.mirror(url), {longpaths = longpaths, outputdir = packagedir}) -- attempt to checkout the given version local revision = package:revision(url_alias) or package:tag() or package:version_str() @@ -134,7 +135,7 @@ function _download(package, url, sourcedir, url_alias, url_excludes) -- we can use local package from the search directories directly if network is too slow os.cp(localfile, packagefile) else - http.download(url, packagefile) + http.download(proxy.mirror(url), packagefile) end end diff --git a/xmake/modules/private/action/require/impl/actions/download_resources.lua b/xmake/modules/private/action/require/impl/actions/download_resources.lua index 9c40b0b81c3..5a85c7c3554 100644 --- a/xmake/modules/private/action/require/impl/actions/download_resources.lua +++ b/xmake/modules/private/action/require/impl/actions/download_resources.lua @@ -23,6 +23,7 @@ import("core.base.option") import("core.package.package", {alias = "core_package"}) import("lib.detect.find_file") import("net.http") +import("net.proxy") import("utils.archive") -- download resources @@ -55,7 +56,7 @@ function _download(package, resource_name, resource_url, resource_hash) -- we can use local resource from the search directories directly if network is too slow os.cp(localfile, resource_file) elseif resource_url:find(string.ipattern("https-://")) or resource_url:find(string.ipattern("ftps-://")) then - http.download(resource_url, resource_file) + http.download(proxy.mirror(resource_url), resource_file) else raise("invalid resource url(%s)", resource_url) end