Skip to content

Commit 2b0b629

Browse files
committed
Webhook support custom proxy
1 parent d9be82b commit 2b0b629

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

custom/conf/app.ini.sample

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,10 @@ DELIVER_TIMEOUT = 5
511511
SKIP_TLS_VERIFY = false
512512
; Number of history information in each page
513513
PAGING_NUM = 10
514+
; Proxy server URL, support http://, https//, socks://, blank will follow environment http_proxy/https_proxy
515+
PROXY_URL =
516+
; All request domains needed to proxy,
517+
PROXY_DOMAINS =
514518

515519
[mailer]
516520
ENABLED = false

modules/setting/webhook.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
package setting
66

7+
import (
8+
"net/url"
9+
10+
"code.gitea.io/gitea/modules/log"
11+
)
12+
713
var (
814
// Webhook settings
915
Webhook = struct {
@@ -12,11 +18,16 @@ var (
1218
SkipTLSVerify bool
1319
Types []string
1420
PagingNum int
21+
ProxyURL string
22+
ProxyURLFixed *url.URL
23+
ProxyHosts []string
1524
}{
1625
QueueLength: 1000,
1726
DeliverTimeout: 5,
1827
SkipTLSVerify: false,
1928
PagingNum: 10,
29+
ProxyURL: "",
30+
ProxyHosts: []string{},
2031
}
2132
)
2233

@@ -27,4 +38,14 @@ func newWebhookService() {
2738
Webhook.SkipTLSVerify = sec.Key("SKIP_TLS_VERIFY").MustBool()
2839
Webhook.Types = []string{"gitea", "gogs", "slack", "discord", "dingtalk", "telegram", "msteams"}
2940
Webhook.PagingNum = sec.Key("PAGING_NUM").MustInt(10)
41+
Webhook.ProxyURL = sec.Key("PROXY_URL").MustString("")
42+
if Webhook.ProxyURL != "" {
43+
var err error
44+
Webhook.ProxyURLFixed, err = url.Parse(Webhook.ProxyURL)
45+
if err != nil {
46+
log.Error("Webhook PROXY_URL is not valid")
47+
Webhook.ProxyURL = ""
48+
}
49+
}
50+
Webhook.ProxyHosts = sec.Key("PROXY_HOSTS").Strings(",")
3051
}

modules/webhook/deliver.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,22 +184,36 @@ func DeliverHooks() {
184184

185185
var webhookHTTPClient *http.Client
186186

187+
func webhookProxy() func(req *http.Request) (*url.URL, error) {
188+
if setting.Webhook.ProxyURL == "" {
189+
return http.ProxyFromEnvironment
190+
}
191+
192+
return func(req *http.Request) (*url.URL, error) {
193+
for _, v := range setting.Webhook.ProxyHosts {
194+
if strings.EqualFold(v, req.URL.Host) {
195+
return http.ProxyURL(setting.Webhook.ProxyURLFixed)(req)
196+
}
197+
}
198+
return http.ProxyFromEnvironment(req)
199+
}
200+
}
201+
187202
// InitDeliverHooks starts the hooks delivery thread
188203
func InitDeliverHooks() {
189204
timeout := time.Duration(setting.Webhook.DeliverTimeout) * time.Second
190205

191206
webhookHTTPClient = &http.Client{
192207
Transport: &http.Transport{
193208
TLSClientConfig: &tls.Config{InsecureSkipVerify: setting.Webhook.SkipTLSVerify},
194-
Proxy: http.ProxyFromEnvironment,
209+
Proxy: webhookProxy(),
195210
Dial: func(netw, addr string) (net.Conn, error) {
196211
conn, err := net.DialTimeout(netw, addr, timeout)
197212
if err != nil {
198213
return nil, err
199214
}
200215

201216
return conn, conn.SetDeadline(time.Now().Add(timeout))
202-
203217
},
204218
},
205219
}

0 commit comments

Comments
 (0)