mirror of
https://github.com/Ernous/TorrServerJellyfin.git
synced 2025-12-19 21:46:11 +05:00
refactor and to go mod
This commit is contained in:
151
server/torr/apihelper.go
Normal file
151
server/torr/apihelper.go
Normal file
@@ -0,0 +1,151 @@
|
||||
package torr
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/anacrolix/torrent"
|
||||
"github.com/anacrolix/torrent/metainfo"
|
||||
"server/log"
|
||||
sets "server/settings"
|
||||
)
|
||||
|
||||
var (
|
||||
bts *BTServer
|
||||
)
|
||||
|
||||
func InitApiHelper(bt *BTServer) {
|
||||
bts = bt
|
||||
}
|
||||
|
||||
func AddTorrent(spec *torrent.TorrentSpec, title, poster string, data string) (*Torrent, error) {
|
||||
torr, err := NewTorrent(spec, bts)
|
||||
if err != nil {
|
||||
log.TLogln("error add torrent:", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
torDB := GetTorrentDB(spec.InfoHash)
|
||||
|
||||
if torr.Title == "" {
|
||||
torr.Title = title
|
||||
if title == "" && torDB != nil {
|
||||
torr.Title = torDB.Title
|
||||
}
|
||||
}
|
||||
if torr.Poster == "" {
|
||||
torr.Poster = poster
|
||||
if torr.Poster == "" && torDB != nil {
|
||||
torr.Poster = torDB.Poster
|
||||
}
|
||||
}
|
||||
if torr.Data == "" {
|
||||
torr.Data = data
|
||||
if torr.Data == "" && torDB != nil {
|
||||
torr.Data = torDB.Data
|
||||
}
|
||||
}
|
||||
|
||||
return torr, nil
|
||||
}
|
||||
|
||||
func SaveTorrentToDB(torr *Torrent) {
|
||||
log.TLogln("save to db:", torr.Hash())
|
||||
AddTorrentDB(torr)
|
||||
}
|
||||
|
||||
func GetTorrent(hashHex string) *Torrent {
|
||||
hash := metainfo.NewHashFromHex(hashHex)
|
||||
tor := bts.GetTorrent(hash)
|
||||
if tor != nil {
|
||||
tor.AddExpiredTime(time.Minute)
|
||||
return tor
|
||||
}
|
||||
|
||||
tr := GetTorrentDB(hash)
|
||||
if tr != nil {
|
||||
tor = tr
|
||||
go func() {
|
||||
tr, _ := NewTorrent(tor.TorrentSpec, bts)
|
||||
if tr != nil {
|
||||
tr.Title = tor.Title
|
||||
tr.Poster = tor.Poster
|
||||
tr.Size = tor.Size
|
||||
tr.Timestamp = tor.Timestamp
|
||||
tr.GotInfo()
|
||||
}
|
||||
}()
|
||||
}
|
||||
return tor
|
||||
}
|
||||
|
||||
func RemTorrent(hashHex string) {
|
||||
hash := metainfo.NewHashFromHex(hashHex)
|
||||
bts.RemoveTorrent(hash)
|
||||
RemTorrentDB(hash)
|
||||
}
|
||||
|
||||
func ListTorrent() []*Torrent {
|
||||
btlist := bts.ListTorrents()
|
||||
dblist := ListTorrentsDB()
|
||||
|
||||
for hash, t := range dblist {
|
||||
if _, ok := btlist[hash]; !ok {
|
||||
btlist[hash] = t
|
||||
}
|
||||
}
|
||||
var ret []*Torrent
|
||||
|
||||
for _, t := range btlist {
|
||||
ret = append(ret, t)
|
||||
}
|
||||
|
||||
sort.Slice(ret, func(i, j int) bool {
|
||||
if ret[i].Timestamp != ret[j].Timestamp {
|
||||
return ret[i].Timestamp > ret[j].Timestamp
|
||||
} else {
|
||||
return ret[i].Title > ret[j].Title
|
||||
}
|
||||
})
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func DropTorrent(hashHex string) {
|
||||
hash := metainfo.NewHashFromHex(hashHex)
|
||||
bts.RemoveTorrent(hash)
|
||||
}
|
||||
|
||||
func SetSettings(set *sets.BTSets) {
|
||||
if sets.ReadOnly {
|
||||
return
|
||||
}
|
||||
bts.Disconnect()
|
||||
sets.SetBTSets(set)
|
||||
bts.Connect()
|
||||
}
|
||||
|
||||
func Shutdown() {
|
||||
bts.Disconnect()
|
||||
sets.CloseDB()
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func WriteStatus(w io.Writer) {
|
||||
bts.client.WriteStatus(w)
|
||||
}
|
||||
|
||||
func Preload(torr *Torrent, index int) {
|
||||
if !sets.BTsets.PreloadBuffer {
|
||||
size := int64(32 * 1024 * 1024)
|
||||
if size > sets.BTsets.CacheSize {
|
||||
size = sets.BTsets.CacheSize
|
||||
}
|
||||
torr.Preload(index, size)
|
||||
} else {
|
||||
size := int64(float32(sets.BTsets.ReaderReadAHead) / 100.0 * float32(sets.BTsets.CacheSize))
|
||||
torr.Preload(index, size)
|
||||
}
|
||||
}
|
||||
138
server/torr/btserver.go
Normal file
138
server/torr/btserver.go
Normal file
@@ -0,0 +1,138 @@
|
||||
package torr
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/anacrolix/torrent"
|
||||
"github.com/anacrolix/torrent/metainfo"
|
||||
"server/settings"
|
||||
"server/torr/storage/torrstor"
|
||||
"server/torr/utils"
|
||||
)
|
||||
|
||||
type BTServer struct {
|
||||
config *torrent.ClientConfig
|
||||
client *torrent.Client
|
||||
|
||||
storage *torrstor.Storage
|
||||
|
||||
torrents map[metainfo.Hash]*Torrent
|
||||
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func NewBTS() *BTServer {
|
||||
bts := new(BTServer)
|
||||
bts.torrents = make(map[metainfo.Hash]*Torrent)
|
||||
return bts
|
||||
}
|
||||
|
||||
func (bt *BTServer) Connect() error {
|
||||
bt.mu.Lock()
|
||||
defer bt.mu.Unlock()
|
||||
var err error
|
||||
bt.configure()
|
||||
bt.client, err = torrent.NewClient(bt.config)
|
||||
bt.torrents = make(map[metainfo.Hash]*Torrent)
|
||||
InitApiHelper(bt)
|
||||
return err
|
||||
}
|
||||
|
||||
func (bt *BTServer) Disconnect() {
|
||||
bt.mu.Lock()
|
||||
defer bt.mu.Unlock()
|
||||
if bt.client != nil {
|
||||
bt.client.Close()
|
||||
bt.client = nil
|
||||
utils.FreeOSMemGC()
|
||||
}
|
||||
}
|
||||
|
||||
func (bt *BTServer) configure() {
|
||||
blocklist, _ := utils.ReadBlockedIP()
|
||||
bt.config = torrent.NewDefaultClientConfig()
|
||||
|
||||
if !settings.BTsets.SaveOnDisk {
|
||||
bt.storage = torrstor.NewStorage(settings.BTsets.CacheSize)
|
||||
bt.config.DefaultStorage = bt.storage
|
||||
} else {
|
||||
bt.config.DataDir = settings.BTsets.ContentPath
|
||||
}
|
||||
|
||||
userAgent := "qBittorrent/4.3.2"
|
||||
peerID := "-qB4320-"
|
||||
cliVers := userAgent //"uTorrent/2210(25302)"
|
||||
|
||||
bt.config.Debug = settings.BTsets.EnableDebug
|
||||
bt.config.DisableIPv6 = settings.BTsets.EnableIPv6 == false
|
||||
bt.config.DisableTCP = settings.BTsets.DisableTCP
|
||||
bt.config.DisableUTP = settings.BTsets.DisableUTP
|
||||
bt.config.NoDefaultPortForwarding = settings.BTsets.DisableUPNP
|
||||
bt.config.NoDHT = settings.BTsets.DisableDHT
|
||||
bt.config.NoUpload = settings.BTsets.DisableUpload
|
||||
bt.config.IPBlocklist = blocklist
|
||||
bt.config.Bep20 = peerID
|
||||
bt.config.PeerID = utils.PeerIDRandom(peerID)
|
||||
bt.config.HTTPUserAgent = userAgent
|
||||
bt.config.ExtendedHandshakeClientVersion = cliVers
|
||||
bt.config.EstablishedConnsPerTorrent = settings.BTsets.ConnectionsLimit
|
||||
bt.config.UpnpID = "YouROK/TorrServer"
|
||||
|
||||
//bt.config.DropMutuallyCompletePeers = true
|
||||
//bt.config.DropDuplicatePeerIds = true
|
||||
|
||||
// Encryption/Obfuscation
|
||||
bt.config.HeaderObfuscationPolicy = torrent.HeaderObfuscationPolicy{
|
||||
RequirePreferred: settings.BTsets.ForceEncrypt,
|
||||
Preferred: true,
|
||||
}
|
||||
|
||||
switch settings.BTsets.Strategy {
|
||||
case 1: // RequestStrategyFuzzing
|
||||
bt.config.DefaultRequestStrategy = torrent.RequestStrategyFuzzing()
|
||||
case 2: // RequestStrategyFastest
|
||||
bt.config.DefaultRequestStrategy = torrent.RequestStrategyFastest()
|
||||
default: // RequestStrategyDuplicateRequestTimeout
|
||||
bt.config.DefaultRequestStrategy = torrent.RequestStrategyDuplicateRequestTimeout(5 * time.Second)
|
||||
}
|
||||
|
||||
if settings.BTsets.DhtConnectionLimit > 0 {
|
||||
bt.config.ConnTracker.SetMaxEntries(settings.BTsets.DhtConnectionLimit)
|
||||
}
|
||||
if settings.BTsets.DownloadRateLimit > 0 {
|
||||
bt.config.DownloadRateLimiter = utils.Limit(settings.BTsets.DownloadRateLimit * 1024)
|
||||
}
|
||||
if settings.BTsets.UploadRateLimit > 0 {
|
||||
bt.config.UploadRateLimiter = utils.Limit(settings.BTsets.UploadRateLimit * 1024)
|
||||
}
|
||||
if settings.BTsets.PeersListenPort > 0 {
|
||||
bt.config.ListenPort = settings.BTsets.PeersListenPort
|
||||
} else {
|
||||
bt.config.ListenPort = 50109
|
||||
}
|
||||
|
||||
log.Println("Configure client:", settings.BTsets)
|
||||
}
|
||||
|
||||
func (bt *BTServer) GetTorrent(hash torrent.InfoHash) *Torrent {
|
||||
if torr, ok := bt.torrents[hash]; ok {
|
||||
return torr
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (bt *BTServer) ListTorrents() map[metainfo.Hash]*Torrent {
|
||||
list := make(map[metainfo.Hash]*Torrent)
|
||||
for k, v := range bt.torrents {
|
||||
list[k] = v
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
func (bt *BTServer) RemoveTorrent(hash torrent.InfoHash) {
|
||||
if torr, ok := bt.torrents[hash]; ok {
|
||||
torr.Close()
|
||||
}
|
||||
}
|
||||
67
server/torr/dbwrapper.go
Normal file
67
server/torr/dbwrapper.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package torr
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"server/settings"
|
||||
"server/torr/state"
|
||||
|
||||
"github.com/anacrolix/torrent/metainfo"
|
||||
)
|
||||
|
||||
func AddTorrentDB(torr *Torrent) {
|
||||
t := new(settings.TorrentDB)
|
||||
t.TorrentSpec = torr.TorrentSpec
|
||||
t.Name = torr.Name()
|
||||
t.Title = torr.Title
|
||||
t.Data = torr.Data
|
||||
if t.Title == "" {
|
||||
t.Title = t.Name
|
||||
}
|
||||
t.Poster = torr.Poster
|
||||
t.Size = torr.Size
|
||||
if t.Size == 0 && torr.Torrent != nil {
|
||||
t.Size = torr.Torrent.Length()
|
||||
}
|
||||
t.Timestamp = time.Now().Unix()
|
||||
settings.AddTorrent(t)
|
||||
}
|
||||
|
||||
func GetTorrentDB(hash metainfo.Hash) *Torrent {
|
||||
list := settings.ListTorrent()
|
||||
for _, db := range list {
|
||||
if hash == db.InfoHash {
|
||||
torr := new(Torrent)
|
||||
torr.TorrentSpec = db.TorrentSpec
|
||||
torr.Title = db.Title
|
||||
torr.Poster = db.Poster
|
||||
torr.Timestamp = db.Timestamp
|
||||
torr.Size = db.Size
|
||||
torr.Data = db.Data
|
||||
torr.Stat = state.TorrentInDB
|
||||
return torr
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func RemTorrentDB(hash metainfo.Hash) {
|
||||
settings.RemTorrent(hash)
|
||||
}
|
||||
|
||||
func ListTorrentsDB() map[metainfo.Hash]*Torrent {
|
||||
ret := make(map[metainfo.Hash]*Torrent)
|
||||
list := settings.ListTorrent()
|
||||
for _, db := range list {
|
||||
torr := new(Torrent)
|
||||
torr.TorrentSpec = db.TorrentSpec
|
||||
torr.Title = db.Title
|
||||
torr.Poster = db.Poster
|
||||
torr.Timestamp = db.Timestamp
|
||||
torr.Size = db.Size
|
||||
torr.Data = db.Data
|
||||
torr.Stat = state.TorrentInDB
|
||||
ret[torr.TorrentSpec.InfoHash] = torr
|
||||
}
|
||||
return ret
|
||||
}
|
||||
72
server/torr/state/state.go
Normal file
72
server/torr/state/state.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package state
|
||||
|
||||
type TorrentStat int
|
||||
|
||||
func (t TorrentStat) String() string {
|
||||
switch t {
|
||||
case TorrentAdded:
|
||||
return "Torrent added"
|
||||
case TorrentGettingInfo:
|
||||
return "Torrent getting info"
|
||||
case TorrentPreload:
|
||||
return "Torrent preload"
|
||||
case TorrentWorking:
|
||||
return "Torrent working"
|
||||
case TorrentClosed:
|
||||
return "Torrent closed"
|
||||
case TorrentInDB:
|
||||
return "Torrent in db"
|
||||
default:
|
||||
return "Torrent unknown status"
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
TorrentAdded = TorrentStat(iota)
|
||||
TorrentGettingInfo
|
||||
TorrentPreload
|
||||
TorrentWorking
|
||||
TorrentClosed
|
||||
TorrentInDB
|
||||
)
|
||||
|
||||
type TorrentStatus struct {
|
||||
Title string `json:"title"`
|
||||
Poster string `json:"poster"`
|
||||
Data string `json:"data,omitempty"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Hash string `json:"hash,omitempty"`
|
||||
Stat TorrentStat `json:"stat"`
|
||||
StatString string `json:"stat_string"`
|
||||
LoadedSize int64 `json:"loaded_size,omitempty"`
|
||||
TorrentSize int64 `json:"torrent_size,omitempty"`
|
||||
PreloadedBytes int64 `json:"preloaded_bytes,omitempty"`
|
||||
PreloadSize int64 `json:"preload_size,omitempty"`
|
||||
DownloadSpeed float64 `json:"download_speed,omitempty"`
|
||||
UploadSpeed float64 `json:"upload_speed,omitempty"`
|
||||
TotalPeers int `json:"total_peers,omitempty"`
|
||||
PendingPeers int `json:"pending_peers,omitempty"`
|
||||
ActivePeers int `json:"active_peers,omitempty"`
|
||||
ConnectedSeeders int `json:"connected_seeders,omitempty"`
|
||||
HalfOpenPeers int `json:"half_open_peers,omitempty"`
|
||||
BytesWritten int64 `json:"bytes_written,omitempty"`
|
||||
BytesWrittenData int64 `json:"bytes_written_data,omitempty"`
|
||||
BytesRead int64 `json:"bytes_read,omitempty"`
|
||||
BytesReadData int64 `json:"bytes_read_data,omitempty"`
|
||||
BytesReadUsefulData int64 `json:"bytes_read_useful_data,omitempty"`
|
||||
ChunksWritten int64 `json:"chunks_written,omitempty"`
|
||||
ChunksRead int64 `json:"chunks_read,omitempty"`
|
||||
ChunksReadUseful int64 `json:"chunks_read_useful,omitempty"`
|
||||
ChunksReadWasted int64 `json:"chunks_read_wasted,omitempty"`
|
||||
PiecesDirtiedGood int64 `json:"pieces_dirtied_good,omitempty"`
|
||||
PiecesDirtiedBad int64 `json:"pieces_dirtied_bad,omitempty"`
|
||||
|
||||
FileStats []*TorrentFileStat `json:"file_stats,omitempty"`
|
||||
}
|
||||
|
||||
type TorrentFileStat struct {
|
||||
Id int `json:"id,omitempty"`
|
||||
Path string `json:"path,omitempty"`
|
||||
Length int64 `json:"length,omitempty"`
|
||||
}
|
||||
29
server/torr/storage/state/state.go
Normal file
29
server/torr/storage/state/state.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"server/torr/state"
|
||||
)
|
||||
|
||||
type CacheState struct {
|
||||
Hash string
|
||||
Capacity int64
|
||||
Filled int64
|
||||
PiecesLength int64
|
||||
PiecesCount int
|
||||
Torrent *state.TorrentStatus
|
||||
Pieces map[int]ItemState
|
||||
Readers []*ReaderState
|
||||
}
|
||||
|
||||
type ItemState struct {
|
||||
Id int
|
||||
Length int64
|
||||
Size int64
|
||||
Completed bool
|
||||
}
|
||||
|
||||
type ReaderState struct {
|
||||
Start int
|
||||
End int
|
||||
Reader int
|
||||
}
|
||||
12
server/torr/storage/storage.go
Normal file
12
server/torr/storage/storage.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"github.com/anacrolix/torrent/metainfo"
|
||||
"github.com/anacrolix/torrent/storage"
|
||||
)
|
||||
|
||||
type Storage interface {
|
||||
storage.ClientImpl
|
||||
|
||||
CloseHash(hash metainfo.Hash)
|
||||
}
|
||||
219
server/torr/storage/torrstor/cache.go
Normal file
219
server/torr/storage/torrstor/cache.go
Normal file
@@ -0,0 +1,219 @@
|
||||
package torrstor
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"github.com/anacrolix/torrent"
|
||||
"server/log"
|
||||
"server/settings"
|
||||
"server/torr/storage/state"
|
||||
"server/torr/utils"
|
||||
|
||||
"github.com/anacrolix/torrent/metainfo"
|
||||
"github.com/anacrolix/torrent/storage"
|
||||
)
|
||||
|
||||
type Cache struct {
|
||||
storage.TorrentImpl
|
||||
storage *Storage
|
||||
|
||||
capacity int64
|
||||
filled int64
|
||||
hash metainfo.Hash
|
||||
|
||||
pieceLength int64
|
||||
pieceCount int
|
||||
|
||||
pieces map[int]*Piece
|
||||
|
||||
readers map[*Reader]struct{}
|
||||
muReaders sync.Mutex
|
||||
|
||||
isRemove bool
|
||||
muRemove sync.Mutex
|
||||
torrent *torrent.Torrent
|
||||
}
|
||||
|
||||
func NewCache(capacity int64, storage *Storage) *Cache {
|
||||
ret := &Cache{
|
||||
capacity: capacity,
|
||||
filled: 0,
|
||||
pieces: make(map[int]*Piece),
|
||||
storage: storage,
|
||||
readers: make(map[*Reader]struct{}),
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func (c *Cache) Init(info *metainfo.Info, hash metainfo.Hash) {
|
||||
log.TLogln("Create cache for:", info.Name, hash.HexString())
|
||||
if c.capacity == 0 {
|
||||
c.capacity = info.PieceLength * 4
|
||||
}
|
||||
|
||||
c.pieceLength = info.PieceLength
|
||||
c.pieceCount = info.NumPieces()
|
||||
c.hash = hash
|
||||
|
||||
for i := 0; i < c.pieceCount; i++ {
|
||||
c.pieces[i] = &Piece{
|
||||
Id: i,
|
||||
Length: info.Piece(i).Length(),
|
||||
Hash: info.Piece(i).Hash().HexString(),
|
||||
cache: c,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) SetTorrent(torr *torrent.Torrent) {
|
||||
c.torrent = torr
|
||||
}
|
||||
|
||||
func (c *Cache) Piece(m metainfo.Piece) storage.PieceImpl {
|
||||
if val, ok := c.pieces[m.Index()]; ok {
|
||||
return val
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cache) Close() error {
|
||||
log.TLogln("Close cache for:", c.hash)
|
||||
if _, ok := c.storage.caches[c.hash]; ok {
|
||||
delete(c.storage.caches, c.hash)
|
||||
}
|
||||
c.pieces = nil
|
||||
|
||||
c.muReaders.Lock()
|
||||
c.readers = nil
|
||||
c.muReaders.Unlock()
|
||||
|
||||
utils.FreeOSMemGC()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cache) removePiece(piece *Piece) {
|
||||
piece.Release()
|
||||
utils.FreeOSMemGC()
|
||||
}
|
||||
|
||||
func (c *Cache) AdjustRA(readahead int64) {
|
||||
if settings.BTsets.CacheSize == 0 {
|
||||
c.capacity = readahead * 3
|
||||
}
|
||||
c.muReaders.Lock()
|
||||
for r, _ := range c.readers {
|
||||
r.SetReadahead(readahead)
|
||||
}
|
||||
c.muReaders.Unlock()
|
||||
}
|
||||
|
||||
func (c *Cache) GetState() *state.CacheState {
|
||||
cState := new(state.CacheState)
|
||||
|
||||
piecesState := make(map[int]state.ItemState, 0)
|
||||
var fill int64 = 0
|
||||
for _, p := range c.pieces {
|
||||
if p.Size > 0 {
|
||||
fill += p.Length
|
||||
piecesState[p.Id] = state.ItemState{
|
||||
Id: p.Id,
|
||||
Size: p.Size,
|
||||
Length: p.Length,
|
||||
Completed: p.complete,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
readersState := make([]*state.ReaderState, 0)
|
||||
c.muReaders.Lock()
|
||||
for r, _ := range c.readers {
|
||||
rng := r.getPiecesRange()
|
||||
pc := r.getReaderPiece()
|
||||
readersState = append(readersState, &state.ReaderState{
|
||||
Start: rng.Start,
|
||||
End: rng.End,
|
||||
Reader: pc,
|
||||
})
|
||||
}
|
||||
c.muReaders.Unlock()
|
||||
|
||||
c.filled = fill
|
||||
cState.Capacity = c.capacity
|
||||
cState.PiecesLength = c.pieceLength
|
||||
cState.PiecesCount = c.pieceCount
|
||||
cState.Hash = c.hash.HexString()
|
||||
cState.Filled = fill
|
||||
cState.Pieces = piecesState
|
||||
cState.Readers = readersState
|
||||
return cState
|
||||
}
|
||||
|
||||
func (c *Cache) cleanPieces() {
|
||||
if c.isRemove {
|
||||
return
|
||||
}
|
||||
c.muRemove.Lock()
|
||||
if c.isRemove {
|
||||
c.muRemove.Unlock()
|
||||
return
|
||||
}
|
||||
c.isRemove = true
|
||||
defer func() { c.isRemove = false }()
|
||||
c.muRemove.Unlock()
|
||||
|
||||
remPieces := c.getRemPieces()
|
||||
if c.filled > c.capacity {
|
||||
rems := (c.filled - c.capacity) / c.pieceLength
|
||||
for _, p := range remPieces {
|
||||
c.removePiece(p)
|
||||
rems--
|
||||
if rems <= 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) getRemPieces() []*Piece {
|
||||
piecesRemove := make([]*Piece, 0)
|
||||
fill := int64(0)
|
||||
|
||||
ranges := make([]Range, 0)
|
||||
c.muReaders.Lock()
|
||||
for r, _ := range c.readers {
|
||||
ranges = append(ranges, r.getPiecesRange())
|
||||
}
|
||||
c.muReaders.Unlock()
|
||||
ranges = mergeRange(ranges)
|
||||
|
||||
for id, p := range c.pieces {
|
||||
if p.Size > 0 {
|
||||
fill += p.Size
|
||||
}
|
||||
if len(ranges) > 0 {
|
||||
if !inRanges(ranges, id) {
|
||||
piece := c.torrent.Piece(id)
|
||||
if piece.State().Priority != torrent.PiecePriorityNone {
|
||||
piece.SetPriority(torrent.PiecePriorityNone)
|
||||
}
|
||||
if p.Size > 0 {
|
||||
piecesRemove = append(piecesRemove, p)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
piece := c.torrent.Piece(id)
|
||||
if piece.State().Priority != torrent.PiecePriorityNone {
|
||||
piece.SetPriority(torrent.PiecePriorityNone)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(piecesRemove, func(i, j int) bool {
|
||||
return piecesRemove[i].accessed < piecesRemove[j].accessed
|
||||
})
|
||||
|
||||
c.filled = fill
|
||||
return piecesRemove
|
||||
}
|
||||
106
server/torr/storage/torrstor/piece.go
Normal file
106
server/torr/storage/torrstor/piece.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package torrstor
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/anacrolix/torrent"
|
||||
"github.com/anacrolix/torrent/storage"
|
||||
)
|
||||
|
||||
type Piece struct {
|
||||
storage.PieceImpl
|
||||
|
||||
Id int
|
||||
Hash string
|
||||
Length int64
|
||||
Size int64
|
||||
|
||||
complete bool
|
||||
readed bool
|
||||
accessed int64
|
||||
buffer []byte
|
||||
|
||||
mu sync.RWMutex
|
||||
cache *Cache
|
||||
}
|
||||
|
||||
func (p *Piece) WriteAt(b []byte, off int64) (n int, err error) {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
if p.buffer == nil {
|
||||
go p.cache.cleanPieces()
|
||||
p.buffer = make([]byte, p.cache.pieceLength)
|
||||
}
|
||||
n = copy(p.buffer[off:], b[:])
|
||||
p.Size += int64(n)
|
||||
p.accessed = time.Now().Unix()
|
||||
return
|
||||
}
|
||||
|
||||
func (p *Piece) ReadAt(b []byte, off int64) (n int, err error) {
|
||||
p.mu.RLock()
|
||||
defer p.mu.RUnlock()
|
||||
|
||||
size := len(b)
|
||||
if size+int(off) > len(p.buffer) {
|
||||
size = len(p.buffer) - int(off)
|
||||
if size < 0 {
|
||||
size = 0
|
||||
}
|
||||
}
|
||||
if len(p.buffer) < int(off) || len(p.buffer) < int(off)+size {
|
||||
return 0, io.EOF
|
||||
}
|
||||
n = copy(b, p.buffer[int(off) : int(off)+size][:])
|
||||
p.accessed = time.Now().Unix()
|
||||
if int(off)+size >= len(p.buffer) {
|
||||
p.readed = true
|
||||
}
|
||||
if int64(len(b))+off >= p.Size {
|
||||
go p.cache.cleanPieces()
|
||||
}
|
||||
if n == 0 && err == nil {
|
||||
return 0, io.EOF
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (p *Piece) MarkComplete() error {
|
||||
if len(p.buffer) == 0 {
|
||||
return errors.New("piece is not complete")
|
||||
}
|
||||
p.complete = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Piece) MarkNotComplete() error {
|
||||
p.complete = false
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Piece) Completion() storage.Completion {
|
||||
return storage.Completion{
|
||||
Complete: p.complete && len(p.buffer) > 0,
|
||||
Ok: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Piece) Release() {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
if p.buffer != nil {
|
||||
p.buffer = nil
|
||||
}
|
||||
p.Size = 0
|
||||
p.complete = false
|
||||
|
||||
//Костыль чтобы двиг понял что куска нет, иногда загружает его по новый хз почему
|
||||
pce := p.cache.torrent.Piece(p.Id)
|
||||
pce.SetPriority(torrent.PiecePriorityNone)
|
||||
pce.UpdateCompletion()
|
||||
pce.SetPriority(torrent.PiecePriorityNone)
|
||||
}
|
||||
49
server/torr/storage/torrstor/ranges.go
Normal file
49
server/torr/storage/torrstor/ranges.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package torrstor
|
||||
|
||||
import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
type Range struct {
|
||||
Start, End int
|
||||
}
|
||||
|
||||
func inRanges(ranges []Range, ind int) bool {
|
||||
for _, r := range ranges {
|
||||
if ind >= r.Start && ind <= r.End {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func mergeRange(ranges []Range) []Range {
|
||||
if len(ranges) <= 1 {
|
||||
return ranges
|
||||
}
|
||||
// copy ranges
|
||||
merged := append([]Range(nil), ranges...)
|
||||
|
||||
sort.Slice(merged, func(i, j int) bool {
|
||||
if merged[i].Start < merged[j].Start {
|
||||
return true
|
||||
}
|
||||
if merged[i].Start == merged[j].Start && merged[i].End < merged[j].End {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
j := 0
|
||||
for i := 1; i < len(merged); i++ {
|
||||
if merged[j].End >= merged[i].Start {
|
||||
if merged[j].End < merged[i].End {
|
||||
merged[j].End = merged[i].End
|
||||
}
|
||||
} else {
|
||||
j++
|
||||
merged[j] = merged[i]
|
||||
}
|
||||
}
|
||||
return merged[:j+1]
|
||||
}
|
||||
114
server/torr/storage/torrstor/reader.go
Normal file
114
server/torr/storage/torrstor/reader.go
Normal file
@@ -0,0 +1,114 @@
|
||||
package torrstor
|
||||
|
||||
import (
|
||||
"io"
|
||||
"sync"
|
||||
|
||||
"github.com/anacrolix/torrent"
|
||||
"server/log"
|
||||
)
|
||||
|
||||
type Reader struct {
|
||||
torrent.Reader
|
||||
offset int64
|
||||
readahead int64
|
||||
file *torrent.File
|
||||
|
||||
cache *Cache
|
||||
isClosed bool
|
||||
|
||||
///Preload
|
||||
muPreload sync.Mutex
|
||||
}
|
||||
|
||||
func newReader(file *torrent.File, cache *Cache) *Reader {
|
||||
r := new(Reader)
|
||||
r.file = file
|
||||
r.Reader = file.NewReader()
|
||||
|
||||
r.SetReadahead(0)
|
||||
r.cache = cache
|
||||
|
||||
cache.muReaders.Lock()
|
||||
cache.readers[r] = struct{}{}
|
||||
cache.muReaders.Unlock()
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Reader) Seek(offset int64, whence int) (n int64, err error) {
|
||||
if r.isClosed {
|
||||
return 0, io.EOF
|
||||
}
|
||||
switch whence {
|
||||
case io.SeekStart:
|
||||
r.offset = offset
|
||||
case io.SeekCurrent:
|
||||
r.offset += offset
|
||||
case io.SeekEnd:
|
||||
r.offset = r.file.Length() + offset
|
||||
}
|
||||
n, err = r.Reader.Seek(offset, whence)
|
||||
r.offset = n
|
||||
return
|
||||
}
|
||||
|
||||
func (r *Reader) Read(p []byte) (n int, err error) {
|
||||
err = io.EOF
|
||||
if r.isClosed {
|
||||
return
|
||||
}
|
||||
if r.file.Torrent() != nil && r.file.Torrent().Info() != nil {
|
||||
n, err = r.Reader.Read(p)
|
||||
r.offset += int64(n)
|
||||
go r.preload()
|
||||
} else {
|
||||
log.TLogln("Torrent closed and readed")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (r *Reader) SetReadahead(length int64) {
|
||||
r.Reader.SetReadahead(length)
|
||||
r.readahead = length
|
||||
}
|
||||
|
||||
func (r *Reader) Offset() int64 {
|
||||
return r.offset
|
||||
}
|
||||
|
||||
func (r *Reader) Readahead() int64 {
|
||||
return r.readahead
|
||||
}
|
||||
|
||||
func (r *Reader) Close() {
|
||||
// file reader close in gotorrent
|
||||
// this struct close in cache
|
||||
r.isClosed = true
|
||||
if len(r.file.Torrent().Files()) > 0 {
|
||||
r.Reader.Close()
|
||||
}
|
||||
go r.cache.getRemPieces()
|
||||
}
|
||||
|
||||
func (c *Cache) NewReader(file *torrent.File) *Reader {
|
||||
return newReader(file, c)
|
||||
}
|
||||
|
||||
func (c *Cache) Readers() int {
|
||||
if c == nil {
|
||||
return 0
|
||||
}
|
||||
c.muReaders.Lock()
|
||||
defer c.muReaders.Unlock()
|
||||
if c == nil || c.readers == nil {
|
||||
return 0
|
||||
}
|
||||
return len(c.readers)
|
||||
}
|
||||
|
||||
func (c *Cache) CloseReader(r *Reader) {
|
||||
r.cache.muReaders.Lock()
|
||||
r.Close()
|
||||
delete(r.cache.readers, r)
|
||||
r.cache.muReaders.Unlock()
|
||||
}
|
||||
54
server/torr/storage/torrstor/readerloader.go
Normal file
54
server/torr/storage/torrstor/readerloader.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package torrstor
|
||||
|
||||
import (
|
||||
"github.com/anacrolix/torrent"
|
||||
"server/settings"
|
||||
)
|
||||
|
||||
func (r *Reader) getPiecesRange() Range {
|
||||
startOff, endOff := r.getOffsetRange()
|
||||
return Range{r.getPieceNum(startOff), r.getPieceNum(endOff)}
|
||||
}
|
||||
|
||||
func (r *Reader) getReaderPiece() int {
|
||||
readerOff := r.offset
|
||||
return r.getPieceNum(readerOff)
|
||||
}
|
||||
|
||||
func (r *Reader) getPieceNum(offset int64) int {
|
||||
return int((offset + r.file.Offset()) / r.cache.pieceLength)
|
||||
}
|
||||
|
||||
func (r *Reader) getOffsetRange() (int64, int64) {
|
||||
prc := int64(settings.BTsets.ReaderReadAHead)
|
||||
readers := int64(len(r.cache.readers))
|
||||
if readers == 0 {
|
||||
readers = 1
|
||||
}
|
||||
|
||||
beginOffset := r.offset - (r.cache.capacity/readers)*(100-prc)/100
|
||||
endOffset := r.offset + (r.cache.capacity/readers)*prc/100
|
||||
|
||||
if beginOffset < 0 {
|
||||
beginOffset = 0
|
||||
}
|
||||
|
||||
if endOffset > r.file.Length() {
|
||||
endOffset = r.file.Length()
|
||||
}
|
||||
return beginOffset, endOffset
|
||||
}
|
||||
|
||||
func (r *Reader) preload() {
|
||||
torr := r.file.Torrent()
|
||||
rrange := r.getPiecesRange()
|
||||
rahPiece := int(r.readahead / torr.Info().PieceLength)
|
||||
readerPiece := r.getReaderPiece()
|
||||
|
||||
// from reader readahead to end of range
|
||||
for i := readerPiece + rahPiece; i < rrange.End; i++ {
|
||||
if torr.Piece(i).State().Priority == torrent.PiecePriorityNone {
|
||||
torr.Piece(i).SetPriority(torrent.PiecePriorityNormal)
|
||||
}
|
||||
}
|
||||
}
|
||||
64
server/torr/storage/torrstor/storage.go
Normal file
64
server/torr/storage/torrstor/storage.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package torrstor
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"server/torr/storage"
|
||||
|
||||
"github.com/anacrolix/torrent/metainfo"
|
||||
storage2 "github.com/anacrolix/torrent/storage"
|
||||
)
|
||||
|
||||
type Storage struct {
|
||||
storage.Storage
|
||||
|
||||
caches map[metainfo.Hash]*Cache
|
||||
capacity int64
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func NewStorage(capacity int64) *Storage {
|
||||
stor := new(Storage)
|
||||
stor.capacity = capacity
|
||||
stor.caches = make(map[metainfo.Hash]*Cache)
|
||||
return stor
|
||||
}
|
||||
|
||||
func (s *Storage) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (storage2.TorrentImpl, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
ch := NewCache(s.capacity, s)
|
||||
ch.Init(info, infoHash)
|
||||
s.caches[infoHash] = ch
|
||||
return ch, nil
|
||||
}
|
||||
|
||||
func (s *Storage) CloseHash(hash metainfo.Hash) {
|
||||
if s.caches == nil {
|
||||
return
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if ch, ok := s.caches[hash]; ok {
|
||||
ch.Close()
|
||||
delete(s.caches, hash)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Storage) Close() error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
for _, ch := range s.caches {
|
||||
ch.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Storage) GetCache(hash metainfo.Hash) *Cache {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if cache, ok := s.caches[hash]; ok {
|
||||
return cache
|
||||
}
|
||||
return nil
|
||||
}
|
||||
60
server/torr/stream.go
Normal file
60
server/torr/stream.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package torr
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/anacrolix/missinggo/httptoo"
|
||||
"github.com/anacrolix/torrent"
|
||||
sets "server/settings"
|
||||
"server/torr/state"
|
||||
)
|
||||
|
||||
func (t *Torrent) Stream(fileID int, req *http.Request, resp http.ResponseWriter) error {
|
||||
if !t.GotInfo() {
|
||||
http.NotFound(resp, req)
|
||||
return errors.New("torrent don't get info")
|
||||
}
|
||||
|
||||
st := t.Status()
|
||||
var stFile *state.TorrentFileStat
|
||||
for _, fileStat := range st.FileStats {
|
||||
if fileStat.Id == fileID {
|
||||
stFile = fileStat
|
||||
break
|
||||
}
|
||||
}
|
||||
if stFile == nil {
|
||||
return fmt.Errorf("file with id %v not found", fileID)
|
||||
}
|
||||
|
||||
files := t.Files()
|
||||
var file *torrent.File
|
||||
for _, tfile := range files {
|
||||
if tfile.Path() == stFile.Path {
|
||||
file = tfile
|
||||
break
|
||||
}
|
||||
}
|
||||
if file == nil {
|
||||
return fmt.Errorf("file with id %v not found", fileID)
|
||||
}
|
||||
|
||||
reader := t.NewReader(file)
|
||||
|
||||
log.Println("Connect client")
|
||||
|
||||
sets.SetViewed(&sets.Viewed{t.Hash().HexString(), fileID})
|
||||
|
||||
resp.Header().Set("Connection", "close")
|
||||
resp.Header().Set("ETag", httptoo.EncodeQuotedString(fmt.Sprintf("%s/%s", t.Hash().HexString(), file.Path())))
|
||||
|
||||
http.ServeContent(resp, req, file.Path(), time.Unix(t.Timestamp, 0), reader)
|
||||
|
||||
t.CloseReader(reader)
|
||||
log.Println("Disconnect client")
|
||||
return nil
|
||||
}
|
||||
443
server/torr/torrent.go
Normal file
443
server/torr/torrent.go
Normal file
@@ -0,0 +1,443 @@
|
||||
package torr
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"server/log"
|
||||
"server/settings"
|
||||
"server/torr/state"
|
||||
cacheSt "server/torr/storage/state"
|
||||
"server/torr/storage/torrstor"
|
||||
"server/torr/utils"
|
||||
utils2 "server/utils"
|
||||
|
||||
"github.com/anacrolix/torrent"
|
||||
"github.com/anacrolix/torrent/metainfo"
|
||||
)
|
||||
|
||||
type Torrent struct {
|
||||
Title string
|
||||
Poster string
|
||||
Data string
|
||||
*torrent.TorrentSpec
|
||||
|
||||
Stat state.TorrentStat
|
||||
Timestamp int64
|
||||
Size int64
|
||||
|
||||
*torrent.Torrent
|
||||
muTorrent sync.Mutex
|
||||
|
||||
bt *BTServer
|
||||
cache *torrstor.Cache
|
||||
|
||||
lastTimeSpeed time.Time
|
||||
DownloadSpeed float64
|
||||
UploadSpeed float64
|
||||
BytesReadUsefulData int64
|
||||
BytesWrittenData int64
|
||||
|
||||
PreloadSize int64
|
||||
PreloadedBytes int64
|
||||
|
||||
expiredTime time.Time
|
||||
|
||||
closed <-chan struct{}
|
||||
|
||||
progressTicker *time.Ticker
|
||||
}
|
||||
|
||||
func NewTorrent(spec *torrent.TorrentSpec, bt *BTServer) (*Torrent, error) {
|
||||
// TODO panic when settings sets
|
||||
if bt == nil {
|
||||
return nil, errors.New("BT client not connected")
|
||||
}
|
||||
switch settings.BTsets.RetrackersMode {
|
||||
case 1:
|
||||
spec.Trackers = append(spec.Trackers, [][]string{utils.GetDefTrackers()}...)
|
||||
case 2:
|
||||
spec.Trackers = nil
|
||||
case 3:
|
||||
spec.Trackers = [][]string{utils.GetDefTrackers()}
|
||||
}
|
||||
|
||||
goTorrent, _, err := bt.client.AddTorrentSpec(spec)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bt.mu.Lock()
|
||||
defer bt.mu.Unlock()
|
||||
if tor, ok := bt.torrents[spec.InfoHash]; ok {
|
||||
return tor, nil
|
||||
}
|
||||
|
||||
torr := new(Torrent)
|
||||
torr.Torrent = goTorrent
|
||||
torr.Stat = state.TorrentAdded
|
||||
torr.lastTimeSpeed = time.Now()
|
||||
torr.bt = bt
|
||||
torr.closed = goTorrent.Closed()
|
||||
torr.TorrentSpec = spec
|
||||
torr.AddExpiredTime(time.Minute)
|
||||
torr.Timestamp = time.Now().Unix()
|
||||
|
||||
go torr.watch()
|
||||
|
||||
bt.torrents[spec.InfoHash] = torr
|
||||
return torr, nil
|
||||
}
|
||||
|
||||
func (t *Torrent) WaitInfo() bool {
|
||||
if t.Torrent == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// Close torrent if not info while 10 minutes
|
||||
tm := time.NewTimer(time.Minute * 10)
|
||||
|
||||
select {
|
||||
case <-t.Torrent.GotInfo():
|
||||
t.cache = t.bt.storage.GetCache(t.Hash())
|
||||
t.cache.SetTorrent(t.Torrent)
|
||||
return true
|
||||
case <-t.closed:
|
||||
return false
|
||||
case <-tm.C:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Torrent) GotInfo() bool {
|
||||
if t.Stat == state.TorrentClosed {
|
||||
return false
|
||||
}
|
||||
t.Stat = state.TorrentGettingInfo
|
||||
if t.WaitInfo() {
|
||||
t.Stat = state.TorrentWorking
|
||||
t.AddExpiredTime(time.Minute * 5)
|
||||
return true
|
||||
} else {
|
||||
t.Close()
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Torrent) AddExpiredTime(duration time.Duration) {
|
||||
t.expiredTime = time.Now().Add(duration)
|
||||
}
|
||||
|
||||
func (t *Torrent) watch() {
|
||||
t.progressTicker = time.NewTicker(time.Second)
|
||||
defer t.progressTicker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-t.progressTicker.C:
|
||||
go t.progressEvent()
|
||||
case <-t.closed:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Torrent) progressEvent() {
|
||||
if t.expired() {
|
||||
log.TLogln("Torrent close by timeout", t.Torrent.InfoHash().HexString())
|
||||
t.bt.RemoveTorrent(t.Hash())
|
||||
return
|
||||
}
|
||||
|
||||
t.muTorrent.Lock()
|
||||
if t.Torrent != nil && t.Torrent.Info() != nil {
|
||||
st := t.Torrent.Stats()
|
||||
deltaDlBytes := st.BytesReadUsefulData.Int64() - t.BytesReadUsefulData
|
||||
deltaUpBytes := st.BytesWrittenData.Int64() - t.BytesWrittenData
|
||||
deltaTime := time.Since(t.lastTimeSpeed).Seconds()
|
||||
|
||||
t.DownloadSpeed = float64(deltaDlBytes) / deltaTime
|
||||
t.UploadSpeed = float64(deltaUpBytes) / deltaTime
|
||||
|
||||
t.BytesWrittenData = st.BytesWrittenData.Int64()
|
||||
t.BytesReadUsefulData = st.BytesReadUsefulData.Int64()
|
||||
|
||||
t.PreloadedBytes = t.Torrent.BytesCompleted()
|
||||
} else {
|
||||
t.DownloadSpeed = 0
|
||||
t.UploadSpeed = 0
|
||||
}
|
||||
t.muTorrent.Unlock()
|
||||
|
||||
t.lastTimeSpeed = time.Now()
|
||||
t.updateRA()
|
||||
}
|
||||
|
||||
func (t *Torrent) updateRA() {
|
||||
t.muTorrent.Lock()
|
||||
defer t.muTorrent.Unlock()
|
||||
if t.Torrent != nil && t.Torrent.Info() != nil {
|
||||
pieceLen := t.Torrent.Info().PieceLength
|
||||
adj := pieceLen * int64(t.Torrent.Stats().ActivePeers) / int64(1+t.cache.Readers())
|
||||
switch {
|
||||
case adj < pieceLen:
|
||||
adj = pieceLen
|
||||
case adj > pieceLen*4:
|
||||
adj = pieceLen * 4
|
||||
}
|
||||
go t.cache.AdjustRA(adj)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Torrent) expired() bool {
|
||||
return t.cache.Readers() == 0 && t.expiredTime.Before(time.Now()) && (t.Stat == state.TorrentWorking || t.Stat == state.TorrentClosed)
|
||||
}
|
||||
|
||||
func (t *Torrent) Files() []*torrent.File {
|
||||
if t.Torrent != nil && t.Torrent.Info() != nil {
|
||||
files := t.Torrent.Files()
|
||||
return files
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Torrent) Hash() metainfo.Hash {
|
||||
if t.Torrent != nil {
|
||||
t.Torrent.InfoHash()
|
||||
}
|
||||
if t.TorrentSpec != nil {
|
||||
return t.TorrentSpec.InfoHash
|
||||
}
|
||||
return [20]byte{}
|
||||
}
|
||||
|
||||
func (t *Torrent) Length() int64 {
|
||||
if t.Info() == nil {
|
||||
return 0
|
||||
}
|
||||
return t.Torrent.Length()
|
||||
}
|
||||
|
||||
func (t *Torrent) NewReader(file *torrent.File) *torrstor.Reader {
|
||||
if t.Stat == state.TorrentClosed {
|
||||
return nil
|
||||
}
|
||||
reader := t.cache.NewReader(file)
|
||||
return reader
|
||||
}
|
||||
|
||||
func (t *Torrent) CloseReader(reader *torrstor.Reader) {
|
||||
t.cache.CloseReader(reader)
|
||||
t.AddExpiredTime(time.Second * time.Duration(settings.BTsets.TorrentDisconnectTimeout))
|
||||
}
|
||||
|
||||
func (t *Torrent) GetCache() *torrstor.Cache {
|
||||
return t.cache
|
||||
}
|
||||
|
||||
func (t *Torrent) Preload(index int, size int64) {
|
||||
if size <= 0 {
|
||||
return
|
||||
}
|
||||
t.PreloadSize = size
|
||||
|
||||
if t.Stat == state.TorrentGettingInfo {
|
||||
if !t.WaitInfo() {
|
||||
return
|
||||
}
|
||||
// wait change status
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
|
||||
t.muTorrent.Lock()
|
||||
if t.Stat != state.TorrentWorking {
|
||||
t.muTorrent.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
t.Stat = state.TorrentPreload
|
||||
t.muTorrent.Unlock()
|
||||
|
||||
defer func() {
|
||||
if t.Stat == state.TorrentPreload {
|
||||
t.Stat = state.TorrentWorking
|
||||
}
|
||||
}()
|
||||
|
||||
file := t.findFileIndex(index)
|
||||
if file == nil {
|
||||
file = t.Files()[0]
|
||||
}
|
||||
|
||||
if t.Info() != nil {
|
||||
pl := t.Info().PieceLength
|
||||
mb5 := int64(5 * 1024 * 1024)
|
||||
|
||||
pieceFileStart := int(file.Offset() / pl)
|
||||
pieceFileEnd := int((file.Offset() + file.Length()) / pl)
|
||||
readerPieceBefore := int((file.Offset() + size - mb5) / pl)
|
||||
readerPieceAfter := int((file.Offset() + file.Length() - mb5) / pl)
|
||||
|
||||
lastStat := time.Now().Add(-time.Second)
|
||||
|
||||
for true {
|
||||
t.muTorrent.Lock()
|
||||
if t.Torrent == nil {
|
||||
return
|
||||
}
|
||||
t.PreloadedBytes = t.Torrent.BytesCompleted()
|
||||
t.muTorrent.Unlock()
|
||||
|
||||
stat := fmt.Sprint(file.Torrent().InfoHash().HexString(), " ", utils2.Format(float64(t.PreloadedBytes)), "/", utils2.Format(float64(t.PreloadSize)), " Speed:", utils2.Format(t.DownloadSpeed), " Peers:[", t.Torrent.Stats().ConnectedSeeders, "]", t.Torrent.Stats().ActivePeers, "/", t.Torrent.Stats().TotalPeers)
|
||||
if time.Since(lastStat) > time.Second {
|
||||
log.TLogln("Preload:", stat)
|
||||
lastStat = time.Now()
|
||||
}
|
||||
|
||||
isComplete := true
|
||||
if readerPieceBefore >= pieceFileStart {
|
||||
for i := pieceFileStart; i < readerPieceBefore; i++ {
|
||||
if !t.Piece(i).State().Complete {
|
||||
isComplete = false
|
||||
if t.Piece(i).State().Priority == torrent.PiecePriorityNone {
|
||||
t.Piece(i).SetPriority(torrent.PiecePriorityNormal)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if readerPieceAfter <= pieceFileEnd {
|
||||
for i := readerPieceAfter; i <= pieceFileEnd; i++ {
|
||||
if !t.Piece(i).State().Complete {
|
||||
isComplete = false
|
||||
if t.Piece(i).State().Priority == torrent.PiecePriorityNone {
|
||||
t.Piece(i).SetPriority(torrent.PiecePriorityNormal)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
t.AddExpiredTime(time.Second * time.Duration(settings.BTsets.TorrentDisconnectTimeout))
|
||||
if isComplete {
|
||||
break
|
||||
}
|
||||
time.Sleep(time.Millisecond * 10)
|
||||
}
|
||||
}
|
||||
log.TLogln("End preload:", file.Torrent().InfoHash().HexString(), "Peers:[", t.Torrent.Stats().ConnectedSeeders, "]", t.Torrent.Stats().ActivePeers, "/", t.Torrent.Stats().TotalPeers)
|
||||
}
|
||||
|
||||
func (t *Torrent) drop() {
|
||||
t.muTorrent.Lock()
|
||||
if t.Torrent != nil {
|
||||
t.Torrent.Drop()
|
||||
t.Torrent = nil
|
||||
}
|
||||
t.muTorrent.Unlock()
|
||||
}
|
||||
|
||||
func (t *Torrent) Close() {
|
||||
t.Stat = state.TorrentClosed
|
||||
|
||||
t.bt.mu.Lock()
|
||||
if _, ok := t.bt.torrents[t.Hash()]; ok {
|
||||
delete(t.bt.torrents, t.Hash())
|
||||
}
|
||||
t.bt.mu.Unlock()
|
||||
|
||||
t.drop()
|
||||
}
|
||||
|
||||
func (t *Torrent) Status() *state.TorrentStatus {
|
||||
t.muTorrent.Lock()
|
||||
defer t.muTorrent.Unlock()
|
||||
|
||||
st := new(state.TorrentStatus)
|
||||
|
||||
st.Stat = t.Stat
|
||||
st.StatString = t.Stat.String()
|
||||
st.Title = t.Title
|
||||
st.Poster = t.Poster
|
||||
st.Data = t.Data
|
||||
st.Timestamp = t.Timestamp
|
||||
st.TorrentSize = t.Size
|
||||
|
||||
if t.TorrentSpec != nil {
|
||||
st.Hash = t.TorrentSpec.InfoHash.HexString()
|
||||
}
|
||||
if t.Torrent != nil {
|
||||
st.Name = t.Torrent.Name()
|
||||
st.Hash = t.Torrent.InfoHash().HexString()
|
||||
st.LoadedSize = t.Torrent.BytesCompleted()
|
||||
|
||||
st.PreloadedBytes = t.PreloadedBytes
|
||||
st.PreloadSize = t.PreloadSize
|
||||
st.DownloadSpeed = t.DownloadSpeed
|
||||
st.UploadSpeed = t.UploadSpeed
|
||||
|
||||
tst := t.Torrent.Stats()
|
||||
st.BytesWritten = tst.BytesWritten.Int64()
|
||||
st.BytesWrittenData = tst.BytesWrittenData.Int64()
|
||||
st.BytesRead = tst.BytesRead.Int64()
|
||||
st.BytesReadData = tst.BytesReadData.Int64()
|
||||
st.BytesReadUsefulData = tst.BytesReadUsefulData.Int64()
|
||||
st.ChunksWritten = tst.ChunksWritten.Int64()
|
||||
st.ChunksRead = tst.ChunksRead.Int64()
|
||||
st.ChunksReadUseful = tst.ChunksReadUseful.Int64()
|
||||
st.ChunksReadWasted = tst.ChunksReadWasted.Int64()
|
||||
st.PiecesDirtiedGood = tst.PiecesDirtiedGood.Int64()
|
||||
st.PiecesDirtiedBad = tst.PiecesDirtiedBad.Int64()
|
||||
st.TotalPeers = tst.TotalPeers
|
||||
st.PendingPeers = tst.PendingPeers
|
||||
st.ActivePeers = tst.ActivePeers
|
||||
st.ConnectedSeeders = tst.ConnectedSeeders
|
||||
st.HalfOpenPeers = tst.HalfOpenPeers
|
||||
|
||||
if t.Torrent.Info() != nil {
|
||||
st.TorrentSize = t.Torrent.Length()
|
||||
|
||||
files := t.Files()
|
||||
sort.Slice(files, func(i, j int) bool {
|
||||
return files[i].Path() < files[j].Path()
|
||||
})
|
||||
for i, f := range files {
|
||||
st.FileStats = append(st.FileStats, &state.TorrentFileStat{
|
||||
Id: i + 1, // in web id 0 is undefined
|
||||
Path: f.Path(),
|
||||
Length: f.Length(),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
return st
|
||||
}
|
||||
|
||||
func (t *Torrent) CacheState() *cacheSt.CacheState {
|
||||
if t.Torrent != nil && t.cache != nil {
|
||||
st := t.cache.GetState()
|
||||
st.Torrent = t.Status()
|
||||
return st
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Torrent) findFileIndex(index int) *torrent.File {
|
||||
st := t.Status()
|
||||
var stFile *state.TorrentFileStat
|
||||
for _, f := range st.FileStats {
|
||||
if index == f.Id {
|
||||
stFile = f
|
||||
break
|
||||
}
|
||||
}
|
||||
if stFile == nil {
|
||||
return nil
|
||||
}
|
||||
for _, file := range t.Files() {
|
||||
if file.Path() == stFile.Path {
|
||||
return file
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
35
server/torr/utils/blockedIP.go
Normal file
35
server/torr/utils/blockedIP.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"server/settings"
|
||||
|
||||
"github.com/anacrolix/torrent/iplist"
|
||||
)
|
||||
|
||||
func ReadBlockedIP() (ranger iplist.Ranger, err error) {
|
||||
buf, err := ioutil.ReadFile(filepath.Join(settings.Path, "blocklist"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
scanner := bufio.NewScanner(strings.NewReader(string(buf)))
|
||||
var ranges []iplist.Range
|
||||
for scanner.Scan() {
|
||||
r, ok, err := iplist.ParseBlocklistP2PLine(scanner.Bytes())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ok {
|
||||
ranges = append(ranges, r)
|
||||
}
|
||||
}
|
||||
err = scanner.Err()
|
||||
if len(ranges) > 0 {
|
||||
ranger = iplist.New(ranges)
|
||||
}
|
||||
return
|
||||
}
|
||||
15
server/torr/utils/freemem.go
Normal file
15
server/torr/utils/freemem.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"runtime/debug"
|
||||
)
|
||||
|
||||
func FreeOSMem() {
|
||||
debug.FreeOSMemory()
|
||||
}
|
||||
|
||||
func FreeOSMemGC() {
|
||||
runtime.GC()
|
||||
debug.FreeOSMemory()
|
||||
}
|
||||
78
server/torr/utils/torrent.go
Normal file
78
server/torr/utils/torrent.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/base32"
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
var defTrackers = []string{
|
||||
"http://retracker.local",
|
||||
"http://bt4.t-ru.org/ann?magnet",
|
||||
"http://retracker.mgts.by:80/announce",
|
||||
"http://tracker.city9x.com:2710/announce",
|
||||
"http://tracker.electro-torrent.pl:80/announce",
|
||||
"http://tracker.internetwarriors.net:1337/announce",
|
||||
"http://tracker2.itzmx.com:6961/announce",
|
||||
"udp://opentor.org:2710",
|
||||
"udp://public.popcorn-tracker.org:6969/announce",
|
||||
"udp://tracker.opentrackr.org:1337/announce",
|
||||
"http://bt.svao-ix.ru/announce",
|
||||
"udp://explodie.org:6969/announce",
|
||||
}
|
||||
|
||||
var loadedTrackers []string
|
||||
|
||||
func GetDefTrackers() []string {
|
||||
loadNewTracker()
|
||||
if len(loadedTrackers) == 0 {
|
||||
return defTrackers
|
||||
}
|
||||
return loadedTrackers
|
||||
}
|
||||
|
||||
func loadNewTracker() {
|
||||
if len(loadedTrackers) > 0 {
|
||||
return
|
||||
}
|
||||
resp, err := http.Get("https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_best_ip.txt")
|
||||
if err == nil {
|
||||
buf, err := ioutil.ReadAll(resp.Body)
|
||||
if err == nil {
|
||||
arr := strings.Split(string(buf), "\n")
|
||||
var ret []string
|
||||
for _, s := range arr {
|
||||
s = strings.TrimSpace(s)
|
||||
if len(s) > 0 {
|
||||
ret = append(ret, s)
|
||||
}
|
||||
}
|
||||
loadedTrackers = append(ret, defTrackers...)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func PeerIDRandom(peer string) string {
|
||||
randomBytes := make([]byte, 32)
|
||||
_, err := rand.Read(randomBytes)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return peer + base32.StdEncoding.EncodeToString(randomBytes)[:20-len(peer)]
|
||||
}
|
||||
|
||||
func Limit(i int) *rate.Limiter {
|
||||
l := rate.NewLimiter(rate.Inf, 0)
|
||||
if i > 0 {
|
||||
b := i
|
||||
if b < 16*1024 {
|
||||
b = 16 * 1024
|
||||
}
|
||||
l = rate.NewLimiter(rate.Limit(i), b)
|
||||
}
|
||||
return l
|
||||
}
|
||||
Reference in New Issue
Block a user