-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfroxy.go
561 lines (481 loc) · 12.4 KB
/
froxy.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
// Froxy - HTTP over SSH proxy
//
// Copyright (C) 2019 and up by Alexander Pevzner ([email protected])
// See LICENSE for license terms and conditions
//
// Froxy proxy instance
package main
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"strings"
"sync"
"sync/atomic"
"github.com/alexpevzner/froxy/internal/pages"
"github.com/alexpevzner/froxy/internal/sysdep"
)
//
// froxy instance
//
type Froxy struct {
*Env // Common environment
*Ebus // Event bus
*KeySet // Key set
// Connection state
connStateLock sync.Mutex // Access lock
connState ConnState // Current state
connStateInfo string // Info string
// Statistic counters
Counters Counters // Collection of statistic counters
// Froxy parts
router *Router // Request router
webapi *WebAPI // JS API handler
sysNotifier *sysdep.SysEventNotifier // System events notifier
connMan *ConnMan // TCP connections manager
localhosts map[string]struct{} // Hosts considered local
localport string // Local port as string
listener net.Listener // TCP listener
httpSrv *http.Server // Local HTTP server instance
// Transports
sshTransport *SSHTransport // SSH transport
directTransport *DirectTransport // Direct transport
ftpProxy *FTPProxy // FTP-over-http proxy
}
// ----- Connection state -----
//
// Connection states
//
type ConnState int
const (
ConnNotConfigured = ConnState(iota)
ConnTrying
ConnEstablished
)
//
// ConnState -> ("name", "default info string")
//
func (s ConnState) Strings() (string, string) {
switch s {
case ConnNotConfigured:
return "noconfig", "server not configured"
case ConnTrying:
return "trying", "trying..."
case ConnEstablished:
return "established", "connected to the server"
}
panic("internal error")
}
//
// Get connection state
//
func (froxy *Froxy) GetConnState() (state ConnState, info string) {
froxy.connStateLock.Lock()
state = froxy.connState
info = froxy.connStateInfo
froxy.connStateLock.Unlock()
return
}
//
// Set connection state
//
func (froxy *Froxy) SetConnState(state ConnState, info string) {
froxy.connStateLock.Lock()
if froxy.connState != state || froxy.connStateInfo != info {
froxy.connState = state
froxy.connStateInfo = info
froxy.Raise(EventConnStateChanged)
}
froxy.connStateLock.Unlock()
}
// ----- Connection management -----
//
// Set server parameters
//
func (froxy *Froxy) SetServerParams(s ServerParams) {
froxy.Env.SetServerParams(s)
froxy.sshTransport.Reconnect(s)
}
// ----- Statistics counters -----
//
// Add value to the statistics counter
//
func (froxy *Froxy) AddCounter(cnt *int32, val int32) {
atomic.AddInt32(cnt, val)
froxy.Raise(EventCountersChanged)
}
//
// Increment the statistics counter
//
func (froxy *Froxy) IncCounter(cnt *int32) {
froxy.AddCounter(cnt, 1)
}
//
// Decrement the statistics counter
//
func (froxy *Froxy) DecCounter(cnt *int32) {
froxy.AddCounter(cnt, -1)
}
// ----- Proxying regular HTTP requests (GET/PUT/HEAD etc) -----
//
// Regular HTTP request handler
//
func (froxy *Froxy) handleRegularHttp(
w http.ResponseWriter,
r *http.Request,
transport Transport) {
froxy.Debug("%s %s %s", r.Method, r.URL, r.Proto)
// Strip hop-by-hop headers. Preserve Upgrade header, if any
upgrade := httpRemoveHopByHopHeaders(r.Header)
if upgrade {
// FIXME
//
// Protocol upgrade is hard to implement with Go < 1.12.
// Starting from Go 1.12 http.Response.Body implements
// io.Writer interface, so protocol upgrade becomes simple
// task.
//
// As for now, we explicitly reject upgrade requests.
// Actually it's not a big problem, because browsers
// implement websockets by calling proxy's CONNECT method
// rather that GET with upgrade
froxy.httpError(w, http.StatusServiceUnavailable,
errors.New("Protocol upgrade is not implemented"))
}
// Perform round-trip
resp, err := transport.RoundTrip(r)
if err != nil {
froxy.httpError(w, http.StatusServiceUnavailable, err)
return
}
httpRemoveHopByHopHeaders(resp.Header)
// Finish response, unless protocol is upgraded
if resp.StatusCode != http.StatusSwitchingProtocols {
froxy.returnHttpResponse(w, resp)
return
}
// Handle protocol switch
// TODO
}
//
// Return HTTP response back to the client
//
func (froxy *Froxy) returnHttpResponse(w http.ResponseWriter, resp *http.Response) {
httpCopyHeaders(w.Header(), resp.Header)
w.WriteHeader(resp.StatusCode)
if resp.Body != nil {
io.Copy(w, resp.Body)
resp.Body.Close()
}
}
// ----- Proxying CONNECT request -----
//
// HTTP CONNECT handler
//
func (froxy *Froxy) handleConnect(
w http.ResponseWriter,
r *http.Request,
transport Transport) {
froxy.Debug("%s %s %s", r.Method, r.Host, r.Proto)
dest_conn, err := transport.Dial("tcp", r.Host)
if err != nil {
froxy.httpError(w, http.StatusServiceUnavailable, err)
return
}
hijacker, ok := w.(http.Hijacker)
if !ok {
froxy.httpError(w, http.StatusInternalServerError,
errors.New("Hijacking not supported"))
return
}
w.WriteHeader(http.StatusOK)
client_conn, _, err := hijacker.Hijack()
if err != nil {
froxy.httpError(w, http.StatusServiceUnavailable, err)
return
}
ioTransferData(froxy.Env, client_conn, dest_conn)
}
// ----- Handling requests to Froxy itself -----
//
// Handle local request
//
func (froxy *Froxy) handleLocalRequest(w http.ResponseWriter, r *http.Request) {
// Handle webapi requests
if strings.HasPrefix(r.URL.Path, "/api/") {
froxy.webapi.ServeHTTP(w, r)
return
}
// Handle requests to Froxy static pages
froxy.Debug("%s %s %s", r.Method, r.URL, r.Proto)
httpNoCache(w)
if r.URL.Path == "/" {
//
// Froxy home page is not very informative, so
// it's better to redirect user to the last visited
// page, if it is known, or to the configuration page
// otherwise
//
url := "/conf/"
if c, err := r.Cookie(COOKIE_LAST_VISITED_PAGE); err == nil {
url = c.Value
}
http.Redirect(w, r, url, http.StatusFound)
return
}
w = &ResponseWriterWithHooks{
ResponseWriter: w,
OnError: froxy.httpOnError,
OnSuccess: func(w http.ResponseWriter, status int) {
froxy.httpOnSuccess(w, r, status)
},
}
// We allow static content to be loaded from any
// origin. This allows CSS to be loaded when we
// substitute a normal response with the
// error page
w.Header().Set("Access-Control-Allow-Origin", "*")
pages.FileServer.ServeHTTP(w, r)
}
// ----- HTTP response generation helpers -----
//
// Format body of error response
//
func (froxy *Froxy) httpFormatError(status int, err error) (
contentType string, content []byte) {
// Fetch HTML error template
var html []byte
if file, err := pages.AssetFS.Open("error/index.html"); err == nil {
html, err = ioutil.ReadAll(file)
file.Close()
if err != nil {
html = nil
}
}
// If no html, format simple text message
if html == nil {
s := fmt.Sprintf("%d %s\n", status, http.StatusText(status))
if err != nil {
s += fmt.Sprintf("%s\n", err)
}
contentType = "text/plain; charset=utf-8"
content = []byte(s)
return
}
// Substitute error information into HTML template
contentType = "text/html; charset=utf-8"
content = []byte(os.Expand(string(html), func(name string) string {
switch name {
case "ERROR":
return http.StatusText(status)
case "STATUS":
return fmt.Sprintf("%d", status)
case "MESSAGE":
if err == nil {
return ""
} else {
return err.Error()
}
}
return ""
}))
return
}
//
// Canonicalize URLs, embedded into HTML document, by replacing
// relative links with absolute
//
func (froxy *Froxy) httpCanonicalizeURLs(input []byte) []byte {
return bytes.Replace(
input,
[]byte(`href="/`),
[]byte(`href="`+froxy.BaseURL()),
-1,
)
}
//
// Reply with HTTP error
//
func (froxy *Froxy) httpError(w http.ResponseWriter, status int, err error) {
contentType, content := froxy.httpFormatError(status, err)
w.Header().Set("Content-Type", contentType)
httpNoCache(w)
w.WriteHeader(status)
content = froxy.httpCanonicalizeURLs(content)
w.Write(content)
}
//
// ResponseWriterWithHooks.OnError hook
//
func (froxy *Froxy) httpOnError(w http.ResponseWriter, status int) []byte {
contentType, content := froxy.httpFormatError(status, nil)
w.Header().Set("Content-Type", contentType)
httpNoCache(w)
return content
}
//
// ResponseWriterWithHooks.OnSuccess hook
//
func (froxy *Froxy) httpOnSuccess(w http.ResponseWriter,
r *http.Request, status int) {
path := r.URL.Path
if path != "/" && strings.HasSuffix(path, "/") {
http.SetCookie(w, &http.Cookie{
Name: COOKIE_LAST_VISITED_PAGE,
Value: r.URL.Path,
Path: "/",
MaxAge: 365 * 24 * 60 * 60,
})
}
}
// ----- HTTP requests handling -----
//
// handle HTTP request. Provides multiplexing between regular request
// and CONNECT request handlers
//
func (froxy *Froxy) httpHandler(w http.ResponseWriter, r *http.Request) {
// Normalize hostname
host, port := NetSplitHostPort(strings.ToLower(r.Host), "")
// Check for request to Froxy itself
if port == froxy.localport {
_, local := froxy.localhosts[host]
if local {
froxy.handleLocalRequest(w, r)
return
}
}
// Check routing
rt := froxy.router.Route(host)
// Update counters
froxy.IncCounter(&froxy.Counters.HTTPRqReceived)
froxy.IncCounter(&froxy.Counters.HTTPRqPending)
defer froxy.DecCounter(&froxy.Counters.HTTPRqPending)
// Choose transport
var transport Transport
switch rt {
case RouterBypass:
froxy.IncCounter(&froxy.Counters.HTTPRqDirect)
transport = froxy.directTransport
case RouterForward:
froxy.IncCounter(&froxy.Counters.HTTPRqForwarded)
transport = froxy.sshTransport
case RouterBlock:
froxy.IncCounter(&froxy.Counters.HTTPRqBlocked)
froxy.httpError(w, http.StatusForbidden, ErrSiteBlocked)
return
default:
panic("internal error")
}
// Handle request
switch {
case r.URL.Scheme == "ftp":
froxy.Debug("%s %s %s", r.Method, r.URL, r.Proto)
froxy.ftpProxy.Handle(w, r, transport)
case r.Method == http.MethodConnect:
froxy.handleConnect(w, r, transport)
default:
froxy.handleRegularHttp(w, r, transport)
}
}
// ----- Events handling -----
//
// Event monitoring goroutine
//
func (froxy *Froxy) eventGoroutine() {
events := froxy.Sub()
for {
e := <-events
switch e {
case EventShutdownRequested:
froxy.Debug("Shutdown requested. Exiting...")
os.Exit(0)
}
}
}
//
// sysdep.SysEventNotifier callback
//
func (froxy *Froxy) sysEventCallback(se sysdep.SysEvent) {
switch se {
case sysdep.SysEventShutdown:
froxy.Info("Shutdown requested")
froxy.Raise(EventShutdownRequested)
case sysdep.SysEventIpAddrChanged:
froxy.Info("IP addresses changed")
froxy.Raise(EventIpAddrChanged)
}
}
// ----- Miscellaneous helpers -----
//
// Get Froxy base URL (i.e., "http://localhost:8888/")
//
func (froxy *Froxy) BaseURL() string {
return "http://" + froxy.httpSrv.Addr + "/"
}
// ----- Froxy initialization -----
//
// Create a Froxy instance
//
func NewFroxy(env *Env, port int) (*Froxy, error) {
// Create Froxy structure
froxy := &Froxy{
Env: env,
Ebus: NewEbus(),
KeySet: NewKeySet(env),
localhosts: make(map[string]struct{}),
}
froxy.webapi = NewWebAPI(froxy)
froxy.router = NewRouter(froxy)
froxy.sysNotifier = sysdep.NewSysEventNotifier(froxy.sysEventCallback)
// Populate table of local host names
for _, h := range []string{
"localhost",
"127.0.0.1",
"127.1",
"[::1]",
} {
froxy.localhosts[h] = struct{}{}
}
froxy.localport = fmt.Sprintf("%d", port)
// Create connections manager
froxy.connMan = NewConnMan(froxy)
// Create transports
froxy.sshTransport = NewSSHTransport(froxy)
froxy.directTransport = NewDirectTransport(froxy)
froxy.ftpProxy = NewFTPProxy(froxy)
// Create HTTP server
froxy.httpSrv = &http.Server{
Addr: fmt.Sprintf("localhost:%d", port),
Handler: http.HandlerFunc(froxy.httpHandler),
ErrorLog: log.New(froxy.NewLogWriter(LogLevelError), "", 0),
}
// Create TCP listener
var err error
froxy.listener, err = NewListener(froxy, froxy.httpSrv.Addr)
if err != nil {
return nil, err
}
froxy.Info("Starting HTTP server at http://%s", froxy.httpSrv.Addr)
// Update last used port
if port != froxy.GetPort() {
froxy.SetPort(port)
}
return froxy, nil
}
//
// Run Froxy
//
func (froxy *Froxy) Run() {
go froxy.eventGoroutine()
froxy.Raise(EventStartup)
err := froxy.httpSrv.Serve(froxy.listener)
if err != nil {
panic("Internal error: " + err.Error())
}
}