Example #1
0
func startTracker(addr string, torrentFiles []string) (err error) {
	t := tracker.NewTracker()
	// TODO(jackpal) Allow caller to choose port number
	t.Addr = addr
	dial := dialerFromFlags()
	for _, torrentFile := range torrentFiles {
		var metaInfo *torrent.MetaInfo
		metaInfo, err = torrent.GetMetaInfo(dial, torrentFile)
		if err != nil {
			return
		}
		name := metaInfo.Info.Name
		if name == "" {
			name = path.Base(torrentFile)
		}
		err = t.Register(metaInfo.InfoHash, name)
		if err != nil {
			return
		}
	}
	go func() {
		quitChan := listenSigInt()
		select {
		case <-quitChan:
			log.Printf("got control-C")
			t.Quit()
		}
	}()

	err = t.ListenAndServe()
	if err != nil {
		return
	}
	return
}
Example #2
0
func NewTracker(listen, trListen, root string, port int) (*Tracker, error) {
	flags := &torrent.TorrentFlags{
		Port:                port,
		FileDir:             root,
		SeedRatio:           math.Inf(0),
		UseDeadlockDetector: true,
		FileSystemProvider:  torrent.OsFsProvider{},
		UseDHT:              false,
	}

	conns, listenPort, err := torrent.ListenForPeerConnections(flags)

	if err != nil {
		return nil, err
	}

	tr := tracker.NewTracker()
	tr.Addr = trListen

	mux := http.NewServeMux()

	t := &Tracker{
		mutex:    sync.Mutex{},
		requests: map[string]*sync.Mutex{},

		downloader: bay.NewDownloader(root),

		flags:        flags,
		conns:        conns,
		sessions:     map[string]*torrent.TorrentSession{},
		sessionCh:    make(chan *torrent.TorrentSession),
		sessionMutex: sync.RWMutex{},

		tr:       tr,
		trListen: trListen,

		mux:    mux,
		listen: listen,

		port: listenPort,
	}

	mux.HandleFunc("/", t.handle)

	return t, nil
}