forked from gramework/gramework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
259 lines (219 loc) · 7.12 KB
/
types.go
File metadata and controls
259 lines (219 loc) · 7.12 KB
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
// Copyright 2017-present Kirill Danshin and Gramework contributors
// Copyright 2019-present Highload LTD (UK CN: 11893420)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
package gramework
import (
"fmt"
"sync"
"time"
"github.com/microcosm-cc/bluemonday"
"github.com/apex/log"
"github.com/gramework/utils/nocopy"
"github.com/valyala/fasthttp"
)
type (
ipList struct {
list map[string]struct{}
mu *sync.RWMutex
}
suspect struct {
hackAttempts int32
}
suspectsList struct {
list map[string]*suspect
mu *sync.RWMutex
}
// App represents a gramework app
App struct {
defaultRouter *Router
domains map[string]*Router
_ [8]byte // callback
firewall *firewall
firewallInit *sync.Once
Flags *Flags
flagsQueue []Flag
Logger log.Interface
name string
Settings Settings
TLSEmails []string
TLSPort uint16
middlewares []func(*Context)
middlewaresAfterRequest []func(*Context)
preMiddlewares []func(*Context)
domainListLock *sync.RWMutex
middlewaresAfterRequestMu *sync.RWMutex
middlewaresMu *sync.RWMutex
preMiddlewaresMu *sync.RWMutex
EnableFirewall bool
flagsRegistered bool
HandleUnknownDomains bool
seed uintptr
cookieDomain string
cookiePath string
NoDefaultPanicHandler bool
PanicHandlerNoPoweredBy bool
PanicHandlerCustomLayout string
internalLog *log.Entry
cookieExpire time.Duration
// Gramework Protection's max detections of suspect before ban
maxHackAttempts *int32
// Gramework Protection's protected endpoint prefixes
protectedPrefixes map[string]struct{}
// Gramework Protection's protected paths of endpoints
protectedEndpoints map[string]struct{}
// Gramework Protection's trusted ip list
trustedIP *ipList
// Gramework Protection's untrusted (banned) ip list
untrustedIP *ipList
// Gramework Protection's suspects ip list
suspectedIP *suspectsList
serverBase *fasthttp.Server
runningServers []runningServerInfo
runningServersMu *sync.Mutex
behind Behind
sanitizerPolicy *bluemonday.Policy
DefaultCacheOptions *CacheOptions
}
// CacheOptions is a handler cache configuration structure.
CacheOptions struct {
// TTL is the time that cached response is valid
TTL time.Duration
// Cacheable function returns if current request is cacheable.
// By deafult, any request with Authentication header or any Cookies will not be cached for security reasons.
// If you want to cache responses for authorized users, please replace both Cacheable and CacheKey functions
// to make sure that CacheKey includes something like session id.
Cacheable func(ctx *Context) bool
// CacheKey function returns the cache key for current request
CacheKey func(ctx *Context) []byte
// ReadCache allows for cache engine replacement. By default, gramework uses github.com/VictoriaMetrics/fastcache.
// ReadCache returns the value and boolean if the value was found and still valid.
ReadCache func(ctx *Context, key []byte) ([]byte, bool)
// StoreCache allows for cache engine replacement. By default, gramework uses github.com/VictoriaMetrics/fastcache.
StoreCache func(ctx *Context, key, value []byte, ttl time.Duration)
// CacheableHeaders is a list of headers that gramework can cache.
// Note, that if X-ABC is present both in cacheable and noncacheable header lists,
// it will not be cached.
CacheableHeaders []string // slice of canonical header names
// NonCacheableHeaders is a list of headers that gramework can not cache.
// Note, that if X-ABC is present both in cacheable and noncacheable header lists,
// it will not be cached.
NonCacheableHeaders []string
}
runningServerInfo struct {
bind string
srv *fasthttp.Server
}
contextKey string
// Context is a gramework request context
Context struct {
*fasthttp.RequestCtx
nocopy nocopy.NoCopy
Logger log.Interface
App *App
auth *Auth
Cookies Cookies
requestID string
middlewaresShouldStopProcessing bool
subPrefixes []string
middlewareKilledReq bool
writer func(p []byte) (int, error)
}
// GQLRequest is a GraphQL request structure
GQLRequest struct {
Query string `json:"query"`
OperationName string `json:"operationName"`
Variables map[string]interface{} `json:"variables"`
}
// Cookies handles a typical cookie storage
Cookies struct {
Storage map[string]string
Mu sync.RWMutex
}
// Settings for an App instance
Settings struct {
Firewall FirewallSettings
}
// FirewallSettings represents a new firewall settings.
// Internal firewall representation copies this settings
// atomically.
FirewallSettings struct {
// MaxReqPerMin is a max request per minute count
MaxReqPerMin int64
// BlockTimeout in seconds
BlockTimeout int64
}
firewall struct {
// Store a copy of current settings
MaxReqPerMin *int64
BlockTimeout *int64
blockListMutex sync.Mutex
requestCounterMutex sync.Mutex
blockList map[string]int64
requestCounter map[string]int64
}
// Flags is a flags storage
Flags struct {
values map[string]Flag
}
// Flag is a flag representation
Flag struct {
Name string
Description string
Default string
Value *string
}
// Router handles internal handler conversion etc.
Router struct {
router *router
httprouter *Router
httpsrouter *Router
root *Router
app *App
mu sync.RWMutex
rootHandler []staticHandler
}
// SubRouter handles subs registration
// like app.Sub("v1").GET("someRoute", "hi")
SubRouter struct {
parent routerable
prefix string
prefixes []string
}
routerable interface {
handleReg(method, route string, handler interface{}, prefixes []string)
determineHandler(handler interface{}) func(*Context)
}
// RequestHandler describes a standard request handler type
RequestHandler func(*Context)
// RequestHandlerErr describes a standard request handler with error returned type
RequestHandlerErr func(*Context) error
// Auth is a struct that handles
// context's basic auth features
Auth struct {
login string
pass string
parsed bool
// if error occurred during parsing,
// it will be always returned for current
// context
err error
ctx *Context
}
// HTML type used to determine prerendered strings
// as an HTML and give proper content-type
HTML string
// JSON type used to determine prerendered strings
// as an JSON and give proper content-type
JSON string
)
// crazy hack to solve nocopy false positive
var _ = fmt.Sprintf("%v", func() interface{} {
ctx := Context{}
return &ctx.nocopy
}())