limiter

package
v1.1.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 13, 2026 License: MPL-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package limiter is to control the links that go into the dispatcher

Index

Constants

View Source
const (
	// OnlineIPExpiry IP 无活动超过该时长即视为下线,释放设备名额
	OnlineIPExpiry = time.Minute
)

Variables

This section is empty.

Functions

func PurgeStaleDeviceIPs added in v1.1.9

func PurgeStaleDeviceIPs(onlineIPs map[string]struct{}, activeMap map[string]time.Time, expiry time.Duration) int

PurgeStaleDeviceIPs 清理超过 expiry 未活跃的 IP,返回剩余活跃 IP 数。

Types

type GlobalDeviceChecker added in v1.1.9

type GlobalDeviceChecker struct {
	// contains filtered or unexported fields
}

GlobalDeviceChecker 基于共享 Redis 的跨节点设备限制检查器。 缓存 key 按 UID 聚合("UID|<uid>"),value 为 IP -> 最近活跃时间(unix 秒), 所有指向同一 Redis 的节点共同维护一份用户在线 IP 集合。 供 Xray 系 limiter 与 Hysteria2 / AnyTLS / TUIC 服务共用。

func NewGlobalDeviceChecker added in v1.1.9

func NewGlobalDeviceChecker(config *GlobalDeviceLimitConfig) *GlobalDeviceChecker

NewGlobalDeviceChecker 未启用全局限制时返回 nil;nil 检查器的 Allow 恒放行。

func (*GlobalDeviceChecker) Allow added in v1.1.9

func (g *GlobalDeviceChecker) Allow(uid int, ip string, deviceLimit int) bool

Allow 判定 uid 的 ip 是否允许在线(全局口径)。 已在线 IP 刷新活跃时间并放行;新 IP 在名额未满时登记放行,超限拒绝。

type GlobalDeviceLimitConfig

type GlobalDeviceLimitConfig struct {
	Enable        bool   `mapstructure:"Enable"`
	RedisNetwork  string `mapstructure:"RedisNetwork"` // tcp or unix
	RedisAddr     string `mapstructure:"RedisAddr"`    // host:port, or /path/to/unix.sock
	RedisUsername string `mapstructure:"RedisUsername"`
	RedisPassword string `mapstructure:"RedisPassword"`
	RedisDB       int    `mapstructure:"RedisDB"`
	Timeout       int    `mapstructure:"Timeout"`
	Expiry        int    `mapstructure:"Expiry"` // second
}

type GuardReader added in v1.1.9

type GuardReader struct {
	// contains filtered or unexported fields
}

func (*GuardReader) ReadMultiBuffer added in v1.1.9

func (r *GuardReader) ReadMultiBuffer() (buf.MultiBuffer, error)

type GuardWriter added in v1.1.9

type GuardWriter struct {
	// contains filtered or unexported fields
}

func (*GuardWriter) Close added in v1.1.9

func (w *GuardWriter) Close() error

func (*GuardWriter) WriteMultiBuffer added in v1.1.9

func (w *GuardWriter) WriteMultiBuffer(mb buf.MultiBuffer) error

type InboundInfo

type InboundInfo struct {
	Tag            string
	NodeSpeedLimit uint64
	UserInfo       *sync.Map // Key: user identifier (usually UID string) -> UserInfo
	BucketHub      *sync.Map // Key: user identifier -> *rate.Limiter
	UserOnlineIP   *sync.Map // Key: user identifier -> *sync.Map (Key: IP, Value: onlineEntry)
	GlobalLimit    *GlobalDeviceChecker
}

type Limiter

type Limiter struct {
	InboundInfo *sync.Map // Key: Tag, Value: *InboundInfo
}

func New

func New() *Limiter

func (*Limiter) AddInboundLimiter

func (l *Limiter) AddInboundLimiter(tag string, nodeSpeedLimit uint64, userList *[]api.UserInfo, globalLimit *GlobalDeviceLimitConfig) error

func (*Limiter) DeleteInboundLimiter

func (l *Limiter) DeleteInboundLimiter(tag string) error

func (*Limiter) EnsureOnline added in v1.1.9

func (l *Limiter) EnsureOnline(tag, userKey, ip string) bool

EnsureOnline 供上行方向(客户端→服务端有真实数据)周期性复查: IP 仍在线则刷新活跃时间;若名额已被占满且该 IP 已被挤出, 返回 false(调用方应断开连接)。

func (*Limiter) GetOnlineDevice

func (l *Limiter) GetOnlineDevice(tag string) (*[]api.OnlineUser, error)

func (*Limiter) GetUserBucket

func (l *Limiter) GetUserBucket(tag string, userKey string, ip string) (limiter *rate.Limiter, SpeedLimit bool, Reject bool)

func (*Limiter) GuardReader added in v1.1.9

func (l *Limiter) GuardReader(reader buf.Reader, tag, userKey, ip string) buf.Reader

GuardReader 上行方向(读客户端数据):核查并续期。

func (*Limiter) GuardUplinkWriter added in v1.1.9

func (l *Limiter) GuardUplinkWriter(writer buf.Writer, tag, userKey, ip string) buf.Writer

GuardUplinkWriter 上行方向的写端(承载客户端→远端的数据):核查并续期。

func (*Limiter) GuardWriter added in v1.1.9

func (l *Limiter) GuardWriter(writer buf.Writer, tag, userKey, ip string) buf.Writer

GuardWriter 下行方向(向客户端写数据):只核查不续期。

func (*Limiter) RateReader added in v0.9.8

func (l *Limiter) RateReader(reader buf.Reader, limiter *rate.Limiter) buf.Reader

func (*Limiter) RateWriter

func (l *Limiter) RateWriter(writer buf.Writer, limiter *rate.Limiter) buf.Writer

func (*Limiter) UpdateInboundLimiter

func (l *Limiter) UpdateInboundLimiter(tag string, updatedUserList *[]api.UserInfo) error

func (*Limiter) VerifyOnline added in v1.1.9

func (l *Limiter) VerifyOnline(tag, userKey, ip string) bool

VerifyOnline 供下行方向(远端→客户端)周期性复查:只读、不续期、不登记。 下行流量不能证明客户端仍然存活——客户端异常离线后,远端仍可能持续向 残留连接推送数据;若据此续期,离线 IP 会被无限"续命",名额永不释放。 放行条件:该 IP 仍持有新鲜名额,或该用户尚有空余名额。

type Reader added in v0.9.8

type Reader struct {
	// contains filtered or unexported fields
}

func (*Reader) ReadMultiBuffer added in v0.9.8

func (r *Reader) ReadMultiBuffer() (buf.MultiBuffer, error)

func (*Reader) ReadMultiBufferTimeout added in v0.9.8

func (r *Reader) ReadMultiBufferTimeout(timeout time.Duration) (buf.MultiBuffer, error)

type UserInfo

type UserInfo struct {
	UID         int
	SpeedLimit  uint64
	DeviceLimit int
}

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

func (*Writer) Close

func (w *Writer) Close() error

func (*Writer) WriteMultiBuffer

func (w *Writer) WriteMultiBuffer(mb buf.MultiBuffer) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL