Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions main.go → cmd/gotorrent/main.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
/*
* Copyright (c) 2026 git-sudo-404 <https://github.com/git-sudo-404/GoTorrent.git>
* 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() {}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
20 changes: 11 additions & 9 deletions torrent/client.go → internal/torrent/client.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
/*
* Copyright (c) 2026 git-sudo-404 <https://github.com/git-sudo-404/GoTorrent.git>
* 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
Expand Down
53 changes: 53 additions & 0 deletions internal/torrent/helper.go
Original file line number Diff line number Diff line change
@@ -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
}
}
186 changes: 11 additions & 175 deletions torrent/metainfo.go → internal/torrent/metainfo.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
/*
* Copyright (c) 2026 git-sudo-404 <https://github.com/git-sudo-404/GoTorrent.git>
* 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:
Expand All @@ -30,8 +32,6 @@ package torrent

import (
"fmt"
"gotorrent/bencode"
"os"
)

//TODO:
Expand Down Expand Up @@ -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
}
Loading
Loading