-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.go
347 lines (305 loc) · 6.97 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"strconv"
"sync"
"time"
"github.com/andyleap/hnh-map/webapp"
"go.etcd.io/bbolt"
"golang.org/x/crypto/bcrypt"
)
type Map struct {
gridStorage string
db *bbolt.DB
characters map[string]Character
chmu sync.RWMutex
*webapp.WebApp
gridUpdates topic
mergeUpdates mergeTopic
}
type Session struct {
ID string
Username string
Auths Auths `json:"-"`
TempAdmin bool
}
var (
gridStorage = flag.String("grids", "grids", "directory to store grids in")
port = flag.Int("port", func() int {
if port, ok := os.LookupEnv("HNHMAP_PORT"); ok {
p, err := strconv.Atoi(port)
if err != nil {
log.Fatal(err)
}
return p
}
return 8080
}(), "Port to listen on")
)
func main() {
flag.Parse()
db, err := bbolt.Open(*gridStorage+"/grids.db", 0600, nil)
if err != nil {
log.Fatal(err)
}
m := Map{
gridStorage: *gridStorage,
db: db,
characters: map[string]Character{},
WebApp: webapp.Must(webapp.New().LoadTemplates("./templates/")),
}
err = db.Update(func(tx *bbolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte("config"))
if err != nil {
return err
}
vraw := b.Get([]byte("version"))
v, _ := strconv.Atoi(string(vraw))
if v < len(migrations) {
for _, f := range migrations[v:] {
err = f(tx)
if err != nil {
return err
}
}
}
return b.Put([]byte("version"), []byte(strconv.Itoa(len(migrations))))
})
if err != nil {
log.Fatal(err)
}
go m.cleanChars()
// Mapping client endpoints
http.HandleFunc("/client/", m.client)
http.HandleFunc("/login", m.login)
http.HandleFunc("/logout", m.logout)
http.HandleFunc("/", m.index)
http.HandleFunc("/generateToken", m.generateToken)
http.HandleFunc("/password", m.changePassword)
// Admin endpoints
http.HandleFunc("/admin/", m.admin)
http.HandleFunc("/admin/user", m.adminUser)
http.HandleFunc("/admin/deleteUser", m.deleteUser)
http.HandleFunc("/admin/wipe", m.wipe)
http.HandleFunc("/admin/setPrefix", m.setPrefix)
http.HandleFunc("/admin/setDefaultHide", m.setDefaultHide)
http.HandleFunc("/admin/setTitle", m.setTitle)
http.HandleFunc("/admin/rebuildZooms", m.rebuildZooms)
http.HandleFunc("/admin/export", m.export)
http.HandleFunc("/admin/merge", m.merge)
http.HandleFunc("/admin/map", m.adminMap)
http.HandleFunc("/admin/mapic", m.adminICMap)
// Map frontend endpoints
http.HandleFunc("/map/api/v1/characters", m.getChars)
http.HandleFunc("/map/api/v1/markers", m.getMarkers)
http.HandleFunc("/map/api/config", m.config)
http.HandleFunc("/map/api/admin/wipeTile", m.wipeTile)
http.HandleFunc("/map/api/admin/setCoords", m.setCoords)
http.HandleFunc("/map/api/admin/hideMarker", m.hideMarker)
http.HandleFunc("/map/updates", m.watchGridUpdates)
http.HandleFunc("/map/grids/", m.gridTile)
http.HandleFunc("/map/api/maps", m.getMaps)
//http.Handle("/map/grids/", http.StripPrefix("/map/grids", http.FileServer(http.Dir(m.gridStorage))))
http.Handle("/map/", http.StripPrefix("/map", http.FileServer(http.Dir("frontend"))))
http.Handle("/js/", http.FileServer(http.Dir("public")))
log.Printf("Listening on port %d", *port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
}
type Character struct {
Name string `json:"name"`
ID int `json:"id"`
Map int `json:"map"`
Position Position `json:"position"`
Type string `json:"type"`
updated time.Time
}
type Marker struct {
Name string `json:"name"`
ID int `json:"id"`
GridID string `json:"gridID"`
Position Position `json:"position"`
Image string `json:"image"`
Hidden bool `json:"hidden"`
}
type FrontendMarker struct {
Name string `json:"name"`
ID int `json:"id"`
Map int `json:"map"`
Position Position `json:"position"`
Image string `json:"image"`
Hidden bool `json:"hidden"`
}
type MapInfo struct {
ID int
Name string
Hidden bool
Priority bool
}
type GridData struct {
ID string
Coord Coord
NextUpdate time.Time
Map int
}
type Coord struct {
X int `json:"x"`
Y int `json:"y"`
}
type Position struct {
X int `json:"x"`
Y int `json:"y"`
}
func (c Coord) Name() string {
return fmt.Sprintf("%d_%d", c.X, c.Y)
}
func (c Coord) Parent() Coord {
if c.X < 0 {
c.X--
}
if c.Y < 0 {
c.Y--
}
return Coord{
X: c.X / 2,
Y: c.Y / 2,
}
}
type Auths []string
func (a Auths) Has(auth string) bool {
for _, v := range a {
if v == auth {
return true
}
}
return false
}
const (
AUTH_ADMIN = "admin"
AUTH_MAP = "map"
AUTH_MARKERS = "markers"
AUTH_UPLOAD = "upload"
)
type User struct {
Pass []byte
Auths Auths
Tokens []string
}
func (m *Map) getSession(req *http.Request) *Session {
c, err := req.Cookie("session")
if err != nil {
return nil
}
var s *Session
m.db.View(func(tx *bbolt.Tx) error {
sessions := tx.Bucket([]byte("sessions"))
if sessions == nil {
return nil
}
session := sessions.Get([]byte(c.Value))
if session == nil {
return nil
}
err := json.Unmarshal(session, &s)
if err != nil {
return err
}
if s.TempAdmin {
s.Auths = Auths{AUTH_ADMIN}
return nil
}
users := tx.Bucket([]byte("users"))
if users == nil {
return nil
}
raw := users.Get([]byte(s.Username))
if raw == nil {
s = nil
return nil
}
u := User{}
err = json.Unmarshal(raw, &u)
if err != nil {
s = nil
return err
}
s.Auths = u.Auths
return nil
})
return s
}
func (m *Map) deleteSession(s *Session) {
m.db.Update(func(tx *bbolt.Tx) error {
sessions, err := tx.CreateBucketIfNotExists([]byte("sessions"))
if err != nil {
return err
}
return sessions.Delete([]byte(s.ID))
})
}
func (m *Map) saveSession(s *Session) {
m.db.Update(func(tx *bbolt.Tx) error {
sessions, err := tx.CreateBucketIfNotExists([]byte("sessions"))
if err != nil {
return err
}
buf, err := json.Marshal(s)
if err != nil {
return err
}
return sessions.Put([]byte(s.ID), buf)
})
}
type Page struct {
Title string `json:"title"`
}
func (m *Map) getPage(req *http.Request) Page {
p := Page{}
m.db.View(func(tx *bbolt.Tx) error {
c := tx.Bucket([]byte("config"))
if c == nil {
return nil
}
p.Title = string(c.Get([]byte("title")))
return nil
})
return p
}
func (m *Map) getUser(user, pass string) (u *User) {
m.db.View(func(tx *bbolt.Tx) error {
users := tx.Bucket([]byte("users"))
if users == nil {
if user == "admin" && pass == "admin" {
u = &User{
Auths: Auths{"admin", "tempadmin"},
}
}
return nil
}
raw := users.Get([]byte(user))
if raw != nil {
json.Unmarshal(raw, &u)
if bcrypt.CompareHashAndPassword(u.Pass, []byte(pass)) != nil {
u = nil
return nil
}
}
return nil
})
return u
}
func (m *Map) cleanChars() {
for range time.Tick(time.Second * 10) {
m.chmu.Lock()
for n, c := range m.characters {
if c.updated.Before(time.Now().Add(-10 * time.Second)) {
delete(m.characters, n)
}
}
m.chmu.Unlock()
}
}