Advertisement
Guest User

Untitled

a guest
Mar 5th, 2018
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.08 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "os"
  6.     "time"
  7.  
  8.     "github.com/anacrolix/dht"
  9.     "github.com/anacrolix/torrent"
  10. )
  11.  
  12. func waitForInfo(clientTorrent *torrent.Torrent, seconds time.Duration) (deleted bool) {
  13.     timeout := make(chan bool, 1) //creating a timeout channel for our gotinfo
  14.     go func() {
  15.         time.Sleep(seconds * time.Second)
  16.         timeout <- true
  17.     }()
  18.     select {
  19.     case <-clientTorrent.GotInfo(): //attempting to retrieve info for torrent
  20.         clientTorrent.DownloadAll()
  21.         return false
  22.     case <-timeout: // getting info for torrent has timed out so purging the torrent
  23.         clientTorrent.Drop()
  24.         return true
  25.     }
  26.  
  27. }
  28.  
  29. func main() {
  30.  
  31.     config := torrent.Config{DataDir: "downloads",
  32.         DHTConfig: dht.ServerConfig{StartingNodes: dht.GlobalBootstrapAddrs},
  33.     }
  34.  
  35.     tclient, err := torrent.NewClient(&config) //pulling out the torrent specific config to use
  36.     if err != nil {
  37.         fmt.Println("Error with creating client", err)
  38.     }
  39.  
  40.     ubuntuTorrent, err := tclient.AddMagnet("magnet:?xt=urn:btih:9f9165d9a281a9b8e782cd5176bbcc8256fd1871&dn=Ubuntu+16.04.1+LTS+Desktop+64-bit&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Fzer0day.ch%3A1337&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969")
  41.     if err != nil {
  42.         fmt.Println("Error adding Torrent", err)
  43.     }
  44.     timedOut := waitForInfo(ubuntuTorrent, 30)
  45.     if timedOut {
  46.         fmt.Println("Timed out awaiting info...")
  47.         os.Exit(3)
  48.     }
  49.     ubuntuTorrent.DownloadAll()
  50.     fmt.Println("downloading all.. now sleeping 60 seconds..")
  51.     time.Sleep(60 * time.Second)
  52.     fmt.Println("now forcing max connections to zero.")
  53.     fmt.Println("current amount downloaded", ubuntuTorrent.Stats().BytesReadUsefulData)
  54.     fmt.Println("Number of open peers", ubuntuTorrent.Stats().ActivePeers)
  55.     ubuntuTorrent.SetMaxEstablishedConns(0)
  56.     fmt.Println("now waiting 10 seconds")
  57.  
  58.     time.Sleep(10 * time.Second)
  59.     oldMax := ubuntuTorrent.SetMaxEstablishedConns(80)
  60.     fmt.Println("Reseting peers", oldMax)
  61.     fmt.Println("now waiting 10 seconds")
  62.     time.Sleep(10 * time.Second)
  63.     ubuntuTorrent.Drop()
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement