-
Notifications
You must be signed in to change notification settings - Fork 2
/
session.go
51 lines (42 loc) · 940 Bytes
/
session.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
package main
import (
"fmt"
"log"
"time"
)
// Session is what we store temporarily from each DNS requestxxrf
type Session struct {
IP string
EDNS string
Expire int64
}
func setCache(uuid, ip, ednsIP string) error {
session := &Session{
Expire: time.Now().Add(10 * time.Second).Unix(),
IP: ip,
EDNS: ednsIP,
}
ok := cache.Add("dns-"+uuid, session)
if !ok {
return fmt.Errorf("%s not saved to the cache", uuid)
}
return nil
}
func getCache(uuid string) (string, string, bool) {
// Use Peek instead of get to just have a "fifo" cache,
// where adding an item again moves it to the front of the
// list again.
get, ok := cache.Peek("dns-" + uuid)
if !ok {
return "", "", false
}
s, ok := get.(*Session)
if !ok {
log.Printf("Session %s wasn't a session type (%T)", uuid, get)
return "", "", false
}
if s.Expire < time.Now().Unix() {
return "", "", false
}
return s.IP, s.EDNS, true
}