/
server.go
448 lines (406 loc) · 11 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
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
/*
Manages a collection of logbases.
*/
package logbase
import (
"os"
"github.com/h00gs/toml"
"github.com/h00gs/gubed"
"github.com/garyburd/go-websocket/websocket"
"net"
"net/http"
"io"
// "encoding/json"
"path"
// "encoding/binary"
"bytes"
"strconv"
"path/filepath"
"strings"
"fmt"
"time"
"runtime"
)
const (
SERVER_ID_LENGTH uint64 = 10 // Should be divisible by 2
SESSION_ID_LENGTH uint64 = 10 // Should be divisible by 2
SERVER_CONFIG_FILENAME string = "logbase_server.cfg"
DEBUG_FILENAME string = "debug.log"
USERS_LOGBASE_NAME string = ".users_logbase"
WS_READ_BUFF_SIZE int = 1024
WS_WRITE_BUFF_SIZE int = 1024
ADMIN_USER string = "Admin"
CLOSEFILE_PATH string = "./.close"
KILOBYTE uint64 = 1024
MEGABYTE uint64 = KILOBYTE * KILOBYTE
)
// Check intervals.
const (
CHECK_CLOSEFILE_SECS int = 5 // Check for close file every x secs
CHECK_MEMORY_SECS int = 10 // Check memory usage every x secs
)
type Server struct {
id string
logbases map[string]*Logbase
config *ServerConfiguration
Debug *gubed.Logger
basedir string
users *Logbase
shutdown bool
listener net.Listener
}
type WebsocketIO struct {
in io.Reader
out io.WriteCloser
}
type WebsocketSession struct {
id string
start time.Time
ws *websocket.Conn
io *WebsocketIO
ok bool // Session has been authorised
user string
}
// Messages.
type CMD uint8
const CMDSIZE = 1
//const (
// WS_SUCCESS string = "SUCCESS"
// WS_FAIL string = "FAIL"
//)
const (
LOGIN CMD = iota
CLOSE
OPEN_LOGBASE
CLOSE_LOGBASE
LIST_LOGBASES
PUT_PAIR // k-v pair
GET_VALUE // k-v pair
)
var CommandCode = map[string]CMD{
"LOGIN": LOGIN,
"CLOSE": CLOSE,
"OPEN_LOGBASE": OPEN_LOGBASE,
"CLOSE_LOGBASE": CLOSE_LOGBASE,
"LIST_LOGBASES": LIST_LOGBASES,
"PUT_PAIR": PUT_PAIR,
"GET_VALUE": GET_VALUE,
}
var CommandName map[CMD]string = make(map[CMD]string)
func init() {
for name, cmd := range CommandCode {
CommandName[cmd] = name
}
}
//type JsonMessage struct {
// cmd string
// args string
//}
func NewWebsocketSession() *WebsocketSession {
return &WebsocketSession{
id: GenerateRandomHexStrings(1, SESSION_ID_LENGTH, SESSION_ID_LENGTH)[0],
start: time.Now(),
io: new(WebsocketIO),
ok: false,
}
}
func NewServer() *Server {
return &Server{
id: GenerateRandomHexStrings(1, SERVER_ID_LENGTH, SERVER_ID_LENGTH)[0],
logbases: make(map[string]*Logbase),
Debug: gubed.MakeScreenFileLogger(DEBUG_FILENAME),
shutdown: false,
}
}
// Server configuration.
// User space constants
type ServerConfiguration struct {
DEBUG_LEVEL string
WEBSOCKET_PORT int
DEFAULT_BASEDIR string
SERVER_PASS_HASH string
}
// Default configuration in case file is absent.
func DefaultServerConfig() *ServerConfiguration {
return &ServerConfiguration{
DEBUG_LEVEL: "ADVISE",
}
}
// Load optional server configuration file parameters.
func LoadServerConfig(path string) (config *ServerConfiguration, err error) {
_, err = os.Stat(path)
if os.IsNotExist(err) {
config = DefaultServerConfig()
err = nil
return
}
if err != nil {return}
config = new(ServerConfiguration)
_, err = toml.DecodeFile(path, &config)
return
}
// Initialise server and start TCP server.
func (server *Server) Start(passhash string) error {
err := server.Init(passhash)
if err != nil {return err}
// TCP server
service := ":" + strconv.Itoa(server.config.WEBSOCKET_PORT)
http.Handle("/script/", http.FileServer(http.Dir("./web")))
http.Handle("/css/", http.FileServer(http.Dir("./web")))
//http.Handle("/", http.FileServer(http.Dir("./web")))
http.HandleFunc("/", server.WebsocketSession)
server.Debug.Advise("Listening on port %s...", service)
listener, err := net.Listen("tcp", service)
if server.Debug.Error(err) == nil {
server.listener = listener
err = http.Serve(listener, nil) // for{} loop
server.Debug.Error(err)
if server.shutdown {
server.GracefulShutdown()
}
}
return nil
}
// Take steps for a graceful shutdown.
func (server *Server) GracefulShutdown() {
server.Debug.Advise("Gracefully shutting down...")
return
}
// Initialise server and configuration.
func (server *Server) Init(passhash string) error {
cfgPath := path.Join(".", SERVER_CONFIG_FILENAME)
config, err := LoadServerConfig(cfgPath)
if server.Debug.Error(err) != nil {
WrapError("Problem loading server config file", err).Fatal()
}
server.config = config
if passhash != config.SERVER_PASS_HASH {
fmt.Println("Incorrect passphrase")
os.Exit(1)
} else {
fmt.Println("Passphrase is good")
}
server.Debug.SetLevel(config.DEBUG_LEVEL)
server.Debug.Advise("Server id = %s", server.Id())
server.basedir = config.DEFAULT_BASEDIR
server.Debug.Advise(
"Directory in which to look for logbases = %s",
server.basedir)
// Make dir if it does not exist
err = server.Debug.Error(os.MkdirAll(server.basedir, 0777))
if err != nil {return err}
// User logbase
users := MakeLogbase(server.UsersLogbasePath(), server.Debug)
err = server.Debug.Error(users.Init(true))
if err != nil {return err}
err = server.Debug.Error(users.InitSecurity(ADMIN_USER, passhash))
if err != nil {return err}
server.users = users
// Start close file checker
go server.CloseFileChecker(CHECK_CLOSEFILE_SECS, CLOSEFILE_PATH)
// Start memory checker
go server.MemoryChecker(CHECK_MEMORY_SECS)
return nil
}
// Regularly checks memory usage does not exceed limits.
func (server *Server) MemoryChecker(secs int) {
server.Debug.Basic(
"Started memory checker (monitors and moderates server memory use)")
memstats := &runtime.MemStats{}
for {
<-time.After(time.Duration(secs) * time.Second)
runtime.GC()
runtime.ReadMemStats(memstats)
server.Debug.Basic(
"Memory (KB): total = %v allocated = %v",
memstats.TotalAlloc / KILOBYTE,
memstats.Alloc / KILOBYTE,
)
}
return
}
// Continually checks to see if close file exists, if so, switches
// server shutdown flag on.
func (server *Server) CloseFileChecker(secs int, fpath string) {
server.Debug.Error(os.RemoveAll(fpath))
server.Debug.Basic(
"Started close file checker (triggers shutdown if %q file appears)",
fpath)
var err error
for {
<-time.After(time.Duration(secs) * time.Second)
_, err = os.Stat(fpath)
if !os.IsNotExist(err) {
server.Debug.Advise("Close file detected, triggering shutdown")
server.shutdown = true
server.listener.Close()
break
}
}
return
}
func (server *Server) UsersLogbasePath() string {
return path.Join(server.basedir, USERS_LOGBASE_NAME)
}
func (server *Server) Id() string {return server.id}
func (session *WebsocketSession) Id() string {return session.id}
// Open an existing Logbase or create it if necessary, identified by a
// directory path.
func (server *Server) Open(lbPath, user, passhash string) (*Logbase, error) {
// Use existing Logbase if present
lbase, present := server.logbases[lbPath]
if present {return lbase, nil}
lbase = MakeLogbase(lbPath, server.Debug)
err := lbase.Init(true)
if err != nil {return nil, err}
err = lbase.InitSecurity(user, passhash)
return lbase, err
}
// Main entry point. Collect and respond to socket messages. When this
// function finishes, the websocket is closed.
func (server *Server) WebsocketSession(w http.ResponseWriter, r *http.Request) {
ws, err :=
websocket.Upgrade(
w, // any responder that supports http.Hijack
r.Header, // request header string->string map
nil, // response header string->string map
WS_READ_BUFF_SIZE, // buffer sizes for read...
WS_WRITE_BUFF_SIZE) // and write
if server.Debug.Error(err) != nil {
http.Error(w, err.Error(), 400)
return
}
defer ws.Close()
session := NewWebsocketSession()
session.ws = ws
server.Debug.Basic("Enter SocketSession with id = %v", session.Id())
//inbyts := make([]byte, WS_READ_BUFF_SIZE)
//var n int
for {
op, r, err := ws.NextReader()
if err != nil {
server.Debug.Error(err)
return
}
session.io.in = r
if op != websocket.OpBinary && op != websocket.OpText {
continue
}
w, err := ws.NextWriter(op)
if err != nil {
server.Debug.Error(err)
return
}
session.io.out = w
/*
if op == websocket.OpBinary {
n, err = r.Read(inbyts)
server.Debug.Fine("Msg rx: %v", inbyts[:n])
bfr := bufio.NewReader(bytes.NewBuffer(inbyts[:n]))
binary.Read(bfr, binary.BigEndian, &cmd)
if cmd == CLOSE {
server.Debug.Fine("SocketSession closed by client")
break
}
go server.RespondToBinary(cmd, inbyts[CMDSIZE:n], w)
}
*/
if op == websocket.OpText {
bfr := new(bytes.Buffer)
bfr.ReadFrom(r)
intxt := bfr.String()
server.Debug.Basic("SocketSession incoming: %q", intxt)
words := strings.Split(intxt, " ")
//decoder := json.NewDecoder(r)
//err = decoder.Decode(&intxt)
//if err != nil {
// server.Debug.Error(err)
// return
//}
cmd, ok := CommandCode[words[0]]
if !ok {
server.Debug.Error(FmtErrBadCommand("Command %q not recognised", words[0]))
}
if cmd == CLOSE {
server.Debug.Basic("SocketSession closed by user %s", session.user)
break
}
server.Respond(session, cmd, words[1:])
}
}
return
}
func (server *Server) Respond(session *WebsocketSession, cmd CMD, args []string) {
defer session.io.out.Close()
if !session.ok {
if cmd == LOGIN {
user := args[0]
pass := args[1]
if !server.users.IsValidUser(user, pass) {
// TODO throttle attempts
msg := fmt.Sprintf("Invalid credentials for user %q", user)
server.Debug.Error(FmtErrUser(msg))
server.Debug.Error(session.SendText(msg))
return
}
session.ok = true
session.user = user
server.Debug.Advise("User %s logged in", user)
} else {
server.Debug.Error(FmtErrUser(
"Session user not authorised to execute command %q",
CommandName[cmd] + " " + strings.Join(args, " ")))
}
return
}
switch cmd {
case LOGIN:
server.Debug.Error(session.SendText("Already logged in"))
return
case OPEN_LOGBASE:
return
case CLOSE_LOGBASE:
return
case LIST_LOGBASES:
list, err := server.ListLogbases()
server.Debug.Error(err)
server.Debug.Basic("List logbases: %s", list)
bfr := bytes.NewBuffer([]byte(strings.Join(list, ";")))
n, err := bfr.WriteTo(session.io.out)
server.Debug.Error(err)
server.Debug.Basic("Wrote %v bytes to socket", n)
return
case PUT_PAIR:
return
case GET_VALUE:
return
}
return
}
func (server *Server) ListLogbases() ([]string, error) {
var names []string
var nscan int = 0
server.Debug.Basic("Compiling list of logbases in %s", server.basedir)
findTopLevelDir :=
func(fpath string, fileInfo os.FileInfo, inerr error) error {
stat, err := os.Stat(fpath)
if err != nil {return err}
if nscan > 0 && stat.IsDir() {
name := filepath.Base(fpath)
if name != USERS_LOGBASE_NAME {
names = append(names, name)
}
return filepath.SkipDir
}
nscan++
return nil
}
err := filepath.Walk(server.basedir, findTopLevelDir)
return names, err
}
// Websocket Session.
func (session *WebsocketSession) SendText(msg string) error {
bfr := bytes.NewBuffer([]byte(msg))
_, err := bfr.WriteTo(session.io.out)
return err
}