mirror of
https://github.com/Ernous/TorrServerJellyfin.git
synced 2025-12-19 21:46:11 +05:00
Merge branch 'master' into 230-torrents-category
This commit is contained in:
@@ -150,19 +150,24 @@ func SetTorrent(hashHex, title, poster, data string) *Torrent {
|
||||
}
|
||||
|
||||
func RemTorrent(hashHex string) {
|
||||
if sets.ReadOnly {
|
||||
log.TLogln("API RemTorrent: Read-only DB mode!", hashHex)
|
||||
return
|
||||
}
|
||||
hash := metainfo.NewHashFromHex(hashHex)
|
||||
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)
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
bts.RemoveTorrent(hash)
|
||||
RemTorrentDB(hash)
|
||||
}
|
||||
|
||||
@@ -199,6 +204,7 @@ func DropTorrent(hashHex string) {
|
||||
|
||||
func SetSettings(set *sets.BTSets) {
|
||||
if sets.ReadOnly {
|
||||
log.TLogln("API SetSettings: Read-only DB mode!")
|
||||
return
|
||||
}
|
||||
sets.SetBTSets(set)
|
||||
@@ -215,6 +221,7 @@ func SetSettings(set *sets.BTSets) {
|
||||
|
||||
func SetDefSettings() {
|
||||
if sets.ReadOnly {
|
||||
log.TLogln("API SetDefSettings: Read-only DB mode!")
|
||||
return
|
||||
}
|
||||
sets.SetDefaultConfig()
|
||||
|
||||
@@ -88,14 +88,13 @@ func (bt *BTServer) configure(ctx context.Context) {
|
||||
upnpID := "TorrServer/" + version.Version
|
||||
cliVers := userAgent
|
||||
|
||||
// bt.config.AlwaysWantConns = true
|
||||
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 // TODO: check memory usage
|
||||
// bt.config.DisableWebseeds = false
|
||||
// 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
|
||||
@@ -109,13 +108,13 @@ func (bt *BTServer) configure(ctx context.Context) {
|
||||
bt.config.EstablishedConnsPerTorrent = settings.BTsets.ConnectionsLimit
|
||||
bt.config.TotalHalfOpenConns = 500
|
||||
// Encryption/Obfuscation
|
||||
bt.config.EncryptionPolicy = torrent.EncryptionPolicy{
|
||||
ForceEncryption: settings.BTsets.ForceEncrypt,
|
||||
}
|
||||
// bt.config.HeaderObfuscationPolicy = torrent.HeaderObfuscationPolicy{
|
||||
// RequirePreferred: settings.BTsets.ForceEncrypt,
|
||||
// Preferred: true,
|
||||
// }
|
||||
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)
|
||||
}
|
||||
@@ -130,21 +129,8 @@ func (bt *BTServer) configure(ctx context.Context) {
|
||||
log.Println("Set listen port", settings.BTsets.PeersListenPort)
|
||||
bt.config.ListenPort = settings.BTsets.PeersListenPort
|
||||
} else {
|
||||
// lport := 32000
|
||||
// for {
|
||||
// log.Println("Check listen port", lport)
|
||||
// l, err := net.Listen("tcp", ":"+strconv.Itoa(lport))
|
||||
// if l != nil {
|
||||
// l.Close()
|
||||
// }
|
||||
// if err == nil {
|
||||
// break
|
||||
// }
|
||||
// lport++
|
||||
// }
|
||||
// log.Println("Set listen port", lport)
|
||||
log.Println("Set listen port to random autoselect (0)")
|
||||
bt.config.ListenPort = 0 // lport
|
||||
bt.config.ListenPort = 0
|
||||
}
|
||||
}
|
||||
|
||||
@@ -200,10 +186,11 @@ func (bt *BTServer) ListTorrents() map[metainfo.Hash]*Torrent {
|
||||
return list
|
||||
}
|
||||
|
||||
func (bt *BTServer) RemoveTorrent(hash torrent.InfoHash) {
|
||||
func (bt *BTServer) RemoveTorrent(hash torrent.InfoHash) bool {
|
||||
if torr, ok := bt.torrents[hash]; ok {
|
||||
torr.Close()
|
||||
return torr.Close()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isPrivateIP(ip net.IP) bool {
|
||||
|
||||
@@ -62,7 +62,7 @@ func (t *Torrent) Preload(index int, size int64) {
|
||||
// Запуск лога в отдельном потоке
|
||||
go func() {
|
||||
for t.Stat == state.TorrentPreload {
|
||||
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)
|
||||
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().ActivePeers, "/", t.Torrent.Stats().TotalPeers, " [Seeds:", t.Torrent.Stats().ConnectedSeeders, "]")
|
||||
log.TLogln("Preload:", stat)
|
||||
t.AddExpiredTime(time.Second * time.Duration(settings.BTsets.TorrentDisconnectTimeout))
|
||||
time.Sleep(time.Second)
|
||||
@@ -158,7 +158,7 @@ func (t *Torrent) Preload(index int, size int64) {
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
log.TLogln("End preload:", file.Torrent().InfoHash().HexString(), "Peers:[", t.Torrent.Stats().ConnectedSeeders, "]", t.Torrent.Stats().ActivePeers, "/", t.Torrent.Stats().TotalPeers)
|
||||
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 {
|
||||
|
||||
@@ -314,6 +314,21 @@ 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
|
||||
|
||||
@@ -25,20 +25,20 @@ func NewStorage(capacity int64) *Storage {
|
||||
}
|
||||
|
||||
func (s *Storage) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (ts.TorrentImpl, error) {
|
||||
// capFunc := func() (int64, bool) {
|
||||
// return s.capacity, true
|
||||
// }
|
||||
// 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
|
||||
// return ts.TorrentImpl{
|
||||
// Piece: ch.Piece,
|
||||
// Close: ch.Close,
|
||||
// Capacity: &capFunc,
|
||||
// }, nil
|
||||
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) {
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/anacrolix/dms/dlna"
|
||||
"github.com/anacrolix/missinggo/httptoo"
|
||||
"github.com/anacrolix/missinggo/v2/httptoo"
|
||||
"github.com/anacrolix/torrent"
|
||||
|
||||
mt "server/mimetype"
|
||||
@@ -47,6 +47,10 @@ func (t *Torrent) Stream(fileID int, req *http.Request, resp http.ResponseWriter
|
||||
if file == nil {
|
||||
return fmt.Errorf("file with id %v not found", fileID)
|
||||
}
|
||||
if int64(sets.MaxSize) > 0 && file.Length() > int64(sets.MaxSize) {
|
||||
log.Println("file", file.DisplayPath(), "size exceeded max allowed", sets.MaxSize, "bytes")
|
||||
return fmt.Errorf("file size exceeded max allowed %d bytes", sets.MaxSize)
|
||||
}
|
||||
|
||||
reader := t.NewReader(file)
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
utils2 "server/utils"
|
||||
|
||||
"github.com/anacrolix/torrent"
|
||||
"github.com/anacrolix/torrent/metainfo"
|
||||
|
||||
@@ -266,7 +268,10 @@ func (t *Torrent) drop() {
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Torrent) Close() {
|
||||
func (t *Torrent) Close() bool {
|
||||
if t.cache != nil && t.cache.GetUseReaders() > 0 {
|
||||
return false
|
||||
}
|
||||
t.Stat = state.TorrentClosed
|
||||
|
||||
t.bt.mu.Lock()
|
||||
@@ -274,6 +279,7 @@ func (t *Torrent) Close() {
|
||||
t.bt.mu.Unlock()
|
||||
|
||||
t.drop()
|
||||
return true
|
||||
}
|
||||
|
||||
func (t *Torrent) Status() *state.TorrentStatus {
|
||||
@@ -329,7 +335,7 @@ func (t *Torrent) Status() *state.TorrentStatus {
|
||||
|
||||
files := t.Files()
|
||||
sort.Slice(files, func(i, j int) bool {
|
||||
return files[i].Path() < files[j].Path()
|
||||
return utils2.CompareStrings(files[i].Path(), files[j].Path())
|
||||
})
|
||||
for i, f := range files {
|
||||
st.FileStats = append(st.FileStats, &state.TorrentFileStat{
|
||||
|
||||
Reference in New Issue
Block a user