From d438ad58fe4582a3d12907b55f74c5aabfb41a1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0irhoe=20Biazhkovi=C4=8D?= Date: Sat, 27 Nov 2021 16:38:27 +0300 Subject: [PATCH] Test v2 hashes with qbt --- .gitignore | 1 + bittorrent/bittorrent.go | 5 +++- dist/example_config.yaml | 15 ++++++----- .../container/directory/directory.go | 12 +++++++-- middleware/torrentapproval/torrentapproval.go | 17 ++++++++---- .../torrentapproval/torrentapproval_test.go | 27 +++++++++---------- storage/memory/storage.go | 2 +- 7 files changed, 50 insertions(+), 29 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..81a860e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/test_data diff --git a/bittorrent/bittorrent.go b/bittorrent/bittorrent.go index dcee705..5c56179 100644 --- a/bittorrent/bittorrent.go +++ b/bittorrent/bittorrent.go @@ -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 { - return i[:InfoHashV1Len] + if len(i) == InfoHashV2Len { + return i[:InfoHashV1Len] + } + return i } // NewInfoHash creates an InfoHash from a byte slice or raw/hex string. diff --git a/dist/example_config.yaml b/dist/example_config.yaml index 61c81d6..6817bd5 100644 --- a/dist/example_config.yaml +++ b/dist/example_config.yaml @@ -188,9 +188,12 @@ chihaya: # This block defines configuration used for torrent approval, it requires to be given # hashes for whitelist or for blacklist. Hashes are hexadecimal-encoaded. - #- name: torrent approval - # options: - # whitelist: - # - "a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5" - # blacklist: - # - "e1d2c3b4a5e1b2c3b4a5e1d2c3b4e5e1d2c3b4a5" + #- name: torrent approval + # options: + # initial_source: list + # configuration: + # hash_list: + # - "a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5" + # invert: false + # storage_ctx: APPROVED_HASH + diff --git a/middleware/torrentapproval/container/directory/directory.go b/middleware/torrentapproval/container/directory/directory.go index 6f39d59..a845ba1 100644 --- a/middleware/torrentapproval/container/directory/directory.go +++ b/middleware/torrentapproval/container/directory/directory.go @@ -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) diff --git a/middleware/torrentapproval/torrentapproval.go b/middleware/torrentapproval/torrentapproval.go index 6c52d9a..95caaa6 100644 --- a/middleware/torrentapproval/torrentapproval.go +++ b/middleware/torrentapproval/torrentapproval.go @@ -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} } } diff --git a/middleware/torrentapproval/torrentapproval_test.go b/middleware/torrentapproval/torrentapproval_test.go index 66e8049..c2f831f 100644 --- a/middleware/torrentapproval/torrentapproval_test.go +++ b/middleware/torrentapproval/torrentapproval_test.go @@ -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, }, diff --git a/storage/memory/storage.go b/storage/memory/storage.go index 1d72b56..364ac11 100644 --- a/storage/memory/storage.go +++ b/storage/memory/storage.go @@ -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",