1
// Copyright 2021 The Gitea Authors. All rights reserved.
2
// SPDX-License-Identifier: MIT
13
"code.gitea.io/gitea/modules/log"
14
"code.gitea.io/gitea/modules/setting"
16
"github.com/gobwas/glob"
21
hostMatchers []glob.Glob
24
// GetProxyURL returns proxy url
25
func GetProxyURL() string {
26
if !setting.Proxy.Enabled {
30
if setting.Proxy.ProxyURL == "" {
31
if os.Getenv("http_proxy") != "" {
32
return os.Getenv("http_proxy")
34
return os.Getenv("https_proxy")
36
return setting.Proxy.ProxyURL
39
// Match return true if url needs to be proxied
40
func Match(u string) bool {
41
if !setting.Proxy.Enabled {
48
for _, v := range hostMatchers {
56
// Proxy returns the system proxy
57
func Proxy() func(req *http.Request) (*url.URL, error) {
58
if !setting.Proxy.Enabled {
59
return func(req *http.Request) (*url.URL, error) {
63
if setting.Proxy.ProxyURL == "" {
64
return http.ProxyFromEnvironment
68
for _, h := range setting.Proxy.ProxyHosts {
69
if g, err := glob.Compile(h); err == nil {
70
hostMatchers = append(hostMatchers, g)
72
log.Error("glob.Compile %s failed: %v", h, err)
77
return func(req *http.Request) (*url.URL, error) {
78
for _, v := range hostMatchers {
79
if v.Match(req.URL.Host) {
80
return http.ProxyURL(setting.Proxy.ProxyURLFixed)(req)
83
return http.ProxyFromEnvironment(req)
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 {
91
if strings.EqualFold(u.Scheme, "http") || strings.EqualFold(u.Scheme, "https") {
93
envs = append(envs, "https_proxy="+GetProxyURL())