mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-08 11:20:27 +08:00
优化WAF性能
This commit is contained in:
@@ -331,11 +331,11 @@ func (this *FileList) Close() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *FileList) GetDBIndex(hash string) uint64 {
|
func (this *FileList) GetDBIndex(hash string) uint64 {
|
||||||
return fnv.Hash(hash) % CountFileDB
|
return fnv.HashString(hash) % CountFileDB
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *FileList) getDB(hash string) *FileListDB {
|
func (this *FileList) getDB(hash string) *FileListDB {
|
||||||
return this.dbList[fnv.Hash(hash)%CountFileDB]
|
return this.dbList[fnv.HashString(hash)%CountFileDB]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *FileList) remove(hash string) (notFound bool, err error) {
|
func (this *FileList) remove(hash string) (notFound bool, err error) {
|
||||||
|
|||||||
@@ -74,12 +74,12 @@ func (this *Cache) Write(key string, value interface{}, expiredAt int64) (ok boo
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
currentTimestamp := time.Now().Unix()
|
var currentTimestamp = utils.UnixTime()
|
||||||
if expiredAt <= currentTimestamp {
|
if expiredAt <= currentTimestamp {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
maxExpiredAt := currentTimestamp + 30*86400
|
var maxExpiredAt = currentTimestamp + 30*86400
|
||||||
if expiredAt > maxExpiredAt {
|
if expiredAt > maxExpiredAt {
|
||||||
expiredAt = maxExpiredAt
|
expiredAt = maxExpiredAt
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,9 +7,20 @@ const (
|
|||||||
prime64 uint64 = 1099511628211
|
prime64 uint64 = 1099511628211
|
||||||
)
|
)
|
||||||
|
|
||||||
// Hash
|
// HashString
|
||||||
// 非unique Hash
|
// 非unique Hash
|
||||||
func Hash(key string) uint64 {
|
func HashString(key string) uint64 {
|
||||||
|
var hash = offset64
|
||||||
|
for _, b := range key {
|
||||||
|
hash ^= uint64(b)
|
||||||
|
hash *= prime64
|
||||||
|
}
|
||||||
|
return hash
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hash
|
||||||
|
// 非unique Hash
|
||||||
|
func Hash(key []byte) uint64 {
|
||||||
var hash = offset64
|
var hash = offset64
|
||||||
for _, b := range key {
|
for _, b := range key {
|
||||||
hash ^= uint64(b)
|
hash ^= uint64(b)
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
|
|
||||||
func TestHash(t *testing.T) {
|
func TestHash(t *testing.T) {
|
||||||
for _, key := range []string{"costarring", "liquid", "hello"} {
|
for _, key := range []string{"costarring", "liquid", "hello"} {
|
||||||
var h = fnv.Hash(key)
|
var h = fnv.HashString(key)
|
||||||
t.Log(key + " => " + types.String(h))
|
t.Log(key + " => " + types.String(h))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
package checkpoints
|
package checkpoints
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeNode/internal/waf/requests"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
type Checkpoint struct {
|
type Checkpoint struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,3 +35,16 @@ func (this *Checkpoint) Start() {
|
|||||||
func (this *Checkpoint) Stop() {
|
func (this *Checkpoint) Stop() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *Checkpoint) RequestBodyIsEmpty(req requests.Request) bool {
|
||||||
|
if req.WAFRaw().ContentLength == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
var method = req.WAFRaw().Method
|
||||||
|
if method == http.MethodHead || method == http.MethodGet {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
@@ -18,6 +18,11 @@ func (this *RequestAllCheckpoint) RequestValue(req requests.Request, param strin
|
|||||||
valueBytes = append(valueBytes, req.WAFRaw().URL.RequestURI()...)
|
valueBytes = append(valueBytes, req.WAFRaw().URL.RequestURI()...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if this.RequestBodyIsEmpty(req) {
|
||||||
|
value = valueBytes
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if req.WAFRaw().Body != nil {
|
if req.WAFRaw().Body != nil {
|
||||||
valueBytes = append(valueBytes, ' ')
|
valueBytes = append(valueBytes, ' ')
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,11 @@ type RequestBodyCheckpoint struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *RequestBodyCheckpoint) RequestValue(req requests.Request, param string, options maps.Map) (value interface{}, sysErr error, userErr error) {
|
func (this *RequestBodyCheckpoint) RequestValue(req requests.Request, param string, options maps.Map) (value interface{}, sysErr error, userErr error) {
|
||||||
|
if this.RequestBodyIsEmpty(req) {
|
||||||
|
value = ""
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if req.WAFRaw().Body == nil {
|
if req.WAFRaw().Body == nil {
|
||||||
value = ""
|
value = ""
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -12,6 +12,11 @@ type RequestFormArgCheckpoint struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *RequestFormArgCheckpoint) RequestValue(req requests.Request, param string, options maps.Map) (value interface{}, sysErr error, userErr error) {
|
func (this *RequestFormArgCheckpoint) RequestValue(req requests.Request, param string, options maps.Map) (value interface{}, sysErr error, userErr error) {
|
||||||
|
if this.RequestBodyIsEmpty(req) {
|
||||||
|
value = ""
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if req.WAFRaw().Body == nil {
|
if req.WAFRaw().Body == nil {
|
||||||
value = ""
|
value = ""
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -17,6 +17,11 @@ type RequestUploadCheckpoint struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *RequestUploadCheckpoint) RequestValue(req requests.Request, param string, options maps.Map) (value interface{}, sysErr error, userErr error) {
|
func (this *RequestUploadCheckpoint) RequestValue(req requests.Request, param string, options maps.Map) (value interface{}, sysErr error, userErr error) {
|
||||||
|
if this.RequestBodyIsEmpty(req) {
|
||||||
|
value = ""
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
value = ""
|
value = ""
|
||||||
if param == "minSize" || param == "maxSize" {
|
if param == "minSize" || param == "maxSize" {
|
||||||
value = 0
|
value = 0
|
||||||
|
|||||||
@@ -22,6 +22,11 @@ func (this *ResponseBodyCheckpoint) RequestValue(req requests.Request, param str
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *ResponseBodyCheckpoint) ResponseValue(req requests.Request, resp *requests.Response, param string, options maps.Map) (value interface{}, sysErr error, userErr error) {
|
func (this *ResponseBodyCheckpoint) ResponseValue(req requests.Request, resp *requests.Response, param string, options maps.Map) (value interface{}, sysErr error, userErr error) {
|
||||||
|
if resp.ContentLength == 0 {
|
||||||
|
value = ""
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
value = ""
|
value = ""
|
||||||
if resp != nil && resp.Body != nil {
|
if resp != nil && resp.Body != nil {
|
||||||
if len(resp.BodyData) > 0 {
|
if len(resp.BodyData) > 0 {
|
||||||
|
|||||||
Reference in New Issue
Block a user