diff --git a/cmd/gotorrent/main.go b/cmd/main.go similarity index 88% rename from cmd/gotorrent/main.go rename to cmd/main.go index c19ad1f..42ef6f5 100644 --- a/cmd/gotorrent/main.go +++ b/cmd/main.go @@ -22,6 +22,14 @@ * SOFTWARE. */ -package gotorrent +package main -func main() {} +import ( + "gotorrent/internal/torrent" + "os" +) + +func main() { + metaInfoFilePath := os.Args[1] + torrent.StartTorrent(metaInfoFilePath, "") +} diff --git a/internal/bencode/decoder.go b/internal/bencode/decoder.go index 65388e4..e66051c 100644 --- a/internal/bencode/decoder.go +++ b/internal/bencode/decoder.go @@ -1,23 +1,25 @@ /* - * Copyright (c) 2026 git-sudo-404 + * MIT License + * + * Copyright (c) 2026 git-sudo-404 * * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal + * Of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is + * Copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. + * The above copyright notice and this permission notice shall be included in all + * Copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. */ package bencode @@ -183,3 +185,197 @@ func Decode(ior io.Reader) (map[string]any, error) { decoder := &decoder{br} return decoder.decodeDict() } + +//NOTE: The below code does not actually belong to bencode, but then it is needed to get the rawInfo from the meta-info file which will be hashed and sent along the tracker request. +// Whenever the below functions are used, they may destroy the bytes Buffer of the decoder . +// So, in the application layer use a seperate decoder structs for these purposes + +func (d *decoder) appendRawInt(rawInfo *[]byte) error { + buf, err := d.ReadBytes('e') + if err != nil { + return err + } + *rawInfo = append(*rawInfo, buf...) + return nil +} + +func (d *decoder) appendRawString(rawInfo *[]byte) error { + stringLenBytes, err := d.ReadBytes(':') + if err != nil { + return err + } + *rawInfo = append(*rawInfo, stringLenBytes...) + stringLen, err := strconv.ParseInt(string(stringLenBytes[:len(stringLenBytes)-1]), 10, 64) + if err != nil { + return err + } + buf := make([]byte, stringLen) + if _, err := io.ReadFull(d, buf); err != nil { + return err + } + *rawInfo = append(*rawInfo, buf...) + return nil +} + +func (d *decoder) appendRawList(rawInfo *[]byte) error { + lbyte, err := d.ReadByte() + *rawInfo = append(*rawInfo, lbyte) + if err != nil { + return err + } + for { + + peakByte, err := d.Peek(1) + if err != nil { + return err + } + + if rune(peakByte[0]) == 'e' { + lastByte, err := d.ReadByte() + if err != nil { + return err + } + *rawInfo = append(*rawInfo, lastByte) + break + } + + switch rune(peakByte[0]) { + case 'i': + err := d.appendRawInt(rawInfo) + if err != nil { + return err + } + case 'l': + err := d.appendRawList(rawInfo) + if err != nil { + return err + } + case 'd': + if err := d.appendRawDict(rawInfo); err != nil { + return err + } + default: // string + err := d.appendRawString(rawInfo) + if err != nil { + return err + } + } + } + return nil +} + +func (d *decoder) appendRawDict(rawInfo *[]byte) error { + dByte, err := d.ReadByte() + if err != nil { + return err + } + *rawInfo = append(*rawInfo, dByte) + + for { + + peakByte, err := d.Peek(1) + if err != nil { + return err + } + + if rune(peakByte[0]) == 'e' { + lastByte, err := d.ReadByte() + if err != nil { + return err + } + *rawInfo = append(*rawInfo, lastByte) + break + } + + switch rune(peakByte[0]) { + case 'e': + + case 'i': + if err := d.appendRawInt(rawInfo); err != nil { + return err + } + case 'l': + if err := d.appendRawList(rawInfo); err != nil { + return err + } + case 'd': + if err := d.appendRawDict(rawInfo); err != nil { + return err + } + default: + if err := d.appendRawString(rawInfo); err != nil { + return err + } + } + + } + return nil +} + +func (d *decoder) getRawInfoBytes() ([]byte, error) { + var rawInfo []byte + d.ReadByte() // consume the 'd' + for { + + peekBytes, err := d.Peek(1) + if err != nil { + panic(err) + } + + if rune(peekBytes[0]) == 'e' { + d.ReadByte() + break + } + + key, err := d.decodeString() + if err != nil { + panic(err) + } + + if key == "info" { + + if err := d.appendRawDict(&rawInfo); err != nil { + return nil, err + } + + break + } else { + + peekBytes, err = d.Peek(1) + if err != nil { + panic(err) + } + + switch rune(peekBytes[0]) { + case 'i': + _, err := d.decodeInt() + if err != nil { + panic(err) + } + case 'l': + _, err := d.decodeList() + if err != nil { + panic(err) + } + case 'd': + _, err := d.decodeDict() + if err != nil { + panic(err) + } + default: + _, err := d.decodeString() + if err != nil { + panic(err) + } + } + + } + } + return rawInfo, nil +} + +func RawInfoBytes(ior io.Reader) ([]byte, error) { + br := bufio.NewReader(ior) + decoder := &decoder{br} + return decoder.getRawInfoBytes() +} diff --git a/internal/bencode/decoder_test.go b/internal/bencode/decoder_test.go index baebc29..36d5a2d 100644 --- a/internal/bencode/decoder_test.go +++ b/internal/bencode/decoder_test.go @@ -1,23 +1,25 @@ /* - * Copyright (c) 2026 git-sudo-404 + * MIT License + * + * Copyright (c) 2026 git-sudo-404 * * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal + * Of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is + * Copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. + * The above copyright notice and this permission notice shall be included in all + * Copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. */ package bencode @@ -109,3 +111,146 @@ func TestDecodeDict(t *testing.T) { } } + +func TestAppendRawInt(t *testing.T) { + encoded := "i123e" + + d := &decoder{ + bufio.NewReader(bytes.NewBufferString(encoded)), + } + + var rawInfo []byte + + err := d.appendRawInt(&rawInfo) + if err != nil { + t.Fatal(err) + } + + want := []byte(encoded) + + if !bytes.Equal(rawInfo, want) { + t.Errorf("\nGOT : %q\nWANT : %q", rawInfo, want) + } +} + +func TestAppendRawString(t *testing.T) { + encoded := "5:hello" + + d := &decoder{ + bufio.NewReader(bytes.NewBufferString(encoded)), + } + + var rawInfo []byte + + err := d.appendRawString(&rawInfo) + if err != nil { + t.Fatal(err) + } + + want := []byte(encoded) + + if !bytes.Equal(rawInfo, want) { + t.Errorf("\nGOT : %q\nWANT : %q", rawInfo, want) + } +} + +func TestAppendRawList(t *testing.T) { + encoded := "li10e2:hili20eee" + + d := &decoder{ + bufio.NewReader(bytes.NewBufferString(encoded)), + } + + var rawInfo []byte + + err := d.appendRawList(&rawInfo) + if err != nil { + t.Fatal(err) + } + + want := []byte(encoded) + + if !bytes.Equal(rawInfo, want) { + t.Errorf("\nGOT : %q\nWANT : %q", rawInfo, want) + } +} + +func TestAppendRawDict(t *testing.T) { + encoded := "d4:name4:test6:lengthi10ee" + + d := &decoder{ + bufio.NewReader(bytes.NewBufferString(encoded)), + } + + var rawInfo []byte + + err := d.appendRawDict(&rawInfo) + if err != nil { + t.Fatal(err) + } + + want := []byte(encoded) + + if !bytes.Equal(rawInfo, want) { + t.Errorf("\nGOT : %q\nWANT : %q", rawInfo, want) + } +} + +func TestAppendRawDictNested(t *testing.T) { + encoded := "d4:name4:test4:infod3:fooi20eee" + + d := &decoder{ + bufio.NewReader(bytes.NewBufferString(encoded)), + } + + var rawInfo []byte + + err := d.appendRawDict(&rawInfo) + if err != nil { + t.Fatal(err) + } + + want := []byte(encoded) + + if !bytes.Equal(rawInfo, want) { + t.Errorf("\nGOT : %q\nWANT : %q", rawInfo, want) + } +} + +func TestGetRawInfoBytes(t *testing.T) { + encoded := "d8:announce3:foo4:infod4:name4:test6:lengthi10eee" + + d := &decoder{ + bufio.NewReader(bytes.NewBufferString(encoded)), + } + + got, err := d.getRawInfoBytes() + if err != nil { + t.Fatal(err) + } + + want := []byte("d4:name4:test6:lengthi10ee") + + if !bytes.Equal(got, want) { + t.Errorf("\nGOT : %q\nWANT : %q", got, want) + } +} + +func TestGetRawInfoBytesInfoFirst(t *testing.T) { + encoded := "d4:infod4:name4:test6:lengthi10eee" + + d := &decoder{ + bufio.NewReader(bytes.NewBufferString(encoded)), + } + + got, err := d.getRawInfoBytes() + if err != nil { + t.Fatal(err) + } + + want := []byte("d4:name4:test6:lengthi10ee") + + if !bytes.Equal(got, want) { + t.Errorf("\nGOT : %q\nWANT : %q", got, want) + } +} diff --git a/internal/torrent/client.go b/internal/torrent/client.go index 51c06e8..48a5beb 100644 --- a/internal/torrent/client.go +++ b/internal/torrent/client.go @@ -44,12 +44,14 @@ type Client struct { peerId [20]byte PeerAddress // ip & port of the client remotePeers []RemotePeer + port int64 } func NewClient() *Client { return &Client{ peerId: generateClientPeerId(), + port: 6881, } } diff --git a/internal/torrent/metainfo.go b/internal/torrent/metainfo.go index 5392aae..b9cd4e8 100644 --- a/internal/torrent/metainfo.go +++ b/internal/torrent/metainfo.go @@ -53,6 +53,8 @@ type info struct { type MetaInfo struct { //info dictionary (here, single file torrent) info + infoHash [20]byte // 20 byte SHA-1 hash of the raw bencoded info dict from the metaInfo file (.torrent) to be added along every tracker request + rawInfo []byte // raw info string from the .torrent file announce string // The announce URL of the tracker (string) announceList *[][]string //(optional) this is an extention to the official specification, offering backwards-compatibility. (list of lists of strings). creationDate *int64 // (optional) the creation time of the torrent, in standard UNIX epoch format (integer, seconds since 1-Jan-1970 00:00:00 UTC) @@ -69,6 +71,8 @@ func NewMetaInfo() *MetaInfo { name: "", length: 0, }, + rawInfo: []byte{}, + infoHash: [20]byte{}, announce: "", } } @@ -128,6 +132,16 @@ func (mi *MetaInfo) SetEncoding(encoding string) *MetaInfo { return mi } +func (mi *MetaInfo) SetInfoHash(infoHash [20]byte) *MetaInfo { + mi.infoHash = infoHash + return mi +} + +func (mi *MetaInfo) SetRawInfo(rawInfo []byte) *MetaInfo { + mi.rawInfo = rawInfo + return mi +} + func (mi *MetaInfo) GetPieceLength() (int64, error) { return mi.pieceLength, nil } @@ -190,6 +204,16 @@ func (mi *MetaInfo) GetEncoding() (string, error) { return *mi.encoding, nil } +func (mi *MetaInfo) GetRawInfo() []byte { + return mi.rawInfo +} + +func (mi *MetaInfo) GetInfoHash() [20]byte { + return mi.infoHash +} + +// WARNING: This infoDIct shouldn't be used in the trackerRequest , since the tracker rewquest expexts SHA-1 hash of the raw bencoded info hash form the .torrent +// this is just used for the internal purposes only func (mi *MetaInfo) GetInfoDict() (map[string]any, error) { var info map[string]any info = map[string]any{} diff --git a/internal/torrent/metainfoParser.go b/internal/torrent/metainfoParser.go index 3750690..3466903 100644 --- a/internal/torrent/metainfoParser.go +++ b/internal/torrent/metainfoParser.go @@ -25,6 +25,7 @@ package torrent import ( + "crypto/sha1" "fmt" bencode "gotorrent/internal/bencode" "os" @@ -64,6 +65,22 @@ func checkAnnounceListPresenceAndType(metaInfoDict map[string]any) ([][]string, return result, true, nil } +func getRawInfoFromMetaInfoFile(metaInfoFilePath string) ([]byte, error) { + var rawInfo []byte + rawMetaInfoBytes, err := os.Open(metaInfoFilePath) + if err != nil { + return nil, err + } + defer rawMetaInfoBytes.Close() + + rawInfo, err = bencode.RawInfoBytes(rawMetaInfoBytes) + if err != nil { + return nil, err + } + + return rawInfo, nil +} + func CreateMetaInfoFromFile(filePath string) (*MetaInfo, error) { metaInfo := NewMetaInfo() @@ -165,5 +182,13 @@ func CreateMetaInfoFromFile(filePath string) (*MetaInfo, error) { metaInfo.SetEncoding(encoding) } + if rawInfo, err := getRawInfoFromMetaInfoFile(filePath); err == nil { + metaInfo.SetRawInfo(rawInfo) + infoHash := sha1.Sum(rawInfo) + metaInfo.SetInfoHash(infoHash) + } else { + panic(err) + } + return metaInfo, nil } diff --git a/internal/torrent/torrent.go b/internal/torrent/torrent.go new file mode 100644 index 0000000..a0f1bc6 --- /dev/null +++ b/internal/torrent/torrent.go @@ -0,0 +1,113 @@ +/* + * MIT License + * + * Copyright (c) 2026 git-sudo-404 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * Of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * Copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * Copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package torrent + +import ( + "bufio" + "fmt" + "gotorrent/internal/bencode" + "net/http" + "time" +) + +func populateInitialTrackerRequestParams(tr *TrackerRequest, mi *MetaInfo, client *Client) { + fmt.Println("[LOG] Populating TrackerRequest...") + tr.SetInfoHash(mi) + tr.SetPeerId(string(client.peerId[:])) + tr.SetPort(int64(client.port)) + tr.SetUploaded(int64(0)) + tr.SetDownloaded(int64(0)) + tr.SetLeft(mi.length) + tr.SetCompact(1) + tr.SetNoPeerId(int64(1)) + tr.SetEvent(STARTED) +} + +func sendTrackerRequest(trackerRequestURL *string) *TrackerResponse { + + trackerResponse := NewTrackerResponse() + + client := &http.Client{ + Timeout: 5 * time.Second, + } + req, err := http.NewRequest(http.MethodGet, *trackerRequestURL, nil) + if err != nil { + fmt.Println(err.Error()) + } + + fmt.Println("[LOG] Sending TrackerRequest...") + fmt.Println("TRACKER REQUEST URL : ", *trackerRequestURL) + resp, err := client.Do(req) + if err != nil { + fmt.Println(err.Error()) + } + + if resp.StatusCode != http.StatusOK { + fmt.Println("Error tracker server return : ", resp.Body) + } + + fmt.Println("STATUS:", resp.Status) + + buf := bufio.NewReader(resp.Body) + + decodedResponse, err := bencode.Decode(buf) + if err != nil { + panic(err) + } + + fmt.Println(decodedResponse) + + return trackerResponse +} + +func StartTorrent(metaInfoFilePath string, destinationFilePath string) { + + fmt.Println("[LOG] Starting Torrent ...") + + fmt.Println("[LOG] Parsing MetaInfo") + metaInfo, err := CreateMetaInfoFromFile(metaInfoFilePath) + if err != nil { + panic(err) + } + + client := NewClient() + trackerRequest := NewTrackerRequest() + + populateInitialTrackerRequestParams(trackerRequest, metaInfo, client) + + fmt.Printf("INFO RAW BYTES : % x\n", metaInfo.GetRawInfo()) + fmt.Printf("INFO HASH string: %q\n", metaInfo.GetInfoHash()) + fmt.Printf("INFO HASH bytes: % x\n", trackerRequest.infoHash) + fmt.Printf("INFO HASH string: %q\n", trackerRequest.infoHash) + fmt.Println("PORT:", client.port) + + trackerRequestURL, err := trackerRequest.GetURLEncodedRequestString(metaInfo) + if err != nil { + panic(err) + } + + sendTrackerRequest(&trackerRequestURL) + +} diff --git a/internal/torrent/trackerRequest.go b/internal/torrent/trackerRequest.go index 90000c2..cf9f74c 100644 --- a/internal/torrent/trackerRequest.go +++ b/internal/torrent/trackerRequest.go @@ -25,9 +25,7 @@ package torrent import ( - "crypto/sha1" "fmt" - bencode "gotorrent/internal/bencode" urlencoder "gotorrent/internal/url-encoder" "strings" ) @@ -48,11 +46,11 @@ type TrackerRequest struct { downloaded int64 left int64 compact int64 // 1 or 0 - noPeerId int64 + noPeerId int64 // Indicates that the tracker can omit peer id field in peers dictionary. This option is ignored if compact is enabled. event *Event } -func CreateNewTrackerRequest() *TrackerRequest { +func NewTrackerRequest() *TrackerRequest { return &TrackerRequest{ infoHash: "", peerId: "", @@ -66,23 +64,8 @@ func CreateNewTrackerRequest() *TrackerRequest { } func (tr *TrackerRequest) SetInfoHash(mi *MetaInfo) *TrackerRequest { - infoDict, err := mi.GetInfoDict() - if err != nil { - return nil - } - - encoder := bencode.CreateNewEncoder() - encoder.Encode(infoDict) - infoBencoded := encoder.Bytes() - - infoHashed := sha1.Sum(infoBencoded) - - urlencoder := urlencoder.NewURLEncoder() - urlencoder.EncodeBytes(infoHashed[:]) - - infoURLEncoded := urlencoder.String() - tr.infoHash = string(infoURLEncoded) - + infoHash := mi.GetInfoHash() + tr.infoHash = string(infoHash[:]) return tr } @@ -138,9 +121,12 @@ func (tr *TrackerRequest) GetURLEncodedRequestString(mi *MetaInfo) (string, erro if err != nil { return "", nil } + encoder := urlencoder.NewURLEncoder() + encoder.EncodeBytes([]byte(tr.infoHash)) URLString.WriteString(baseURL) - fmt.Fprintf(&URLString, "?info_hash=%s", tr.infoHash) + // fmt.Fprintf(&URLString, "?info_hash=%s", tr.infoHash) + fmt.Fprintf(&URLString, "?info_hash=%s", encoder.String()) fmt.Fprintf(&URLString, "&peer_id=%s", tr.peerId) fmt.Fprintf(&URLString, "&port=%d", tr.port) fmt.Fprintf(&URLString, "&uploaded=%d", tr.uploaded) diff --git a/internal/torrent/trackerRequest_test.go b/internal/torrent/trackerRequest_test.go index 965db1f..6afe74a 100644 --- a/internal/torrent/trackerRequest_test.go +++ b/internal/torrent/trackerRequest_test.go @@ -48,7 +48,7 @@ func TestGetURLEncodedRequestString(t *testing.T) { noPeerId := 1 event := STARTED - trackerRequest := CreateNewTrackerRequest(). + trackerRequest := NewTrackerRequest(). SetInfoHash(metaInfo). SetPeerId(peerId). SetPort(int64(port)). diff --git a/internal/torrent/trackerResponse.go b/internal/torrent/trackerResponse.go index c2e735d..3fc6d7e 100644 --- a/internal/torrent/trackerResponse.go +++ b/internal/torrent/trackerResponse.go @@ -40,6 +40,16 @@ type TrackerResponse struct { peers []PeerAddress } +func NewTrackerResponse() *TrackerResponse { + return &TrackerResponse{ + interval: 1, + trackerId: "", + complete: 0, + incomplete: 0, + peers: []PeerAddress{}, + } +} + func (tr *TrackerResponse) SetPeers(peers []PeerAddress) *TrackerResponse { tr.peers = peers return tr diff --git a/internal/url-encoder/urlencoder.go b/internal/url-encoder/urlencoder.go index 4a3e2cd..f6f397e 100644 --- a/internal/url-encoder/urlencoder.go +++ b/internal/url-encoder/urlencoder.go @@ -31,22 +31,7 @@ type URLEncoder struct { const hexTable = "0123456789ABCDEF" func (e *URLEncoder) EncodeString(s string) { - for i, _ := range s { - c := s[i] - if (c >= '0' && c <= '9') || - (c >= 'a' && c <= 'z') || - (c >= 'A' && c <= 'Z') || - c == '-' || - c == '_' || - c == '.' || - c == '~' { - e.WriteByte(c) - } else { - e.WriteByte('%') - e.WriteByte(hexTable[c>>4]) - e.WriteByte(hexTable[c&0x0f]) - } - } + e.EncodeBytes([]byte(s)) } func (e *URLEncoder) EncodeBytes(b []byte) { diff --git a/testdata/ubuntu-26.04.1-desktop-amd64.iso.torrent b/testdata/ubuntu-26.04.1-desktop-amd64.iso.torrent new file mode 100644 index 0000000..4a414bd Binary files /dev/null and b/testdata/ubuntu-26.04.1-desktop-amd64.iso.torrent differ