From aec915933e2705c97a1fbf0102f7b24c1f9500a1 Mon Sep 17 00:00:00 2001 From: Gusted Date: Sun, 5 Feb 2023 21:45:42 +0100 Subject: [PATCH 1/3] Use proxy for pull mirror - Use the proxy (if one is specified) for pull mirrors syncs. - Pulled the code from https://github.com/go-gitea/gitea/blob/c2774d9e80d9a436d9c2044960369c4db227e3a0/modules/git/repo.go#L164-L170 --- services/mirror/mirror_pull.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/services/mirror/mirror_pull.go b/services/mirror/mirror_pull.go index 7dee90352ea2..d90e1701b357 100644 --- a/services/mirror/mirror_pull.go +++ b/services/mirror/mirror_pull.go @@ -6,6 +6,7 @@ package mirror import ( "context" "fmt" + "os" "strings" "time" @@ -18,6 +19,7 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" "code.gitea.io/gitea/modules/process" + "code.gitea.io/gitea/modules/proxy" repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" @@ -215,6 +217,13 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo return nil, false } + envs := []string{} + if strings.EqualFold(remoteURL.Scheme, "http") || strings.EqualFold(remoteURL.Scheme, "https") { + if proxy.Match(remoteURL.Host) { + envs = append(envs, fmt.Sprintf("https_proxy=%s", proxy.GetProxyURL())) + } + } + stdoutBuilder := strings.Builder{} stderrBuilder := strings.Builder{} if err := cmd. @@ -222,6 +231,7 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo Run(&git.RunOpts{ Timeout: timeout, Dir: repoPath, + Env: envs, Stdout: &stdoutBuilder, Stderr: &stderrBuilder, }); err != nil { From 05ea8e297eb073e4edd16d2713179e9f0f4e8ea6 Mon Sep 17 00:00:00 2001 From: Gusted Date: Sun, 5 Feb 2023 21:52:14 +0100 Subject: [PATCH 2/3] Fix non-using import --- services/mirror/mirror_pull.go | 1 - 1 file changed, 1 deletion(-) diff --git a/services/mirror/mirror_pull.go b/services/mirror/mirror_pull.go index d90e1701b357..f0b79fc0b95a 100644 --- a/services/mirror/mirror_pull.go +++ b/services/mirror/mirror_pull.go @@ -6,7 +6,6 @@ package mirror import ( "context" "fmt" - "os" "strings" "time" From 786a07e2f7a0d3ffb6d7aec87876dade55dbd62d Mon Sep 17 00:00:00 2001 From: Gusted Date: Mon, 6 Feb 2023 22:00:21 +0100 Subject: [PATCH 3/3] Refactor code --- modules/git/repo.go | 6 ++---- modules/proxy/proxy.go | 14 ++++++++++++++ services/mirror/mirror_pull.go | 7 +------ 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/modules/git/repo.go b/modules/git/repo.go index e77a3a6ad8e3..233f7f20cfc2 100644 --- a/modules/git/repo.go +++ b/modules/git/repo.go @@ -163,10 +163,8 @@ func CloneWithArgs(ctx context.Context, args TrustedCmdArgs, from, to string, op envs := os.Environ() u, err := url.Parse(from) - if err == nil && (strings.EqualFold(u.Scheme, "http") || strings.EqualFold(u.Scheme, "https")) { - if proxy.Match(u.Host) { - envs = append(envs, fmt.Sprintf("https_proxy=%s", proxy.GetProxyURL())) - } + if err == nil { + envs = proxy.EnvWithProxy(u) } stderr := new(bytes.Buffer) diff --git a/modules/proxy/proxy.go b/modules/proxy/proxy.go index f0cd366c1283..1a6bdad7fb5d 100644 --- a/modules/proxy/proxy.go +++ b/modules/proxy/proxy.go @@ -7,6 +7,7 @@ import ( "net/http" "net/url" "os" + "strings" "sync" "code.gitea.io/gitea/modules/log" @@ -82,3 +83,16 @@ func Proxy() func(req *http.Request) (*url.URL, error) { return http.ProxyFromEnvironment(req) } } + +// EnvWithProxy returns os.Environ(), with a https_proxy env, if the given url +// needs to be proxied. +func EnvWithProxy(u *url.URL) []string { + envs := os.Environ() + if strings.EqualFold(u.Scheme, "http") || strings.EqualFold(u.Scheme, "https") { + if Match(u.Host) { + envs = append(envs, "https_proxy="+GetProxyURL()) + } + } + + return envs +} diff --git a/services/mirror/mirror_pull.go b/services/mirror/mirror_pull.go index f0b79fc0b95a..126d2bf35468 100644 --- a/services/mirror/mirror_pull.go +++ b/services/mirror/mirror_pull.go @@ -216,12 +216,7 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo return nil, false } - envs := []string{} - if strings.EqualFold(remoteURL.Scheme, "http") || strings.EqualFold(remoteURL.Scheme, "https") { - if proxy.Match(remoteURL.Host) { - envs = append(envs, fmt.Sprintf("https_proxy=%s", proxy.GetProxyURL())) - } - } + envs := proxy.EnvWithProxy(remoteURL.URL) stdoutBuilder := strings.Builder{} stderrBuilder := strings.Builder{}