-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathserver.go
197 lines (172 loc) · 5.3 KB
/
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
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
/*
Example nexus WAMP router that handles websockets (with/out TLS), TCP
rawsockets (with/out TLS), and unix rawsockets.
*/
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"os/signal"
"path"
"time"
"github.com/gammazero/nexus/v3/client"
"github.com/gammazero/nexus/v3/router"
"github.com/gammazero/nexus/v3/wamp"
)
const (
certFile = "cert.pem"
keyFile = "rsakey.pem"
)
func main() {
var (
realm = "realm1"
netAddr = "localhost"
unixAddr = "/tmp/exmpl_nexus_sock"
wsPort = 8080
wssPort = 8443
tcpPort = 8000
tcpsPort = 8001
)
flag.StringVar(&netAddr, "netaddr", netAddr, "network address to listen on")
flag.StringVar(&unixAddr, "unixaddr", unixAddr, "unix address to listen on")
flag.IntVar(&wsPort, "ws-port", wsPort, "websocket port")
flag.IntVar(&wssPort, "wss-port", wssPort, "websocket TLS port")
flag.IntVar(&tcpPort, "tcp-port", tcpPort, "TCP port")
flag.IntVar(&tcpsPort, "tcps-port", tcpsPort, "TCP TLS port")
flag.StringVar(&realm, "realm", realm, "realm name")
flag.Parse()
// Create router instance.
routerConfig := &router.Config{
RealmConfigs: []*router.RealmConfig{
{
URI: wamp.URI(realm),
AnonymousAuth: true,
AllowDisclose: true,
},
},
}
nxr, err := router.NewRouter(routerConfig, nil)
if err != nil {
log.Fatal(err)
}
defer nxr.Close()
// create local (embedded) RPC callee client that provides the time in the
// requested timezones.
callee, err := createLocalCallee(nxr, realm)
if err != nil {
log.Fatal(err)
}
defer callee.Close()
// Create websocket server.
wss := router.NewWebsocketServer(nxr)
// Enable websocket compression, which is used if clients request it.
wss.Upgrader.EnableCompression = true
// Configure server to send and look for client tracking cookie.
wss.EnableTrackingCookie = true
// Set keep-alive period to 30 seconds.
wss.KeepAlive = 30 * time.Second
// Create rawsocket server.
rss := router.NewRawSocketServer(nxr)
// ---- Start servers ----
// Run websocket server.
wsAddr := fmt.Sprintf("%s:%d", netAddr, wsPort)
wsCloser, err := wss.ListenAndServe(wsAddr)
if err != nil {
log.Fatal(err)
}
defer wsCloser.Close()
log.Printf("Websocket server listening on ws://%s/", wsAddr)
// Run TCP rawsocket server.
tcpAddr := fmt.Sprintf("%s:%d", netAddr, tcpPort)
tcpCloser, err := rss.ListenAndServe("tcp", tcpAddr)
if err != nil {
log.Fatal(err)
}
defer tcpCloser.Close()
log.Printf("RawSocket TCP server listening on tcp://%s/", tcpAddr)
// Run unix rawsocket server.
os.Remove(unixAddr)
unixCloser, err := rss.ListenAndServe("unix", unixAddr)
if err != nil {
log.Fatal(err)
}
defer unixCloser.Close()
log.Printf("RawSocket unix server listening on unix://%s", unixAddr)
// ---- Start TLS servers ----
var certPath, keyPath string
if _, err = os.Stat(certFile); os.IsNotExist(err) {
certPath = path.Join("server", certFile)
keyPath = path.Join("server", keyFile)
} else {
certPath = certFile
keyPath = keyFile
}
// Run TLS websocket server.
wsAddrTLS := fmt.Sprintf("%s:%d", netAddr, wssPort)
wsTlsCloser, err := wss.ListenAndServeTLS(wsAddrTLS, nil, certPath, keyPath)
if err != nil {
log.Fatal(err)
}
defer wsTlsCloser.Close()
log.Printf("TLS Websocket server listening on wss://%s/", wsAddrTLS)
// Run TLS TCP rawsocket server.
tcpAddrTLS := fmt.Sprintf("%s:%d", netAddr, tcpsPort)
tcpTlsCloser, err := rss.ListenAndServeTLS("tcp", tcpAddrTLS, nil, certPath, keyPath)
if err != nil {
log.Fatal(err)
}
defer tcpTlsCloser.Close()
log.Printf("TLS RawSocket TCP server listening on tcps://%s/", tcpAddrTLS)
// Wait for SIGINT (CTRL-c), then close servers and exit.
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, os.Interrupt)
<-shutdown
// Servers close at exit due to defer calls.
}
// createLocalCallee creates a local callee client that is embedded in this
// server. This client functions as a trusted client and does not require IPC
// to communicate with the router.
//
// The purpose of this client is to demonstrate how to create a local client
// that is part of the same application running the WAMP router.
func createLocalCallee(nxr router.Router, realm string) (*client.Client, error) {
logger := log.New(os.Stdout, "CALLEE> ", log.LstdFlags)
cfg := client.Config{
Realm: realm,
Logger: logger,
}
callee, err := client.ConnectLocal(nxr, cfg)
if err != nil {
return nil, err
}
// Register procedure "time"
const timeProc = "worldtime"
if err = callee.Register(timeProc, worldTime, nil); err != nil {
return nil, fmt.Errorf("Failed to register %q: %s", timeProc, err)
}
log.Printf("Registered procedure %q with router", timeProc)
return callee, nil
}
// worldTime is a RPC function that returns times for the specified timezones.
// This function is registered by the local callee client that is embedded in
// the server.
func worldTime(ctx context.Context, inv *wamp.Invocation) client.InvokeResult {
now := time.Now()
results := wamp.List{fmt.Sprintf("UTC: %s", now.UTC())}
for _, arg := range inv.Arguments {
locName, ok := wamp.AsString(arg)
if !ok {
continue
}
loc, err := time.LoadLocation(locName)
if err != nil {
results = append(results, fmt.Sprintf("%s: %s", locName, err))
continue
}
results = append(results, fmt.Sprintf("%s: %s", locName, now.In(loc)))
}
return client.InvokeResult{Args: results}
}