mirror of
https://github.com/Ernous/TorrServerJellyfin.git
synced 2025-12-19 21:46:11 +05:00
Merge branch 'master' into old-engine
This commit is contained in:
@@ -196,12 +196,12 @@ func SetSettings(set *sets.BTSets) {
|
||||
sets.SetBTSets(set)
|
||||
log.TLogln("drop all torrents")
|
||||
dropAllTorrent()
|
||||
time.Sleep(time.Second * 2)
|
||||
time.Sleep(time.Second * 1)
|
||||
log.TLogln("disconect")
|
||||
bts.Disconnect()
|
||||
log.TLogln("connect")
|
||||
bts.Connect()
|
||||
time.Sleep(time.Second * 2)
|
||||
time.Sleep(time.Second * 1)
|
||||
log.TLogln("end set settings")
|
||||
}
|
||||
|
||||
@@ -212,12 +212,12 @@ func SetDefSettings() {
|
||||
sets.SetDefault()
|
||||
log.TLogln("drop all torrents")
|
||||
dropAllTorrent()
|
||||
time.Sleep(time.Second * 2)
|
||||
time.Sleep(time.Second * 1)
|
||||
log.TLogln("disconect")
|
||||
bts.Disconnect()
|
||||
log.TLogln("connect")
|
||||
bts.Connect()
|
||||
time.Sleep(time.Second * 2)
|
||||
time.Sleep(time.Second * 1)
|
||||
log.TLogln("end set default settings")
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
package torr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"strconv"
|
||||
"sync"
|
||||
|
||||
"github.com/anacrolix/publicip"
|
||||
"github.com/anacrolix/torrent"
|
||||
"github.com/anacrolix/torrent/metainfo"
|
||||
|
||||
@@ -58,7 +60,7 @@ func (bt *BTServer) Connect() error {
|
||||
bt.mu.Lock()
|
||||
defer bt.mu.Unlock()
|
||||
var err error
|
||||
bt.configure()
|
||||
bt.configure(context.TODO())
|
||||
bt.client, err = torrent.NewClient(bt.config)
|
||||
bt.torrents = make(map[metainfo.Hash]*Torrent)
|
||||
InitApiHelper(bt)
|
||||
@@ -75,7 +77,7 @@ func (bt *BTServer) Disconnect() {
|
||||
}
|
||||
}
|
||||
|
||||
func (bt *BTServer) configure() {
|
||||
func (bt *BTServer) configure(ctx context.Context) (err error) {
|
||||
blocklist, _ := utils.ReadBlockedIP()
|
||||
bt.config = torrent.NewDefaultClientConfig()
|
||||
|
||||
@@ -121,24 +123,28 @@ func (bt *BTServer) configure() {
|
||||
if settings.BTsets.UploadRateLimit > 0 {
|
||||
bt.config.UploadRateLimiter = utils.Limit(settings.BTsets.UploadRateLimit * 1024)
|
||||
}
|
||||
if settings.BTsets.PeersListenPort > 0 {
|
||||
log.Println("Set listen port", settings.BTsets.PeersListenPort)
|
||||
bt.config.ListenPort = settings.BTsets.PeersListenPort
|
||||
if settings.TorAddr != "" {
|
||||
bt.config.SetListenAddr(settings.TorAddr)
|
||||
} else {
|
||||
upnpport := 32000
|
||||
for {
|
||||
log.Println("Check upnp port", upnpport)
|
||||
l, err := net.Listen("tcp", ":"+strconv.Itoa(upnpport))
|
||||
if l != nil {
|
||||
l.Close()
|
||||
if settings.BTsets.PeersListenPort > 0 {
|
||||
log.Println("Set listen port", settings.BTsets.PeersListenPort)
|
||||
bt.config.ListenPort = settings.BTsets.PeersListenPort
|
||||
} else {
|
||||
upnpport := 32000
|
||||
for {
|
||||
log.Println("Check upnp port", upnpport)
|
||||
l, err := net.Listen("tcp", ":"+strconv.Itoa(upnpport))
|
||||
if l != nil {
|
||||
l.Close()
|
||||
}
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
upnpport++
|
||||
}
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
upnpport++
|
||||
log.Println("Set upnp port", upnpport)
|
||||
bt.config.ListenPort = upnpport
|
||||
}
|
||||
log.Println("Set upnp port", upnpport)
|
||||
bt.config.ListenPort = upnpport
|
||||
}
|
||||
|
||||
log.Println("Client config:", settings.BTsets)
|
||||
@@ -150,7 +156,10 @@ func (bt *BTServer) configure() {
|
||||
}
|
||||
}
|
||||
if bt.config.PublicIp4 == nil {
|
||||
bt.config.PublicIp4 = getPublicIp4()
|
||||
bt.config.PublicIp4, err = publicip.Get4(ctx)
|
||||
if err != nil {
|
||||
log.Printf("error getting public ipv4 address: %v", err)
|
||||
}
|
||||
}
|
||||
if bt.config.PublicIp4 != nil {
|
||||
log.Println("PublicIp4:", bt.config.PublicIp4)
|
||||
@@ -163,11 +172,15 @@ func (bt *BTServer) configure() {
|
||||
}
|
||||
}
|
||||
if bt.config.PublicIp6 == nil {
|
||||
bt.config.PublicIp6 = getPublicIp6()
|
||||
bt.config.PublicIp6, err = publicip.Get6(ctx)
|
||||
if err != nil {
|
||||
log.Printf("error getting public ipv6 address: %v", err)
|
||||
}
|
||||
}
|
||||
if bt.config.PublicIp6 != nil {
|
||||
log.Println("PublicIp6:", bt.config.PublicIp6)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (bt *BTServer) GetTorrent(hash torrent.InfoHash) *Torrent {
|
||||
|
||||
@@ -63,17 +63,17 @@ func (t *Torrent) Preload(index int, size int64) {
|
||||
}
|
||||
}()
|
||||
|
||||
// mb5 -> 8/16 MB
|
||||
mb5 := int64(t.Info().PieceLength)
|
||||
if mb5 < 8*1024*1024 {
|
||||
mb5 = 8 * 1024 * 1024
|
||||
// startend -> 8/16 MB
|
||||
startend := int64(t.Info().PieceLength)
|
||||
if startend < 8*1024*1024 {
|
||||
startend = 8 * 1024 * 1024
|
||||
}
|
||||
|
||||
readerStart := file.NewReader()
|
||||
defer readerStart.Close()
|
||||
readerStart.SetResponsive()
|
||||
readerStart.SetReadahead(0)
|
||||
readerStartEnd := size - mb5
|
||||
readerStartEnd := size - startend
|
||||
|
||||
if readerStartEnd < 0 {
|
||||
// Если конец начального ридера оказался за началом
|
||||
@@ -84,7 +84,7 @@ func (t *Torrent) Preload(index int, size int64) {
|
||||
readerStartEnd = file.Length()
|
||||
}
|
||||
|
||||
readerEndStart := file.Length() - mb5
|
||||
readerEndStart := file.Length() - startend
|
||||
readerEndEnd := file.Length()
|
||||
|
||||
var wa sync.WaitGroup
|
||||
|
||||
@@ -255,9 +255,9 @@ func (c *Cache) getRemPieces() []*Piece {
|
||||
readerPos := r.getReaderPiece()
|
||||
readerRAHPos := r.getReaderRAHPiece()
|
||||
end := r.getPiecesRange().End
|
||||
count := int(16 * 1024 * 1024 / c.pieceLength * 5) // 80 MB
|
||||
if count > 40 {
|
||||
count = 40
|
||||
count := int(16 * 1024 * 1024 * 4 / c.pieceLength) // 64 MB window
|
||||
if count > 64 {
|
||||
count = 64
|
||||
}
|
||||
limit := 0
|
||||
for i := readerPos; i < end && limit < count; i++ {
|
||||
|
||||
@@ -62,7 +62,8 @@ func (t *Torrent) Stream(fileID int, req *http.Request, resp http.ResponseWriter
|
||||
sets.SetViewed(&sets.Viewed{t.Hash().HexString(), fileID})
|
||||
|
||||
resp.Header().Set("Connection", "close")
|
||||
resp.Header().Set("ETag", httptoo.EncodeQuotedString(fmt.Sprintf("%s/%s", t.Hash().HexString(), hex.EncodeToString([]byte(file.Path())))))
|
||||
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")
|
||||
mime, err := mt.MimeTypeByPath(file.Path())
|
||||
|
||||
@@ -50,8 +50,8 @@ type Torrent struct {
|
||||
}
|
||||
|
||||
func NewTorrent(spec *torrent.TorrentSpec, bt *BTServer) (*Torrent, error) {
|
||||
// TODO panic when settings sets
|
||||
if bt == nil {
|
||||
// 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 {
|
||||
|
||||
Reference in New Issue
Block a user