-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
189 lines (153 loc) · 4.18 KB
/
http.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package http
import (
"bytes"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"time"
"github.com/gliderlabs/logspout/adapters/raw"
"github.com/gliderlabs/logspout/router"
)
func init() {
router.AdapterTransports.Register(new(httpTransport), "http")
router.AdapterTransports.Register(new(httpsTransport), "https")
// convenience adapters around raw adapter
router.AdapterFactories.Register(rawHTTPAdapter, "http")
router.AdapterFactories.Register(rawHTTPSAdapter, "https")
}
func rawHTTPAdapter(route *router.Route) (router.LogAdapter, error) {
route.Adapter = "raw+http"
return raw.NewRawAdapter(route)
}
func rawHTTPSAdapter(route *router.Route) (router.LogAdapter, error) {
route.Adapter = "raw+https"
return raw.NewRawAdapter(route)
}
type httpTransport int
type httpsTransport int
type httpConnection struct {
client *http.Client
url string
user string
pass string
}
func (c *httpConnection) Read(b []byte) (n int, err error) {
return 0, errors.New("Not implemented")
}
func (c *httpConnection) Write(b []byte) (n int, err error) {
req, err := http.NewRequest(http.MethodPost, c.url, bytes.NewReader(b))
if err != nil {
return 0, err
}
req.Header.Add("Content-Type", "application/json")
if c.user != "" {
req.SetBasicAuth(c.user, c.pass)
}
res, err := c.client.Do(req)
if err != nil {
return 0, err
}
io.Copy(ioutil.Discard, res.Body)
if res.StatusCode > 200 {
return 0, errors.New(fmt.Sprintf("HTTP endpoint returned status code %s", res.StatusCode))
} else {
return len(b), res.Body.Close()
}
}
func (c *httpConnection) Close() error {
return nil
}
func (c *httpConnection) LocalAddr() net.Addr {
return nil
}
func (c *httpConnection) RemoteAddr() net.Addr {
return nil
}
func (c *httpConnection) SetDeadline(t time.Time) error {
return errors.New("Not implemented")
}
func (c *httpConnection) SetReadDeadline(t time.Time) error {
return errors.New("Not implemented")
}
func (c *httpConnection) SetWriteDeadline(t time.Time) error {
return errors.New("Not implemented")
}
func (t *httpTransport) Dial(addr string, options map[string]string) (net.Conn, error) {
return Dial(addr, "http", options)
}
func (t *httpsTransport) Dial(addr string, options map[string]string) (net.Conn, error) {
return Dial(addr, "https", options)
}
func Dial(addr string, protocol string, options map[string]string) (net.Conn, error) {
client := getClient(options)
path, ok := options["http.path"]
if !ok {
path = "/"
} else if path[0] != '/' {
path = "/" + path
}
conn := &httpConnection{client, fmt.Sprintf("%s://%s%s", protocol, addr, path), options["http.user"], options["http.pass"]}
return conn, nil
}
func getClient(options map[string]string) *http.Client {
tlsConfig, err := getTLSConfig(options)
if err != nil {
log.Fatalf("Error during TLS handshake: %s\n", err.Error())
}
transport := &http.Transport{
TLSClientConfig: tlsConfig,
IdleConnTimeout: 1 * time.Hour,
Proxy: http.ProxyFromEnvironment,
}
return &http.Client{Transport: transport}
}
func getTLSClientCerts(options map[string]string) ([]tls.Certificate, error) {
certPath, certOk := options["http.cert"]
keyPath, keyOk := options["http.key"]
var certs []tls.Certificate
if !certOk && !keyOk {
return certs, nil
} else if !certOk && !keyOk {
fmt.Printf("Missing TLS client certificate or key")
return nil, errors.New("TLS client configuration error")
}
cert, err := tls.LoadX509KeyPair(certPath, keyPath)
if err != nil {
return certs, err
}
return []tls.Certificate{cert}, nil
}
func getRootCAs(options map[string]string) (*x509.CertPool, error) {
caPath, ok := options["http.ca"]
if !ok {
return nil, nil
}
caPool := x509.NewCertPool()
pem, err := ioutil.ReadFile(caPath)
if err != nil {
return nil, err
}
ok = caPool.AppendCertsFromPEM(pem)
if !ok {
return nil, errors.New("Error parsing CA pem: " + caPath)
}
return caPool, nil
}
func getTLSConfig(options map[string]string) (*tls.Config, error) {
certs, err := getTLSClientCerts(options)
if err != nil {
return nil, err
}
CAs, err := getRootCAs(options)
if err != nil {
return nil, err
}
conf := &tls.Config{Certificates: certs, RootCAs: CAs}
return conf, nil
}