Skip to content

Commit

Permalink
Merge pull request #43 from Sioro-Neoku/torrent-update
Browse files Browse the repository at this point in the history
Torrent update
  • Loading branch information
Sioro-Neoku committed Jan 14, 2019
2 parents abb60b4 + e605fba commit a8eb11c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
@@ -1,12 +1,10 @@
sudo: false
language: go
go:
- 1.5
- 1.6
- 1.7beta2
- 1.11
install:
- go get
- go get github.com/alecthomas/gometalinter
- gometalinter --install --update
script:
- gometalinter ./... --deadline=30s
- gometalinter ./... --deadline=90s
48 changes: 29 additions & 19 deletions client.go
Expand Up @@ -71,14 +71,16 @@ func NewClient(cfg ClientConfig) (client Client, err error) {

client.Config = cfg

blocklist := getBlocklist()
torrentConfig := torrent.NewDefaultClientConfig()
torrentConfig.DataDir = os.TempDir()
torrentConfig.NoUpload = !cfg.Seed
torrentConfig.DisableTCP = !cfg.TCP
torrentConfig.ListenPort = cfg.TorrentPort
torrentConfig.IPBlocklist = blocklist

// Create client.
c, err = torrent.NewClient(&torrent.Config{
DataDir: os.TempDir(),
NoUpload: !cfg.Seed,
Seed: cfg.Seed,
DisableTCP: !cfg.TCP,
ListenAddr: fmt.Sprintf(":%d", cfg.TorrentPort),
})
c, err = torrent.NewClient(torrentConfig)

if err != nil {
return client, ClientError{Type: "creating torrent client", Origin: err}
Expand Down Expand Up @@ -116,16 +118,19 @@ func NewClient(cfg ClientConfig) (client Client, err error) {
t.DownloadAll()

// Prioritize first 5% of the file.
client.getLargestFile().PrioritizeRegion(0, int64(t.NumPieces()/100*5))
largestFile := client.getLargestFile()
firstPieceIndex := largestFile.Offset() * int64(t.NumPieces()) / t.Length()
endPieceIndex := (largestFile.Offset() + largestFile.Length()) * int64(t.NumPieces()) / t.Length()
for idx := firstPieceIndex; idx <= endPieceIndex*5/100; idx++ {
t.Piece(int(idx)).SetPriority(torrent.PiecePriorityNow)
}
}()

go client.addBlocklist()

return
}

// Download and add the blocklist.
func (c *Client) addBlocklist() {
func getBlocklist() iplist.Ranger {
var err error
blocklistPath := os.TempDir() + "/go-peerflix-blocklist.gz"

Expand All @@ -135,32 +140,34 @@ func (c *Client) addBlocklist() {

if err != nil {
log.Printf("Error downloading blocklist: %s", err)
return
return nil
}

// Load blocklist.
// #nosec
// We trust our temporary directory as we just wrote the file there ourselves.
blocklistReader, err := os.Open(blocklistPath)
if err != nil {
log.Printf("Error opening blocklist: %s", err)
return
return nil
}

// Extract file.
gzipReader, err := gzip.NewReader(blocklistReader)
if err != nil {
log.Printf("Error extracting blocklist: %s", err)
return
return nil
}

// Read as iplist.
blocklist, err := iplist.NewFromReader(gzipReader)
if err != nil {
log.Printf("Error reading blocklist: %s", err)
return
return nil
}

log.Printf("Loading blocklist.\nFound %d ranges\n", blocklist.NumRanges())
c.Client.SetIPBlockList(blocklist)
return blocklist
}

func downloadBlockList(blocklistPath string) (err error) {
Expand Down Expand Up @@ -195,7 +202,8 @@ func (c *Client) Render() {
complete := humanize.Bytes(uint64(currentProgress))
size := humanize.Bytes(uint64(t.Info().TotalLength()))

uploadProgress := t.Stats().DataBytesWritten - c.Uploaded
bytesWrittenData := t.Stats().BytesWrittenData
uploadProgress := (&bytesWrittenData).Int64() - c.Uploaded
uploadSpeed := humanize.Bytes(uint64(uploadProgress)) + "/s"
c.Uploaded = uploadProgress

Expand All @@ -217,7 +225,7 @@ func (c *Client) Render() {
}

func (c Client) getLargestFile() *torrent.File {
var target torrent.File
var target *torrent.File
var maxSize int64

for _, file := range c.Torrent.Files() {
Expand All @@ -227,7 +235,7 @@ func (c Client) getLargestFile() *torrent.File {
}
}

return &target
return target
}

/*
Expand Down Expand Up @@ -302,6 +310,8 @@ func downloadFile(URL string) (fileName string, err error) {
}
}()

// #nosec
// We are downloading the url the user passed to us, we trust it is a torrent file.
response, err := http.Get(URL)
if err != nil {
return
Expand Down
7 changes: 3 additions & 4 deletions fileEntry.go
Expand Up @@ -2,7 +2,6 @@ package main

import (
"io"
"os"

"github.com/anacrolix/torrent"
)
Expand All @@ -16,11 +15,11 @@ type SeekableContent interface {
// FileEntry helps reading a torrent file.
type FileEntry struct {
*torrent.File
*torrent.Reader
torrent.Reader
}

// Seek seeks to the correct file position, paying attention to the offset.
func (f FileEntry) Seek(offset int64, whence int) (int64, error) {
func (f *FileEntry) Seek(offset int64, whence int) (int64, error) {
return f.Reader.Seek(offset+f.File.Offset(), whence)
}

Expand All @@ -32,7 +31,7 @@ func NewFileReader(f *torrent.File) (SeekableContent, error) {
// We read ahead 1% of the file continuously.
reader.SetReadahead(f.Length() / 100)
reader.SetResponsive()
_, err := reader.Seek(f.Offset(), os.SEEK_SET)
_, err := reader.Seek(f.Offset(), io.SeekStart)

return &FileEntry{
File: f,
Expand Down
2 changes: 2 additions & 0 deletions player.go
Expand Up @@ -33,6 +33,8 @@ func (p GenericPlayer) Open(url string) error {
}
command = append(command, p.Args...)
command = append(command, url)
// #nosec
// It is the user's responsibility to pass the correct arguments to open the url.
return exec.Command(command[0], command[1:]...).Start()
}

Expand Down

0 comments on commit a8eb11c

Please sign in to comment.