github.com/TeaOSLab/EdgeNode@v1.3.8/internal/nodes/http_request_limit.go (about)

     1  // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
     2  
     3  package nodes
     4  
     5  import (
     6  	"github.com/TeaOSLab/EdgeNode/internal/iplibrary"
     7  	"net/http"
     8  )
     9  
    10  func (this *HTTPRequest) doRequestLimit() (shouldStop bool) {
    11  	// 是否在全局名单中
    12  	_, isInAllowedList, _ := iplibrary.AllowIP(this.RemoteAddr(), this.ReqServer.Id)
    13  	if isInAllowedList {
    14  		return false
    15  	}
    16  
    17  	// 检查请求Body尺寸
    18  	// TODO 处理分片提交的内容
    19  	if this.web.RequestLimit.MaxBodyBytes() > 0 &&
    20  		this.RawReq.ContentLength > this.web.RequestLimit.MaxBodyBytes() {
    21  		this.writeCode(http.StatusRequestEntityTooLarge, "", "")
    22  		return true
    23  	}
    24  
    25  	// 设置连接相关参数
    26  	if this.web.RequestLimit.MaxConns > 0 || this.web.RequestLimit.MaxConnsPerIP > 0 {
    27  		var requestConn = this.RawReq.Context().Value(HTTPConnContextKey)
    28  		if requestConn != nil {
    29  			clientConn, ok := requestConn.(ClientConnInterface)
    30  			if ok && !clientConn.IsBound() {
    31  				if !clientConn.Bind(this.ReqServer.Id, this.requestRemoteAddr(true), this.web.RequestLimit.MaxConns, this.web.RequestLimit.MaxConnsPerIP) {
    32  					this.writeCode(http.StatusTooManyRequests, "", "")
    33  					this.Close()
    34  					return true
    35  				}
    36  			}
    37  		}
    38  	}
    39  
    40  	return false
    41  }