forked from reconquest/shadowd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongodb.go
257 lines (212 loc) · 4.75 KB
/
mongodb.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
package main
import (
"log"
"regexp"
"sort"
"strings"
"time"
"github.com/reconquest/hierr-go"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type mongodb struct {
dsn string
hashTTL time.Duration
session *mgo.Session
database *mgo.Database
shadows *mgo.Collection
keys *mgo.Collection
clients *mgo.Collection
}
func (db *mongodb) GetPublicKeys(token string) (string, error) {
var docs []map[string]interface{}
err := db.keys.Find(bson.M{"token": token}).All(&docs)
if err != nil {
return "", hierr.Errorf(
err, "can't obtain public keys from database",
)
}
keys := []string{}
for _, doc := range docs {
keys = append(keys, doc["key"].(string))
}
return strings.Join(keys, "\n"), nil
}
func (db *mongodb) AddPublicKey(
token string, key []byte, truncate bool,
) error {
if truncate {
_, err := db.keys.RemoveAll(bson.M{"token": token})
if err != nil {
return hierr.Errorf(
err, "can't remove public keys",
)
}
}
err := db.keys.Insert(bson.M{"token": token, "key": string(key)})
if err != nil {
return hierr.Errorf(
err, "can't add key to database",
)
}
return nil
}
func (db *mongodb) SetHashTable(token string, table []string) error {
_, err := db.shadows.RemoveAll(bson.M{"token": token})
if err != nil {
return hierr.Errorf(
err, "can't remove existing hash table",
)
}
docs := []interface{}{}
for _, hash := range table {
docs = append(docs, bson.M{
"token": token,
"hash": hash,
})
}
err = db.shadows.Insert(docs...)
if err != nil {
return hierr.Errorf(
err, "can't insert table hash to database",
)
}
return nil
}
func (db *mongodb) IsHashExists(token string, hash string) (bool, error) {
var doc map[string]interface{}
err := db.shadows.Find(bson.M{"token": token, "hash": hash}).One(&doc)
if err != nil {
if err == mgo.ErrNotFound {
return false, nil
}
return false, err
}
return true, nil
}
func (db *mongodb) GetHash(token string, number int64) (string, error) {
var doc map[string]interface{}
err := db.shadows.Find(
bson.M{"token": token},
).Skip(int(number - int64(1))).Limit(1).One(&doc)
if err != nil {
if err == mgo.ErrNotFound {
return "", ErrNotFound
}
return "", err
}
return doc["hash"].(string), nil
}
func (db *mongodb) IsRecentClient(identifier string) (bool, error) {
var doc map[string]interface{}
err := db.clients.Find(bson.M{"client": identifier}).One(&doc)
if err != nil {
if err == mgo.ErrNotFound {
return false, nil
}
return false, err
}
return true, nil
}
func (db *mongodb) AddRecentClient(identifier string) error {
err := db.clients.Insert(
bson.M{"client": identifier, "create_date": time.Now().Unix()},
)
if err != nil {
return hierr.Errorf(
err, "can't add recent client to database",
)
}
return nil
}
func (db *mongodb) GetTableSize(token string) (int64, error) {
count, err := db.shadows.Find(bson.M{"token": token}).Count()
if err != nil {
return 0, hierr.Errorf(
err, "can't obtain table size from database",
)
}
if count == 0 {
return 0, ErrNotFound
}
return int64(count), nil
}
func (db *mongodb) GetTokens(prefix string) ([]string, error) {
var docs []string
err := db.shadows.Find(
bson.M{
"token": bson.M{"$regex": "^" + regexp.QuoteMeta(prefix) + ".*"},
},
).Select(bson.M{"token": 1}).Distinct("token", &docs)
if err != nil {
return nil, hierr.Errorf(
err, "can't obtain tokens from database",
)
}
for i, doc := range docs {
docs[i] = strings.TrimPrefix(doc, prefix)
}
sort.Strings(docs)
return docs, nil
}
func (db *mongodb) Init() error {
err := db.connect()
if err != nil {
return hierr.Errorf(
err, "can't establish database connection",
)
}
go func() {
for range time.Tick(time.Minute) {
db.cleanupRecentClients()
}
}()
go func() {
for range time.Tick(time.Second * 5) {
db.ensureConnection()
}
}()
return nil
}
func (db *mongodb) connect() error {
session, err := mgo.Dial(db.dsn)
if err != nil {
return err
}
db.session = session
db.database = db.session.DB("")
db.shadows = db.database.C("shadows")
db.keys = db.database.C("keys")
db.clients = db.database.C("clients")
return nil
}
func (db *mongodb) ensureConnection() {
err := db.session.Ping()
if err == nil {
return
}
log.Println(
"database connection has gone away, " +
"trying to reestablish database connection...",
)
err = db.connect()
if err != nil {
log.Printf("can't establish database connection: %s", err)
return
}
log.Println("database connection established")
}
func (db *mongodb) cleanupRecentClients() {
_, err := db.clients.RemoveAll(
bson.M{
"create_date": bson.M{
"$lt": time.Now().Unix() - int64(db.hashTTL),
},
},
)
if err != nil {
log.Println(
hierr.Errorf(err, "can't cleanup recent clients"),
)
}
}