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