diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2019-11-09 05:25:53 +0800 |
---|---|---|
committer | techknowlogick <techknowlogick@gitea.io> | 2019-11-08 16:25:53 -0500 |
commit | 55bdc9aa38177f75fdae4cb96e98bf465d7ecb2a (patch) | |
tree | 621333fde9de8cec79dac36bcaced0e4b499dfb3 /modules/webhook/deliver.go | |
parent | 016a5d0438e551d4630819683dd6dc4fccb0cb51 (diff) | |
download | gitea-55bdc9aa38177f75fdae4cb96e98bf465d7ecb2a.tar.gz gitea-55bdc9aa38177f75fdae4cb96e98bf465d7ecb2a.zip |
Webhook support custom proxy (#8760)
* Webhook support custom proxy
* Add glob support on webhook proxy host rules
* fix app.ini.sample
* improve code and app.ini.sample
* update cheetsheet about added webhook options
Diffstat (limited to 'modules/webhook/deliver.go')
-rw-r--r-- | modules/webhook/deliver.go | 36 |
1 files changed, 33 insertions, 3 deletions
diff --git a/modules/webhook/deliver.go b/modules/webhook/deliver.go index 54f20171fa..b262505cea 100644 --- a/modules/webhook/deliver.go +++ b/modules/webhook/deliver.go @@ -12,11 +12,13 @@ import ( "net/http" "net/url" "strings" + "sync" "time" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" + "github.com/gobwas/glob" "github.com/unknwon/com" ) @@ -182,7 +184,36 @@ func DeliverHooks() { } } -var webhookHTTPClient *http.Client +var ( + webhookHTTPClient *http.Client + once sync.Once + hostMatchers []glob.Glob +) + +func webhookProxy() func(req *http.Request) (*url.URL, error) { + if setting.Webhook.ProxyURL == "" { + return http.ProxyFromEnvironment + } + + once.Do(func() { + for _, h := range setting.Webhook.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.Webhook.ProxyURLFixed)(req) + } + } + return http.ProxyFromEnvironment(req) + } +} // InitDeliverHooks starts the hooks delivery thread func InitDeliverHooks() { @@ -191,7 +222,7 @@ func InitDeliverHooks() { webhookHTTPClient = &http.Client{ Transport: &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: setting.Webhook.SkipTLSVerify}, - Proxy: http.ProxyFromEnvironment, + Proxy: webhookProxy(), Dial: func(netw, addr string) (net.Conn, error) { conn, err := net.DialTimeout(netw, addr, timeout) if err != nil { @@ -199,7 +230,6 @@ func InitDeliverHooks() { } return conn, conn.SetDeadline(time.Now().Add(timeout)) - }, }, } |