Initial commit: docker compose config
Release Docker multi arch / docker (push) Has been cancelled
Test Install Script / Test Script Syntax (push) Has been cancelled
Test Install Script / Test on almalinux-10 (default) (push) Has been cancelled
Test Install Script / Test on almalinux-10 (root) (push) Has been cancelled
Test Install Script / Test on almalinux-8 (default) (push) Has been cancelled
Test Install Script / Test on almalinux-8 (root) (push) Has been cancelled
Test Install Script / Test on almalinux-9 (default) (push) Has been cancelled
Test Install Script / Test on almalinux-9 (root) (push) Has been cancelled
Test Install Script / Test on amazonlinux-2 (default) (push) Has been cancelled
Test Install Script / Test on amazonlinux-2 (root) (push) Has been cancelled
Test Install Script / Test on debian-11 (default) (push) Has been cancelled
Test Install Script / Test on debian-11 (root) (push) Has been cancelled
Test Install Script / Test on debian-12 (default) (push) Has been cancelled
Test Install Script / Test on debian-12 (root) (push) Has been cancelled
Test Install Script / Test on debian-13 (default) (push) Has been cancelled
Test Install Script / Test on debian-13 (root) (push) Has been cancelled
Test Install Script / Test on fedora-latest (default) (push) Has been cancelled
Test Install Script / Test on fedora-latest (root) (push) Has been cancelled
Test Install Script / Test on rocky-10 (default) (push) Has been cancelled
Test Install Script / Test on rocky-10 (root) (push) Has been cancelled
Test Install Script / Test on rocky-8 (default) (push) Has been cancelled
Test Install Script / Test on rocky-8 (root) (push) Has been cancelled
Test Install Script / Test on rocky-9 (default) (push) Has been cancelled
Test Install Script / Test on rocky-9 (root) (push) Has been cancelled
Test Install Script / Test on ubuntu-22.04 (default) (push) Has been cancelled
Test Install Script / Test on ubuntu-22.04 (root) (push) Has been cancelled
Test Install Script / Test on ubuntu-24.04 (default) (push) Has been cancelled
Test Install Script / Test on ubuntu-24.04 (root) (push) Has been cancelled
Release Docker multi arch / docker (push) Has been cancelled
Test Install Script / Test Script Syntax (push) Has been cancelled
Test Install Script / Test on almalinux-10 (default) (push) Has been cancelled
Test Install Script / Test on almalinux-10 (root) (push) Has been cancelled
Test Install Script / Test on almalinux-8 (default) (push) Has been cancelled
Test Install Script / Test on almalinux-8 (root) (push) Has been cancelled
Test Install Script / Test on almalinux-9 (default) (push) Has been cancelled
Test Install Script / Test on almalinux-9 (root) (push) Has been cancelled
Test Install Script / Test on amazonlinux-2 (default) (push) Has been cancelled
Test Install Script / Test on amazonlinux-2 (root) (push) Has been cancelled
Test Install Script / Test on debian-11 (default) (push) Has been cancelled
Test Install Script / Test on debian-11 (root) (push) Has been cancelled
Test Install Script / Test on debian-12 (default) (push) Has been cancelled
Test Install Script / Test on debian-12 (root) (push) Has been cancelled
Test Install Script / Test on debian-13 (default) (push) Has been cancelled
Test Install Script / Test on debian-13 (root) (push) Has been cancelled
Test Install Script / Test on fedora-latest (default) (push) Has been cancelled
Test Install Script / Test on fedora-latest (root) (push) Has been cancelled
Test Install Script / Test on rocky-10 (default) (push) Has been cancelled
Test Install Script / Test on rocky-10 (root) (push) Has been cancelled
Test Install Script / Test on rocky-8 (default) (push) Has been cancelled
Test Install Script / Test on rocky-8 (root) (push) Has been cancelled
Test Install Script / Test on rocky-9 (default) (push) Has been cancelled
Test Install Script / Test on rocky-9 (root) (push) Has been cancelled
Test Install Script / Test on ubuntu-22.04 (default) (push) Has been cancelled
Test Install Script / Test on ubuntu-22.04 (root) (push) Has been cancelled
Test Install Script / Test on ubuntu-24.04 (default) (push) Has been cancelled
Test Install Script / Test on ubuntu-24.04 (root) (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,277 @@
|
||||
package torr
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"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 LoadTorrent(tor *Torrent) *Torrent {
|
||||
if tor.TorrentSpec == nil {
|
||||
return nil
|
||||
}
|
||||
tr, err := NewTorrent(tor.TorrentSpec, bts)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
if !tr.WaitInfo() {
|
||||
return nil
|
||||
}
|
||||
tr.Title = tor.Title
|
||||
tr.Poster = tor.Poster
|
||||
tr.Data = tor.Data
|
||||
return tr
|
||||
}
|
||||
|
||||
func AddTorrent(spec *torrent.TorrentSpec, title, poster string, data string, category 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.Title == "" && torr.Torrent != nil && torr.Torrent.Info() != nil {
|
||||
torr.Title = torr.Info().Name
|
||||
}
|
||||
}
|
||||
|
||||
if torr.Category == "" {
|
||||
torr.Category = category
|
||||
if torr.Category == "" && torDB != nil {
|
||||
torr.Category = torDB.Category
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
timeout := time.Second * time.Duration(sets.BTsets.TorrentDisconnectTimeout)
|
||||
if timeout > time.Minute {
|
||||
timeout = time.Minute
|
||||
}
|
||||
tor := bts.GetTorrent(hash)
|
||||
if tor != nil {
|
||||
tor.AddExpiredTime(timeout)
|
||||
return tor
|
||||
}
|
||||
|
||||
tr := GetTorrentDB(hash)
|
||||
if tr != nil {
|
||||
tor = tr
|
||||
go func() {
|
||||
log.TLogln("New torrent", tor.Hash())
|
||||
tr, _ := NewTorrent(tor.TorrentSpec, bts)
|
||||
if tr != nil {
|
||||
tr.Title = tor.Title
|
||||
tr.Poster = tor.Poster
|
||||
tr.Data = tor.Data
|
||||
tr.Size = tor.Size
|
||||
tr.Timestamp = tor.Timestamp
|
||||
tr.Category = tor.Category
|
||||
tr.GotInfo()
|
||||
}
|
||||
}()
|
||||
}
|
||||
return tor
|
||||
}
|
||||
|
||||
func SetTorrent(hashHex, title, poster, category string, data string) *Torrent {
|
||||
hash := metainfo.NewHashFromHex(hashHex)
|
||||
torr := bts.GetTorrent(hash)
|
||||
torrDb := GetTorrentDB(hash)
|
||||
|
||||
if title == "" && torr == nil && torrDb != nil {
|
||||
torr = GetTorrent(hashHex)
|
||||
torr.GotInfo()
|
||||
if torr.Torrent != nil && torr.Torrent.Info() != nil {
|
||||
title = torr.Info().Name
|
||||
}
|
||||
}
|
||||
|
||||
if torr != nil {
|
||||
if title == "" && torr.Torrent != nil && torr.Torrent.Info() != nil {
|
||||
title = torr.Info().Name
|
||||
}
|
||||
torr.Title = title
|
||||
torr.Poster = poster
|
||||
torr.Category = category
|
||||
if data != "" {
|
||||
torr.Data = data
|
||||
}
|
||||
}
|
||||
// update torrent data in DB
|
||||
if torrDb != nil {
|
||||
torrDb.Title = title
|
||||
torrDb.Poster = poster
|
||||
torrDb.Category = category
|
||||
if data != "" {
|
||||
torrDb.Data = data
|
||||
}
|
||||
AddTorrentDB(torrDb)
|
||||
}
|
||||
if torr != nil {
|
||||
return torr
|
||||
} else {
|
||||
return torrDb
|
||||
}
|
||||
}
|
||||
|
||||
func RemTorrent(hashHex string) {
|
||||
if sets.ReadOnly {
|
||||
log.TLogln("API RemTorrent: Read-only DB mode!", hashHex)
|
||||
return
|
||||
}
|
||||
hash := metainfo.NewHashFromHex(hashHex)
|
||||
if bts.RemoveTorrent(hash) {
|
||||
if sets.BTsets.UseDisk && hashHex != "" && hashHex != "/" {
|
||||
name := filepath.Join(sets.BTsets.TorrentsSavePath, hashHex)
|
||||
ff, _ := os.ReadDir(name)
|
||||
for _, f := range ff {
|
||||
os.Remove(filepath.Join(name, f.Name()))
|
||||
}
|
||||
err := os.Remove(name)
|
||||
if err != nil {
|
||||
log.TLogln("Error remove cache:", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
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 {
|
||||
log.TLogln("API SetSettings: Read-only DB mode!")
|
||||
return
|
||||
}
|
||||
sets.SetBTSets(set)
|
||||
log.TLogln("drop all torrents")
|
||||
dropAllTorrent()
|
||||
time.Sleep(time.Second * 1)
|
||||
log.TLogln("disconect")
|
||||
bts.Disconnect()
|
||||
log.TLogln("connect")
|
||||
bts.Connect()
|
||||
time.Sleep(time.Second * 1)
|
||||
log.TLogln("end set settings")
|
||||
}
|
||||
|
||||
func SetDefSettings() {
|
||||
if sets.ReadOnly {
|
||||
log.TLogln("API SetDefSettings: Read-only DB mode!")
|
||||
return
|
||||
}
|
||||
sets.SetDefaultConfig()
|
||||
log.TLogln("drop all torrents")
|
||||
dropAllTorrent()
|
||||
time.Sleep(time.Second * 1)
|
||||
log.TLogln("disconect")
|
||||
bts.Disconnect()
|
||||
log.TLogln("connect")
|
||||
bts.Connect()
|
||||
time.Sleep(time.Second * 1)
|
||||
log.TLogln("end set default settings")
|
||||
}
|
||||
|
||||
func dropAllTorrent() {
|
||||
for _, torr := range bts.torrents {
|
||||
torr.drop()
|
||||
<-torr.closed
|
||||
}
|
||||
}
|
||||
|
||||
func Shutdown() {
|
||||
bts.Disconnect()
|
||||
sets.CloseDB()
|
||||
log.TLogln("Received shutdown. Quit")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func WriteStatus(w io.Writer) {
|
||||
bts.client.WriteStatus(w)
|
||||
}
|
||||
|
||||
func Preload(torr *Torrent, index int) {
|
||||
cache := float32(sets.BTsets.CacheSize)
|
||||
preload := float32(sets.BTsets.PreloadCache)
|
||||
size := int64((cache / 100.0) * preload)
|
||||
if size <= 0 {
|
||||
return
|
||||
}
|
||||
if size > sets.BTsets.CacheSize {
|
||||
size = sets.BTsets.CacheSize
|
||||
}
|
||||
torr.Preload(index, size)
|
||||
}
|
||||
@@ -0,0 +1,338 @@
|
||||
package torr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"maps"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"server/proxy"
|
||||
"sync"
|
||||
|
||||
"github.com/anacrolix/publicip"
|
||||
"github.com/anacrolix/torrent"
|
||||
"github.com/anacrolix/torrent/metainfo"
|
||||
"github.com/wlynxg/anet"
|
||||
|
||||
"server/settings"
|
||||
"server/torr/storage/torrstor"
|
||||
"server/torr/utils"
|
||||
"server/version"
|
||||
)
|
||||
|
||||
type BTServer struct {
|
||||
config *torrent.ClientConfig
|
||||
client *torrent.Client
|
||||
|
||||
storage *torrstor.Storage
|
||||
|
||||
torrents map[metainfo.Hash]*Torrent
|
||||
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
var privateIPBlocks []*net.IPNet
|
||||
|
||||
func init() {
|
||||
for _, cidr := range []string{
|
||||
"127.0.0.0/8", // IPv4 loopback
|
||||
"10.0.0.0/8", // RFC1918
|
||||
"172.16.0.0/12", // RFC1918
|
||||
"192.168.0.0/16", // RFC1918
|
||||
"169.254.0.0/16", // RFC3927 link-local
|
||||
"::1/128", // IPv6 loopback
|
||||
"fe80::/10", // IPv6 link-local
|
||||
"fc00::/7", // IPv6 unique local addr
|
||||
} {
|
||||
_, block, err := net.ParseCIDR(cidr)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("parse error on %q: %v", cidr, err))
|
||||
}
|
||||
privateIPBlocks = append(privateIPBlocks, block)
|
||||
}
|
||||
}
|
||||
|
||||
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(context.TODO())
|
||||
bt.client, err = torrent.NewClient(bt.config)
|
||||
bt.torrents = make(map[metainfo.Hash]*Torrent)
|
||||
InitApiHelper(bt)
|
||||
|
||||
proxy.Start()
|
||||
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()
|
||||
}
|
||||
proxy.Stop()
|
||||
}
|
||||
|
||||
func (bt *BTServer) configure(ctx context.Context) {
|
||||
blocklist, _ := utils.ReadBlockedIP()
|
||||
bt.config = torrent.NewDefaultClientConfig()
|
||||
|
||||
bt.storage = torrstor.NewStorage(settings.BTsets.CacheSize)
|
||||
bt.config.DefaultStorage = bt.storage
|
||||
|
||||
userAgent := "qBittorrent/4.3.9"
|
||||
peerID := "-qB4390-"
|
||||
upnpID := "TorrServer/" + version.Version
|
||||
cliVers := userAgent
|
||||
|
||||
bt.config.Debug = settings.BTsets.EnableDebug
|
||||
bt.config.DisableIPv6 = !settings.BTsets.EnableIPv6
|
||||
bt.config.DisableTCP = settings.BTsets.DisableTCP
|
||||
bt.config.DisableUTP = settings.BTsets.DisableUTP
|
||||
// https://github.com/anacrolix/torrent/issues/703
|
||||
// bt.config.DisableWebtorrent = true // NE
|
||||
// bt.config.DisableWebseeds = false // NE
|
||||
bt.config.NoDefaultPortForwarding = settings.BTsets.DisableUPNP
|
||||
bt.config.NoDHT = settings.BTsets.DisableDHT
|
||||
bt.config.DisablePEX = settings.BTsets.DisablePEX
|
||||
bt.config.NoUpload = settings.BTsets.DisableUpload
|
||||
bt.config.IPBlocklist = blocklist
|
||||
bt.config.Bep20 = peerID
|
||||
bt.config.PeerID = utils.PeerIDRandom(peerID)
|
||||
bt.config.UpnpID = upnpID
|
||||
bt.config.HTTPUserAgent = userAgent
|
||||
bt.config.ExtendedHandshakeClientVersion = cliVers
|
||||
bt.config.EstablishedConnsPerTorrent = settings.BTsets.ConnectionsLimit
|
||||
bt.config.TotalHalfOpenConns = 500
|
||||
// Encryption/Obfuscation
|
||||
bt.config.EncryptionPolicy = torrent.EncryptionPolicy{ // OE
|
||||
ForceEncryption: settings.BTsets.ForceEncrypt, // OE
|
||||
} // OE
|
||||
// bt.config.HeaderObfuscationPolicy = torrent.HeaderObfuscationPolicy{ // NE
|
||||
// RequirePreferred: settings.BTsets.ForceEncrypt, // NE
|
||||
// Preferred: true, // NE
|
||||
// } // NE
|
||||
if settings.BTsets.DownloadRateLimit > 0 {
|
||||
bt.config.DownloadRateLimiter = utils.Limit(settings.BTsets.DownloadRateLimit * 1024)
|
||||
}
|
||||
if settings.BTsets.UploadRateLimit > 0 {
|
||||
bt.config.Seed = true
|
||||
bt.config.UploadRateLimiter = utils.Limit(settings.BTsets.UploadRateLimit * 1024)
|
||||
}
|
||||
if settings.TorAddr != "" {
|
||||
log.Println("Set listen addr", settings.TorAddr)
|
||||
bt.config.SetListenAddr(settings.TorAddr)
|
||||
} else {
|
||||
if settings.BTsets.PeersListenPort > 0 {
|
||||
log.Println("Set listen port", settings.BTsets.PeersListenPort)
|
||||
bt.config.ListenPort = settings.BTsets.PeersListenPort
|
||||
} else {
|
||||
log.Println("Set listen port to random autoselect (0)")
|
||||
bt.config.ListenPort = 0
|
||||
}
|
||||
}
|
||||
|
||||
// Configure proxy if enabled
|
||||
if err := bt.configureProxy(); err != nil {
|
||||
log.Println("Proxy configuration error:", err)
|
||||
}
|
||||
|
||||
log.Println("Client config:", settings.BTsets)
|
||||
|
||||
var err error
|
||||
|
||||
// set public IPv4
|
||||
if settings.PubIPv4 != "" {
|
||||
if ip4 := net.ParseIP(settings.PubIPv4); ip4.To4() != nil && !isPrivateIP(ip4) {
|
||||
bt.config.PublicIp4 = ip4
|
||||
}
|
||||
}
|
||||
if bt.config.PublicIp4 == nil {
|
||||
bt.config.PublicIp4, err = publicip.Get4(ctx)
|
||||
if err != nil {
|
||||
log.Printf("error getting public ipv4 address: %v", err)
|
||||
}
|
||||
}
|
||||
if bt.config.PublicIp4.To4() == nil { // possible IPv6 from publicip.Get4(ctx)
|
||||
bt.config.PublicIp4 = nil
|
||||
}
|
||||
if bt.config.PublicIp4 != nil {
|
||||
log.Println("PublicIp4:", bt.config.PublicIp4)
|
||||
}
|
||||
|
||||
// set public IPv6
|
||||
if settings.PubIPv6 != "" {
|
||||
if ip6 := net.ParseIP(settings.PubIPv6); ip6.To16() != nil && ip6.To4() == nil && !isPrivateIP(ip6) {
|
||||
bt.config.PublicIp6 = ip6
|
||||
}
|
||||
}
|
||||
if bt.config.PublicIp6 == nil && settings.BTsets.EnableIPv6 {
|
||||
bt.config.PublicIp6, err = publicip.Get6(ctx)
|
||||
if err != nil {
|
||||
log.Printf("error getting public ipv6 address: %v", err)
|
||||
}
|
||||
}
|
||||
if bt.config.PublicIp6.To16() == nil { // just 4 sure it's valid IPv6
|
||||
bt.config.PublicIp6 = nil
|
||||
}
|
||||
if bt.config.PublicIp6 != nil {
|
||||
log.Println("PublicIp6:", bt.config.PublicIp6)
|
||||
}
|
||||
}
|
||||
|
||||
func (bt *BTServer) configureProxy() error {
|
||||
proxyURL := settings.Args.ProxyURL
|
||||
|
||||
if proxyURL == "" {
|
||||
return nil // No proxy configured
|
||||
}
|
||||
|
||||
proxyMode := settings.Args.ProxyMode
|
||||
if proxyMode == "" {
|
||||
proxyMode = "tracker" // default
|
||||
}
|
||||
|
||||
// Parse and validate proxy URL
|
||||
parsedURL, err := url.Parse(proxyURL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid proxy URL: %w", err)
|
||||
}
|
||||
|
||||
scheme := parsedURL.Scheme
|
||||
// Validate proxy protocol
|
||||
switch scheme {
|
||||
case "socks5", "socks5h", "socks4", "socks4a", "http", "https":
|
||||
// Supported protocols
|
||||
default:
|
||||
return fmt.Errorf("unsupported proxy protocol: %s (supported: http, https, socks4, socks4a, socks5, socks5h)", scheme)
|
||||
}
|
||||
|
||||
if proxyMode == "full" {
|
||||
log.Printf("Configuring proxy for all BitTorrent traffic: %s://%s", scheme, parsedURL.Host)
|
||||
|
||||
// Set ProxyURL - this will be used by anacrolix/torrent for all BitTorrent traffic
|
||||
bt.config.ProxyURL = proxyURL
|
||||
|
||||
// Also set HTTPProxy explicitly for HTTP tracker requests
|
||||
bt.config.HTTPProxy = func(req *http.Request) (*url.URL, error) {
|
||||
return parsedURL, nil
|
||||
}
|
||||
|
||||
log.Println("Proxy configured successfully for all BitTorrent connections (tracker, DHT, peers)")
|
||||
} else if proxyMode == "peers" {
|
||||
log.Printf("Configuring proxy for peer connections only: %s://%s", scheme, parsedURL.Host)
|
||||
|
||||
// Set ProxyURL for peer connections, but don't set HTTPProxy
|
||||
// This routes DHT and peer connections through proxy, but not HTTP tracker requests
|
||||
bt.config.ProxyURL = proxyURL
|
||||
|
||||
log.Println("Proxy configured successfully for peer and DHT connections only")
|
||||
} else {
|
||||
log.Printf("Configuring proxy for HTTP tracker requests only: %s://%s", scheme, parsedURL.Host)
|
||||
|
||||
// Only set HTTPProxy for tracker requests, don't set ProxyURL
|
||||
bt.config.HTTPProxy = func(req *http.Request) (*url.URL, error) {
|
||||
return parsedURL, nil
|
||||
}
|
||||
|
||||
log.Println("Proxy configured successfully for HTTP tracker connections only")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
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)
|
||||
maps.Copy(list, bt.torrents)
|
||||
return list
|
||||
}
|
||||
|
||||
func (bt *BTServer) RemoveTorrent(hash torrent.InfoHash) bool {
|
||||
if torr, ok := bt.torrents[hash]; ok {
|
||||
return torr.Close()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isPrivateIP(ip net.IP) bool {
|
||||
if ip.IsLoopback() || ip.IsPrivate() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() {
|
||||
return true
|
||||
}
|
||||
|
||||
for _, block := range privateIPBlocks {
|
||||
if block.Contains(ip) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func getPublicIp4() net.IP {
|
||||
ifaces, err := anet.Interfaces()
|
||||
if err != nil {
|
||||
log.Println("Error get public IPv4")
|
||||
return nil
|
||||
}
|
||||
for _, i := range ifaces {
|
||||
addrs, _ := anet.InterfaceAddrsByInterface(&i)
|
||||
if i.Flags&net.FlagUp == net.FlagUp {
|
||||
for _, addr := range addrs {
|
||||
var ip net.IP
|
||||
switch v := addr.(type) {
|
||||
case *net.IPNet:
|
||||
ip = v.IP
|
||||
case *net.IPAddr:
|
||||
ip = v.IP
|
||||
}
|
||||
if !isPrivateIP(ip) && ip.To4() != nil {
|
||||
return ip
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getPublicIp6() net.IP {
|
||||
ifaces, err := anet.Interfaces()
|
||||
if err != nil {
|
||||
log.Println("Error get public IPv6")
|
||||
return nil
|
||||
}
|
||||
for _, i := range ifaces {
|
||||
addrs, _ := anet.InterfaceAddrsByInterface(&i)
|
||||
if i.Flags&net.FlagUp == net.FlagUp {
|
||||
for _, addr := range addrs {
|
||||
var ip net.IP
|
||||
switch v := addr.(type) {
|
||||
case *net.IPNet:
|
||||
ip = v.IP
|
||||
case *net.IPAddr:
|
||||
ip = v.IP
|
||||
}
|
||||
if !isPrivateIP(ip) && ip.To16() != nil && ip.To4() == nil {
|
||||
return ip
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package torr
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"server/settings"
|
||||
"server/torr/state"
|
||||
"server/torr/utils"
|
||||
|
||||
"github.com/anacrolix/torrent/metainfo"
|
||||
)
|
||||
|
||||
type tsFiles struct {
|
||||
TorrServer struct {
|
||||
Files []*state.TorrentFileStat `json:"Files"`
|
||||
} `json:"TorrServer"`
|
||||
}
|
||||
|
||||
func AddTorrentDB(torr *Torrent) {
|
||||
t := new(settings.TorrentDB)
|
||||
t.TorrentSpec = torr.TorrentSpec
|
||||
t.Title = torr.Title
|
||||
t.Category = torr.Category
|
||||
if torr.Data == "" {
|
||||
files := new(tsFiles)
|
||||
files.TorrServer.Files = torr.Status().FileStats
|
||||
buf, err := json.Marshal(files)
|
||||
if err == nil {
|
||||
t.Data = string(buf)
|
||||
torr.Data = t.Data
|
||||
}
|
||||
} else {
|
||||
t.Data = torr.Data
|
||||
}
|
||||
|
||||
if torr.Poster != "" && utils.CheckImgUrl(torr.Poster) {
|
||||
t.Poster = torr.Poster
|
||||
}
|
||||
t.Size = torr.Size
|
||||
if t.Size == 0 && torr.Torrent != nil {
|
||||
t.Size = torr.Torrent.Length()
|
||||
}
|
||||
// don't override timestamp from DB on edit
|
||||
t.Timestamp = torr.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.Category = db.Category
|
||||
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.Category = db.Category
|
||||
torr.Timestamp = db.Timestamp
|
||||
torr.Size = db.Size
|
||||
torr.Data = db.Data
|
||||
torr.Stat = state.TorrentInDB
|
||||
ret[torr.TorrentSpec.InfoHash] = torr
|
||||
}
|
||||
return ret
|
||||
}
|
||||
@@ -0,0 +1,293 @@
|
||||
package torr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"server/ffprobe"
|
||||
|
||||
"server/log"
|
||||
"server/settings"
|
||||
"server/torr/state"
|
||||
utils2 "server/utils"
|
||||
|
||||
"github.com/anacrolix/torrent"
|
||||
)
|
||||
|
||||
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() {
|
||||
t.muTorrent.Lock()
|
||||
if t.Stat == state.TorrentPreload {
|
||||
t.Stat = state.TorrentWorking
|
||||
}
|
||||
t.muTorrent.Unlock()
|
||||
// Очистка по окончании прелоада
|
||||
t.BitRate = ""
|
||||
t.DurationSeconds = 0
|
||||
}()
|
||||
|
||||
file := t.findFileIndex(index)
|
||||
if file == nil {
|
||||
file = t.Files()[0]
|
||||
}
|
||||
|
||||
if size > file.Length() {
|
||||
size = file.Length()
|
||||
}
|
||||
|
||||
if t.Info() == nil {
|
||||
return
|
||||
}
|
||||
|
||||
timeout := time.Second * time.Duration(settings.BTsets.TorrentDisconnectTimeout)
|
||||
if timeout > time.Minute {
|
||||
timeout = time.Minute
|
||||
}
|
||||
|
||||
// Create a stop channel for the logging goroutine
|
||||
logStopChan := make(chan struct{})
|
||||
defer close(logStopChan) // Ensure logging stops when function returns
|
||||
|
||||
// Запуск лога в отдельном потоке
|
||||
go func(stopChan <-chan struct{}) {
|
||||
ticker := time.NewTicker(time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
t.muTorrent.Lock()
|
||||
stat := t.Stat
|
||||
t.muTorrent.Unlock()
|
||||
|
||||
if stat != state.TorrentPreload {
|
||||
return
|
||||
}
|
||||
|
||||
statStr := 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().ActivePeers, "/",
|
||||
t.Torrent.Stats().TotalPeers, " [Seeds:",
|
||||
t.Torrent.Stats().ConnectedSeeders, "]")
|
||||
log.TLogln("Preload:", statStr)
|
||||
t.AddExpiredTime(timeout)
|
||||
case <-stopChan:
|
||||
return
|
||||
}
|
||||
}
|
||||
}(logStopChan)
|
||||
|
||||
if ffprobe.Exists() {
|
||||
link := "http://127.0.0.1:" + settings.Port + "/play/" + t.Hash().HexString() + "/" + strconv.Itoa(index)
|
||||
if settings.Ssl {
|
||||
link = "https://127.0.0.1:" + settings.SslPort + "/play/" + t.Hash().HexString() + "/" + strconv.Itoa(index)
|
||||
}
|
||||
if data, err := ffprobe.ProbeUrl(link); err == nil {
|
||||
t.BitRate = data.Format.BitRate
|
||||
t.DurationSeconds = data.Format.DurationSeconds
|
||||
}
|
||||
}
|
||||
|
||||
// Check if torrent was closed
|
||||
t.muTorrent.Lock()
|
||||
isClosed := t.Stat == state.TorrentClosed
|
||||
t.muTorrent.Unlock()
|
||||
|
||||
if isClosed {
|
||||
log.TLogln("End preload: torrent closed")
|
||||
return
|
||||
}
|
||||
|
||||
// startend -> 8/16 MB
|
||||
startend := t.Info().PieceLength
|
||||
if startend < 8<<20 {
|
||||
startend = 8 << 20
|
||||
}
|
||||
|
||||
readerStart := file.NewReader()
|
||||
if readerStart == nil {
|
||||
log.TLogln("End preload: null reader")
|
||||
return
|
||||
}
|
||||
defer readerStart.Close()
|
||||
|
||||
readerStart.SetResponsive()
|
||||
readerStart.SetReadahead(0)
|
||||
readerStartEnd := size - startend
|
||||
|
||||
if readerStartEnd < 0 {
|
||||
// Если конец начального ридера оказался за началом
|
||||
readerStartEnd = size
|
||||
}
|
||||
if readerStartEnd > file.Length() {
|
||||
// Если конец начального ридера оказался после конца файла
|
||||
readerStartEnd = file.Length()
|
||||
}
|
||||
|
||||
readerEndStart := file.Length() - startend
|
||||
readerEndEnd := file.Length()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
var preloadErr error
|
||||
|
||||
// Start end range preload if needed
|
||||
if readerEndStart > readerStartEnd {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
|
||||
// Check if we should still preload
|
||||
t.muTorrent.Lock()
|
||||
shouldPreload := t.Stat == state.TorrentPreload
|
||||
t.muTorrent.Unlock()
|
||||
|
||||
if !shouldPreload {
|
||||
return
|
||||
}
|
||||
|
||||
readerEnd := file.NewReader()
|
||||
if readerEnd == nil {
|
||||
log.TLogln("Err preload: null reader")
|
||||
preloadErr = fmt.Errorf("null reader for end range")
|
||||
return
|
||||
}
|
||||
defer readerEnd.Close() // Ensure reader is always closed
|
||||
|
||||
readerEnd.SetResponsive()
|
||||
readerEnd.SetReadahead(0)
|
||||
|
||||
_, err := readerEnd.Seek(readerEndStart, io.SeekStart)
|
||||
if err != nil {
|
||||
log.TLogln("Err preload seek:", err)
|
||||
preloadErr = err
|
||||
return
|
||||
}
|
||||
|
||||
offset := readerEndStart
|
||||
tmp := make([]byte, 32768)
|
||||
for offset+int64(len(tmp)) < readerEndEnd {
|
||||
n, err := readerEnd.Read(tmp)
|
||||
if err != nil {
|
||||
if err != io.EOF {
|
||||
log.TLogln("Err preload read:", err)
|
||||
preloadErr = err
|
||||
}
|
||||
break
|
||||
}
|
||||
offset += int64(n)
|
||||
|
||||
// Check if we should continue
|
||||
t.muTorrent.Lock()
|
||||
shouldContinue := t.Stat == state.TorrentPreload
|
||||
t.muTorrent.Unlock()
|
||||
|
||||
if !shouldContinue {
|
||||
break
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Main preload section
|
||||
pieceLength := t.Info().PieceLength
|
||||
readahead := pieceLength * 4
|
||||
if readerStartEnd < readahead {
|
||||
readahead = 0
|
||||
}
|
||||
readerStart.SetReadahead(readahead)
|
||||
|
||||
offset := int64(0)
|
||||
tmp := make([]byte, 32768)
|
||||
for offset+int64(len(tmp)) < readerStartEnd {
|
||||
// Check if we should continue
|
||||
t.muTorrent.Lock()
|
||||
shouldContinue := t.Stat == state.TorrentPreload
|
||||
t.muTorrent.Unlock()
|
||||
|
||||
if !shouldContinue {
|
||||
log.TLogln("Preload cancelled")
|
||||
break
|
||||
}
|
||||
|
||||
n, err := readerStart.Read(tmp)
|
||||
if err != nil {
|
||||
if err != io.EOF {
|
||||
log.TLogln("Error preload:", err)
|
||||
}
|
||||
break
|
||||
}
|
||||
offset += int64(n)
|
||||
|
||||
if readahead > 0 && readerStartEnd-(offset+int64(len(tmp))) < readahead {
|
||||
readahead = 0
|
||||
readerStart.SetReadahead(0)
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for end range preload to complete
|
||||
wg.Wait()
|
||||
|
||||
// Check if end range preload failed
|
||||
if preloadErr != nil {
|
||||
log.TLogln("End range preload failed:", preloadErr)
|
||||
}
|
||||
|
||||
// Final log
|
||||
t.muTorrent.Lock()
|
||||
finalStat := t.Stat
|
||||
t.muTorrent.Unlock()
|
||||
|
||||
if finalStat == state.TorrentPreload {
|
||||
log.TLogln("End preload:", file.Torrent().InfoHash().HexString(),
|
||||
"Peers:", t.Torrent.Stats().ActivePeers, "/",
|
||||
t.Torrent.Stats().TotalPeers, "[ Seeds:",
|
||||
t.Torrent.Stats().ConnectedSeeders, "]")
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
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"`
|
||||
Category string `json:"category"`
|
||||
Poster string `json:"poster"`
|
||||
Data string `json:"data,omitempty"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Hash string `json:"hash,omitempty"`
|
||||
TorrsHash string `json:"torrs_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"`
|
||||
DurationSeconds float64 `json:"duration_seconds,omitempty"`
|
||||
BitRate string `json:"bit_rate,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"`
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
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
|
||||
Priority int
|
||||
}
|
||||
|
||||
type ReaderState struct {
|
||||
Start int
|
||||
End int
|
||||
Reader int
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -0,0 +1,389 @@
|
||||
package torrstor
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"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
|
||||
isClosed 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
|
||||
|
||||
if settings.BTsets.UseDisk {
|
||||
name := filepath.Join(settings.BTsets.TorrentsSavePath, hash.HexString())
|
||||
err := os.MkdirAll(name, 0o777)
|
||||
if err != nil {
|
||||
log.TLogln("Error create dir:", err)
|
||||
}
|
||||
}
|
||||
|
||||
for i := 0; i < c.pieceCount; i++ {
|
||||
c.pieces[i] = NewPiece(i, 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 &PieceFake{}
|
||||
}
|
||||
|
||||
func (c *Cache) Close() error {
|
||||
if c.torrent != nil {
|
||||
log.TLogln("Close cache for:", c.torrent.Name(), c.hash)
|
||||
} else {
|
||||
log.TLogln("Close cache for:", c.hash)
|
||||
}
|
||||
c.isClosed = true
|
||||
|
||||
delete(c.storage.caches, c.hash)
|
||||
|
||||
if settings.BTsets.RemoveCacheOnDrop {
|
||||
name := filepath.Join(settings.BTsets.TorrentsSavePath, c.hash.HexString())
|
||||
if name != "" && name != "/" {
|
||||
for _, v := range c.pieces {
|
||||
if v.dPiece != nil {
|
||||
os.Remove(v.dPiece.name)
|
||||
}
|
||||
}
|
||||
os.Remove(name)
|
||||
}
|
||||
}
|
||||
|
||||
c.muReaders.Lock()
|
||||
c.readers = nil
|
||||
c.pieces = nil
|
||||
c.muReaders.Unlock()
|
||||
|
||||
utils.FreeOSMemGC()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cache) removePiece(piece *Piece) {
|
||||
if !c.isClosed {
|
||||
piece.Release()
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) AdjustRA(readahead int64) {
|
||||
if settings.BTsets.CacheSize == 0 {
|
||||
c.capacity = readahead * 3
|
||||
}
|
||||
if c.Readers() > 0 {
|
||||
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
|
||||
|
||||
if len(c.pieces) > 0 {
|
||||
for _, p := range c.pieces {
|
||||
if p.Size > 0 {
|
||||
fill += p.Size
|
||||
piecesState[p.Id] = state.ItemState{
|
||||
Id: p.Id,
|
||||
Size: p.Size,
|
||||
Length: c.pieceLength,
|
||||
Completed: p.Complete,
|
||||
Priority: int(c.torrent.PieceState(p.Id).Priority),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
readersState := make([]*state.ReaderState, 0)
|
||||
|
||||
if c.Readers() > 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 || c.isClosed {
|
||||
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 + 1
|
||||
for _, p := range remPieces {
|
||||
c.removePiece(p)
|
||||
rems--
|
||||
if rems <= 0 {
|
||||
utils.FreeOSMemGC()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) getRemPieces() []*Piece {
|
||||
piecesRemove := make([]*Piece, 0)
|
||||
fill := int64(0)
|
||||
|
||||
ranges := make([]Range, 0)
|
||||
c.muReaders.Lock()
|
||||
for r := range c.readers {
|
||||
r.checkReader()
|
||||
if r.isUse {
|
||||
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) {
|
||||
if p.Size > 0 && !c.isIdInFileBE(ranges, id) {
|
||||
piecesRemove = append(piecesRemove, p)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// on preload clean
|
||||
if p.Size > 0 && !c.isIdInFileBE(ranges, id) {
|
||||
piecesRemove = append(piecesRemove, p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
c.clearPriority()
|
||||
c.setLoadPriority(ranges)
|
||||
|
||||
sort.Slice(piecesRemove, func(i, j int) bool {
|
||||
return piecesRemove[i].Accessed < piecesRemove[j].Accessed
|
||||
})
|
||||
|
||||
c.filled = fill
|
||||
return piecesRemove
|
||||
}
|
||||
|
||||
func (c *Cache) setLoadPriority(ranges []Range) {
|
||||
c.muReaders.Lock()
|
||||
for r := range c.readers {
|
||||
if !r.isUse {
|
||||
continue
|
||||
}
|
||||
if c.isIdInFileBE(ranges, r.getReaderPiece()) {
|
||||
continue
|
||||
}
|
||||
readerPos := r.getReaderPiece()
|
||||
readerRAHPos := r.getReaderRAHPiece()
|
||||
end := r.getPiecesRange().End
|
||||
count := settings.BTsets.ConnectionsLimit / len(c.readers) // max concurrent loading blocks
|
||||
limit := 0
|
||||
for i := readerPos; i < end && limit < count; i++ {
|
||||
if !c.pieces[i].Complete {
|
||||
if i == readerPos {
|
||||
c.torrent.Piece(i).SetPriority(torrent.PiecePriorityNow)
|
||||
} else if i == readerPos+1 {
|
||||
c.torrent.Piece(i).SetPriority(torrent.PiecePriorityNext)
|
||||
} else if i > readerPos && i <= readerRAHPos {
|
||||
c.torrent.Piece(i).SetPriority(torrent.PiecePriorityReadahead)
|
||||
} else if i > readerRAHPos && i <= readerRAHPos+5 && c.torrent.PieceState(i).Priority != torrent.PiecePriorityHigh {
|
||||
c.torrent.Piece(i).SetPriority(torrent.PiecePriorityHigh)
|
||||
} else if i > readerRAHPos+5 && c.torrent.PieceState(i).Priority != torrent.PiecePriorityNormal {
|
||||
c.torrent.Piece(i).SetPriority(torrent.PiecePriorityNormal)
|
||||
}
|
||||
limit++
|
||||
}
|
||||
}
|
||||
}
|
||||
c.muReaders.Unlock()
|
||||
}
|
||||
|
||||
func (c *Cache) isIdInFileBE(ranges []Range, id int) bool {
|
||||
// keep 8/16 MB
|
||||
FileRangeNotDelete := int64(c.pieceLength)
|
||||
if FileRangeNotDelete < 8<<20 {
|
||||
FileRangeNotDelete = 8 << 20
|
||||
}
|
||||
|
||||
for _, rng := range ranges {
|
||||
ss := int(rng.File.Offset() / c.pieceLength)
|
||||
se := int((rng.File.Offset() + FileRangeNotDelete) / c.pieceLength)
|
||||
|
||||
es := int((rng.File.Offset() + rng.File.Length() - FileRangeNotDelete) / c.pieceLength)
|
||||
ee := int((rng.File.Offset() + rng.File.Length()) / c.pieceLength)
|
||||
|
||||
if id >= ss && id < se || id > es && id <= ee {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
//////////////////
|
||||
// Reader section
|
||||
////////
|
||||
|
||||
func (c *Cache) NewReader(file *torrent.File) *Reader {
|
||||
return newReader(file, c)
|
||||
}
|
||||
|
||||
func (c *Cache) GetUseReaders() int {
|
||||
if c == nil {
|
||||
return 0
|
||||
}
|
||||
c.muReaders.Lock()
|
||||
defer c.muReaders.Unlock()
|
||||
readers := 0
|
||||
for reader := range c.readers {
|
||||
if reader.isUse {
|
||||
readers++
|
||||
}
|
||||
}
|
||||
return readers
|
||||
}
|
||||
|
||||
func (c *Cache) Readers() int {
|
||||
if c == nil {
|
||||
return 0
|
||||
}
|
||||
c.muReaders.Lock()
|
||||
defer c.muReaders.Unlock()
|
||||
if 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()
|
||||
go c.clearPriority()
|
||||
}
|
||||
|
||||
func (c *Cache) clearPriority() {
|
||||
time.Sleep(time.Second)
|
||||
ranges := make([]Range, 0)
|
||||
c.muReaders.Lock()
|
||||
for r := range c.readers {
|
||||
r.checkReader()
|
||||
if r.isUse {
|
||||
ranges = append(ranges, r.getPiecesRange())
|
||||
}
|
||||
}
|
||||
c.muReaders.Unlock()
|
||||
ranges = mergeRange(ranges)
|
||||
|
||||
for id := range c.pieces {
|
||||
if len(ranges) > 0 {
|
||||
if !inRanges(ranges, id) {
|
||||
if c.torrent.PieceState(id).Priority != torrent.PiecePriorityNone {
|
||||
c.torrent.Piece(id).SetPriority(torrent.PiecePriorityNone)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if c.torrent.PieceState(id).Priority != torrent.PiecePriorityNone {
|
||||
c.torrent.Piece(id).SetPriority(torrent.PiecePriorityNone)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) GetCapacity() int64 {
|
||||
if c == nil {
|
||||
return 0
|
||||
}
|
||||
return c.capacity
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package torrstor
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"server/log"
|
||||
"server/settings"
|
||||
)
|
||||
|
||||
type DiskPiece struct {
|
||||
piece *Piece
|
||||
|
||||
name string
|
||||
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
func NewDiskPiece(p *Piece) *DiskPiece {
|
||||
name := filepath.Join(settings.BTsets.TorrentsSavePath, p.cache.hash.HexString(), strconv.Itoa(p.Id))
|
||||
ff, err := os.Stat(name)
|
||||
if err == nil {
|
||||
p.Size = ff.Size()
|
||||
p.Complete = ff.Size() == p.cache.pieceLength
|
||||
p.Accessed = ff.ModTime().Unix()
|
||||
}
|
||||
return &DiskPiece{piece: p, name: name}
|
||||
}
|
||||
|
||||
func (p *DiskPiece) WriteAt(b []byte, off int64) (n int, err error) {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
ff, err := os.OpenFile(p.name, os.O_RDWR|os.O_CREATE, 0o666)
|
||||
if err != nil {
|
||||
log.TLogln("Error open file:", err)
|
||||
return 0, err
|
||||
}
|
||||
defer ff.Close()
|
||||
n, err = ff.WriteAt(b, off)
|
||||
|
||||
p.piece.Size += int64(n)
|
||||
if p.piece.Size > p.piece.cache.pieceLength {
|
||||
p.piece.Size = p.piece.cache.pieceLength
|
||||
}
|
||||
p.piece.Accessed = time.Now().Unix()
|
||||
return
|
||||
}
|
||||
|
||||
func (p *DiskPiece) ReadAt(b []byte, off int64) (n int, err error) {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
ff, err := os.OpenFile(p.name, os.O_RDONLY, 0o666)
|
||||
if os.IsNotExist(err) {
|
||||
return 0, io.EOF
|
||||
}
|
||||
if err != nil {
|
||||
log.TLogln("Error open file:", err)
|
||||
return 0, err
|
||||
}
|
||||
defer ff.Close()
|
||||
|
||||
n, err = ff.ReadAt(b, off)
|
||||
|
||||
p.piece.Accessed = time.Now().Unix()
|
||||
if int64(len(b))+off >= p.piece.Size {
|
||||
go p.piece.cache.cleanPieces()
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (p *DiskPiece) Release() {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
p.piece.Size = 0
|
||||
p.piece.Complete = false
|
||||
|
||||
os.Remove(p.name)
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package torrstor
|
||||
|
||||
import (
|
||||
"io"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type MemPiece struct {
|
||||
piece *Piece
|
||||
|
||||
buffer []byte
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
func NewMemPiece(p *Piece) *MemPiece {
|
||||
return &MemPiece{piece: p}
|
||||
}
|
||||
|
||||
func (p *MemPiece) WriteAt(b []byte, off int64) (n int, err error) {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
if p.buffer == nil {
|
||||
go p.piece.cache.cleanPieces()
|
||||
p.buffer = make([]byte, p.piece.cache.pieceLength, p.piece.cache.pieceLength)
|
||||
}
|
||||
n = copy(p.buffer[off:], b[:])
|
||||
p.piece.Size += int64(n)
|
||||
if p.piece.Size > p.piece.cache.pieceLength {
|
||||
p.piece.Size = p.piece.cache.pieceLength
|
||||
}
|
||||
p.piece.Accessed = time.Now().Unix()
|
||||
return
|
||||
}
|
||||
|
||||
func (p *MemPiece) 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.piece.Accessed = time.Now().Unix()
|
||||
if int64(len(b))+off >= p.piece.Size {
|
||||
go p.piece.cache.cleanPieces()
|
||||
}
|
||||
if n == 0 {
|
||||
return 0, io.EOF
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (p *MemPiece) Release() {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
if p.buffer != nil {
|
||||
p.buffer = nil
|
||||
}
|
||||
p.piece.Size = 0
|
||||
p.piece.Complete = false
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package torrstor
|
||||
|
||||
import (
|
||||
"github.com/anacrolix/torrent"
|
||||
"github.com/anacrolix/torrent/storage"
|
||||
"server/settings"
|
||||
)
|
||||
|
||||
type Piece struct {
|
||||
storage.PieceImpl `json:"-"`
|
||||
|
||||
Id int `json:"-"`
|
||||
Size int64 `json:"size"`
|
||||
|
||||
Complete bool `json:"complete"`
|
||||
Accessed int64 `json:"accessed"`
|
||||
|
||||
mPiece *MemPiece `json:"-"`
|
||||
dPiece *DiskPiece `json:"-"`
|
||||
|
||||
cache *Cache `json:"-"`
|
||||
}
|
||||
|
||||
func NewPiece(id int, cache *Cache) *Piece {
|
||||
p := &Piece{
|
||||
Id: id,
|
||||
cache: cache,
|
||||
}
|
||||
|
||||
if !settings.BTsets.UseDisk {
|
||||
p.mPiece = NewMemPiece(p)
|
||||
} else {
|
||||
p.dPiece = NewDiskPiece(p)
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *Piece) WriteAt(b []byte, off int64) (n int, err error) {
|
||||
if !settings.BTsets.UseDisk {
|
||||
return p.mPiece.WriteAt(b, off)
|
||||
} else {
|
||||
return p.dPiece.WriteAt(b, off)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Piece) ReadAt(b []byte, off int64) (n int, err error) {
|
||||
if !settings.BTsets.UseDisk {
|
||||
return p.mPiece.ReadAt(b, off)
|
||||
} else {
|
||||
return p.dPiece.ReadAt(b, off)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Piece) MarkComplete() error {
|
||||
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,
|
||||
Ok: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Piece) Release() {
|
||||
if !settings.BTsets.UseDisk {
|
||||
p.mPiece.Release()
|
||||
} else {
|
||||
p.dPiece.Release()
|
||||
}
|
||||
if !p.cache.isClosed {
|
||||
p.cache.torrent.Piece(p.Id).SetPriority(torrent.PiecePriorityNone)
|
||||
p.cache.torrent.Piece(p.Id).UpdateCompletion()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package torrstor
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/anacrolix/torrent/storage"
|
||||
)
|
||||
|
||||
type PieceFake struct{}
|
||||
|
||||
func (PieceFake) ReadAt(p []byte, off int64) (n int, err error) {
|
||||
err = errors.New("fake")
|
||||
return
|
||||
}
|
||||
|
||||
func (PieceFake) WriteAt(p []byte, off int64) (n int, err error) {
|
||||
err = errors.New("fake")
|
||||
return
|
||||
}
|
||||
|
||||
func (PieceFake) MarkComplete() error {
|
||||
return errors.New("fake")
|
||||
}
|
||||
|
||||
func (PieceFake) MarkNotComplete() error {
|
||||
return errors.New("fake")
|
||||
}
|
||||
|
||||
func (PieceFake) Completion() storage.Completion {
|
||||
return storage.Completion{
|
||||
Complete: false,
|
||||
Ok: true,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package torrstor
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/anacrolix/torrent"
|
||||
)
|
||||
|
||||
type Range struct {
|
||||
Start, End int
|
||||
File *torrent.File
|
||||
}
|
||||
|
||||
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]
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
package torrstor
|
||||
|
||||
import (
|
||||
"io"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/anacrolix/torrent"
|
||||
|
||||
"server/log"
|
||||
"server/settings"
|
||||
)
|
||||
|
||||
type Reader struct {
|
||||
torrent.Reader
|
||||
offset int64
|
||||
readahead int64
|
||||
file *torrent.File
|
||||
|
||||
cache *Cache
|
||||
isClosed bool
|
||||
|
||||
///Preload
|
||||
lastAccess int64
|
||||
isUse bool
|
||||
mu 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
|
||||
r.isUse = true
|
||||
|
||||
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
|
||||
}
|
||||
r.readerOn()
|
||||
n, err = r.Reader.Seek(offset, whence)
|
||||
r.offset = n
|
||||
r.lastAccess = time.Now().Unix()
|
||||
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 {
|
||||
r.readerOn()
|
||||
n, err = r.Reader.Read(p)
|
||||
|
||||
// samsung tv fix xvid/divx
|
||||
//if r.offset == 0 && len(p) >= 192 {
|
||||
// str := strings.ToLower(string(p[112:116]))
|
||||
// if str == "xvid" || str == "divx" {
|
||||
// p[112] = 0x4D // M
|
||||
// p[113] = 0x50 // P
|
||||
// p[114] = 0x34 // 4
|
||||
// p[115] = 0x56 // V
|
||||
// }
|
||||
// str = strings.ToLower(string(p[188:192]))
|
||||
// if str == "xvid" || str == "divx" {
|
||||
// p[188] = 0x4D // M
|
||||
// p[189] = 0x50 // P
|
||||
// p[190] = 0x34 // 4
|
||||
// p[191] = 0x56 // V
|
||||
// }
|
||||
//}
|
||||
|
||||
r.offset += int64(n)
|
||||
r.lastAccess = time.Now().Unix()
|
||||
} else {
|
||||
log.TLogln("Torrent closed and readed")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (r *Reader) SetReadahead(length int64) {
|
||||
if r.cache != nil && length > r.cache.capacity {
|
||||
length = r.cache.capacity
|
||||
}
|
||||
if r.isUse {
|
||||
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 (r *Reader) getPiecesRange() Range {
|
||||
startOff, endOff := r.getOffsetRange()
|
||||
return Range{r.getPieceNum(startOff), r.getPieceNum(endOff), r.file}
|
||||
}
|
||||
|
||||
func (r *Reader) getReaderPiece() int {
|
||||
return r.getPieceNum(r.offset)
|
||||
}
|
||||
|
||||
func (r *Reader) getReaderRAHPiece() int {
|
||||
return r.getPieceNum(r.offset + r.readahead)
|
||||
}
|
||||
|
||||
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(r.getUseReaders())
|
||||
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) checkReader() {
|
||||
if time.Now().Unix() > r.lastAccess+60 && len(r.cache.readers) > 1 {
|
||||
r.readerOff()
|
||||
} else {
|
||||
r.readerOn()
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Reader) readerOn() {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
if !r.isUse {
|
||||
if pos, err := r.Reader.Seek(0, io.SeekCurrent); err == nil && pos == 0 {
|
||||
r.Reader.Seek(r.offset, io.SeekStart)
|
||||
}
|
||||
r.SetReadahead(r.readahead)
|
||||
r.isUse = true
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Reader) readerOff() {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
if r.isUse {
|
||||
r.SetReadahead(0)
|
||||
r.isUse = false
|
||||
if r.offset > 0 {
|
||||
r.Reader.Seek(0, io.SeekStart)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Reader) getUseReaders() int {
|
||||
readers := 0
|
||||
if r.cache != nil {
|
||||
for reader := range r.cache.readers {
|
||||
if reader.isUse {
|
||||
readers++
|
||||
}
|
||||
}
|
||||
}
|
||||
return readers
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package torrstor
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"server/torr/storage"
|
||||
|
||||
"github.com/anacrolix/torrent/metainfo"
|
||||
ts "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) (ts.TorrentImpl, error) {
|
||||
// capFunc := func() (int64, bool) { // NE
|
||||
// return s.capacity, true // NE
|
||||
// } // NE
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
ch := NewCache(s.capacity, s)
|
||||
ch.Init(info, infoHash)
|
||||
s.caches[infoHash] = ch
|
||||
return ch, nil // OE
|
||||
// return ts.TorrentImpl{ // NE
|
||||
// Piece: ch.Piece, // NE
|
||||
// Close: ch.Close, // NE
|
||||
// Capacity: &capFunc, // NE
|
||||
// }, nil // NE
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
package torr
|
||||
|
||||
import (
|
||||
// "context"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/anacrolix/dms/dlna"
|
||||
"github.com/anacrolix/missinggo/v2/httptoo"
|
||||
"github.com/anacrolix/torrent"
|
||||
|
||||
mt "server/mimetype"
|
||||
sets "server/settings"
|
||||
"server/torr/state"
|
||||
)
|
||||
|
||||
// Add atomic counter for concurrent streams
|
||||
var activeStreams int32
|
||||
|
||||
// type contextResponseWriter struct {
|
||||
// http.ResponseWriter
|
||||
// ctx context.Context
|
||||
// }
|
||||
|
||||
// func (w *contextResponseWriter) Write(p []byte) (n int, err error) {
|
||||
// // Check context before each write
|
||||
// select {
|
||||
// case <-w.ctx.Done():
|
||||
// return 0, w.ctx.Err()
|
||||
// default:
|
||||
// return w.ResponseWriter.Write(p)
|
||||
// }
|
||||
// }
|
||||
|
||||
func (t *Torrent) Stream(fileID int, req *http.Request, resp http.ResponseWriter) error {
|
||||
// Increment active streams counter
|
||||
streamID := atomic.AddInt32(&activeStreams, 1)
|
||||
defer atomic.AddInt32(&activeStreams, -1)
|
||||
// Stream disconnect timeout (same as torrent)
|
||||
streamTimeout := sets.BTsets.TorrentDisconnectTimeout
|
||||
|
||||
if !t.GotInfo() {
|
||||
http.NotFound(resp, req)
|
||||
return errors.New("torrent doesn't have info yet")
|
||||
}
|
||||
// Get file information
|
||||
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)
|
||||
}
|
||||
// Find the actual torrent file
|
||||
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)
|
||||
}
|
||||
// Check file size limit
|
||||
if int64(sets.MaxSize) > 0 && file.Length() > int64(sets.MaxSize) {
|
||||
err := fmt.Errorf("file size exceeded max allowed %d bytes", sets.MaxSize)
|
||||
log.Printf("File %s size (%d) exceeded max allowed %d bytes", file.DisplayPath(), file.Length(), sets.MaxSize)
|
||||
http.Error(resp, err.Error(), http.StatusForbidden)
|
||||
return err
|
||||
}
|
||||
// Create reader with context for timeout
|
||||
reader := t.NewReader(file)
|
||||
if reader == nil {
|
||||
return errors.New("cannot create torrent reader")
|
||||
}
|
||||
// Ensure reader is always closed
|
||||
defer t.CloseReader(reader)
|
||||
|
||||
if sets.BTsets.ResponsiveMode {
|
||||
reader.SetResponsive()
|
||||
}
|
||||
// Log connection
|
||||
host, port, clerr := net.SplitHostPort(req.RemoteAddr)
|
||||
|
||||
if sets.BTsets.EnableDebug {
|
||||
if clerr != nil {
|
||||
log.Printf("[Stream:%d] Connect client (Active streams: %d)", streamID, atomic.LoadInt32(&activeStreams))
|
||||
} else {
|
||||
log.Printf("[Stream:%d] Connect client %s:%s (Active streams: %d)",
|
||||
streamID, host, port, atomic.LoadInt32(&activeStreams))
|
||||
}
|
||||
}
|
||||
|
||||
// Mark as viewed
|
||||
sets.SetViewed(&sets.Viewed{
|
||||
Hash: t.Hash().HexString(),
|
||||
FileIndex: fileID,
|
||||
})
|
||||
|
||||
// Set response headers
|
||||
resp.Header().Set("Connection", "close")
|
||||
// Add timeout header if configured
|
||||
if streamTimeout > 0 {
|
||||
resp.Header().Set("X-Stream-Timeout", fmt.Sprintf("%d", streamTimeout))
|
||||
}
|
||||
// Add ETag
|
||||
etag := hex.EncodeToString([]byte(fmt.Sprintf("%s/%s", t.Hash().HexString(), file.Path())))
|
||||
resp.Header().Set("ETag", httptoo.EncodeQuotedString(etag))
|
||||
// DLNA headers
|
||||
resp.Header().Set("transferMode.dlna.org", "Streaming")
|
||||
// add MimeType
|
||||
mime, err := mt.MimeTypeByPath(file.Path())
|
||||
if err == nil && mime.IsMedia() {
|
||||
resp.Header().Set("content-type", mime.String())
|
||||
}
|
||||
// DLNA Seek
|
||||
if req.Header.Get("getContentFeatures.dlna.org") != "" {
|
||||
resp.Header().Set("contentFeatures.dlna.org", dlna.ContentFeatures{
|
||||
SupportRange: true,
|
||||
SupportTimeSeek: true,
|
||||
}.String())
|
||||
}
|
||||
// Add support for range requests
|
||||
if req.Header.Get("Range") != "" {
|
||||
resp.Header().Set("Accept-Ranges", "bytes")
|
||||
}
|
||||
// // Create a context with timeout if configured
|
||||
// ctx := req.Context()
|
||||
// if streamTimeout > 0 {
|
||||
// var cancel context.CancelFunc
|
||||
// ctx, cancel = context.WithTimeout(ctx, time.Duration(streamTimeout)*time.Second)
|
||||
// defer cancel()
|
||||
// }
|
||||
// // Update request with new context
|
||||
// req = req.WithContext(ctx)
|
||||
// // Handle client disconnections better
|
||||
// wrappedResp := &contextResponseWriter{
|
||||
// ResponseWriter: resp,
|
||||
// ctx: ctx,
|
||||
// }
|
||||
// http.ServeContent(wrappedResp, req, file.Path(), time.Unix(t.Timestamp, 0), reader)
|
||||
|
||||
http.ServeContent(resp, req, file.Path(), time.Unix(t.Timestamp, 0), reader)
|
||||
|
||||
if sets.BTsets.EnableDebug {
|
||||
if clerr != nil {
|
||||
log.Printf("[Stream:%d] Disconnect client", streamID)
|
||||
} else {
|
||||
log.Printf("[Stream:%d] Disconnect client %s:%s", streamID, host, port)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetActiveStreams returns number of currently active streams
|
||||
func GetActiveStreams() int32 {
|
||||
return atomic.LoadInt32(&activeStreams)
|
||||
}
|
||||
@@ -0,0 +1,395 @@
|
||||
package torr
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"server/torrshash"
|
||||
"sort"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
utils2 "server/utils"
|
||||
|
||||
"github.com/anacrolix/torrent"
|
||||
"github.com/anacrolix/torrent/metainfo"
|
||||
|
||||
"server/log"
|
||||
"server/settings"
|
||||
"server/torr/state"
|
||||
cacheSt "server/torr/storage/state"
|
||||
"server/torr/storage/torrstor"
|
||||
"server/torr/utils"
|
||||
)
|
||||
|
||||
type Torrent struct {
|
||||
Title string
|
||||
Category 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
|
||||
|
||||
DurationSeconds float64
|
||||
BitRate string
|
||||
|
||||
expiredTime time.Time
|
||||
|
||||
closed <-chan struct{}
|
||||
|
||||
progressTicker *time.Ticker
|
||||
}
|
||||
|
||||
func NewTorrent(spec *torrent.TorrentSpec, bt *BTServer) (*Torrent, error) {
|
||||
// https://github.com/anacrolix/torrent/issues/747
|
||||
if bt == nil || bt.client == 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()}
|
||||
}
|
||||
|
||||
trackers := utils.GetTrackerFromFile()
|
||||
if len(trackers) > 0 {
|
||||
spec.Trackers = append(spec.Trackers, [][]string{trackers}...)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
timeout := time.Second * time.Duration(settings.BTsets.TorrentDisconnectTimeout)
|
||||
if timeout > time.Minute {
|
||||
timeout = time.Minute
|
||||
}
|
||||
|
||||
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(timeout)
|
||||
torr.Timestamp = time.Now().Unix()
|
||||
|
||||
go torr.watch()
|
||||
|
||||
bt.torrents[spec.InfoHash] = torr
|
||||
return torr, nil
|
||||
}
|
||||
|
||||
func (t *Torrent) WaitInfo() bool {
|
||||
if t == nil || t.Torrent == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// Close torrent if no info in 1 minute + TorrentDisconnectTimeout config option
|
||||
tm := time.NewTimer(time.Minute + time.Second*time.Duration(settings.BTsets.TorrentDisconnectTimeout))
|
||||
|
||||
select {
|
||||
case <-t.Torrent.GotInfo():
|
||||
if t.bt != nil && t.bt.storage != nil {
|
||||
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 {
|
||||
// log.TLogln("GotInfo state:", t.Stat)
|
||||
if t == nil || t.Stat == state.TorrentClosed {
|
||||
return false
|
||||
}
|
||||
// assume we have info in preload state
|
||||
// and dont override with TorrentWorking
|
||||
if t.Stat == state.TorrentPreload {
|
||||
return true
|
||||
}
|
||||
t.Stat = state.TorrentGettingInfo
|
||||
if t.WaitInfo() {
|
||||
t.Stat = state.TorrentWorking
|
||||
t.AddExpiredTime(time.Second * time.Duration(settings.BTsets.TorrentDisconnectTimeout))
|
||||
return true
|
||||
} else {
|
||||
t.Close()
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Torrent) AddExpiredTime(duration time.Duration) {
|
||||
newExpiredTime := time.Now().Add(duration)
|
||||
if t.expiredTime.Before(newExpiredTime) {
|
||||
t.expiredTime = newExpiredTime
|
||||
}
|
||||
}
|
||||
|
||||
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() {
|
||||
if t.TorrentSpec != nil {
|
||||
log.TLogln("Torrent close by timeout", t.TorrentSpec.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.BytesRead.Int64() - t.BytesReadUsefulData
|
||||
deltaUpBytes := st.BytesWritten.Int64() - t.BytesWrittenData
|
||||
deltaTime := time.Since(t.lastTimeSpeed).Seconds()
|
||||
|
||||
t.DownloadSpeed = float64(deltaDlBytes) / deltaTime
|
||||
t.UploadSpeed = float64(deltaUpBytes) / deltaTime
|
||||
|
||||
t.BytesReadUsefulData = st.BytesRead.Int64()
|
||||
t.BytesWrittenData = st.BytesWritten.Int64()
|
||||
|
||||
if t.cache != nil {
|
||||
t.PreloadedBytes = t.cache.GetState().Filled
|
||||
}
|
||||
} 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)
|
||||
// }
|
||||
adj := int64(16 << 20) // 16 MB fixed RA
|
||||
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 {
|
||||
return 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) drop() {
|
||||
t.muTorrent.Lock()
|
||||
defer t.muTorrent.Unlock()
|
||||
if t.Torrent != nil {
|
||||
t.Torrent.Drop()
|
||||
t.Torrent = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Torrent) Close() bool {
|
||||
if t == nil {
|
||||
return false
|
||||
}
|
||||
if settings.ReadOnly && t.cache != nil && t.cache.GetUseReaders() > 0 {
|
||||
return false
|
||||
}
|
||||
t.Stat = state.TorrentClosed
|
||||
|
||||
if t.bt != nil {
|
||||
t.bt.mu.Lock()
|
||||
delete(t.bt.torrents, t.Hash())
|
||||
t.bt.mu.Unlock()
|
||||
}
|
||||
|
||||
t.drop()
|
||||
return true
|
||||
}
|
||||
|
||||
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.Category = t.Category
|
||||
st.Poster = t.Poster
|
||||
st.Data = t.Data
|
||||
st.Timestamp = t.Timestamp
|
||||
st.TorrentSize = t.Size
|
||||
st.BitRate = t.BitRate
|
||||
st.DurationSeconds = t.DurationSeconds
|
||||
|
||||
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 utils2.CompareStrings(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(),
|
||||
})
|
||||
}
|
||||
|
||||
th := torrshash.New(st.Hash)
|
||||
th.AddField(torrshash.TagTitle, st.Title)
|
||||
th.AddField(torrshash.TagPoster, st.Poster)
|
||||
th.AddField(torrshash.TagCategory, st.Category)
|
||||
th.AddField(torrshash.TagSize, strconv.FormatInt(st.TorrentSize, 10))
|
||||
|
||||
if t.TorrentSpec != nil {
|
||||
if len(t.TorrentSpec.Trackers) > 0 && len(t.TorrentSpec.Trackers[0]) > 0 {
|
||||
for _, tr := range t.TorrentSpec.Trackers[0] {
|
||||
th.AddField(torrshash.TagTracker, tr)
|
||||
}
|
||||
}
|
||||
}
|
||||
token, err := torrshash.Pack(th)
|
||||
if err == nil {
|
||||
st.TorrsHash = token
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"server/log"
|
||||
|
||||
"server/settings"
|
||||
|
||||
"github.com/anacrolix/torrent/iplist"
|
||||
)
|
||||
|
||||
func ReadBlockedIP() (ranger iplist.Ranger, err error) {
|
||||
buf, err := os.ReadFile(filepath.Join(settings.Path, "blocklist"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.TLogln("Read block list...")
|
||||
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)
|
||||
log.TLogln("Readed ranges:", len(ranges))
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"runtime/debug"
|
||||
)
|
||||
|
||||
func FreeOSMem() {
|
||||
debug.FreeOSMemory()
|
||||
}
|
||||
|
||||
func FreeOSMemGC() {
|
||||
runtime.GC()
|
||||
debug.FreeOSMemory()
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/base32"
|
||||
"io"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"server/settings"
|
||||
|
||||
"github.com/anacrolix/torrent"
|
||||
"github.com/anacrolix/torrent/metainfo"
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
var defTrackers = []string{
|
||||
"http://retracker.local/announce",
|
||||
"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",
|
||||
"wss://tracker.btorrent.xyz",
|
||||
"wss://tracker.openwebtorrent.com",
|
||||
}
|
||||
|
||||
var loadedTrackers []string
|
||||
|
||||
func GetTrackerFromFile() []string {
|
||||
name := filepath.Join(settings.Path, "trackers.txt")
|
||||
buf, err := os.ReadFile(name)
|
||||
if err == nil {
|
||||
list := strings.Split(string(buf), "\n")
|
||||
var ret []string
|
||||
for _, l := range list {
|
||||
if strings.HasPrefix(l, "udp") || strings.HasPrefix(l, "http") {
|
||||
ret = append(ret, l)
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
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 {
|
||||
defer resp.Body.Close()
|
||||
buf, err := io.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
|
||||
}
|
||||
|
||||
func OpenTorrentFile(path string) (*torrent.TorrentSpec, error) {
|
||||
minfo, err := metainfo.LoadFromFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
info, err := minfo.UnmarshalInfo()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// mag := minfo.Magnet(info.Name, minfo.HashInfoBytes())
|
||||
mag := minfo.Magnet(nil, &info)
|
||||
return &torrent.TorrentSpec{
|
||||
InfoBytes: minfo.InfoBytes,
|
||||
Trackers: [][]string{mag.Trackers},
|
||||
DisplayName: info.Name,
|
||||
InfoHash: minfo.HashInfoBytes(),
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"image"
|
||||
_ "image/gif"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/image/webp"
|
||||
|
||||
"server/log"
|
||||
)
|
||||
|
||||
func CheckImgUrl(link string) bool {
|
||||
if link == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", link, nil)
|
||||
if err != nil {
|
||||
log.TLogln("Error create request for image:", err)
|
||||
return false
|
||||
}
|
||||
|
||||
client := &http.Client{
|
||||
Timeout: 5 * time.Second,
|
||||
}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.TLogln("Error check image:", err)
|
||||
return false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
limitedReader := io.LimitReader(resp.Body, 2*1024*1024)
|
||||
|
||||
if strings.HasSuffix(link, ".webp") {
|
||||
_, err = webp.Decode(limitedReader)
|
||||
} else {
|
||||
_, _, err = image.Decode(limitedReader)
|
||||
}
|
||||
if err != nil {
|
||||
log.TLogln("Error decode image:", err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user