-
Notifications
You must be signed in to change notification settings - Fork 1
/
mypi-router.go
712 lines (609 loc) · 16.5 KB
/
mypi-router.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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
package main
import (
"crypto/rand"
"crypto/tls"
"crypto/x509"
"flag"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"os/signal"
"path"
"strings"
"sync"
"time"
"github.com/dueckminor/mypi-tools/go/auth"
"github.com/dueckminor/mypi-tools/go/config"
"github.com/dueckminor/mypi-tools/go/dns"
"github.com/dueckminor/mypi-tools/go/ginutil"
"github.com/dueckminor/mypi-tools/go/util"
"github.com/dueckminor/mypi-tools/go/util/network"
"github.com/dueckminor/mypi-tools/go/util/panic"
"github.com/fsnotify/fsnotify"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/memstore"
"github.com/gin-gonic/gin"
)
var (
target string
portHTTP int
portHTTPS int
gatewayInternalName string
store memstore.Store
localhostOnly = flag.Bool("localhost-only", false, "Listen on localhost only")
mypiRoot = flag.String("mypi-root", "", "The root of the mypi filesystem")
)
func init() {
flag.StringVar(&target, "target", "", "the target (<host>:<port>)")
flag.IntVar(&portHTTPS, "https-port", 8443, "the listening port for https")
flag.IntVar(&portHTTP, "http-port", -1, "the listening port for http")
flag.StringVar(&gatewayInternalName, "router-name", "", "the (internal) name of the router (fritz.box, 192.168.0.1, ...)")
}
type CertConfig struct {
CertFile string `yaml:"cert"`
KeyFile string `yaml:"key"`
cert tls.Certificate
}
type HostConfig struct {
Name string `yaml:"name"`
Target string `yaml:"target"`
Mode string `yaml:"mode"`
Options []string `yaml:"options"`
}
func (h *HostConfig) hasOption(option string) bool {
for _, o := range h.Options {
if o == option {
return true
}
}
return false
}
// /////////////////////////////////////////////////////////////////////////////
type HostImpl interface {
String() string
HandleConnection(conn net.Conn)
}
// /////////////////////////////////////////////////////////////////////////////
type HostImplBase struct {
HostConfig
}
func (h *HostImplBase) String() string {
return h.Target
}
// /////////////////////////////////////////////////////////////////////////////
type HostImplSocket struct {
HostImplBase
}
func (h *HostImplSocket) HandleConnection(conn net.Conn) {
panic.Panic("HandleConnectionSocket expected")
}
func (h *HostImplSocket) HandleConnectionSocket(conn net.Conn, buf []byte) {
defer conn.Close()
targetConn, err := net.Dial("tcp", h.Target)
if err != nil {
return
}
_, err = targetConn.Write(buf)
if err != nil {
return
}
forwardConnect(conn, targetConn)
}
func NewHostImplSocket(hostConfig *HostConfig) *HostImplSocket {
h := new(HostImplSocket)
h.HostConfig = *hostConfig
return h
}
// /////////////////////////////////////////////////////////////////////////////
type HostImplTLS struct {
HostImplBase
}
func (h *HostImplTLS) HandleConnection(conn net.Conn) {
defer conn.Close()
conf := &tls.Config{
InsecureSkipVerify: h.hasOption("insecure"),
ServerName: h.Target,
}
targetConn, err := tls.Dial("tcp", h.Target, conf)
if err != nil {
fmt.Println("Dial Err:", err)
return
}
forwardConnect(conn, targetConn)
}
func NewHostImplTLS(hostConfig *HostConfig) *HostImplTLS {
h := new(HostImplTLS)
h.HostConfig = *hostConfig
return h
}
// /////////////////////////////////////////////////////////////////////////////
type HostImplPort struct {
HostImplBase
}
func (h *HostImplPort) HandleConnection(conn net.Conn) {
defer conn.Close()
targetConn, err := net.Dial("tcp", h.Target)
if err != nil {
fmt.Println("Dial Err:", err)
return
}
forwardConnect(conn, targetConn)
}
func NewHostImplPort(hostConfig *HostConfig) *HostImplPort {
h := new(HostImplPort)
h.HostConfig = *hostConfig
return h
}
// /////////////////////////////////////////////////////////////////////////////
type HostImplReverseProxy struct {
HostImplBase
listener *Listener
r *gin.Engine
}
func (h *HostImplReverseProxy) HandleConnection(conn net.Conn) {
// no need to do this here: defer conn.Close()
// the connection will be closed by the gin.Engine
h.listener.Connections <- conn
}
func NewHostImplReverseProxy(hostConfig *HostConfig, uri string, ac *auth.AuthClient) *HostImplReverseProxy {
h := new(HostImplReverseProxy)
h.HostConfig = *hostConfig
h.r = gin.Default()
if hostConfig.hasOption("external-hostname") {
h.Options = []string{"external-hostname"}
}
if ac != nil {
h.r.Use(sessions.Sessions("MYPI_ROUTER_SESSION", store))
ac.RegisterHandler(h.r)
}
h.listener = MakeListener()
go h.r.RunListener(h.listener) // nolint:errcheck
h.r.Use(ginutil.SingleHostReverseProxy(uri, h.Options...))
return h
}
func NewHostImplHTTP(hostConfig *HostConfig, ac *auth.AuthClient) *HostImplReverseProxy {
return NewHostImplReverseProxy(hostConfig, "http://"+hostConfig.Target, ac)
}
func NewHostImplHTTPS(hostConfig *HostConfig, ac *auth.AuthClient) *HostImplReverseProxy {
return NewHostImplReverseProxy(hostConfig, "https://"+hostConfig.Target, ac)
}
// /////////////////////////////////////////////////////////////////////////////
type AuthConfig struct {
URI string `yaml:"uri"`
ClientID string `yaml:"client_id"`
ClientSecret string `yaml:"client_secret"`
ServerKey string `yaml:"server_key"`
Secret string `yaml:"secret"`
}
// /////////////////////////////////////////////////////////////////////////////
type GatewayConfig struct {
Certs []*CertConfig `yaml:"certs"`
Hosts []*HostConfig `yaml:"hosts"`
Auth AuthConfig `yaml:"auth"`
certByName map[string]*CertConfig
hostByName map[string]HostImpl
configFile string
mutex sync.RWMutex
}
type connWrapper struct {
conn net.Conn
cacheRead bool
buff []byte
}
func (w *connWrapper) Read(b []byte) (n int, err error) {
if w.conn == nil {
return 0, nil
}
n, err = w.conn.Read(b)
if w.cacheRead && n > 0 {
w.buff = append(w.buff, b[0:n]...)
}
return
}
func (w *connWrapper) Write(b []byte) (n int, err error) {
if w.conn == nil {
return len(b), nil
}
return w.conn.Write(b)
}
func (w *connWrapper) Close() error {
if w.conn == nil {
return nil
}
return w.conn.Close()
}
func (w *connWrapper) LocalAddr() net.Addr {
if w.conn == nil {
return nil
}
return w.conn.LocalAddr()
}
func (w *connWrapper) RemoteAddr() net.Addr {
if w.conn == nil {
return nil
}
return w.conn.RemoteAddr()
}
func (w *connWrapper) SetDeadline(t time.Time) error {
if w.conn == nil {
return nil
}
return w.conn.SetDeadline(t)
}
func (w *connWrapper) SetReadDeadline(t time.Time) error {
if w.conn == nil {
return nil
}
return w.conn.SetReadDeadline(t)
}
func (w *connWrapper) SetWriteDeadline(t time.Time) error {
if w.conn == nil {
return nil
}
return w.conn.SetWriteDeadline(t)
}
func forwardConnect(client, server net.Conn) {
done := make(chan bool, 2)
go func() {
// when the server closes the connection,
// it's no longer necessary to send something
// -> lets close the client connection
defer client.Close()
io.Copy(client, server) // nolint: errcheck
done <- true
}()
go func() {
io.Copy(server, client) // nolint: errcheck
done <- true
}()
<-done
<-done
}
func (gateway *GatewayConfig) createHostMap() map[string]HostImpl {
ac := gateway.GetAuthClient()
hostByName := make(map[string]HostImpl)
for _, hostConfig := range gateway.Hosts {
var impl HostImpl
switch hostConfig.Mode {
case "http":
if util.StringsContains(hostConfig.Options, "auth") {
impl = NewHostImplHTTP(hostConfig, ac)
} else {
impl = NewHostImplHTTP(hostConfig, nil)
}
case "https":
if util.StringsContains(hostConfig.Options, "auth") {
impl = NewHostImplHTTPS(hostConfig, ac)
} else {
impl = NewHostImplHTTPS(hostConfig, nil)
}
case "socket":
impl = NewHostImplSocket(hostConfig)
case "tls":
impl = NewHostImplTLS(hostConfig)
default:
impl = NewHostImplPort(hostConfig)
}
hostByName[hostConfig.Name] = impl
}
return hostByName
}
func (gatewayConfig *GatewayConfig) createCertMap() map[string]*CertConfig {
fmt.Println("Checking certificates...")
certByName := make(map[string]*CertConfig)
var err error
for _, certConfig := range gatewayConfig.Certs {
fmt.Println(certConfig.CertFile)
certConfig.cert, err = tls.LoadX509KeyPair(certConfig.CertFile, certConfig.KeyFile)
panic.OnError(err)
x509Cert, err := x509.ParseCertificate(certConfig.cert.Certificate[0])
panic.OnError(err)
for _, dnsName := range x509Cert.DNSNames {
certByName[dnsName] = certConfig
fmt.Println(" ", dnsName)
}
}
return certByName
}
func (gateway *GatewayConfig) updateMaps() {
gateway.setCertMap(gateway.createCertMap())
gateway.setHostMap(gateway.createHostMap())
}
func (gateway *GatewayConfig) setCertMap(certByName map[string]*CertConfig) {
gateway.mutex.Lock()
defer gateway.mutex.Unlock()
gateway.certByName = certByName
}
func (gateway *GatewayConfig) setHostMap(hostByName map[string]HostImpl) {
gateway.mutex.Lock()
defer gateway.mutex.Unlock()
gateway.hostByName = hostByName
}
func (gateway *GatewayConfig) startWatcher() (err error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
log.Println("event:", event)
if event.Name == gateway.configFile {
err = gateway.loadConfig()
if err != nil {
return
}
}
if event.Op&fsnotify.Write == fsnotify.Write {
gateway.setCertMap(gateway.createCertMap())
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}()
for _, certConfig := range gateway.Certs {
err = watcher.Add(certConfig.CertFile)
if err != nil {
return err
}
}
if len(gateway.configFile) > 0 {
err = watcher.Add(gateway.configFile)
if err != nil {
return err
}
}
return nil
}
func (gateway *GatewayConfig) loadConfig() (err error) {
if len(gateway.configFile) > 0 {
var newConfig *GatewayConfig
err = config.ReadYAML(&newConfig, gateway.configFile)
if err != nil {
return err
}
if newConfig != nil {
gateway.Certs = newConfig.Certs
gateway.Hosts = newConfig.Hosts
gateway.Auth = newConfig.Auth
}
}
gateway.updateMaps()
return nil
}
func (gateway *GatewayConfig) getHostImpl(serverName string) HostImpl {
gateway.mutex.RLock()
defer gateway.mutex.RUnlock()
if hostImpl, ok := gateway.hostByName[serverName]; ok {
return hostImpl
}
if strings.HasPrefix(serverName, "*.") {
return nil
}
serverNameParts := strings.SplitN(serverName, ".", 2)
if len(serverNameParts) != 2 {
return nil
}
if hostImpl, ok := gateway.hostByName["*."+serverNameParts[1]]; ok {
return hostImpl
}
return nil
}
func (gateway *GatewayConfig) getCertConfig(serverName string) *CertConfig {
gateway.mutex.RLock()
defer gateway.mutex.RUnlock()
if certConfig, ok := gateway.certByName[serverName]; ok {
return certConfig
}
if strings.HasPrefix(serverName, "*.") {
return nil
}
serverNameParts := strings.SplitN(serverName, ".", 2)
if len(serverNameParts) != 2 {
return nil
}
if certConfig, ok := gateway.certByName["*."+serverNameParts[1]]; ok {
return certConfig
}
return nil
}
type Listener struct {
Connections chan net.Conn
}
func (l *Listener) Accept() (net.Conn, error) {
conn := <-l.Connections
return conn, nil
}
func (l *Listener) Close() error {
return nil
}
func (l *Listener) Addr() net.Addr {
return nil
}
func MakeListener() *Listener {
l := &Listener{}
l.Connections = make(chan net.Conn)
return l
}
func (gateway *GatewayConfig) handleConnection(client net.Conn) {
clientWrapper := &connWrapper{conn: client, cacheRead: true}
closeConn := true
defer func() {
if closeConn {
client.Close()
}
}()
var serverName string
var hostImpl HostImpl
var hostImplSocket *HostImplSocket
tlsConn := tls.Server(clientWrapper, &tls.Config{GetConfigForClient: func(clientHelloInfo *tls.ClientHelloInfo) (*tls.Config, error) {
clientWrapper.cacheRead = false
serverName = clientHelloInfo.ServerName
fmt.Println("ServerName:", serverName)
hostImpl = gateway.getHostImpl(serverName)
if nil == hostImpl {
fmt.Println("-> dropped")
return nil, os.ErrInvalid
}
var ok bool
if hostImplSocket, ok = hostImpl.(*HostImplSocket); ok {
// from now on the connection is handled by hostImplSocket
clientWrapper.conn = nil
return nil, os.ErrInvalid
}
certConfig := gateway.getCertConfig(serverName)
if nil == certConfig {
fmt.Println("-> dropped (have no cert")
return nil, os.ErrInvalid
}
fmt.Println("->", hostImpl.String())
return &tls.Config{
Certificates: []tls.Certificate{certConfig.cert},
}, nil
// return nil, os.ErrInvalid
}})
err := tlsConn.Handshake()
if nil == hostImpl {
fmt.Println("ServerName:", serverName, "rejected")
return
}
if err != nil && hostImplSocket == nil {
fmt.Println("Handshake Err:", err)
return
}
fmt.Println("ServerName:", serverName)
// from now on hostImpl is responsible to close the connection
closeConn = false
if hostImplSocket != nil {
hostImplSocket.HandleConnectionSocket(client, clientWrapper.buff)
} else {
hostImpl.HandleConnection(tlsConn)
}
}
func (c *GatewayConfig) GetAuthClient() *auth.AuthClient {
if len(c.Auth.URI) == 0 {
return nil
}
ac := new(auth.AuthClient)
ac.AuthURI = c.Auth.URI
ac.ClientID = c.Auth.ClientID
ac.ClientSecret = c.Auth.ClientSecret
ac.Secret = c.Auth.Secret
ServerKey := c.Auth.ServerKey
if len(ServerKey) == 0 || len(ac.ClientSecret) == 0 {
clientConfig, err := config.ReadConfigFile("etc/mypi-auth/clients", ac.ClientID+".yml")
panic.OnError(err)
if len(ac.ClientSecret) == 0 {
ac.ClientSecret = clientConfig.GetString("client_secret")
panic.OnCond(ac.ClientSecret == "", "No client secret specified")
}
ServerKey = clientConfig.GetString("server_key")
}
var err error
ac.ServerKey, err = config.StringToRSAPublicKey(ServerKey)
panic.OnError(err)
return ac
}
func main() {
flag.Parse()
if mypiRoot != nil && len(*mypiRoot) > 0 {
err := config.InitApp(*mypiRoot)
panic.OnError(err)
}
_, err := dns.NewServer()
panic.OnError(err)
if len(gatewayInternalName) > 0 {
network.SetRouterInternalName(gatewayInternalName)
}
fmt.Println("Root-Dir:", config.GetRoot())
ip, _ := network.GetRouterInternalIP()
fmt.Println("Router internal IP:", ip)
ip, _ = network.GetRouterExternalIP()
fmt.Println("Router external IP:", ip)
key := make([]byte, 64)
_, err = rand.Read(key)
panic.OnError(err)
store = memstore.NewStore([]byte(key))
var gatewayConfig *GatewayConfig
nArgs := flag.CommandLine.NArg()
panic.OnCond(nArgs > 1, "To many args specified")
gatewayConfig = &GatewayConfig{}
configFile := config.GetFilename("etc/mypi-router/router.yml")
fmt.Println("Using config file:", configFile)
if nArgs == 1 {
configFile = flag.CommandLine.Arg(0)
}
gatewayConfig.configFile = configFile
err = gatewayConfig.loadConfig()
panic.OnError(err)
err = gatewayConfig.startWatcher()
panic.OnError(err)
signals := make(chan os.Signal, 1)
stop := make(chan bool)
signal.Notify(signals, os.Interrupt)
go func() {
for range signals {
fmt.Println("\nReceived an interrupt, stopping...")
stop <- true
}
}()
host := ""
if *localhostOnly {
host = "localhost"
}
if portHTTP > 0 {
http.HandleFunc("/.well-known/acme-challenge/", func(w http.ResponseWriter, r *http.Request) {
_, token := path.Split(r.URL.Path)
if !util.FileIsSafe(r.Host) || !util.FileIsSafe(token) {
return
}
acmeChallenge := path.Join("/etc/letsencrypt/acme-challenge", r.Host, token)
if util.FileExists(acmeChallenge) {
if stream, err := os.Open(acmeChallenge); err == nil {
io.Copy(w, stream) // nolint:errcheck
}
}
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
target := "https://" + r.Host + r.URL.Path
if len(r.URL.RawQuery) > 0 {
target += "?" + r.URL.RawQuery
}
http.Redirect(w, r, target, http.StatusMovedPermanently)
})
go func() {
http.ListenAndServe(fmt.Sprintf("%s:%d", host, portHTTP), nil) // nolint:errcheck
}()
}
incoming, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, portHTTPS))
if err != nil {
log.Fatalf("could not start server on %d: %v", portHTTPS, err)
}
fmt.Printf("server running on %d\n", portHTTPS)
go func() {
for {
client, err := incoming.Accept()
if err != nil {
log.Fatal("could not accept client connection", err)
}
go func() {
remoteAddr := client.RemoteAddr()
fmt.Printf("client '%v' connected!\n", remoteAddr)
gatewayConfig.handleConnection(client)
fmt.Printf("client '%v' disconnected!\n", remoteAddr)
}()
}
}()
<-stop
}