Skip to content

Commit

Permalink
feat: add blacklist peerid support
Browse files Browse the repository at this point in the history
  • Loading branch information
xxxsen committed Apr 18, 2024
1 parent f9db161 commit 01d4ec5
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 17 deletions.
15 changes: 12 additions & 3 deletions cleaner/cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -21,6 +22,7 @@ type Cleaner struct {
uaRule *strRuleSet
regionRule *strRuleSet
ipRule *ipRuleSet
peerIDRule *strRuleSet
}

func New(opts ...Option) (*Cleaner, error) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}
Expand Down
7 changes: 7 additions & 0 deletions cleaner/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type config struct {
uaRs []string
ipRs []string
regionRs []string
peerIDRs []string

//
interval time.Duration
Expand Down Expand Up @@ -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...)
Expand Down
4 changes: 3 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package config

import (
"encoding/json"
"github.com/xxxsen/common/logger"
"os"

"github.com/xxxsen/common/logger"
)

type QBConfig struct {
Expand All @@ -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"`
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
12 changes: 2 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 {
Expand Down

0 comments on commit 01d4ec5

Please sign in to comment.