merge master

This commit is contained in:
ap-pauloafonso 2021-01-22 17:27:24 -03:00
commit c978a5d054
3 changed files with 35 additions and 10 deletions

View file

@ -1,8 +1,10 @@
package beencode
import (
"bytes"
"crypto/sha1"
"net/url"
"fmt"
"regexp"
"strconv"
)
@ -56,12 +58,20 @@ func TorrentDictParse(dat []byte) (*TorrentInfo, error) {
}
func (T *torrentDict) extractInfoHashURLEncoded(rawData []byte) string {
byteOffsets := T.resultMap[torrentInfoKey].(map[string]interface{})[torrentDictOffsetsKey].([]int)
byteOffsets := T.resultMap["info"].(map[string]interface{})["byte_offsets"].([]int)
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 (T *torrentDict) extractTotalSize() int {

View file

@ -267,10 +267,10 @@ func (R *ratioSpoofState) firstAnnounce() {
}
func (R *ratioSpoofState) updateInterval(resp trackerResponse) {
if resp.minInterval > 0 {
R.announceInterval = resp.minInterval
} else {
if resp.interval > 0 {
R.announceInterval = resp.interval
} else {
R.announceInterval = 1800
}
}
@ -368,8 +368,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.trackerState.urls {
completeURL := buildFullUrl(baseUrl, query)
R.lastAnounceRequest = completeURL
req, _ := http.NewRequest("GET", completeURL, nil)
for header, value := range R.bitTorrentClient.Headers() {
@ -403,6 +403,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

View file

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