Skip to content

Commit 8fa419c

Browse files
Gustedzeripath
andauthored
Use proxy for pull mirror (#22771) (#22772)
- 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 <[email protected]>
1 parent 77c8957 commit 8fa419c

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

modules/git/repo.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,8 @@ func CloneWithArgs(ctx context.Context, args []CmdArg, from, to string, opts Clo
164164

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

173171
stderr := new(bytes.Buffer)

modules/proxy/proxy.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"net/url"
1010
"os"
11+
"strings"
1112
"sync"
1213

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

services/mirror/mirror_pull.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"code.gitea.io/gitea/modules/log"
2020
"code.gitea.io/gitea/modules/notification"
2121
"code.gitea.io/gitea/modules/process"
22+
"code.gitea.io/gitea/modules/proxy"
2223
repo_module "code.gitea.io/gitea/modules/repository"
2324
"code.gitea.io/gitea/modules/setting"
2425
"code.gitea.io/gitea/modules/timeutil"
@@ -216,13 +217,16 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo
216217
return nil, false
217218
}
218219

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

0 commit comments

Comments
 (0)