Test v2 hashes with qbt

This commit is contained in:
Širhoe Biazhkovič
2021-11-27 16:38:27 +03:00
committed by Lawrence, Rendall
parent e7c5263dd7
commit d438ad58fe
7 changed files with 50 additions and 29 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/test_data

View File

@@ -68,7 +68,10 @@ var (
// If InfoHash is V2 (32 bytes), it will be truncated to 20 bytes // If InfoHash is V2 (32 bytes), it will be truncated to 20 bytes
// according to BEP52. // according to BEP52.
func (i InfoHash) TruncateV1() InfoHash { func (i InfoHash) TruncateV1() InfoHash {
return i[:InfoHashV1Len] if len(i) == InfoHashV2Len {
return i[:InfoHashV1Len]
}
return i
} }
// NewInfoHash creates an InfoHash from a byte slice or raw/hex string. // NewInfoHash creates an InfoHash from a byte slice or raw/hex string.

View File

@@ -188,9 +188,12 @@ chihaya:
# This block defines configuration used for torrent approval, it requires to be given # This block defines configuration used for torrent approval, it requires to be given
# hashes for whitelist or for blacklist. Hashes are hexadecimal-encoaded. # hashes for whitelist or for blacklist. Hashes are hexadecimal-encoaded.
#- name: torrent approval #- name: torrent approval
# options: # options:
# whitelist: # initial_source: list
# - "a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5" # configuration:
# blacklist: # hash_list:
# - "e1d2c3b4a5e1b2c3b4a5e1d2c3b4e5e1d2c3b4a5" # - "a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5"
# invert: false
# storage_ctx: APPROVED_HASH

View File

@@ -53,7 +53,7 @@ func build(confBytes []byte, st storage.Storage) (container.Container, error) {
} }
d.watcher = w d.watcher = w
if len(d.StorageCtx) == 0 { if len(d.StorageCtx) == 0 {
log.Info("Storage context not set, using default value: " + container.DefaultStorageCtxName) log.Info("storage context not set, using default value: " + container.DefaultStorageCtxName)
d.StorageCtx = container.DefaultStorageCtxName d.StorageCtx = container.DefaultStorageCtxName
} }
go func() { go func() {
@@ -63,6 +63,12 @@ func build(confBytes []byte, st storage.Storage) (container.Container, error) {
s256 := sha256.New() s256 := sha256.New()
s256.Write(mi.InfoBytes) s256.Write(mi.InfoBytes)
v2hash, _ := bittorrent.NewInfoHash(s256.Sum(nil)) v2hash, _ := bittorrent.NewInfoHash(s256.Sum(nil))
lf := log.Fields{
"file": event.TorrentFilePath,
"v1hash": event.InfoHash.String(),
"v2hash": v2hash.String(),
"v2to1hash": v2hash.TruncateV1().String(),
}
switch event.Change { switch event.Change {
case dirwatch.Added: case dirwatch.Added:
var name string var name string
@@ -74,7 +80,7 @@ func build(confBytes []byte, st storage.Storage) (container.Container, error) {
if len(name) == 0 { if len(name) == 0 {
name = list.DUMMY name = list.DUMMY
} }
d.Storage.BulkPut(c.StorageCtx, d.Storage.BulkPut(d.StorageCtx,
storage.Pair{ storage.Pair{
Left: event.InfoHash.AsString(), Left: event.InfoHash.AsString(),
Right: name, Right: name,
@@ -85,12 +91,14 @@ func build(confBytes []byte, st storage.Storage) (container.Container, error) {
Left: v2hash.TruncateV1().RawString(), Left: v2hash.TruncateV1().RawString(),
Right: name, Right: name,
}) })
log.Debug("approval torrent added", lf)
case dirwatch.Removed: case dirwatch.Removed:
d.Storage.Delete(c.StorageCtx, d.Storage.Delete(c.StorageCtx,
event.InfoHash.AsString(), event.InfoHash.AsString(),
v2hash.RawString(), v2hash.RawString(),
v2hash.TruncateV1().RawString(), v2hash.TruncateV1().RawString(),
) )
log.Debug("approval torrent deleted", lf)
} }
} else { } else {
log.Err(err) log.Err(err)

View File

@@ -24,28 +24,35 @@ func init() {
middleware.RegisterDriver(Name, driver{}) middleware.RegisterDriver(Name, driver{})
} }
type baseConfig struct {
// Source - name of container for initial values
Source string `yaml:"initial_source"`
// Configuration depends on used container
Configuration map[string]interface{} `yaml:"configuration"`
}
type driver struct{} type driver struct{}
func (d driver) NewHook(optionBytes []byte, storage storage.Storage) (middleware.Hook, error) { func (d driver) NewHook(optionBytes []byte, storage storage.Storage) (middleware.Hook, error) {
var cfg middleware.Config var cfg baseConfig
err := yaml.Unmarshal(optionBytes, &cfg) err := yaml.Unmarshal(optionBytes, &cfg)
if err != nil { if err != nil {
return nil, fmt.Errorf("invalid options for middleware %s: %s", Name, err) return nil, fmt.Errorf("invalid options for middleware %s: %s", Name, err)
} }
if len(cfg.Name) == 0 { if len(cfg.Source) == 0 {
return nil, fmt.Errorf("invalid options for middleware %s: name not provided", Name) return nil, fmt.Errorf("invalid options for middleware %s: name not provided", Name)
} }
if cfg.Options == nil { if cfg.Configuration == nil {
return nil, fmt.Errorf("invalid options for middleware %s: options not provided", Name) return nil, fmt.Errorf("invalid options for middleware %s: options not provided", Name)
} }
var confBytes []byte var confBytes []byte
var h *hook var h *hook
if confBytes, err = yaml.Marshal(cfg.Options); err == nil { if confBytes, err = yaml.Marshal(cfg.Configuration); err == nil {
var c container.Container var c container.Container
if c, err = container.GetContainer(cfg.Name, confBytes, storage); err == nil { if c, err = container.GetContainer(cfg.Source, confBytes, storage); err == nil {
h = &hook{c} h = &hook{c}
} }
} }

View File

@@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"github.com/chihaya/chihaya/bittorrent" "github.com/chihaya/chihaya/bittorrent"
"github.com/chihaya/chihaya/middleware"
"github.com/chihaya/chihaya/storage/memory" "github.com/chihaya/chihaya/storage/memory"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
@@ -12,15 +11,15 @@ import (
) )
var cases = []struct { var cases = []struct {
cfg middleware.Config cfg baseConfig
ih string ih string
approved bool approved bool
}{ }{
// Infohash is whitelisted // Infohash is whitelisted
{ {
middleware.Config{ baseConfig{
Name: "list", Source: "list",
Options: map[string]interface{}{ Configuration: map[string]interface{}{
"hash_list": []string{"3532cf2d327fad8448c075b4cb42c8136964a435"}, "hash_list": []string{"3532cf2d327fad8448c075b4cb42c8136964a435"},
}, },
}, },
@@ -29,9 +28,9 @@ var cases = []struct {
}, },
// Infohash is not whitelisted // Infohash is not whitelisted
{ {
middleware.Config{ baseConfig{
Name: "list", Source: "list",
Options: map[string]interface{}{ Configuration: map[string]interface{}{
"hash_list": []string{"3532cf2d327fad8448c075b4cb42c8136964a435"}, "hash_list": []string{"3532cf2d327fad8448c075b4cb42c8136964a435"},
}, },
}, },
@@ -40,9 +39,9 @@ var cases = []struct {
}, },
// Infohash is not blacklisted // Infohash is not blacklisted
{ {
middleware.Config{ baseConfig{
Name: "list", Source: "list",
Options: map[string]interface{}{ Configuration: map[string]interface{}{
"hash_list": []string{"3532cf2d327fad8448c075b4cb42c8136964a435"}, "hash_list": []string{"3532cf2d327fad8448c075b4cb42c8136964a435"},
"invert": true, "invert": true,
}, },
@@ -52,9 +51,9 @@ var cases = []struct {
}, },
// Infohash is blacklisted // Infohash is blacklisted
{ {
middleware.Config{ baseConfig{
Name: "list", Source: "list",
Options: map[string]interface{}{ Configuration: map[string]interface{}{
"hash_list": []string{"3532cf2d327fad8448c075b4cb42c8136964a435"}, "hash_list": []string{"3532cf2d327fad8448c075b4cb42c8136964a435"},
"invert": true, "invert": true,
}, },

View File

@@ -98,7 +98,7 @@ func (cfg Config) Validate() Config {
}) })
} }
if cfg.PrometheusReportingInterval <= 0 { if cfg.PrometheusReportingInterval < 0 {
validcfg.PrometheusReportingInterval = defaultPrometheusReportingInterval validcfg.PrometheusReportingInterval = defaultPrometheusReportingInterval
log.Warn("falling back to default configuration", log.Fields{ log.Warn("falling back to default configuration", log.Fields{
"name": Name + ".PrometheusReportingInterval", "name": Name + ".PrometheusReportingInterval",