github.com/TeaOSLab/EdgeNode@v1.3.8/internal/nodes/http_request_traffic_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/EdgeCommon/pkg/serverconfigs"
     7  )
     8  
     9  // 流量限制
    10  func (this *HTTPRequest) doTrafficLimit(status *serverconfigs.TrafficLimitStatus) (blocked bool) {
    11  	if status == nil {
    12  		return false
    13  	}
    14  
    15  	// 如果是网站单独设置的流量限制,则检查是否已关闭
    16  	var config = this.ReqServer.TrafficLimit
    17  	if (config == nil || !config.IsOn) && status.PlanId == 0 {
    18  		return false
    19  	}
    20  
    21  	// 如果是套餐设置的流量限制,即使套餐变更了(变更套餐或者变更套餐的限制),仍然会提示流量超限
    22  
    23  	this.tags = append(this.tags, "trafficLimit")
    24  
    25  	var statusCode = 509
    26  	this.writer.statusCode = statusCode
    27  	this.ProcessResponseHeaders(this.writer.Header(), statusCode)
    28  
    29  	this.writer.Header().Set("Content-Type", "text/html; charset=utf-8")
    30  	this.writer.WriteHeader(statusCode)
    31  
    32  	// check plan traffic limit
    33  	if (config == nil || !config.IsOn) && this.ReqServer.PlanId() > 0 && this.nodeConfig != nil {
    34  		var planConfig = this.nodeConfig.FindPlan(this.ReqServer.PlanId())
    35  		if planConfig != nil && planConfig.TrafficLimit != nil && planConfig.TrafficLimit.IsOn {
    36  			config = planConfig.TrafficLimit
    37  		}
    38  	}
    39  
    40  	if config != nil && len(config.NoticePageBody) != 0 {
    41  		_, _ = this.writer.WriteString(this.Format(config.NoticePageBody))
    42  	} else {
    43  		_, _ = this.writer.WriteString(this.Format(serverconfigs.DefaultTrafficLimitNoticePageBody))
    44  	}
    45  
    46  	return true
    47  }