forked from koding/kite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kite.go
271 lines (219 loc) · 8.11 KB
/
kite.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
// Package kite is a library for creating micro-services. Two main types
// implemented by this package are Kite for creating a micro-service server
// called "Kite" and Client for communicating with another kites.
package kite
import (
"crypto/tls"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"os"
"strings"
"time"
jwt "github.com/dgrijalva/jwt-go"
"github.com/gorilla/mux"
"github.com/koding/kite/config"
"github.com/koding/kite/protocol"
uuid "github.com/satori/go.uuid"
"gopkg.in/igm/sockjs-go.v2/sockjs"
)
var hostname string
func init() {
var err error
hostname, err = os.Hostname()
if err != nil {
panic(fmt.Sprintf("kite: cannot get hostname: %s", err.Error()))
}
}
// Kite defines a single process that enables distributed service messaging
// amongst the peers it is connected. A Kite process acts as a Client and as a
// Server. That means it can receive request, process them, but it also can
// make request to other kites.
//
// Do not use this struct directly. Use kite.New function, add your handlers
// with HandleFunc mehtod, then call Run method to start the inbuilt server (or
// pass it to any http.Handler compatible server)
type Kite struct {
Config *config.Config
// Log logs with the given Logger interface
Log Logger
// SetLogLevel changes the level of the logger. Default is INFO.
SetLogLevel func(Level)
// Contains different functions for authenticating user from request.
// Keys are the authentication types (options.auth.type).
Authenticators map[string]func(*Request) error
// Kontrol keys to trust. Kontrol will issue access tokens for kites
// that are signed with the private counterpart of these keys.
// Key data must be PEM encoded.
trustedKontrolKeys map[string]string
// Handlers added with Kite.HandleFunc().
handlers map[string]*Method // method map for exported methods
preHandlers []Handler // a list of handlers that are executed before any handler
postHandlers []Handler // a list of handlers that are executed after any handler
// MethodHandling defines how the kite is returning the response for
// multiple handlers
MethodHandling MethodHandling
// HTTP muxer
muxer *mux.Router
// kontrolclient is used to register to kontrol and query third party kites
// from kontrol
kontrol *kontrolClient
// Handlers to call when a new connection is received.
onConnectHandlers []func(*Client)
// Handlers to call before the first request of connected kite.
onFirstRequestHandlers []func(*Client)
// Handlers to call when a client has disconnected.
onDisconnectHandlers []func(*Client)
// server fields, are initialized and used when
// TODO: move them to their own struct, just like KontrolClient
listener net.Listener
TLSConfig *tls.Config
readyC chan bool // To signal when kite is ready to accept connections
closeC chan bool // To signal when kite is closed with Close()
name string
version string
Id string // Unique kite instance id
}
// New creates, initialize and then returns a new Kite instance. Version must
// be in 3-digit semantic form. Name is important that it's also used to be
// searched by others.
func New(name, version string) *Kite {
if name == "" {
panic("kite: name cannot be empty")
}
if digits := strings.Split(version, "."); len(digits) != 3 {
panic("kite: version must be 3-digits semantic version")
}
kiteID := uuid.NewV4()
l, setlevel := newLogger(name)
kClient := &kontrolClient{
readyConnected: make(chan struct{}),
readyRegistered: make(chan struct{}),
registerChan: make(chan *url.URL, 1),
}
k := &Kite{
Config: config.New(),
Log: l,
SetLogLevel: setlevel,
Authenticators: make(map[string]func(*Request) error),
trustedKontrolKeys: make(map[string]string),
handlers: make(map[string]*Method),
preHandlers: make([]Handler, 0),
postHandlers: make([]Handler, 0),
kontrol: kClient,
name: name,
version: version,
Id: kiteID.String(),
readyC: make(chan bool),
closeC: make(chan bool),
muxer: mux.NewRouter(),
}
// We change the heartbeat interval from 25 seconds to 10 seconds. This is
// better for environments such as AWS ELB.
sockjsOpts := sockjs.DefaultOptions
sockjsOpts.HeartbeatDelay = 10 * time.Second
// All sockjs communication is done through this endpoint..
k.muxer.PathPrefix("/kite").Handler(sockjs.NewHandler("/kite", sockjsOpts, k.sockjsHandler))
// Add useful debug logs
k.OnConnect(func(c *Client) { k.Log.Debug("New session: %s", c.session.ID()) })
k.OnFirstRequest(func(c *Client) { k.Log.Debug("Session %q is identified as %q", c.session.ID(), c.Kite) })
k.OnDisconnect(func(c *Client) { k.Log.Debug("Kite has disconnected: %q", c.Kite) })
// Every kite should be able to authenticate the user from token.
// Tokens are granted by Kontrol Kite.
k.Authenticators["token"] = k.AuthenticateFromToken
// A kite accepts requests with the same username.
k.Authenticators["kiteKey"] = k.AuthenticateFromKiteKey
// Register default methods and handlers.
k.addDefaultHandlers()
return k
}
// Kite returns the definition of the kite.
func (k *Kite) Kite() *protocol.Kite {
return &protocol.Kite{
Username: k.Config.Username,
Environment: k.Config.Environment,
Name: k.name,
Version: k.version,
Region: k.Config.Region,
Hostname: hostname,
ID: k.Id,
}
}
// Trust a Kontrol key for validating tokens.
func (k *Kite) TrustKontrolKey(issuer, key string) {
k.trustedKontrolKeys[issuer] = key
}
// HandleHTTP registers the HTTP handler for the given pattern into the
// underlying HTTP muxer.
func (k *Kite) HandleHTTP(pattern string, handler http.Handler) {
k.muxer.Handle(pattern, handler)
}
// HandleHTTPFunc registers the HTTP handler for the given pattern into the
// underlying HTTP muxer.
func (k *Kite) HandleHTTPFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) {
k.muxer.HandleFunc(pattern, handler)
}
// ServeHTTP helps Kite to satisfy the http.Handler interface. So kite can be
// used as a standard http server.
func (k *Kite) ServeHTTP(w http.ResponseWriter, req *http.Request) {
k.muxer.ServeHTTP(w, req)
}
func (k *Kite) sockjsHandler(session sockjs.Session) {
defer session.Close(0, "")
// This Client also handles the connected client.
// Since both sides can send/receive messages the client code is reused here.
c := k.NewClient("")
c.m.Lock()
c.session = session
c.m.Unlock()
go c.sendHub()
c.wg.Add(1) // with sendHub we added a new listener
k.callOnConnectHandlers(c)
// Run after methods are registered and delegate is set
c.readLoop()
c.callOnDisconnectHandlers()
k.callOnDisconnectHandlers(c)
}
func (k *Kite) OnConnect(handler func(*Client)) {
k.onConnectHandlers = append(k.onConnectHandlers, handler)
}
// OnFirstRequest registers a function to run when a Kite connects to this Kite.
func (k *Kite) OnFirstRequest(handler func(*Client)) {
k.onFirstRequestHandlers = append(k.onFirstRequestHandlers, handler)
}
// OnDisconnect registers a function to run when a connected Kite is disconnected.
func (k *Kite) OnDisconnect(handler func(*Client)) {
k.onDisconnectHandlers = append(k.onDisconnectHandlers, handler)
}
func (k *Kite) callOnConnectHandlers(c *Client) {
for _, handler := range k.onConnectHandlers {
handler(c)
}
}
func (k *Kite) callOnFirstRequestHandlers(c *Client) {
for _, handler := range k.onFirstRequestHandlers {
handler(c)
}
}
func (k *Kite) callOnDisconnectHandlers(c *Client) {
for _, handler := range k.onDisconnectHandlers {
handler(c)
}
}
// RSAKey returns the corresponding public key for the issuer of the token.
// It is called by jwt-go package when validating the signature in the token.
func (k *Kite) RSAKey(token *jwt.Token) (interface{}, error) {
if k.Config.KontrolKey == "" {
panic("kontrol key is not set in config")
}
issuer, ok := token.Claims["iss"].(string)
if !ok {
return nil, errors.New("token does not contain a valid issuer claim")
}
if issuer != k.Config.KontrolUser {
return nil, fmt.Errorf("issuer is not trusted: %s", issuer)
}
return []byte(k.Config.KontrolKey), nil
}