forked from docker-archive/classicswarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
138 lines (118 loc) · 2.78 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package api
import (
"crypto/tls"
"errors"
"fmt"
"io"
"net"
"net/http"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/docker/swarm/cluster"
)
func newClientAndScheme(tlsConfig *tls.Config) (*http.Client, string) {
if tlsConfig != nil {
return &http.Client{Transport: &http.Transport{TLSClientConfig: tlsConfig}}, "https"
}
return &http.Client{}, "http"
}
func getContainerFromVars(c *context, vars map[string]string) (*cluster.Container, error) {
if name, ok := vars["name"]; ok {
if container := c.cluster.Container(name); container != nil {
return container, nil
}
return nil, fmt.Errorf("No such container: %s", name)
}
if ID, ok := vars["execid"]; ok {
for _, container := range c.cluster.Containers() {
for _, execID := range container.Info.ExecIDs {
if ID == execID {
return container, nil
}
}
}
return nil, fmt.Errorf("Exec %s not found", ID)
}
return nil, errors.New("Not found")
}
// from https://github.com/golang/go/blob/master/src/net/http/httputil/reverseproxy.go#L82
func copyHeader(dst, src http.Header) {
for k, vv := range src {
for _, v := range vv {
dst.Add(k, v)
}
}
}
// prevents leak with https
func closeIdleConnections(client *http.Client) {
if tr, ok := client.Transport.(*http.Transport); ok {
tr.CloseIdleConnections()
}
}
func proxy(tlsConfig *tls.Config, addr string, w http.ResponseWriter, r *http.Request) error {
// Use a new client for each request
client, scheme := newClientAndScheme(tlsConfig)
// RequestURI may not be sent to client
r.RequestURI = ""
r.URL.Scheme = scheme
r.URL.Host = addr
log.WithFields(log.Fields{"method": r.Method, "url": r.URL}).Debug("Proxy request")
resp, err := client.Do(r)
if err != nil {
return err
}
copyHeader(w.Header(), resp.Header)
w.WriteHeader(resp.StatusCode)
io.Copy(NewWriteFlusher(w), resp.Body)
// cleanup
resp.Body.Close()
closeIdleConnections(client)
return nil
}
func hijack(tlsConfig *tls.Config, addr string, w http.ResponseWriter, r *http.Request) error {
if parts := strings.SplitN(addr, "://", 2); len(parts) == 2 {
addr = parts[1]
}
log.WithField("addr", addr).Debug("Proxy hijack request")
var (
d net.Conn
err error
)
if tlsConfig != nil {
d, err = tls.Dial("tcp", addr, tlsConfig)
} else {
d, err = net.Dial("tcp", addr)
}
if err != nil {
return err
}
hj, ok := w.(http.Hijacker)
if !ok {
return err
}
nc, _, err := hj.Hijack()
if err != nil {
return err
}
defer nc.Close()
defer d.Close()
err = r.Write(d)
if err != nil {
return err
}
errc := make(chan error, 2)
cp := func(dst io.Writer, src io.Reader) {
_, err := io.Copy(dst, src)
if conn, ok := dst.(interface {
CloseWrite() error
}); ok {
conn.CloseWrite()
}
errc <- err
}
go cp(d, nc)
go cp(nc, d)
<-errc
<-errc
return nil
}