forked from Psiphon-Labs/psiphon-tunnel-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webServer.go
330 lines (271 loc) · 8.74 KB
/
webServer.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* Copyright (c) 2016, Psiphon Inc.
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package server
import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
golanglog "log"
"net"
"net/http"
"sync"
"time"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
)
const WEB_SERVER_IO_TIMEOUT = 10 * time.Second
type webServer struct {
support *SupportServices
tunnelServer *TunnelServer
serveMux *http.ServeMux
}
// RunWebServer runs a web server which supports tunneled and untunneled
// Psiphon API requests.
//
// The HTTP request handlers are light wrappers around the base Psiphon
// API request handlers from the SSH API transport. The SSH API transport
// is preferred by new clients; however the web API transport is still
// required for untunneled final status requests. The web API transport
// may be retired once untunneled final status requests are made obsolete
// (e.g., by server-side bytes transferred stats, by client-side local
// storage of stats for retry, or some other future development).
//
// The API is compatible with all tunnel-core clients but not backwards
// compatible with older clients.
//
func RunWebServer(
support *SupportServices,
shutdownBroadcast <-chan struct{}) error {
webServer := &webServer{
support: support,
}
serveMux := http.NewServeMux()
serveMux.HandleFunc("/handshake", webServer.handshakeHandler)
serveMux.HandleFunc("/connected", webServer.connectedHandler)
serveMux.HandleFunc("/status", webServer.statusHandler)
serveMux.HandleFunc("/client_verification", webServer.clientVerificationHandler)
certificate, err := tls.X509KeyPair(
[]byte(support.Config.WebServerCertificate),
[]byte(support.Config.WebServerPrivateKey))
if err != nil {
return common.ContextError(err)
}
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{certificate},
}
// TODO: inherits global log config?
logWriter := NewLogWriter()
defer logWriter.Close()
// Note: WriteTimeout includes time awaiting request, as per:
// https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts
server := &HTTPSServer{
http.Server{
MaxHeaderBytes: MAX_API_PARAMS_SIZE,
Handler: serveMux,
TLSConfig: tlsConfig,
ReadTimeout: WEB_SERVER_IO_TIMEOUT,
WriteTimeout: WEB_SERVER_IO_TIMEOUT,
ErrorLog: golanglog.New(logWriter, "", 0),
// Disable auto HTTP/2 (https://golang.org/doc/go1.6)
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
},
}
localAddress := fmt.Sprintf("%s:%d",
support.Config.ServerIPAddress, support.Config.WebServerPort)
listener, err := net.Listen("tcp", localAddress)
if err != nil {
return common.ContextError(err)
}
log.WithContextFields(
LogFields{"localAddress": localAddress}).Info("starting")
err = nil
errors := make(chan error)
waitGroup := new(sync.WaitGroup)
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
// Note: will be interrupted by listener.Close()
err := server.ServeTLS(listener)
// Can't check for the exact error that Close() will cause in Accept(),
// (see: https://code.google.com/p/go/issues/detail?id=4373). So using an
// explicit stop signal to stop gracefully.
select {
case <-shutdownBroadcast:
default:
if err != nil {
select {
case errors <- common.ContextError(err):
default:
}
}
}
log.WithContextFields(
LogFields{"localAddress": localAddress}).Info("stopped")
}()
select {
case <-shutdownBroadcast:
case err = <-errors:
}
listener.Close()
waitGroup.Wait()
log.WithContextFields(
LogFields{"localAddress": localAddress}).Info("exiting")
return err
}
// convertHTTPRequestToAPIRequest converts the HTTP request query
// parameters and request body to the JSON object import format
// expected by the API request handlers.
func convertHTTPRequestToAPIRequest(
w http.ResponseWriter,
r *http.Request,
requestBodyName string) (requestJSONObject, error) {
params := make(requestJSONObject)
for name, values := range r.URL.Query() {
for _, value := range values {
// Note: multiple values per name are ignored
// TODO: faster lookup?
isArray := false
for _, paramSpec := range baseRequestParams {
if paramSpec.name == name {
isArray = (paramSpec.flags&requestParamArray != 0)
break
}
}
if isArray {
// Special case: a JSON encoded array
var arrayValue []interface{}
err := json.Unmarshal([]byte(value), &arrayValue)
if err != nil {
return nil, common.ContextError(err)
}
params[name] = arrayValue
} else {
// All other query parameters are simple strings
params[name] = value
}
break
}
}
if requestBodyName != "" {
r.Body = http.MaxBytesReader(w, r.Body, MAX_API_PARAMS_SIZE)
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, common.ContextError(err)
}
var bodyParams map[string]interface{}
if len(body) != 0 {
err = json.Unmarshal(body, &bodyParams)
if err != nil {
return nil, common.ContextError(err)
}
params[requestBodyName] = bodyParams
}
}
return params, nil
}
func (webServer *webServer) lookupGeoIPData(params requestJSONObject) GeoIPData {
clientSessionID, err := getStringRequestParam(params, "client_session_id")
if err != nil {
// Not all clients send this parameter
return NewGeoIPData()
}
return webServer.support.GeoIPService.GetSessionCache(clientSessionID)
}
func (webServer *webServer) handshakeHandler(w http.ResponseWriter, r *http.Request) {
params, err := convertHTTPRequestToAPIRequest(w, r, "")
var responsePayload []byte
if err == nil {
responsePayload, err = dispatchAPIRequestHandler(
webServer.support,
protocol.PSIPHON_WEB_API_PROTOCOL,
webServer.lookupGeoIPData(params),
protocol.PSIPHON_API_HANDSHAKE_REQUEST_NAME,
params)
}
if err != nil {
log.WithContextFields(LogFields{"error": err}).Warning("failed")
w.WriteHeader(http.StatusNotFound)
return
}
// The legacy response format is newline seperated, name prefixed values.
// Within that legacy format, the modern JSON response (containing all the
// legacy response values and more) is single value with a "Config:" prefix.
// This response uses the legacy format but omits all but the JSON value.
responseBody := append([]byte("Config: "), responsePayload...)
w.WriteHeader(http.StatusOK)
w.Write(responseBody)
}
func (webServer *webServer) connectedHandler(w http.ResponseWriter, r *http.Request) {
params, err := convertHTTPRequestToAPIRequest(w, r, "")
var responsePayload []byte
if err == nil {
responsePayload, err = dispatchAPIRequestHandler(
webServer.support,
protocol.PSIPHON_WEB_API_PROTOCOL,
webServer.lookupGeoIPData(params),
protocol.PSIPHON_API_CONNECTED_REQUEST_NAME,
params)
}
if err != nil {
log.WithContextFields(LogFields{"error": err}).Warning("failed")
w.WriteHeader(http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
w.Write(responsePayload)
}
func (webServer *webServer) statusHandler(w http.ResponseWriter, r *http.Request) {
params, err := convertHTTPRequestToAPIRequest(w, r, "statusData")
var responsePayload []byte
if err == nil {
responsePayload, err = dispatchAPIRequestHandler(
webServer.support,
protocol.PSIPHON_WEB_API_PROTOCOL,
webServer.lookupGeoIPData(params),
protocol.PSIPHON_API_STATUS_REQUEST_NAME,
params)
}
if err != nil {
log.WithContextFields(LogFields{"error": err}).Warning("failed")
w.WriteHeader(http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
w.Write(responsePayload)
}
func (webServer *webServer) clientVerificationHandler(w http.ResponseWriter, r *http.Request) {
params, err := convertHTTPRequestToAPIRequest(w, r, "verificationData")
var responsePayload []byte
if err == nil {
responsePayload, err = dispatchAPIRequestHandler(
webServer.support,
protocol.PSIPHON_WEB_API_PROTOCOL,
webServer.lookupGeoIPData(params),
protocol.PSIPHON_API_CLIENT_VERIFICATION_REQUEST_NAME,
params)
}
if err != nil {
log.WithContextFields(LogFields{"error": err}).Warning("failed")
w.WriteHeader(http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
w.Write(responsePayload)
}