FedP2P/smartban.go

57 lines
1.3 KiB
Go
Raw Normal View History

2022-01-10 13:19:16 +08:00
package torrent
import (
"bytes"
"crypto/sha1"
"net/netip"
2022-01-10 13:19:16 +08:00
2023-04-04 17:12:26 +08:00
g "github.com/anacrolix/generics"
2022-11-15 20:22:10 +08:00
2022-01-10 13:19:16 +08:00
"github.com/anacrolix/torrent/smartban"
)
type bannableAddr = netip.Addr
2022-01-10 13:19:16 +08:00
type smartBanCache = smartban.Cache[bannableAddr, RequestIndex, [sha1.Size]byte]
2022-01-10 13:19:16 +08:00
type blockCheckingWriter struct {
cache *smartBanCache
requestIndex RequestIndex
// Peers that didn't match blocks written now.
badPeers map[bannableAddr]struct{}
2022-01-10 13:19:16 +08:00
blockBuffer bytes.Buffer
chunkSize int
}
func (me *blockCheckingWriter) checkBlock() {
b := me.blockBuffer.Next(me.chunkSize)
for _, peer := range me.cache.CheckBlock(me.requestIndex, b) {
2023-04-04 17:12:26 +08:00
g.MakeMapIfNilAndSet(&me.badPeers, peer, struct{}{})
2022-01-10 13:19:16 +08:00
}
me.requestIndex++
}
func (me *blockCheckingWriter) checkFullBlocks() {
for me.blockBuffer.Len() >= me.chunkSize {
me.checkBlock()
}
}
func (me *blockCheckingWriter) Write(b []byte) (int, error) {
n, err := me.blockBuffer.Write(b)
if err != nil {
// bytes.Buffer.Write should never fail.
panic(err)
}
me.checkFullBlocks()
return n, err
}
// Check any remaining block data. Terminal pieces or piece sizes that don't divide into the chunk
// size cleanly may leave fragments that should be checked.
func (me *blockCheckingWriter) Flush() {
for me.blockBuffer.Len() != 0 {
me.checkBlock()
}
}