From 01d4ec50459ddb108125f84c6157a306491e5e35 Mon Sep 17 00:00:00 2001 From: xxxsen Date: Thu, 18 Apr 2024 20:55:17 +0800 Subject: [PATCH] feat: add blacklist peerid support --- cleaner/cleaner.go | 15 ++++++++++++--- cleaner/config.go | 7 +++++++ config/config.go | 4 +++- go.mod | 2 +- go.sum | 12 ++---------- main.go | 6 ++++-- 6 files changed, 29 insertions(+), 17 deletions(-) diff --git a/cleaner/cleaner.go b/cleaner/cleaner.go index 762510a..2426790 100644 --- a/cleaner/cleaner.go +++ b/cleaner/cleaner.go @@ -3,12 +3,13 @@ package cleaner import ( "context" "fmt" + "time" + "github.com/xxxsen/common/logutil" "github.com/xxxsen/common/trace" "github.com/xxxsen/qbapi" "github.com/xxxsen/runner" "go.uber.org/zap" - "time" ) const ( @@ -21,6 +22,7 @@ type Cleaner struct { uaRule *strRuleSet regionRule *strRuleSet ipRule *ipRuleSet + peerIDRule *strRuleSet } func New(opts ...Option) (*Cleaner, error) { @@ -47,7 +49,11 @@ func New(opts ...Option) (*Cleaner, error) { if err != nil { return nil, fmt.Errorf("make ip rule set failed, err:%w", err) } - return &Cleaner{api: api, c: c, uaRule: uaRule, regionRule: regionRule, ipRule: ipRule}, nil + peerIDRule, err := makeStrRuleSet(c.peerIDRs) + if err != nil { + return nil, fmt.Errorf("make peer rule set failed, err:%w", err) + } + return &Cleaner{api: api, c: c, uaRule: uaRule, regionRule: regionRule, ipRule: ipRule, peerIDRule: peerIDRule}, nil } func (c *Cleaner) Start() error { @@ -113,6 +119,9 @@ func (c *Cleaner) isInBlackList(info *qbapi.TorrentPeerItem) bool { if c.ipRule.isMatch(info.Ip) { return true } + if c.peerIDRule.isMatch(info.PeerIdClient) { + return true + } return false } @@ -143,7 +152,7 @@ func (c *Cleaner) banClients(ctx context.Context, banMap map[string]*qbapi.Torre logutil.GetLogger(ctx).Info("hit rule, ban it", zap.String("addr", addr), zap.String("client", item.Client), zap.String("country", item.Country), zap.String("code", item.CountryCode), zap.String("flags", item.Flags), - zap.Float64("progress", item.Progress)) + zap.Float64("progress", item.Progress), zap.String("peer_id", item.PeerIdClient)) } return c.banClient(ctx, peerList) } diff --git a/cleaner/config.go b/cleaner/config.go index 71f1544..a26fd14 100644 --- a/cleaner/config.go +++ b/cleaner/config.go @@ -8,6 +8,7 @@ type config struct { uaRs []string ipRs []string regionRs []string + peerIDRs []string // interval time.Duration @@ -37,6 +38,12 @@ func WithAddIPRule(rs ...string) Option { } } +func WithAddPeerIDRule(rs ...string) Option { + return func(c *config) { + c.peerIDRs = append(c.peerIDRs, rs...) + } +} + func WithAddRegionRule(rs ...string) Option { return func(c *config) { c.regionRs = append(c.regionRs, rs...) diff --git a/config/config.go b/config/config.go index 2669d4b..ca1287a 100644 --- a/config/config.go +++ b/config/config.go @@ -2,8 +2,9 @@ package config import ( "encoding/json" - "github.com/xxxsen/common/logger" "os" + + "github.com/xxxsen/common/logger" ) type QBConfig struct { @@ -18,6 +19,7 @@ type Config struct { BlacklistUa []string `json:"blacklist_ua"` BlacklistRegion []string `json:"blacklist_region"` BlacklistIP []string `json:"blacklist_ip"` + BlacklistPeerID []string `json:"blacklist_peer_id"` Interval int `json:"interval"` } diff --git a/go.mod b/go.mod index 5c7dd66..452e5b9 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.21 require ( github.com/stretchr/testify v1.8.4 github.com/xxxsen/common v0.1.2 - github.com/xxxsen/qbapi v0.0.1 + github.com/xxxsen/qbapi v0.0.3 github.com/xxxsen/runner v0.0.1 go.uber.org/zap v1.23.0 ) diff --git a/go.sum b/go.sum index 887f5e4..7d38564 100644 --- a/go.sum +++ b/go.sum @@ -11,13 +11,12 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/xxxsen/common v0.1.2 h1:TlLy8ABlvRcofeIZ5uGnRJMzku6Z9f3m95FO4GLnxgk= github.com/xxxsen/common v0.1.2/go.mod h1:cjx93T1XeFdlfx95RrIWB9jN7aTr0Ji1f8nhcNB3QPs= -github.com/xxxsen/qbapi v0.0.1 h1:F4vpAqySOk88TXcna0IfuVoh++u5thybDEjb9tA2svs= -github.com/xxxsen/qbapi v0.0.1/go.mod h1:vIUGnlsOLi2zlw9EzxCpoEi0/g0ME4t1xO9YuNmPr0w= +github.com/xxxsen/qbapi v0.0.3 h1:5DwWeblrZltnQ+qCb9lQQfpXaH/KQ6pXpbL7nAMXQLQ= +github.com/xxxsen/qbapi v0.0.3/go.mod h1:MQuFE422saVC91Ud9IxEDU4ZZeIhh9jIwnN20ysQXGc= github.com/xxxsen/runner v0.0.1 h1:KTtV2YZktttVrviCu4EiN2piGzdakKDHacRXm8s9yW8= github.com/xxxsen/runner v0.0.1/go.mod h1:IlfXilqN/O0odCq3K/cC4ZyBe6fJ4RmEhFXzXB0TdPo= go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= @@ -28,22 +27,15 @@ go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY= go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY= -golang.org/x/net v0.0.0-20211005215030-d2e5035098b3/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8= gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 0f75544..6607f76 100644 --- a/main.go +++ b/main.go @@ -2,12 +2,13 @@ package main import ( "flag" - "github.com/xxxsen/common/logger" - "go.uber.org/zap" "log" "qb-helper/cleaner" "qb-helper/config" "time" + + "github.com/xxxsen/common/logger" + "go.uber.org/zap" ) var cfg = flag.String("config", "./config.json", "config file") @@ -26,6 +27,7 @@ func main() { cleaner.WithInterval(time.Duration(conf.Interval)*time.Second), cleaner.WithAddUaRule(conf.BlacklistUa...), cleaner.WithAddIPRule(conf.BlacklistIP...), + cleaner.WithAddPeerIDRule(conf.BlacklistPeerID...), cleaner.WithAddRegionRule(conf.BlacklistRegion...), ) if err != nil {