Skip to content

Commit 1cb8d14

Browse files
Gustedlafriks
andauthored
Use proxy for pull mirror (#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: Lauris BH <lauris@nix.lv>
1 parent affdd40 commit 1cb8d14

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

Diff for: modules/git/repo.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,8 @@ func CloneWithArgs(ctx context.Context, args TrustedCmdArgs, from, to string, op
163163

164164
envs := os.Environ()
165165
u, err := url.Parse(from)
166-
if err == nil && (strings.EqualFold(u.Scheme, "http") || strings.EqualFold(u.Scheme, "https")) {
167-
if proxy.Match(u.Host) {
168-
envs = append(envs, fmt.Sprintf("https_proxy=%s", proxy.GetProxyURL()))
169-
}
166+
if err == nil {
167+
envs = proxy.EnvWithProxy(u)
170168
}
171169

172170
stderr := new(bytes.Buffer)

Diff for: modules/proxy/proxy.go

+14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"net/url"
99
"os"
10+
"strings"
1011
"sync"
1112

1213
"code.gitea.io/gitea/modules/log"
@@ -82,3 +83,16 @@ func Proxy() func(req *http.Request) (*url.URL, error) {
8283
return http.ProxyFromEnvironment(req)
8384
}
8485
}
86+
87+
// EnvWithProxy returns os.Environ(), with a https_proxy env, if the given url
88+
// needs to be proxied.
89+
func EnvWithProxy(u *url.URL) []string {
90+
envs := os.Environ()
91+
if strings.EqualFold(u.Scheme, "http") || strings.EqualFold(u.Scheme, "https") {
92+
if Match(u.Host) {
93+
envs = append(envs, "https_proxy="+GetProxyURL())
94+
}
95+
}
96+
97+
return envs
98+
}

Diff for: services/mirror/mirror_pull.go

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"code.gitea.io/gitea/modules/log"
1919
"code.gitea.io/gitea/modules/notification"
2020
"code.gitea.io/gitea/modules/process"
21+
"code.gitea.io/gitea/modules/proxy"
2122
repo_module "code.gitea.io/gitea/modules/repository"
2223
"code.gitea.io/gitea/modules/setting"
2324
"code.gitea.io/gitea/modules/timeutil"
@@ -215,13 +216,16 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo
215216
return nil, false
216217
}
217218

219+
envs := proxy.EnvWithProxy(remoteURL.URL)
220+
218221
stdoutBuilder := strings.Builder{}
219222
stderrBuilder := strings.Builder{}
220223
if err := cmd.
221224
SetDescription(fmt.Sprintf("Mirror.runSync: %s", m.Repo.FullName())).
222225
Run(&git.RunOpts{
223226
Timeout: timeout,
224227
Dir: repoPath,
228+
Env: envs,
225229
Stdout: &stdoutBuilder,
226230
Stderr: &stderrBuilder,
227231
}); err != nil {

0 commit comments

Comments
 (0)