Use proxy for pull mirror (#22771)

- Use the proxy (if one is specified) for pull mirrors syncs.
- Pulled the code from
c2774d9e80/modules/git/repo.go (L164-L170)

Downstream issue: https://codeberg.org/forgejo/forgejo/issues/302

---------

Co-authored-by: Lauris BH <lauris@nix.lv>
This commit is contained in:
Gusted 2023-02-11 01:39:50 +01:00 committed by GitHub
parent affdd40296
commit 1cb8d14bf7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 4 deletions

View File

@ -163,10 +163,8 @@ func CloneWithArgs(ctx context.Context, args TrustedCmdArgs, from, to string, op
envs := os.Environ() envs := os.Environ()
u, err := url.Parse(from) u, err := url.Parse(from)
if err == nil && (strings.EqualFold(u.Scheme, "http") || strings.EqualFold(u.Scheme, "https")) { if err == nil {
if proxy.Match(u.Host) { envs = proxy.EnvWithProxy(u)
envs = append(envs, fmt.Sprintf("https_proxy=%s", proxy.GetProxyURL()))
}
} }
stderr := new(bytes.Buffer) stderr := new(bytes.Buffer)

View File

@ -7,6 +7,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"strings"
"sync" "sync"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
@ -82,3 +83,16 @@ func Proxy() func(req *http.Request) (*url.URL, error) {
return http.ProxyFromEnvironment(req) 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
}

View File

@ -18,6 +18,7 @@ import (
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification" "code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/process"
"code.gitea.io/gitea/modules/proxy"
repo_module "code.gitea.io/gitea/modules/repository" repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/timeutil"
@ -215,6 +216,8 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo
return nil, false return nil, false
} }
envs := proxy.EnvWithProxy(remoteURL.URL)
stdoutBuilder := strings.Builder{} stdoutBuilder := strings.Builder{}
stderrBuilder := strings.Builder{} stderrBuilder := strings.Builder{}
if err := cmd. if err := cmd.
@ -222,6 +225,7 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo
Run(&git.RunOpts{ Run(&git.RunOpts{
Timeout: timeout, Timeout: timeout,
Dir: repoPath, Dir: repoPath,
Env: envs,
Stdout: &stdoutBuilder, Stdout: &stdoutBuilder,
Stderr: &stderrBuilder, Stderr: &stderrBuilder,
}); err != nil { }); err != nil {