fix: apply per-client limits to udp listener

This commit is contained in:
sagitchu
2026-04-27 23:05:31 +08:00
parent 46394388b1
commit 7a9ba8bd81
4 changed files with 194 additions and 2 deletions
+30
View File
@@ -0,0 +1,30 @@
package conn
import (
"io"
"testing"
corelogger "github.com/go-gost/core/logger"
xlogger "github.com/go-gost/x/logger"
)
func TestIPLimitKeyCreatesIndependentLimiters(t *testing.T) {
limiter := NewConnLimiter(
LimitsOption("$$ 1"),
LoggerOption(xlogger.NewLogger(xlogger.OutputOption(io.Discard), xlogger.LevelOption(corelogger.ErrorLevel))),
)
first := limiter.Limiter("192.0.2.1")
second := limiter.Limiter("192.0.2.2")
if first == nil || second == nil {
t.Fatalf("expected non-nil per-IP limiters")
}
if !first.Allow(1) {
t.Fatalf("expected first IP first connection to be allowed")
}
if first.Allow(1) {
t.Fatalf("expected first IP second connection to be rejected")
}
if !second.Allow(1) {
t.Fatalf("expected second IP first connection to be allowed independently")
}
}
+28
View File
@@ -0,0 +1,28 @@
package traffic
import (
"context"
"io"
"testing"
corelogger "github.com/go-gost/core/logger"
xlogger "github.com/go-gost/x/logger"
)
func TestCIDRLimitCreatesIndependentClientLimiters(t *testing.T) {
limiter := NewTrafficLimiter(
LimitsOption("0.0.0.0/0 2B 2B"),
LoggerOption(xlogger.NewLogger(xlogger.OutputOption(io.Discard), xlogger.LevelOption(corelogger.ErrorLevel))),
)
first := limiter.In(context.Background(), "192.0.2.1:1000")
second := limiter.In(context.Background(), "192.0.2.2:1000")
if first == nil || second == nil {
t.Fatalf("expected non-nil CIDR client limiters")
}
if first == second {
t.Fatalf("expected different clients to receive independent limiter instances")
}
if first.Limit() != 2 || second.Limit() != 2 {
t.Fatalf("expected both limits to be 2, got %d and %d", first.Limit(), second.Limit())
}
}