Skip to content

Commit a27032f

Browse files
calmhAudriusButkevicius
authored andcommitted
cmd/strelaysrv: Don't patch the default HTTP client (fixes syncthing#4745)
1 parent 5e041dc commit a27032f

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

cmd/strelaysrv/main.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ var (
8686
pprofEnabled bool
8787
)
8888

89+
// httpClient is the HTTP client we use for outbound requests. It has a
90+
// timeout and may get further options set during initialization.
91+
var httpClient = &http.Client{
92+
Timeout: 30 * time.Second,
93+
}
94+
8995
func main() {
9096
log.SetFlags(log.Lshortfile | log.LstdFlags)
9197

@@ -129,14 +135,14 @@ func main() {
129135
if err != nil {
130136
log.Fatal(err)
131137
}
138+
132139
if laddr.IP != nil && !laddr.IP.IsUnspecified() {
140+
// We bind to a specific address. Our outgoing HTTP requests should
141+
// also come from that address.
133142
laddr.Port = 0
134-
transport, ok := http.DefaultTransport.(*http.Transport)
135-
if ok {
136-
transport.Dial = (&net.Dialer{
137-
Timeout: 30 * time.Second,
138-
LocalAddr: laddr,
139-
}).Dial
143+
boundDialer := &net.Dialer{LocalAddr: laddr}
144+
httpClient.Transport = &http.Transport{
145+
DialContext: boundDialer.DialContext,
140146
}
141147
}
142148

cmd/strelaysrv/pool.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"encoding/json"
88
"io/ioutil"
99
"log"
10-
"net/http"
1110
"net/url"
1211
"time"
1312
)
@@ -27,7 +26,7 @@ func poolHandler(pool string, uri *url.URL, mapping mapping) {
2726
uriCopy.String(),
2827
})
2928

30-
resp, err := http.Post(pool, "application/json", &b)
29+
resp, err := httpClient.Post(pool, "application/json", &b)
3130
if err != nil {
3231
log.Println("Error joining pool", pool, err)
3332
} else if resp.StatusCode == 500 {

0 commit comments

Comments
 (0)