2021-12-12 11:48:01 +08:00
|
|
|
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
|
|
|
|
|
|
|
|
package nodes
|
|
|
|
|
|
2022-04-25 11:11:25 +08:00
|
|
|
import (
|
|
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/iplibrary"
|
|
|
|
|
"net/http"
|
|
|
|
|
)
|
2021-12-12 11:48:01 +08:00
|
|
|
|
|
|
|
|
func (this *HTTPRequest) doRequestLimit() (shouldStop bool) {
|
2022-04-25 11:11:25 +08:00
|
|
|
// 是否在全局名单中
|
|
|
|
|
_, isInAllowedList := iplibrary.AllowIP(this.RemoteAddr(), this.ReqServer.Id)
|
|
|
|
|
if isInAllowedList {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-12 11:48:01 +08:00
|
|
|
// 检查请求Body尺寸
|
|
|
|
|
// TODO 处理分片提交的内容
|
|
|
|
|
if this.web.RequestLimit.MaxBodyBytes() > 0 &&
|
|
|
|
|
this.RawReq.ContentLength > this.web.RequestLimit.MaxBodyBytes() {
|
|
|
|
|
this.writeCode(http.StatusRequestEntityTooLarge)
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置连接相关参数
|
|
|
|
|
if this.web.RequestLimit.MaxConns > 0 || this.web.RequestLimit.MaxConnsPerIP > 0 {
|
|
|
|
|
requestConn := this.RawReq.Context().Value(HTTPConnContextKey)
|
|
|
|
|
if requestConn != nil {
|
|
|
|
|
clientConn, ok := requestConn.(ClientConnInterface)
|
|
|
|
|
if ok && !clientConn.IsBound() {
|
2022-01-01 20:15:39 +08:00
|
|
|
if !clientConn.Bind(this.ReqServer.Id, this.requestRemoteAddr(true), this.web.RequestLimit.MaxConns, this.web.RequestLimit.MaxConnsPerIP) {
|
2021-12-12 11:48:01 +08:00
|
|
|
this.writeCode(http.StatusTooManyRequests)
|
2022-01-01 20:15:39 +08:00
|
|
|
this.Close()
|
2021-12-12 11:48:01 +08:00
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|