Merge pull request #21 from ap-pauloafonso/removal-stop-printer-channel

removal of stop printer channel
This commit is contained in:
ap-pauloafonso 2021-03-23 19:27:27 -03:00 committed by GitHub
commit 00a239e02e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 68 additions and 17 deletions

View file

@ -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. 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 ## 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 ## Resources
http://www.bittorrent.org/beps/bep_0003.html http://www.bittorrent.org/beps/bep_0003.html

View file

@ -13,14 +13,8 @@ import (
) )
func PrintState(state *ratiospoof.RatioSpoof) { func PrintState(state *ratiospoof.RatioSpoof) {
exit := false
go func() {
_ = <-state.StopPrintCH
exit = true
}()
for { for {
if exit { if !state.Print {
break break
} }
width := terminalSize() width := terminalSize()

View file

@ -34,7 +34,7 @@ type RatioSpoof struct {
AnnounceCount int AnnounceCount int
Status string Status string
AnnounceHistory announceHistory AnnounceHistory announceHistory
StopPrintCH chan interface{} Print bool
} }
type AnnounceEntry struct { type AnnounceEntry struct {
@ -50,7 +50,6 @@ type announceHistory struct {
} }
func NewRatioSpoofState(input input.InputArgs) (*RatioSpoof, error) { func NewRatioSpoofState(input input.InputArgs) (*RatioSpoof, error) {
stopPrintCh := make(chan interface{})
dat, err := os.ReadFile(input.TorrentPath) dat, err := os.ReadFile(input.TorrentPath)
if err != nil { if err != nil {
return nil, err return nil, err
@ -83,7 +82,7 @@ func NewRatioSpoofState(input input.InputArgs) (*RatioSpoof, error) {
Input: inputParsed, Input: inputParsed,
NumWant: 200, NumWant: 200,
Status: "started", Status: "started",
StopPrintCH: stopPrintCh, Print: true,
}, nil }, nil
} }
@ -117,7 +116,7 @@ func (r *RatioSpoof) Run() {
} }
}() }()
<-sigCh <-sigCh
r.StopPrintCH <- "exit print" r.Print = false
r.gracefullyExit() r.gracefullyExit()
} }
func (r *RatioSpoof) firstAnnounce() { func (r *RatioSpoof) firstAnnounce() {

View file

@ -41,7 +41,7 @@ func NewHttpTracker(torrentInfo *bencode.TorrentInfo) (*HttpTracker, error) {
return &HttpTracker{Urls: torrentInfo.TrackerInfo.Urls}, nil return &HttpTracker{Urls: torrentInfo.TrackerInfo.Urls}, nil
} }
func (t *HttpTracker) SwapFirst(currentIdx int) { func (t *HttpTracker) swapFirst(currentIdx int) {
aux := t.Urls[0] aux := t.Urls[0]
t.Urls[0] = t.Urls[currentIdx] t.Urls[0] = t.Urls[currentIdx]
t.Urls[currentIdx] = aux t.Urls[currentIdx] = aux
@ -50,7 +50,7 @@ func (t *HttpTracker) SwapFirst(currentIdx int) {
func (t *HttpTracker) updateEstimatedTimeToAnnounce(interval int) { func (t *HttpTracker) updateEstimatedTimeToAnnounce(interval int) {
t.EstimatedTimeToAnnounce = time.Now().Add(time.Duration(interval) * time.Second) 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 { if resp.Interval <= 0 {
resp.Interval = 1800 resp.Interval = 1800
} }
@ -76,7 +76,7 @@ func (t *HttpTracker) Announce(query string, headers map[string]string, retry bo
} }
continue continue
} }
t.HandleSuccessfulResponse(trackerResp) t.handleSuccessfulResponse(trackerResp)
return trackerResp, nil return trackerResp, nil
} }
@ -85,7 +85,7 @@ func (t *HttpTracker) Announce(query string, headers map[string]string, retry bo
if err != nil { if err != nil {
return nil, err return nil, err
} }
t.HandleSuccessfulResponse(resp) t.handleSuccessfulResponse(resp)
return resp, nil return resp, nil
} }
} }
@ -121,7 +121,7 @@ func (t *HttpTracker) tryMakeRequest(query string, headers map[string]string) (*
continue continue
} }
if idx != 0 { if idx != 0 {
t.SwapFirst(idx) t.swapFirst(idx)
} }
return &ret, nil return &ret, nil

View file

@ -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)
}
})
}