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
// according to BEP52.
func (i InfoHash) TruncateV1() InfoHash {
if len(i) == InfoHashV2Len {
return i[:InfoHashV1Len]
}
return i
}
// NewInfoHash creates an InfoHash from a byte slice or raw/hex string.

View File

@@ -190,7 +190,10 @@ chihaya:
# hashes for whitelist or for blacklist. Hashes are hexadecimal-encoaded.
#- name: torrent approval
# options:
# whitelist:
# initial_source: list
# configuration:
# hash_list:
# - "a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5"
# blacklist:
# - "e1d2c3b4a5e1b2c3b4a5e1d2c3b4e5e1d2c3b4a5"
# 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
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
}
go func() {
@@ -63,6 +63,12 @@ func build(confBytes []byte, st storage.Storage) (container.Container, error) {
s256 := sha256.New()
s256.Write(mi.InfoBytes)
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 {
case dirwatch.Added:
var name string
@@ -74,7 +80,7 @@ func build(confBytes []byte, st storage.Storage) (container.Container, error) {
if len(name) == 0 {
name = list.DUMMY
}
d.Storage.BulkPut(c.StorageCtx,
d.Storage.BulkPut(d.StorageCtx,
storage.Pair{
Left: event.InfoHash.AsString(),
Right: name,
@@ -85,12 +91,14 @@ func build(confBytes []byte, st storage.Storage) (container.Container, error) {
Left: v2hash.TruncateV1().RawString(),
Right: name,
})
log.Debug("approval torrent added", lf)
case dirwatch.Removed:
d.Storage.Delete(c.StorageCtx,
event.InfoHash.AsString(),
v2hash.RawString(),
v2hash.TruncateV1().RawString(),
)
log.Debug("approval torrent deleted", lf)
}
} else {
log.Err(err)

View File

@@ -24,28 +24,35 @@ func init() {
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{}
func (d driver) NewHook(optionBytes []byte, storage storage.Storage) (middleware.Hook, error) {
var cfg middleware.Config
var cfg baseConfig
err := yaml.Unmarshal(optionBytes, &cfg)
if err != nil {
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)
}
if cfg.Options == nil {
if cfg.Configuration == nil {
return nil, fmt.Errorf("invalid options for middleware %s: options not provided", Name)
}
var confBytes []byte
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
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}
}
}

View File

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

View File

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