/
connection.go
212 lines (180 loc) · 4.76 KB
/
connection.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"encoding/pem"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/gorilla/websocket"
)
func (s *syncer) getClient(server string, serverCert string) (*http.Client, error) {
// Parse the server URL
u, err := url.ParseRequestURI(server)
if err != nil {
return nil, err
}
var transport *http.Transport
if u.Scheme == "http" {
// Basic transport for clear-text HTTP
transport = &http.Transport{
DisableKeepAlives: true,
}
} else if u.Scheme == "https" {
// Be picky on our cipher list
tlsConfig := &tls.Config{
MinVersion: tls.VersionTLS13,
}
// If provided, pin the certificate
if serverCert != "" {
certBlock, _ := pem.Decode([]byte(serverCert))
if certBlock == nil {
return nil, fmt.Errorf("Failed to load pinned certificate")
}
cert, err := x509.ParseCertificate(certBlock.Bytes)
if err != nil {
return nil, fmt.Errorf("Failed to parse pinned certificate: %v", err)
}
caCertPool := tlsConfig.RootCAs
if caCertPool == nil {
caCertPool = x509.NewCertPool()
}
caCertPool.AddCert(cert)
tlsConfig.RootCAs = caCertPool
}
transport = &http.Transport{
TLSClientConfig: tlsConfig,
DisableKeepAlives: true,
}
} else {
return nil, fmt.Errorf("Unsupported server URL: %s", server)
}
// Create the new HTTP client
client := http.Client{
Transport: transport,
}
return &client, nil
}
func (s *syncer) websocket(server string, path string) (*websocket.Conn, error) {
// Server-specific configuration
var srv *http.Client
var url string
if server == "askgod" {
srv = s.httpAskgod
url = fmt.Sprintf("%s/1.0%s", s.config.AskgodURL, path)
} else {
return nil, fmt.Errorf("Unknown server: %s", server)
}
if strings.HasPrefix(url, "https://") {
url = fmt.Sprintf("wss://%s", strings.TrimPrefix(url, "https://"))
} else {
url = fmt.Sprintf("ws://%s", strings.TrimPrefix(url, "http://"))
}
// Grab the http transport handler
httpTransport := srv.Transport.(*http.Transport)
// Setup a new websocket dialer based on it
dialer := websocket.Dialer{
TLSClientConfig: httpTransport.TLSClientConfig,
Proxy: httpTransport.Proxy,
}
// Establish the connection
conn, _, err := dialer.Dial(url, nil)
if err != nil {
return nil, err
}
return conn, err
}
type queryArgs struct {
discourseUser string
discourseKey string
}
func (s *syncer) queryStruct(server string, method string, path string, data interface{}, target interface{}, args *queryArgs) error {
var req *http.Request
var err error
// Server-specific configuration
var srv *http.Client
var url string
if server == "askgod" {
srv = s.httpAskgod
url = fmt.Sprintf("%s/1.0%s", s.config.AskgodURL, path)
} else if server == "discourse" {
srv = s.httpDiscourse
url = fmt.Sprintf("%s%s", s.config.DiscourseURL, path)
} else {
return fmt.Errorf("Unknown server: %s", server)
}
// Get a new HTTP request setup
if data != nil {
// Encode the provided data
buf := bytes.Buffer{}
err := json.NewEncoder(&buf).Encode(data)
if err != nil {
return err
}
// Some data to be sent along with the request
req, err = http.NewRequest(method, url, &buf)
if err != nil {
return err
}
// Set the encoding accordingly
req.Header.Set("Content-Type", "application/json")
// Handle authentication
if server == "discourse" {
if args != nil && args.discourseUser != "" {
req.Header.Set("Api-Username", args.discourseUser)
} else {
req.Header.Set("Api-Username", s.config.DiscourseAPIUser)
}
if args != nil && args.discourseKey != "" {
req.Header.Set("Api-Key", args.discourseKey)
} else {
req.Header.Set("Api-Key", s.config.DiscourseAPIKey)
}
}
} else {
// No data to be sent along with the request
req, err = http.NewRequest(method, url, nil)
if err != nil {
return err
}
// Handle authentication
if server == "discourse" {
if args != nil && args.discourseUser != "" {
req.Header.Set("Api-Username", args.discourseUser)
} else {
req.Header.Set("Api-Username", s.config.DiscourseAPIUser)
}
if args != nil && args.discourseKey != "" {
req.Header.Set("Api-Key", args.discourseKey)
} else {
req.Header.Set("Api-Key", s.config.DiscourseAPIKey)
}
}
}
// Send the request
resp, err := srv.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
content, err := ioutil.ReadAll(resp.Body)
if err == nil && string(content) != "" {
return fmt.Errorf("%s", strings.TrimSpace(string(content)))
}
return fmt.Errorf("%s: %s", url, resp.Status)
}
// Decode the response
if target != nil {
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&target)
if err != nil {
return err
}
}
return nil
}