-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghttp.go
328 lines (292 loc) · 9.07 KB
/
ghttp.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
package ghttp
import (
"fmt"
"log"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/geeksteam/GoTools/logger"
"github.com/geeksteam/GoTools/shutdown"
"github.com/geeksteam/GoTools/stringutils"
"github.com/geeksteam/SHM-Backend/core/users"
"github.com/geeksteam/SHM-Backend/panicerr"
"github.com/geeksteam/SHM-Backend/plugins"
"github.com/geeksteam/ghttp/bruteforce"
"github.com/geeksteam/ghttp/journal"
"github.com/geeksteam/ghttp/moduleutils"
"github.com/geeksteam/ghttp/sessions"
"github.com/getsentry/raven-go"
"github.com/gorilla/mux"
)
var (
// cfg = config.Get().GHttp
cfg Config
)
// SetConfig Global config settings
func SetConfig(c Config) {
cfg = c
bruteforce.SetConfig(bruteforce.BruteForce{
BlockAttempts: cfg.BruteForce.BlockAttempts,
BanTime: cfg.BruteForce.BanTime,
DataEncoding: cfg.BruteForce.DataEncoding,
})
journal.SetConfig(journal.Journal{
BoltDB: cfg.Journal.BoltDB,
BucketForOperations: cfg.Journal.BucketForOperations,
Capacity: cfg.Journal.Capacity,
DataEncoding: cfg.Journal.DataEncoding,
})
sessions.SetConfig(sessions.SessionsConf{
SessionIDKey: cfg.SessionsConf.SessionIDKey,
SessionIDKeyLength: cfg.SessionsConf.SessionIDKeyLength,
SessionLifeTime: cfg.SessionsConf.SessionLifeTime,
StrictIP: cfg.SessionsConf.StrictIP,
})
}
// NewRouter constructs Router instances
func NewRouter() *Router {
sessions.NewSessions()
return &Router{
curID: 0,
handlers: map[uint64]rhandler{},
mutex: sync.RWMutex{},
Router: *mux.NewRouter(),
}
}
// Handlers returns copy of an internal Router's handlers list.
func (r *Router) Handlers() map[uint64]rhandler {
// Make it atomic
r.mutex.RLock()
defer r.mutex.RUnlock()
// Make a copy of handlers slice
result := make(map[uint64]rhandler, len(r.handlers))
for k, v := range r.handlers {
result[k] = v
}
return result
}
// Delete handler from slice
func (r *Router) deleteHandler(id uint64) {
delete(r.handlers, id)
}
// HandleInternalFunc is a gorilla Router's wrapper function.
// This handles standart modules functions.
func (router *Router) HandleInternalFunc(path string, f func(http.ResponseWriter, *http.Request, *sessions.Sessions)) *mux.Route {
routerFunc := func(w http.ResponseWriter, r *http.Request) {
// Trigger starting of new process
if !isIgnored(r.RequestURI) {
if err := shutdown.DefaultWatcher.Start(); err != nil {
http.Error(w, http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable)
return
}
defer shutdown.DefaultWatcher.Finish()
}
/*
# Pre run
*/
// 1. Clean expired sessions
sessions.SessionsStorage.CleanExpired()
// 2. Define empty handler
var handler rhandler
// 3. Set headers
setHeaderNoCache(w)
// 4. Defered run and catch panics
defer func() {
// Remove handler from running list
router.mutex.Lock()
router.deleteHandler(handler.id)
router.mutex.Unlock()
// Catch all panics from handler
if rec := recover(); rec != nil {
switch rec := rec.(type) {
// Panicerr catched
case panicerr.Error:
logger.Info(fmt.Sprintf("%v [ %v ] Error catched: Code '%v' Text '%v'", strings.Split(r.RemoteAddr, ":")[0], r.RequestURI, rec.Code, rec.Err))
//Send response with json error description
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(500)
w.Write([]byte(rec.ToJSONString()))
// Unknown panic
default:
raven.CaptureError(fmt.Errorf("%v", rec), nil)
logger.Error(fmt.Sprintf("%v [ %v ] Unknown Error catched: '%v'", strings.Split(r.RemoteAddr, ":")[0], r.RequestURI, rec))
http.Error(w, http.StatusText(500), 500)
panic(rec)
}
}
}()
/*
# Permissions checks
*/
// 1. Prevent bruteforce of sessionID
ok, duration := bruteforce.Check(strings.Split(r.RemoteAddr, ":")[0])
if !ok {
logger.Warning("IP " + strings.Split(r.RemoteAddr, ":")[0] + " banned by bruteforce (no session) for " + strconv.FormatInt(duration, 10) + " sec.")
http.Error(w, http.StatusText(429), 429)
return
}
// 2. Check if session started and getting session info
sess, err := sessions.SessionsStorage.Get(r)
if err != nil {
logger.Warning(err.Error())
http.Error(w, http.StatusText(401), 401)
return
}
// 3. Clear IP in bruteforce check
bruteforce.Clean(strings.Split(r.RemoteAddr, ":")[0])
// 4. Check for timeout before actions for particular handlers
if err := bruteforce.CheckTimeout(r, sessions.SessionsStorage); err != nil {
http.Error(w, http.StatusText(429), 429)
log.Println("Timeout error: ", err)
return
}
// 5. Register session activity for sessions timeout
sessions.SessionsStorage.RegisterActivity(r)
// 6. Check module access permisions
if sess.Username != "root" {
userInfo := users.Get(sess.Username)
if userInfo == nil {
panicerr.Core.Auth("Can't get template" + sess.Username)
}
allowedModules := userInfo.GetTemplate().Modules
if err != nil || !hasPermissions(r.RequestURI, allowedModules) {
http.Error(w, http.StatusText(403), 403)
logger.Warning(fmt.Sprintf("Permission denied to access '%v' for %v as user %v", r.RequestURI, strings.Split(r.RemoteAddr, ":")[0], sess.Username))
return
}
}
// 7. Check for simultaneous connections from a single user
router.CheckNumConnection(sess.Username)
/*
# Append handler to running list for tracking
*/
func() {
// Make it atomic
router.mutex.Lock()
defer router.mutex.Unlock()
// Increment handler ID
router.curID++
// create handler struct
handler = rhandler{
id: router.curID,
URI: r.RequestURI,
Username: sess.Username,
IP: r.RemoteAddr,
StartTime: time.Now().Format(time.StampMilli),
SessionID: sess.ID,
}
// Append new handler to list
router.handlers[router.curID] = handler
}()
/*
# Add action to journal
*/
journal.Add(journal.Operation{
SessionID: sess.ID,
Date: time.Now().Format(journal.TimeLayout),
Username: sess.Username,
Operation: moduleutils.GetCurrentModule(r.RequestURI),
Content: r.RequestURI,
//Extra:
})
/*
# Run handler's function
*/
f(w, r, sessions.SessionsStorage)
/*
# Make api trigger call
*/
plugins.DefaultManager.Trigger(w, r, sess)
}
// Insert func to gorilla/mux router
return router.HandleFunc(path, routerFunc)
}
// HandleLoginFunc is uniq handler for Authorization and create new session only
func (router *Router) HandleLoginFunc(path string, f func(http.ResponseWriter, *http.Request, *sessions.Sessions)) *mux.Route {
routerFunc := func(w http.ResponseWriter, r *http.Request) {
/*
Set headers
*/
setHeaderNoCache(w)
/*
Defered run and catch panics
*/
defer func() {
// Catch all panics from handler
if rec := recover(); rec != nil {
switch rec := rec.(type) {
// Panicerr catched
case panicerr.Error:
logger.Info(fmt.Sprintf("%v [ %v ] Error catched: Code '%v' Text '%v'", strings.Split(r.RemoteAddr, ":")[0], r.RequestURI, rec.Code, rec.Err))
//Send response with json error description
w.WriteHeader(500)
w.Write([]byte(rec.ToJSONString()))
// Unknown panic
default:
raven.CaptureError(fmt.Errorf("%v", rec), nil)
logger.Error(fmt.Sprintf("%v [ %v ] Unknown Error catched: '%v'", strings.Split(r.RemoteAddr, ":")[0], r.RequestURI, rec))
http.Error(w, http.StatusText(500), 500)
panic(rec)
}
}
}()
/*
Run handler's function
*/
f(w, r, sessions.SessionsStorage)
/*
Make api trigger call
*/
plugins.DefaultManager.Trigger(w, r, nil)
}
// Insert func to gorilla/mux router
return router.HandleFunc(path, routerFunc)
}
// CheckNumConnection - Checking for number of simultaneous requests for user
// panicerr if exceeded
func (router *Router) CheckNumConnection(username string) {
handlersCount := 0
router.mutex.RLock()
defer router.mutex.RUnlock()
for _, handler := range router.handlers {
if handler.Username == username {
handlersCount++
if handlersCount > cfg.MaxHandlersForUser {
panicerr.Handlers.RequestsExceeded(fmt.Sprint("Exceeded the number of simultaneous requests for user (", cfg.MaxHandlersForUser, ")"))
}
}
}
}
// Check for user permissions to module for /uri
func hasPermissions(path string, modules []string) bool {
// If no modules allow access
if len(modules) == 0 {
return true
}
// Iterate and check for each module in grant list
for _, userModule := range modules {
if userModule == moduleutils.GetCurrentModule(path) {
return true
}
}
return false
}
// Set http headers to no-cache, content json
func setHeaderNoCache(w http.ResponseWriter) {
w.Header().Set("Server", cfg.WebServerName)
w.Header().Set("Version", cfg.Version)
w.Header().Set("Cache-Control", "post-check=0, pre-check=0, no-store, no-cache, must-revalidate, max-age=0")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "-1")
w.Header().Set("Content-Type", "application/json")
}
func isIgnored(path string) bool {
var ignored = []string{
"/api/info/actualizer/",
"/api/shell/console",
"/api/core/livesysstat",
}
return stringutils.Contains(ignored, path)
}