...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package transport
16
17 import (
18 "context"
19 "net"
20 "net/http"
21 "strings"
22 "time"
23 )
24
25 type unixTransport struct{ *http.Transport }
26
27 func NewTransport(info TLSInfo, dialtimeoutd time.Duration) (*http.Transport, error) {
28 cfg, err := info.ClientConfig()
29 if err != nil {
30 return nil, err
31 }
32
33 t := &http.Transport{
34 Proxy: http.ProxyFromEnvironment,
35 DialContext: (&net.Dialer{
36 Timeout: dialtimeoutd,
37
38 KeepAlive: 30 * time.Second,
39 }).DialContext,
40
41 TLSHandshakeTimeout: 10 * time.Second,
42 TLSClientConfig: cfg,
43 }
44
45 dialer := &net.Dialer{
46 Timeout: dialtimeoutd,
47 KeepAlive: 30 * time.Second,
48 }
49
50 dialContext := func(ctx context.Context, net, addr string) (net.Conn, error) {
51 return dialer.DialContext(ctx, "unix", addr)
52 }
53 tu := &http.Transport{
54 Proxy: http.ProxyFromEnvironment,
55 DialContext: dialContext,
56 TLSHandshakeTimeout: 10 * time.Second,
57 TLSClientConfig: cfg,
58
59
60
61
62 IdleConnTimeout: time.Microsecond,
63 }
64 ut := &unixTransport{tu}
65
66 t.RegisterProtocol("unix", ut)
67 t.RegisterProtocol("unixs", ut)
68
69 return t, nil
70 }
71
72 func (urt *unixTransport) RoundTrip(req *http.Request) (*http.Response, error) {
73 url := *req.URL
74 req.URL = &url
75 req.URL.Scheme = strings.Replace(req.URL.Scheme, "unix", "http", 1)
76 return urt.Transport.RoundTrip(req)
77 }
78
View as plain text