diff --git a/ratiospoof/ratiospoof.go b/ratiospoof/ratiospoof.go index 7f804db..37c8e15 100644 --- a/ratiospoof/ratiospoof.go +++ b/ratiospoof/ratiospoof.go @@ -10,9 +10,9 @@ import ( "math" "math/rand" "net/http" - "net/url" "os" "os/signal" + "regexp" "strconv" "strings" "sync" @@ -367,8 +367,8 @@ func (R *ratioSpoofState) changeCurrentTimer(newAnnounceRate int) { } func (R *ratioSpoofState) tryMakeRequest(query string) *trackerResponse { - for idx, url := range R.torrentInfo.trackerInfo.urls { - completeURL := url + "?" + strings.TrimLeft(query, "?") + for idx, baseUrl := range R.torrentInfo.trackerInfo.urls { + completeURL := buildFullUrl(baseUrl, query) R.lastAnounceRequest = completeURL req, _ := http.NewRequest("GET", completeURL, nil) for header, value := range R.bitTorrentClient.Headers() { @@ -402,6 +402,13 @@ func (R *ratioSpoofState) tryMakeRequest(query string) *trackerResponse { } +func buildFullUrl(baseurl, query string) string { + if len(strings.Split(baseurl, "?")) > 1 { + return baseurl + "&" + strings.TrimLeft(query, "&") + } + return baseurl + "?" + strings.TrimLeft(query, "?") +} + func calculateNextTotalSizeByte(speedBytePerSecond, currentByte, pieceSizeByte, seconds, limitTotalBytes int) int { if speedBytePerSecond == 0 { return currentByte @@ -421,7 +428,16 @@ func extractInfoHashURLEncoded(rawData []byte, torrentData map[string]interface{ h := sha1.New() h.Write([]byte(rawData[byteOffsets[0]:byteOffsets[1]])) ret := h.Sum(nil) - return url.QueryEscape(string(ret)) + var buf bytes.Buffer + re := regexp.MustCompile(`[a-zA-Z0-9\.\-\_\~]`) + for _, b := range ret { + if re.Match([]byte{b}) { + buf.WriteByte(b) + } else { + buf.WriteString(fmt.Sprintf("%%%02x", b)) + } + } + return buf.String() } func extractTotalSize(torrentData map[string]interface{}) int { diff --git a/ratiospoof/ratiospoof_test.go b/ratiospoof/ratiospoof_test.go index 461182a..2db0e0a 100644 --- a/ratiospoof/ratiospoof_test.go +++ b/ratiospoof/ratiospoof_test.go @@ -8,7 +8,7 @@ import ( func assertAreEqual(t *testing.T, got, want interface{}) { t.Helper() if got != want { - t.Errorf("\ngot: %v\n want: %v", got, want) + t.Errorf("\ngot : %v\nwant: %v", got, want) } } @@ -55,3 +55,11 @@ func TestClculateNextTotalSizeByte(T *testing.T) { // want := "%60N%7d%1f%8b%3a%9bT%d5%fc%ad%d1%27%ab5%02%1c%fb%03%b0" // assertAreEqual(T, got, want) // } + +// func TestUrlEncodeInfoHash2(T *testing.T) { + +// b, _ := ioutil.ReadFile("") +// got := extractInfoHashURLEncoded(b, beencode.Decode(b)) +// want := "%02r%fd%fe%bf%fbt%d0%0f%cf%d9%8c%e0%a9%97%f8%08%9b%00%b2" +// assertAreEqual(T, got, want) +// }