]> source.dussan.org Git - gitea.git/commitdiff
Use proxy for pull mirror (#22771) (#22772)
authorGusted <postmaster@gusted.xyz>
Sat, 11 Feb 2023 08:11:54 +0000 (09:11 +0100)
committerGitHub <noreply@github.com>
Sat, 11 Feb 2023 08:11:54 +0000 (16:11 +0800)
- Backport #22771
  - 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
  - Downstream issue: https://codeberg.org/forgejo/forgejo/issues/302

---------

Co-authored-by: zeripath <art27@cantab.net>
modules/git/repo.go
modules/proxy/proxy.go
services/mirror/mirror_pull.go

index 8ba3ae4fdafd585394948dce16aa1d2eaa647f9e..9dd67c71cf996505b23440eb4c6f0013cbc5b4d5 100644 (file)
@@ -164,10 +164,8 @@ func CloneWithArgs(ctx context.Context, args []CmdArg, from, to string, opts Clo
 
        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)
index 61730544b078bbd07cb47775451ac16447944c2b..1dea0cf450057ae85ef3ac8ed601c443c8188165 100644 (file)
@@ -8,6 +8,7 @@ import (
        "net/http"
        "net/url"
        "os"
+       "strings"
        "sync"
 
        "code.gitea.io/gitea/modules/log"
@@ -83,3 +84,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
+}
index 6002e6b8edb28c7601f69aad09dc6ee46171ee84..ac9c9cc76d4b36835ce95713bc3ac97a452781a4 100644 (file)
@@ -19,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"
@@ -216,6 +217,8 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo
                return nil, false
        }
 
+       envs := proxy.EnvWithProxy(remoteURL.URL)
+
        stdoutBuilder := strings.Builder{}
        stderrBuilder := strings.Builder{}
        if err := git.NewCommand(ctx, gitArgs...).
@@ -223,6 +226,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 {