-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Add proxy settings and support for migration and webhook #16704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c1ac622
9aa2dd3
51db33b
7c6f205
803b7c1
264f963
c0e5bbc
634c613
6eca452
a076155
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,11 +9,15 @@ import ( | |
"bytes" | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/url" | ||
"os" | ||
"path" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"code.gitea.io/gitea/modules/proxy" | ||
) | ||
|
||
// GPGSettings represents the default GPG settings for this repository | ||
|
@@ -99,12 +103,12 @@ type CloneRepoOptions struct { | |
} | ||
|
||
// Clone clones original repository to target path. | ||
func Clone(from, to string, opts CloneRepoOptions) (err error) { | ||
func Clone(from, to string, opts CloneRepoOptions) error { | ||
return CloneWithContext(DefaultContext, from, to, opts) | ||
} | ||
|
||
// CloneWithContext clones original repository to target path. | ||
func CloneWithContext(ctx context.Context, from, to string, opts CloneRepoOptions) (err error) { | ||
func CloneWithContext(ctx context.Context, from, to string, opts CloneRepoOptions) error { | ||
cargs := make([]string, len(GlobalCommandArgs)) | ||
copy(cargs, GlobalCommandArgs) | ||
return CloneWithArgs(ctx, from, to, cargs, opts) | ||
|
@@ -146,8 +150,24 @@ func CloneWithArgs(ctx context.Context, from, to string, args []string, opts Clo | |
opts.Timeout = -1 | ||
} | ||
|
||
_, err = cmd.RunTimeout(opts.Timeout) | ||
return err | ||
var 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())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
} | ||
|
||
var stderr = new(bytes.Buffer) | ||
if err = cmd.RunWithContext(&RunContext{ | ||
Timeout: opts.Timeout, | ||
Env: envs, | ||
Stdout: io.Discard, | ||
Stderr: stderr, | ||
}); err != nil { | ||
return ConcatenateError(err, stderr.String()) | ||
} | ||
return nil | ||
} | ||
|
||
// PullRemoteOptions options when pull from remote | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2021 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package proxy | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"sync" | ||
|
||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/setting" | ||
|
||
"github.com/gobwas/glob" | ||
) | ||
|
||
var ( | ||
once sync.Once | ||
hostMatchers []glob.Glob | ||
) | ||
|
||
// GetProxyURL returns proxy url | ||
func GetProxyURL() string { | ||
if !setting.Proxy.Enabled { | ||
return "" | ||
} | ||
|
||
if setting.Proxy.ProxyURL == "" { | ||
if os.Getenv("http_proxy") != "" { | ||
return os.Getenv("http_proxy") | ||
} | ||
return os.Getenv("https_proxy") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We cannot use that, because we will set it as envs of git clone. |
||
} | ||
return setting.Proxy.ProxyURL | ||
} | ||
|
||
// Match return true if url needs to be proxied | ||
func Match(u string) bool { | ||
if !setting.Proxy.Enabled { | ||
return false | ||
} | ||
|
||
// enforce do once | ||
Proxy() | ||
|
||
for _, v := range hostMatchers { | ||
if v.Match(u) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// Proxy returns the system proxy | ||
func Proxy() func(req *http.Request) (*url.URL, error) { | ||
if !setting.Proxy.Enabled { | ||
return nil | ||
} | ||
if setting.Proxy.ProxyURL == "" { | ||
return http.ProxyFromEnvironment | ||
} | ||
|
||
once.Do(func() { | ||
for _, h := range setting.Proxy.ProxyHosts { | ||
if g, err := glob.Compile(h); err == nil { | ||
hostMatchers = append(hostMatchers, g) | ||
} else { | ||
log.Error("glob.Compile %s failed: %v", h, err) | ||
} | ||
} | ||
}) | ||
|
||
return func(req *http.Request) (*url.URL, error) { | ||
for _, v := range hostMatchers { | ||
if v.Match(req.URL.Host) { | ||
return http.ProxyURL(setting.Proxy.ProxyURLFixed)(req) | ||
} | ||
} | ||
return http.ProxyFromEnvironment(req) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2021 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package setting | ||
|
||
import ( | ||
"net/url" | ||
|
||
"code.gitea.io/gitea/modules/log" | ||
) | ||
|
||
var ( | ||
// Proxy settings | ||
Proxy = struct { | ||
Enabled bool | ||
ProxyURL string | ||
ProxyURLFixed *url.URL | ||
ProxyHosts []string | ||
}{ | ||
Enabled: false, | ||
ProxyURL: "", | ||
ProxyHosts: []string{}, | ||
} | ||
) | ||
|
||
func newProxyService() { | ||
sec := Cfg.Section("proxy") | ||
Proxy.Enabled = sec.Key("PROXY_ENABLED").MustBool(false) | ||
Proxy.ProxyURL = sec.Key("PROXY_URL").MustString("") | ||
if Proxy.ProxyURL != "" { | ||
var err error | ||
Proxy.ProxyURLFixed, err = url.Parse(Proxy.ProxyURL) | ||
if err != nil { | ||
log.Error("Global PROXY_URL is not valid") | ||
Proxy.ProxyURL = "" | ||
} | ||
} | ||
Proxy.ProxyHosts = sec.Key("PROXY_HOSTS").Strings(",") | ||
} |
Uh oh!
There was an error while loading. Please reload this page.