-
Notifications
You must be signed in to change notification settings - Fork 4
/
wsgate-server.go
140 lines (124 loc) · 3.59 KB
/
wsgate-server.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
package main
import (
"context"
"flag"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"runtime"
"sync"
"syscall"
"time"
"github.com/gorilla/mux"
"github.com/kazeburo/wsgate-server/handler"
"github.com/kazeburo/wsgate-server/mapping"
"github.com/kazeburo/wsgate-server/publickey"
ss "github.com/lestrrat/go-server-starter-listener"
"go.uber.org/zap"
)
var (
// Version wsgate-server version
Version string
showVersion = flag.Bool("version", false, "Show version")
listen = flag.String("listen", "127.0.0.1:8086", "Address to listen to")
handshakeTimeout = flag.Duration("handshake_timeout", 10*time.Second, "Handshake timeout")
dialTimeout = flag.Duration("dial_timeout", 10*time.Second, "Dial timeout")
writeTimeout = flag.Duration("write_timeout", 10*time.Second, "Write timeout")
shutdownTimeout = flag.Duration("shutdown_timeout", 86400*time.Second, "Timeout to wait for all connections to be closed")
enableCompression = flag.Bool("enable_compression", false, "To enable WebSocket Per-Message Compression Extensions (RFC 7692)")
mapFile = flag.String("map", "", "Path and proxy host mapping file")
publicKeyFile = flag.String("public-key", "", "Public key for verifying JWT auth header")
jwtFreshness = flag.Duration("jwt-freshness", 3600*time.Second, "Time in seconds to allow generated jwt tokens")
dumpTCP = flag.Uint("dump-tcp", 0, "Dump TCP. 0 = disable, 1 = src to dest, 2 = both")
)
func printVersion() {
fmt.Printf(`wsgate-server %s
Compiler: %s %s
`,
Version,
runtime.Compiler,
runtime.Version())
}
func main() {
flag.Parse()
if *showVersion {
printVersion()
return
}
logger, _ := zap.NewProduction()
mp, err := mapping.New(*mapFile, logger)
if err != nil {
logger.Fatal("Failed init mapping", zap.Error(err))
}
pk, err := publickey.New(*publicKeyFile, *jwtFreshness, logger)
if err != nil {
logger.Fatal("Failed init publickey", zap.Error(err))
}
proxyHandler, err := handler.New(
*handshakeTimeout,
*dialTimeout,
*writeTimeout,
*enableCompression,
mp,
pk,
*dumpTCP,
logger,
)
if err != nil {
logger.Fatal("Failed init handler", zap.Error(err))
}
wg := &sync.WaitGroup{}
defer func() {
c := make(chan struct{})
go func() {
defer close(c)
wg.Wait()
}()
select {
case <-c:
logger.Info("All connections closed. Shutdown")
return
case <-time.After(*shutdownTimeout):
logger.Info("Timeout, close some connections. Shutdown")
return
}
}()
m := mux.NewRouter()
m.HandleFunc("/", proxyHandler.Hello())
m.HandleFunc("/live", proxyHandler.Hello())
m.HandleFunc("/proxy/{dest}", proxyHandler.Proxy(wg))
s := &http.Server{
Handler: m,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
idleConnsClosed := make(chan struct{})
go func() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGTERM)
<-sigChan
logger.Info("Signal received. Start to shutdown")
ctx, cancel := context.WithTimeout(context.Background(), *shutdownTimeout)
if es := s.Shutdown(ctx); es != nil {
logger.Warn("Shutdown error", zap.Error(es))
}
cancel()
close(idleConnsClosed)
logger.Info("Waiting for all connections to be closed")
}()
l, err := ss.NewListener()
if l == nil || err != nil {
// Fallback if not running under Server::Starter
l, err = net.Listen("tcp", *listen)
if err != nil {
logger.Fatal("Failed to listen to port", zap.String("listen", *listen))
}
}
if err := s.Serve(l); err != http.ErrServerClosed {
logger.Error("Error in Serve", zap.Error(err))
}
<-idleConnsClosed
}