support protocol rule

This commit is contained in:
Yuzuki616
2023-07-20 21:14:18 +08:00
parent 7cc57fe2ba
commit adf98fbc81
6 changed files with 92 additions and 48 deletions

View File

@@ -3,15 +3,16 @@ package limiter
import (
"errors"
"fmt"
"regexp"
"sync"
"time"
"github.com/Yuzuki616/V2bX/api/panel"
"github.com/Yuzuki616/V2bX/common/format"
"github.com/Yuzuki616/V2bX/conf"
"github.com/juju/ratelimit"
log "github.com/sirupsen/logrus"
"github.com/xtls/xray-core/common/task"
"regexp"
"sync"
"time"
)
var limitLock sync.RWMutex
@@ -32,7 +33,7 @@ func Init() {
}
type Limiter struct {
Rules []*regexp.Regexp
DomainRules []*regexp.Regexp
ProtocolRules []string
SpeedLimit int
UserLimitInfo *sync.Map // Key: Uid value: UserLimitInfo

View File

@@ -1,14 +1,15 @@
package limiter
import (
"reflect"
"regexp"
"github.com/Yuzuki616/V2bX/api/panel"
)
func (l *Limiter) CheckDomainRule(destination string) (reject bool) {
// have rule
for i := range l.Rules {
if l.Rules[i].MatchString(destination) {
for i := range l.DomainRules {
if l.DomainRules[i].MatchString(destination) {
reject = true
break
}
@@ -26,9 +27,11 @@ func (l *Limiter) CheckProtocolRule(protocol string) (reject bool) {
return
}
func (l *Limiter) UpdateRule(newRuleList []*regexp.Regexp) error {
if !reflect.DeepEqual(l.Rules, newRuleList) {
l.Rules = newRuleList
func (l *Limiter) UpdateRule(rule *panel.Rules) error {
l.DomainRules = make([]*regexp.Regexp, len(rule.Regexp))
for i := range rule.Regexp {
l.DomainRules[i] = regexp.MustCompile(rule.Regexp[i])
}
l.ProtocolRules = rule.Protocol
return nil
}