mirror of
https://github.com/sot-tech/mochi.git
synced 2026-05-24 08:44:48 -07:00
Test v2 hashes with qbt
This commit is contained in:
committed by
Lawrence, Rendall
parent
e7c5263dd7
commit
d438ad58fe
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/test_data
|
||||||
@@ -68,8 +68,11 @@ 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 {
|
||||||
|
if len(i) == InfoHashV2Len {
|
||||||
return i[:InfoHashV1Len]
|
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.
|
||||||
func NewInfoHash(b interface{}) (InfoHash, error) {
|
func NewInfoHash(b interface{}) (InfoHash, error) {
|
||||||
|
|||||||
9
dist/example_config.yaml
vendored
9
dist/example_config.yaml
vendored
@@ -190,7 +190,10 @@ chihaya:
|
|||||||
# 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
|
||||||
|
# configuration:
|
||||||
|
# hash_list:
|
||||||
# - "a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5"
|
# - "a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5"
|
||||||
# blacklist:
|
# invert: false
|
||||||
# - "e1d2c3b4a5e1b2c3b4a5e1d2c3b4e5e1d2c3b4a5"
|
# storage_ctx: APPROVED_HASH
|
||||||
|
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
@@ -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}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -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",
|
||||||
|
|||||||
Reference in New Issue
Block a user