cmd/tracker-announce: Rework for better utility in shell scripts

This commit is contained in:
Matt Joiner 2020-05-12 11:31:56 +10:00
parent cc5d2abe18
commit 533fec840a
1 changed files with 20 additions and 10 deletions

View File

@ -3,8 +3,10 @@ package main
import (
"log"
"net/url"
"os"
"strings"
"sync"
"sync/atomic"
"github.com/davecgh/go-spew/spew"
@ -36,39 +38,47 @@ func main() {
Port: 50007,
}
tagflag.Parse(&flags)
ar := tracker.AnnounceRequest{
NumWant: -1,
Left: -1,
Port: flags.Port,
}
var exitCode int32
var wg sync.WaitGroup
for _, arg := range flags.Torrents {
ts, err := argSpec(arg)
if err != nil {
log.Fatal(err)
}
ar.InfoHash = ts.InfoHash
for _, tier := range ts.Trackers {
for _, tURI := range tier {
ar := tracker.AnnounceRequest{
NumWant: -1,
Left: -1,
Port: flags.Port,
InfoHash: ts.InfoHash,
}
wg.Add(1)
go doTracker(tURI, wg.Done, ar)
go func(tURI string) {
defer wg.Done()
if doTracker(tURI, ar) {
atomic.StoreInt32(&exitCode, 1)
}
}(tURI)
}
}
}
wg.Wait()
os.Exit(int(exitCode))
}
func doTracker(tURI string, done func(), ar tracker.AnnounceRequest) {
defer done()
func doTracker(tURI string, ar tracker.AnnounceRequest) (hadError bool) {
for _, res := range announces(tURI, ar) {
err := res.error
resp := res.AnnounceResponse
if err != nil {
hadError = true
log.Printf("error announcing to %q: %s", tURI, err)
continue
}
log.Printf("tracker response from %q: %s", tURI, spew.Sdump(resp))
spew.Dump(resp)
}
return
}
type announceResult struct {