@@ -12,11 +12,13 @@ import (
12
12
"net/http"
13
13
"net/url"
14
14
"strings"
15
+ "sync"
15
16
"time"
16
17
17
18
"code.gitea.io/gitea/models"
18
19
"code.gitea.io/gitea/modules/log"
19
20
"code.gitea.io/gitea/modules/setting"
21
+ "github.com/gobwas/glob"
20
22
"github.com/unknwon/com"
21
23
)
22
24
@@ -182,7 +184,36 @@ func DeliverHooks() {
182
184
}
183
185
}
184
186
185
- var webhookHTTPClient * http.Client
187
+ var (
188
+ webhookHTTPClient * http.Client
189
+ once sync.Once
190
+ hostMatchers []glob.Glob
191
+ )
192
+
193
+ func webhookProxy () func (req * http.Request ) (* url.URL , error ) {
194
+ if setting .Webhook .ProxyURL == "" {
195
+ return http .ProxyFromEnvironment
196
+ }
197
+
198
+ once .Do (func () {
199
+ for _ , h := range setting .Webhook .ProxyHosts {
200
+ if g , err := glob .Compile (h ); err == nil {
201
+ hostMatchers = append (hostMatchers , g )
202
+ } else {
203
+ log .Error ("glob.Compile %s failed: %v" , h , err )
204
+ }
205
+ }
206
+ })
207
+
208
+ return func (req * http.Request ) (* url.URL , error ) {
209
+ for _ , v := range hostMatchers {
210
+ if v .Match (req .URL .Host ) {
211
+ return http .ProxyURL (setting .Webhook .ProxyURLFixed )(req )
212
+ }
213
+ }
214
+ return http .ProxyFromEnvironment (req )
215
+ }
216
+ }
186
217
187
218
// InitDeliverHooks starts the hooks delivery thread
188
219
func InitDeliverHooks () {
@@ -191,15 +222,14 @@ func InitDeliverHooks() {
191
222
webhookHTTPClient = & http.Client {
192
223
Transport : & http.Transport {
193
224
TLSClientConfig : & tls.Config {InsecureSkipVerify : setting .Webhook .SkipTLSVerify },
194
- Proxy : http . ProxyFromEnvironment ,
225
+ Proxy : webhookProxy () ,
195
226
Dial : func (netw , addr string ) (net.Conn , error ) {
196
227
conn , err := net .DialTimeout (netw , addr , timeout )
197
228
if err != nil {
198
229
return nil , err
199
230
}
200
231
201
232
return conn , conn .SetDeadline (time .Now ().Add (timeout ))
202
-
203
233
},
204
234
},
205
235
}
0 commit comments