From 55f9b288695ebfb024aacd84911da17d264eef7f Mon Sep 17 00:00:00 2001 From: Yuzuki616 Date: Tue, 18 Feb 2025 09:52:48 +0900 Subject: [PATCH] add limiter config --- common/number/number.go | 28 ++++++++++++++++++++++++++++ conf/node.go | 6 ++++++ handler/node.go | 3 +++ 3 files changed, 37 insertions(+) create mode 100644 common/number/number.go diff --git a/common/number/number.go b/common/number/number.go new file mode 100644 index 0000000..9a5f7f8 --- /dev/null +++ b/common/number/number.go @@ -0,0 +1,28 @@ +package number + +type Number interface { + ~int | ~int8 | ~int16 | ~int32 | ~int64 | + ~float32 | ~float64 | + ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 +} + +func SelectBigger[T Number](num1 T, num2 T) T { + if num1 >= num2 { + return num1 + } + return num2 +} + +func SelectSmaller[T Number](num1 T, num2 T) T { + if num1 <= num2 { + return num1 + } + return num2 +} + +func SelectNotZero[T Number](num1 T, num2 T) T { + if num1 != 0 { + return num1 + } + return num1 +} diff --git a/conf/node.go b/conf/node.go index 9560438..4b387b1 100644 --- a/conf/node.go +++ b/conf/node.go @@ -26,6 +26,7 @@ type Options struct { Acme string `json:"Acme"` Cert Cert `json:"Cert"` Hook Hook `json:"Hook"` + Limit Limit `json:"Limit"` Expand map[string]interface{} `json:"Other"` } @@ -49,6 +50,11 @@ type Hook struct { AfterDelNode string `json:"AfterDelNode"` } +type Limit struct { + IPLimit int `json:"IPLimit"` + SpeedLimit uint64 `json:"SpeedLimit"` +} + type Node struct { Name string `json:"Name"` Remote Remote `json:"-"` diff --git a/handler/node.go b/handler/node.go index b2d22e4..1b8e89e 100644 --- a/handler/node.go +++ b/handler/node.go @@ -5,6 +5,7 @@ import ( "github.com/InazumaV/Ratte-Interface/core" "github.com/InazumaV/Ratte-Interface/panel" "github.com/InazumaV/Ratte/common/maps" + "github.com/InazumaV/Ratte/common/number" ) func (h *Handler) PullNodeHandle(n *panel.NodeInfo) error { @@ -43,6 +44,8 @@ func (h *Handler) PullNodeHandle(n *panel.NodeInfo) error { } ni := (*core.NodeInfo)(n) ni.OtherOptions = maps.Merge[string, any](ni.OtherOptions, h.Options.Expand) + ni.Limit.IPLimit = number.SelectBigger(ni.Limit.IPLimit, h.Limit.IPLimit) + ni.Limit.SpeedLimit = number.SelectBigger(ni.Limit.SpeedLimit, int(h.Limit.SpeedLimit)) err = h.c.AddNode(&core.AddNodeParams{ NodeInfo: ni, TlsOptions: core.TlsOptions{