mirror of
https://github.com/sot-tech/mochi.git
synced 2026-07-05 08:08:12 -07:00
(done) replace redigo with go-redis
* replace redis keys with RawString encoded values (delete SerializedPeer) * merge peers got from pre-hools with store data
This commit is contained in:
+20
-5
@@ -50,7 +50,7 @@ func (h *swarmInteractionHook) HandleAnnounce(ctx context.Context, req *bittorre
|
||||
err = h.store.GraduateLeecher(req.InfoHash, req.Peer)
|
||||
return ctx, err
|
||||
case req.Left == 0:
|
||||
// Completed events will also have Left == 0, but by making this
|
||||
// Completed events will also have Key == 0, but by making this
|
||||
// an extra case we can treat "old" seeders differently from
|
||||
// graduating leechers. (Calling PutSeeder is probably faster
|
||||
// than calling GraduateLeecher.)
|
||||
@@ -124,14 +124,29 @@ func (h *responseHook) appendPeers(req *bittorrent.AnnounceRequest, resp *bittor
|
||||
|
||||
switch req.IP.AddressFamily {
|
||||
case bittorrent.IPv4:
|
||||
resp.IPv4Peers = peers
|
||||
resp.IPv4Peers = mergePeers(resp.IPv4Peers, peers)
|
||||
case bittorrent.IPv6:
|
||||
resp.IPv6Peers = peers
|
||||
resp.IPv6Peers = mergePeers(resp.IPv6Peers, peers)
|
||||
default:
|
||||
panic("attempted to append peer that is neither IPv4 nor IPv6")
|
||||
err = bittorrent.ErrInvalidAddressFamily
|
||||
}
|
||||
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
func mergePeers(p0, p1 []bittorrent.Peer) (result []bittorrent.Peer) {
|
||||
peers := make(map[string]bittorrent.Peer, len(p0)+len(p1))
|
||||
for _, p := range p0 {
|
||||
peers[p.RawString()] = p
|
||||
}
|
||||
for _, p := range p1 {
|
||||
peers[p.RawString()] = p
|
||||
}
|
||||
result = make([]bittorrent.Peer, 0, len(peers))
|
||||
for _, v := range peers {
|
||||
result = append(result, v)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (h *responseHook) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest, resp *bittorrent.ScrapeResponse) (context.Context, error) {
|
||||
|
||||
@@ -61,49 +61,56 @@ func build(confBytes []byte, st storage.Storage) (container.Container, error) {
|
||||
go func() {
|
||||
for event := range d.watcher.Events {
|
||||
var mi *metainfo.MetaInfo
|
||||
lf := log.Fields{
|
||||
"file": event.TorrentFilePath,
|
||||
"v1hash": event.InfoHash,
|
||||
}
|
||||
if mi, err = metainfo.LoadFromFile(event.TorrentFilePath); err == nil {
|
||||
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(),
|
||||
}
|
||||
lf["v2hash"] = v2hash
|
||||
lf["v2to1hash"] = v2hash.TruncateV1()
|
||||
switch event.Change {
|
||||
case dirwatch.Added:
|
||||
var name string
|
||||
if info, err := mi.UnmarshalInfo(); err == nil {
|
||||
name = info.Name
|
||||
} else {
|
||||
log.Warn(err)
|
||||
lf["error"] = err
|
||||
log.Warn("unable to unmarshal torrent info", lf)
|
||||
delete(lf, "error")
|
||||
}
|
||||
if len(name) == 0 {
|
||||
name = list.DUMMY
|
||||
}
|
||||
d.Storage.BulkPut(d.StorageCtx,
|
||||
storage.Pair{
|
||||
Left: event.InfoHash.AsString(),
|
||||
Right: name,
|
||||
}, storage.Pair{
|
||||
Left: v2hash.RawString(),
|
||||
Right: name,
|
||||
}, storage.Pair{
|
||||
Left: v2hash.TruncateV1().RawString(),
|
||||
Right: name,
|
||||
})
|
||||
if err := d.Storage.BulkPut(d.StorageCtx,
|
||||
storage.Entry{
|
||||
Key: event.InfoHash.AsString(),
|
||||
Value: name,
|
||||
}, storage.Entry{
|
||||
Key: v2hash.RawString(),
|
||||
Value: name,
|
||||
}, storage.Entry{
|
||||
Key: v2hash.TruncateV1().RawString(),
|
||||
Value: name,
|
||||
}); err != nil {
|
||||
lf["error"] = err
|
||||
}
|
||||
log.Debug("approval torrent added", lf)
|
||||
case dirwatch.Removed:
|
||||
d.Storage.Delete(c.StorageCtx,
|
||||
if err := d.Storage.Delete(c.StorageCtx,
|
||||
event.InfoHash.AsString(),
|
||||
v2hash.RawString(),
|
||||
v2hash.TruncateV1().RawString(),
|
||||
)
|
||||
); err != nil {
|
||||
lf["error"] = err
|
||||
}
|
||||
log.Debug("approval torrent deleted", lf)
|
||||
}
|
||||
} else {
|
||||
log.Err(err)
|
||||
lf["error"] = err
|
||||
log.Error("unable to load torrent file", lf)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -51,18 +51,20 @@ func build(confBytes []byte, st storage.Storage) (container.Container, error) {
|
||||
}
|
||||
|
||||
if len(c.HashList) > 0 {
|
||||
init := make([]storage.Pair, 0, len(c.HashList))
|
||||
init := make([]storage.Entry, 0, len(c.HashList))
|
||||
for _, hashString := range c.HashList {
|
||||
ih, err := bittorrent.NewInfoHash(hashString)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("whitelist : %s : %w", hashString, err)
|
||||
}
|
||||
init = append(init, storage.Pair{Left: ih.RawString(), Right: DUMMY})
|
||||
init = append(init, storage.Entry{Key: ih.RawString(), Value: DUMMY})
|
||||
if len(ih) == bittorrent.InfoHashV2Len {
|
||||
init = append(init, storage.Pair{Left: ih.TruncateV1().RawString(), Right: DUMMY})
|
||||
init = append(init, storage.Entry{Key: ih.TruncateV1().RawString(), Value: DUMMY})
|
||||
}
|
||||
}
|
||||
l.Storage.BulkPut(l.StorageCtx, init...)
|
||||
if err := l.Storage.BulkPut(l.StorageCtx, init...); err != nil {
|
||||
return nil, fmt.Errorf("unable to put initial data: %w", err)
|
||||
}
|
||||
}
|
||||
return l, nil
|
||||
}
|
||||
@@ -80,10 +82,19 @@ type List struct {
|
||||
// Approved checks if specified hash is approved or not.
|
||||
// If List.Invert set to true and hash found in storage, function will return false,
|
||||
// that means that hash is blacklisted.
|
||||
func (l *List) Approved(hash bittorrent.InfoHash) bool {
|
||||
b := l.Storage.Contains(l.StorageCtx, hash.RawString())
|
||||
if len(hash) == bittorrent.InfoHashV2Len {
|
||||
b = b || l.Storage.Contains(l.StorageCtx, hash.TruncateV1().RawString())
|
||||
func (l *List) Approved(hash bittorrent.InfoHash) (contains bool) {
|
||||
var err error
|
||||
if contains, err = l.Storage.Contains(l.StorageCtx, hash.RawString()); err == nil {
|
||||
if len(hash) == bittorrent.InfoHashV2Len {
|
||||
if containsV2, errV2 := l.Storage.Contains(l.StorageCtx, hash.TruncateV1().RawString()); err == nil {
|
||||
contains = contains || containsV2
|
||||
} else {
|
||||
err = errV2
|
||||
}
|
||||
}
|
||||
}
|
||||
return b != l.Invert
|
||||
if err != nil {
|
||||
log.Err(err)
|
||||
}
|
||||
return contains != l.Invert
|
||||
}
|
||||
|
||||
@@ -6,10 +6,11 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/sot-tech/mochi/bittorrent"
|
||||
"github.com/sot-tech/mochi/middleware"
|
||||
"github.com/sot-tech/mochi/middleware/torrentapproval/container"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
// import directory watcher to enable appropriate support
|
||||
_ "github.com/sot-tech/mochi/middleware/torrentapproval/container/directory"
|
||||
|
||||
Reference in New Issue
Block a user