diff --git a/main.go b/cmd/gotorrent/main.go similarity index 64% rename from main.go rename to cmd/gotorrent/main.go index 1eff534..c19ad1f 100644 --- a/main.go +++ b/cmd/gotorrent/main.go @@ -1,27 +1,27 @@ /* - * 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 main - -func main() { +package gotorrent -} +func main() {} diff --git a/bencode/decoder.go b/internal/bencode/decoder.go similarity index 100% rename from bencode/decoder.go rename to internal/bencode/decoder.go diff --git a/bencode/decoder_test.go b/internal/bencode/decoder_test.go similarity index 100% rename from bencode/decoder_test.go rename to internal/bencode/decoder_test.go diff --git a/bencode/encoder.go b/internal/bencode/encoder.go similarity index 100% rename from bencode/encoder.go rename to internal/bencode/encoder.go diff --git a/bencode/encoder_test.go b/internal/bencode/encoder_test.go similarity index 100% rename from bencode/encoder_test.go rename to internal/bencode/encoder_test.go diff --git a/torrent/client.go b/internal/torrent/client.go similarity index 77% rename from torrent/client.go rename to internal/torrent/client.go index 25d4dd8..fb08cf2 100644 --- a/torrent/client.go +++ b/internal/torrent/client.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 torrent diff --git a/internal/torrent/helper.go b/internal/torrent/helper.go new file mode 100644 index 0000000..57d0698 --- /dev/null +++ b/internal/torrent/helper.go @@ -0,0 +1,53 @@ +/* + * 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 "fmt" + +func checkFieldPresenceAndType[T any](dict map[string]any, field string) (T, error) { + var zero T + value, ok := dict[field] + if !ok { + return zero, fmt.Errorf("Meta-Info missing needed field : %s", field) + } + if v, ok := value.(T); !ok { + return zero, fmt.Errorf("Undesired field value type , Field : %s ", field) + } else { + return v, nil + } +} + +func checkOptionalFieldPresenceAndType[T any](dict map[string]any, field string) (T, bool, error) { + var zero T + value, ok := dict[field] + if !ok { + return zero, false, nil + } + if v, ok := value.(T); !ok { + return zero, false, fmt.Errorf("Undesired field value type , Field : %s ", field) + } else { + return v, true, nil + } +} diff --git a/torrent/metainfo.go b/internal/torrent/metainfo.go similarity index 58% rename from torrent/metainfo.go rename to internal/torrent/metainfo.go index 605fb42..5392aae 100644 --- a/torrent/metainfo.go +++ b/internal/torrent/metainfo.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. */ //NOTE: @@ -30,8 +32,6 @@ package torrent import ( "fmt" - "gotorrent/bencode" - "os" ) //TODO: @@ -225,167 +225,3 @@ func (mi *MetaInfo) GetInfoDict() (map[string]any, error) { return info, nil } - -func checkFieldPresenceAndType[T any](metaInfoDict map[string]any, field string) (T, error) { - var zero T - value, ok := metaInfoDict[field] - if !ok { - return zero, fmt.Errorf("Meta-Info missing needed field : %s", field) - } - if v, ok := value.(T); !ok { - return zero, fmt.Errorf("Undesired field value type , Field : %s ", field) - } else { - return v, nil - } -} - -func checkOptionalFieldPresenceAndType[T any](metaInfoDict map[string]any, field string) (T, bool, error) { - var zero T - value, ok := metaInfoDict[field] - if !ok { - return zero, false, nil - } - if v, ok := value.(T); !ok { - return zero, false, fmt.Errorf("Undesired field value type , Field : %s ", field) - } else { - return v, true, nil - } -} - -// since announce-list is optional this function returns an err if the announce list key is not found in the metaInfoDict -func checkAnnounceListPresenceAndType(metaInfoDict map[string]any) ([][]string, bool, error) { - - outerVal, ok := metaInfoDict["announce-list"] - if !ok { - return nil, false, nil - } - - outerList, ok := outerVal.([]any) - if !ok { - return nil, false, fmt.Errorf("announce-list type does not match the required type") - } - - var result [][]string - - for _, innerVal := range outerList { - innerList, ok := innerVal.([]any) - if !ok { - return nil, false, fmt.Errorf("announce-list type does not meet the required type") - } - var innerResult []string - for _, items := range innerList { - item, ok := items.(string) - if !ok { - return nil, false, fmt.Errorf("announce-list type does not meet the required type") - } - innerResult = append(innerResult, item) - } - result = append(result, innerResult) - } - - return result, true, nil -} - -func CreateMetaInfoFromFile(filePath string) (*MetaInfo, error) { - metaInfo := NewMetaInfo() - - metaInfoFile, err := os.Open(filePath) - if err != nil { - return nil, fmt.Errorf("Error opening the meta-info file") - } - defer metaInfoFile.Close() - - metaInfoDict, err := bencode.Decode(metaInfoFile) - if err != nil { - return nil, fmt.Errorf("Error Decoding the meta-info file") - } - - //NOTE:Standard .torrent files use space-separated keys - infoRaw, ok := metaInfoDict["info"] - if !ok { - panic(fmt.Errorf("info not in decoded meta-info file")) - } - info, ok := infoRaw.(map[string]any) - if !ok { - panic(fmt.Errorf("info not of desired type : map[string]any")) - } - pieceLength, err := checkFieldPresenceAndType[int64](info, "piece length") - if err != nil { - return nil, err - } - metaInfo.SetPieceLength(pieceLength) - - pieces, err := checkFieldPresenceAndType[string](info, "pieces") - if err != nil { - return nil, err - } - metaInfo.SetPieces(pieces) - - private, present, err := checkOptionalFieldPresenceAndType[int64](info, "private") - if err != nil { - return nil, err - } - if present { - metaInfo.SetPrivate(private) - } - - name, err := checkFieldPresenceAndType[string](info, "name") - if err != nil { - return nil, err - } - metaInfo.SetName(name) - - length, err := checkFieldPresenceAndType[int64](info, "length") - if err != nil { - return nil, err - } - metaInfo.SetLength(length) - - announce, err := checkFieldPresenceAndType[string](metaInfoDict, "announce") - if err != nil { - return nil, err - } - metaInfo.SetAnnounce(announce) - - announceList, present, err := checkAnnounceListPresenceAndType(metaInfoDict) - if err != nil { - return nil, err - } - if present { - metaInfo.SetAnnounceList(announceList) - } - - creationDate, present, err := checkOptionalFieldPresenceAndType[int64](metaInfoDict, "creation date") - if err != nil { - return nil, err - } - if present { - metaInfo.SetCreationDate(creationDate) - } - - comment, present, err := checkOptionalFieldPresenceAndType[string](metaInfoDict, "comment") - if err != nil { - return nil, err - } - if present { - metaInfo.SetComment(comment) - } - - createdBy, present, err := checkOptionalFieldPresenceAndType[string](metaInfoDict, "created by") - if err != nil { - return nil, err - } - if present { - metaInfo.SetCreatedBy(createdBy) - } - - encoding, present, err := checkOptionalFieldPresenceAndType[string](metaInfoDict, "encoding") - if err != nil { - return nil, err - } - if present { - metaInfo.SetEncoding(encoding) - } - - return metaInfo, nil -} diff --git a/internal/torrent/metainfoParser.go b/internal/torrent/metainfoParser.go new file mode 100644 index 0000000..3750690 --- /dev/null +++ b/internal/torrent/metainfoParser.go @@ -0,0 +1,169 @@ +/* + * 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 ( + "fmt" + bencode "gotorrent/internal/bencode" + "os" +) + +// since announce-list is optional this function returns an err if the announce list key is not found in the metaInfoDict +func checkAnnounceListPresenceAndType(metaInfoDict map[string]any) ([][]string, bool, error) { + + outerVal, ok := metaInfoDict["announce-list"] + if !ok { + return nil, false, nil + } + + outerList, ok := outerVal.([]any) + if !ok { + return nil, false, fmt.Errorf("announce-list type does not match the required type") + } + + var result [][]string + + for _, innerVal := range outerList { + innerList, ok := innerVal.([]any) + if !ok { + return nil, false, fmt.Errorf("announce-list type does not meet the required type") + } + var innerResult []string + for _, items := range innerList { + item, ok := items.(string) + if !ok { + return nil, false, fmt.Errorf("announce-list type does not meet the required type") + } + innerResult = append(innerResult, item) + } + result = append(result, innerResult) + } + + return result, true, nil +} + +func CreateMetaInfoFromFile(filePath string) (*MetaInfo, error) { + metaInfo := NewMetaInfo() + + metaInfoFile, err := os.Open(filePath) + if err != nil { + return nil, fmt.Errorf("Error opening the meta-info file") + } + defer metaInfoFile.Close() + + metaInfoDict, err := bencode.Decode(metaInfoFile) + if err != nil { + return nil, fmt.Errorf("Error Decoding the meta-info file") + } + + //NOTE:Standard .torrent files use space-separated keys + infoRaw, ok := metaInfoDict["info"] + if !ok { + panic(fmt.Errorf("info not in decoded meta-info file")) + } + info, ok := infoRaw.(map[string]any) + if !ok { + panic(fmt.Errorf("info not of desired type : map[string]any")) + } + pieceLength, err := checkFieldPresenceAndType[int64](info, "piece length") + if err != nil { + return nil, err + } + metaInfo.SetPieceLength(pieceLength) + + pieces, err := checkFieldPresenceAndType[string](info, "pieces") + if err != nil { + return nil, err + } + metaInfo.SetPieces(pieces) + + private, present, err := checkOptionalFieldPresenceAndType[int64](info, "private") + if err != nil { + return nil, err + } + if present { + metaInfo.SetPrivate(private) + } + + name, err := checkFieldPresenceAndType[string](info, "name") + if err != nil { + return nil, err + } + metaInfo.SetName(name) + + length, err := checkFieldPresenceAndType[int64](info, "length") + if err != nil { + return nil, err + } + metaInfo.SetLength(length) + + announce, err := checkFieldPresenceAndType[string](metaInfoDict, "announce") + if err != nil { + return nil, err + } + metaInfo.SetAnnounce(announce) + + announceList, present, err := checkAnnounceListPresenceAndType(metaInfoDict) + if err != nil { + return nil, err + } + if present { + metaInfo.SetAnnounceList(announceList) + } + + creationDate, present, err := checkOptionalFieldPresenceAndType[int64](metaInfoDict, "creation date") + if err != nil { + return nil, err + } + if present { + metaInfo.SetCreationDate(creationDate) + } + + comment, present, err := checkOptionalFieldPresenceAndType[string](metaInfoDict, "comment") + if err != nil { + return nil, err + } + if present { + metaInfo.SetComment(comment) + } + + createdBy, present, err := checkOptionalFieldPresenceAndType[string](metaInfoDict, "created by") + if err != nil { + return nil, err + } + if present { + metaInfo.SetCreatedBy(createdBy) + } + + encoding, present, err := checkOptionalFieldPresenceAndType[string](metaInfoDict, "encoding") + if err != nil { + return nil, err + } + if present { + metaInfo.SetEncoding(encoding) + } + + return metaInfo, nil +} diff --git a/torrent/metainfo_test.go b/internal/torrent/metainfoParser_test.go similarity index 95% rename from torrent/metainfo_test.go rename to internal/torrent/metainfoParser_test.go index b983438..39a3c60 100644 --- a/torrent/metainfo_test.go +++ b/internal/torrent/metainfoParser_test.go @@ -1,30 +1,32 @@ /* - * 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 torrent import ( "bufio" - "gotorrent/bencode" + "gotorrent/internal/bencode" "os" "path/filepath" "testing" diff --git a/torrent/trackerRequest.go b/internal/torrent/trackerRequest.go similarity index 88% rename from torrent/trackerRequest.go rename to internal/torrent/trackerRequest.go index 3dee626..90000c2 100644 --- a/torrent/trackerRequest.go +++ b/internal/torrent/trackerRequest.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 torrent @@ -25,8 +27,8 @@ package torrent import ( "crypto/sha1" "fmt" - "gotorrent/bencode" - urlencoder "gotorrent/url-encoder" + bencode "gotorrent/internal/bencode" + urlencoder "gotorrent/internal/url-encoder" "strings" ) diff --git a/torrent/trackerRequest_test.go b/internal/torrent/trackerRequest_test.go similarity index 83% rename from torrent/trackerRequest_test.go rename to internal/torrent/trackerRequest_test.go index 84f3211..965db1f 100644 --- a/torrent/trackerRequest_test.go +++ b/internal/torrent/trackerRequest_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 torrent @@ -25,8 +27,8 @@ package torrent import ( "crypto/sha1" "fmt" - "gotorrent/bencode" - urlencoder "gotorrent/url-encoder" + "gotorrent/internal/bencode" + urlencoder "gotorrent/internal/url-encoder" "testing" ) diff --git a/internal/torrent/trackerResponse.go b/internal/torrent/trackerResponse.go new file mode 100644 index 0000000..51c570f --- /dev/null +++ b/internal/torrent/trackerResponse.go @@ -0,0 +1,71 @@ +/* + * 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 "net" + +type Peer struct { // no peerId when in compact mode = 1 + ip net.IP // peer's IP address either IPv6 (hexed) or IPv4 (dotted quad) or DNS name (string) + port int64 // peer's port number (integer) +} + +type TrackerResponse struct { + interval int64 // Interval in seconds that the client should wait between sending regular requests to the tracker + minInterval *int64 // (optional) Minimum announce interval. If present clients must not reannounce more frequently than this. + trackerId string // A string that the client should send back on its next announcements. If absent and a previous announce sent a tracker id, do not discard the old value; keep using it. + complete int64 // number of peers with the entire file, i.e. seeders (integer) + incomplete int64 // number of non-seeder peers, aka "leechers" (integer) + peers []Peer +} + +func (tr *TrackerResponse) SetPeers(peers []Peer) *TrackerResponse { + tr.peers = peers + return tr +} + +func (tr *TrackerResponse) SetInterval(interval int64) *TrackerResponse { + tr.interval = interval + return tr +} + +func (tr *TrackerResponse) SetMinInterval(minInterval int64) *TrackerResponse { + tr.minInterval = &minInterval + return tr +} + +func (tr *TrackerResponse) SetTrackerId(trackerId string) *TrackerResponse { + tr.trackerId = trackerId + return tr +} + +func (tr *TrackerResponse) SetComplete(complete int64) *TrackerResponse { + tr.complete = complete + return tr +} + +func (tr *TrackerResponse) SetIncomplete(incomplete int64) *TrackerResponse { + tr.incomplete = incomplete + return tr +} diff --git a/internal/torrent/trackerResponseParser.go b/internal/torrent/trackerResponseParser.go new file mode 100644 index 0000000..f860509 --- /dev/null +++ b/internal/torrent/trackerResponseParser.go @@ -0,0 +1,121 @@ +/* + * 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 ( + "encoding/binary" + "fmt" + "gotorrent/internal/bencode" + "io" + "net" +) + +func parsePeersCompactString(peersCompactString string) ([]Peer, error) { + // first 4-bytes -> IP + // next 2-bytes -> port + + data := []byte(peersCompactString) + dataLen := len(data) + if dataLen%6 != 0 { + return nil, fmt.Errorf("The peers compact string length must be a multiple of 6 , but got of length : %d", dataLen) + } + + numberOfPeers := dataLen / 6 + peers := make([]Peer, numberOfPeers) + + for i := 0; i < numberOfPeers; i++ { + offset := i * 6 + + ip := make(net.IP, 4) + copy(ip, data[offset:offset+4]) + + port := int64(binary.BigEndian.Uint16(data[offset+4 : offset+6])) + + peers[i] = Peer{ + ip: ip, + port: port, + } + } + + return peers, nil +} + +func ParseTrackerResponse(r io.Reader) (*TrackerResponse, error) { + + trackerResponse := &TrackerResponse{} + + decodedResponseDict, err := bencode.Decode(r) + if err != nil { + return nil, err + } + if _, exists := decodedResponseDict["failure reason"]; exists { + return nil, fmt.Errorf("Tracker Response Failed") + } + + interval, err := checkFieldPresenceAndType[int64](decodedResponseDict, "interval") + if err != nil { + return nil, err + } + trackerResponse.SetInterval(interval) + + minInterval, minIntervalPresent, err := checkOptionalFieldPresenceAndType[int64](decodedResponseDict, "min interval") + if err != nil { + return nil, err + } + if minIntervalPresent { + trackerResponse.SetMinInterval(minInterval) + } + + trackerId, err := checkFieldPresenceAndType[string](decodedResponseDict, "tracker id") + if err != nil { + return nil, err + } + trackerResponse.SetTrackerId(trackerId) + + complete, err := checkFieldPresenceAndType[int64](decodedResponseDict, "complete") + if err != nil { + return nil, err + } + trackerResponse.SetComplete(complete) + + incomplete, err := checkFieldPresenceAndType[int64](decodedResponseDict, "incomplete") + if err != nil { + return nil, err + } + trackerResponse.SetIncomplete(incomplete) + + peersCompactString, err := checkFieldPresenceAndType[string](decodedResponseDict, "peers") + if err != nil { + return nil, err + } + + peers, err := parsePeersCompactString(peersCompactString) + if err != nil { + return nil, err + } + trackerResponse.SetPeers(peers) + + return trackerResponse, nil +} diff --git a/torrent/trackerResponse.go b/internal/torrent/trackerResponseParser_test.go similarity index 64% rename from torrent/trackerResponse.go rename to internal/torrent/trackerResponseParser_test.go index 77bf4a8..f1cd3b5 100644 --- a/torrent/trackerResponse.go +++ b/internal/torrent/trackerResponseParser_test.go @@ -1,26 +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 torrent - -type TrackerResponse struct { -} diff --git a/url-encoder/urlencoder.go b/internal/url-encoder/urlencoder.go similarity index 100% rename from url-encoder/urlencoder.go rename to internal/url-encoder/urlencoder.go diff --git a/url-encoder/urlencoder_test.go b/internal/url-encoder/urlencoder_test.go similarity index 100% rename from url-encoder/urlencoder_test.go rename to internal/url-encoder/urlencoder_test.go