From c94fd60f771a48b9d1f5c3a365924065c77d5f31 Mon Sep 17 00:00:00 2001 From: ap-pauloafonso Date: Mon, 22 Mar 2021 22:09:38 -0300 Subject: [PATCH 1/2] removal of stop printer channel --- internal/printer/printer.go | 8 +---- internal/ratiospoof/ratiospoof.go | 7 ++-- internal/tracker/tracker.go | 10 +++--- internal/tracker/tracker_test.go | 58 +++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 16 deletions(-) create mode 100644 internal/tracker/tracker_test.go diff --git a/internal/printer/printer.go b/internal/printer/printer.go index 16ccdcb..5d93839 100644 --- a/internal/printer/printer.go +++ b/internal/printer/printer.go @@ -13,14 +13,8 @@ import ( ) func PrintState(state *ratiospoof.RatioSpoof) { - exit := false - go func() { - _ = <-state.StopPrintCH - exit = true - }() - for { - if exit { + if !state.Print { break } width := terminalSize() diff --git a/internal/ratiospoof/ratiospoof.go b/internal/ratiospoof/ratiospoof.go index 67f5148..856fa3e 100644 --- a/internal/ratiospoof/ratiospoof.go +++ b/internal/ratiospoof/ratiospoof.go @@ -34,7 +34,7 @@ type RatioSpoof struct { AnnounceCount int Status string AnnounceHistory announceHistory - StopPrintCH chan interface{} + Print bool } type AnnounceEntry struct { @@ -50,7 +50,6 @@ type announceHistory struct { } func NewRatioSpoofState(input input.InputArgs) (*RatioSpoof, error) { - stopPrintCh := make(chan interface{}) dat, err := os.ReadFile(input.TorrentPath) if err != nil { return nil, err @@ -83,7 +82,7 @@ func NewRatioSpoofState(input input.InputArgs) (*RatioSpoof, error) { Input: inputParsed, NumWant: 200, Status: "started", - StopPrintCH: stopPrintCh, + Print: true, }, nil } @@ -117,7 +116,7 @@ func (r *RatioSpoof) Run() { } }() <-sigCh - r.StopPrintCH <- "exit print" + r.Print = false r.gracefullyExit() } func (r *RatioSpoof) firstAnnounce() { diff --git a/internal/tracker/tracker.go b/internal/tracker/tracker.go index b42d30f..3479d81 100644 --- a/internal/tracker/tracker.go +++ b/internal/tracker/tracker.go @@ -41,7 +41,7 @@ func NewHttpTracker(torrentInfo *bencode.TorrentInfo) (*HttpTracker, error) { return &HttpTracker{Urls: torrentInfo.TrackerInfo.Urls}, nil } -func (t *HttpTracker) SwapFirst(currentIdx int) { +func (t *HttpTracker) swapFirst(currentIdx int) { aux := t.Urls[0] t.Urls[0] = t.Urls[currentIdx] t.Urls[currentIdx] = aux @@ -50,7 +50,7 @@ func (t *HttpTracker) SwapFirst(currentIdx int) { func (t *HttpTracker) updateEstimatedTimeToAnnounce(interval int) { t.EstimatedTimeToAnnounce = time.Now().Add(time.Duration(interval) * time.Second) } -func (t *HttpTracker) HandleSuccessfulResponse(resp *TrackerResponse) { +func (t *HttpTracker) handleSuccessfulResponse(resp *TrackerResponse) { if resp.Interval <= 0 { resp.Interval = 1800 } @@ -76,7 +76,7 @@ func (t *HttpTracker) Announce(query string, headers map[string]string, retry bo } continue } - t.HandleSuccessfulResponse(trackerResp) + t.handleSuccessfulResponse(trackerResp) return trackerResp, nil } @@ -85,7 +85,7 @@ func (t *HttpTracker) Announce(query string, headers map[string]string, retry bo if err != nil { return nil, err } - t.HandleSuccessfulResponse(resp) + t.handleSuccessfulResponse(resp) return resp, nil } } @@ -121,7 +121,7 @@ func (t *HttpTracker) tryMakeRequest(query string, headers map[string]string) (* continue } if idx != 0 { - t.SwapFirst(idx) + t.swapFirst(idx) } return &ret, nil diff --git a/internal/tracker/tracker_test.go b/internal/tracker/tracker_test.go new file mode 100644 index 0000000..7ed68ac --- /dev/null +++ b/internal/tracker/tracker_test.go @@ -0,0 +1,58 @@ +package tracker + +import ( + "reflect" + "testing" + + "github.com/ap-pauloafonso/ratio-spoof/internal/bencode" +) + +func TestNewHttpTracker(t *testing.T) { + _, err := NewHttpTracker(&bencode.TorrentInfo{TrackerInfo: &bencode.TrackerInfo{Urls: []string{"udp://url1", "udp://url2"}}}) + got := err.Error() + want := "No tcp/http tracker url announce found" + + if got != want { + t.Errorf("got: %v want %v", got, want) + } +} + +func TestSwapFirst(t *testing.T) { + tracker, _ := NewHttpTracker(&bencode.TorrentInfo{TrackerInfo: &bencode.TrackerInfo{Urls: []string{"http://url1", "http://url2", "http://url3", "http://url4"}}}) + tracker.swapFirst(3) + + got := tracker.Urls + want := []string{"http://url4", "http://url2", "http://url3", "http://url1"} + + if !reflect.DeepEqual(got, want) { + t.Errorf("got: %v want %v", got, want) + } +} + +func TestHandleSuccessfulResponse(t *testing.T) { + + t.Run("Empty interval should be overided with 1800 ", func(t *testing.T) { + tracker, _ := NewHttpTracker(&bencode.TorrentInfo{TrackerInfo: &bencode.TrackerInfo{Urls: []string{"http://url1", "http://url2", "http://url3", "http://url4"}}}) + r := TrackerResponse{} + tracker.handleSuccessfulResponse(&r) + got := r.Interval + want := 1800 + if !reflect.DeepEqual(got, want) { + t.Errorf("got: %v want %v", got, want) + } + + }) + + t.Run("Valid interval shouldn't be overwritten", func(t *testing.T) { + tracker, _ := NewHttpTracker(&bencode.TorrentInfo{TrackerInfo: &bencode.TrackerInfo{Urls: []string{"http://url1", "http://url2", "http://url3", "http://url4"}}}) + r := TrackerResponse{Interval: 900} + tracker.handleSuccessfulResponse(&r) + got := r.Interval + want := 900 + if !reflect.DeepEqual(got, want) { + t.Errorf("got: %v want %v", got, want) + } + + }) + +} From aabcc229d10bfa3f9dedae3d597d50d5cf2c38b8 Mon Sep 17 00:00:00 2001 From: ap-pauloafonso Date: Mon, 22 Mar 2021 22:26:24 -0300 Subject: [PATCH 2/2] update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ae481cb..39599f6 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ required arguments: Depends wether you use it carefuly, Its a hard task to catch cheaters, but if you start uploading crazy amounts out of nowhere or seeding something with no active leecher on the swarm you may be in risk. ## Bittorrent client supported -The currently emulation is hard coded to be a popular and accepted client qbittorrent v4.0.3. +The default client emulation is qbittorrent v4.0.3, however you can change it by using the -c argument ## Resources http://www.bittorrent.org/beps/bep_0003.html