diff options
| author | codecrafters-bot <hello@codecrafters.io> | 2024-06-03 16:03:29 +0000 |
|---|---|---|
| committer | codecrafters-bot <hello@codecrafters.io> | 2024-06-03 16:03:29 +0000 |
| commit | afe50dd881ca58c6331cc84b151df9bafd9e82e2 (patch) | |
| tree | 82d841a151c33544bed4b61d763b813955775649 | |
| download | bittorrent-go-afe50dd881ca58c6331cc84b151df9bafd9e82e2.tar.gz bittorrent-go-afe50dd881ca58c6331cc84b151df9bafd9e82e2.zip | |
init [skip ci]
| -rw-r--r-- | .gitattributes | 2 | ||||
| -rw-r--r-- | README.md | 36 | ||||
| -rw-r--r-- | cmd/mybittorrent/main.go | 63 | ||||
| -rw-r--r-- | codecrafters.yml | 11 | ||||
| -rw-r--r-- | go.mod | 13 | ||||
| -rw-r--r-- | go.sum | 2 | ||||
| -rw-r--r-- | sample.torrent | 1 | ||||
| -rwxr-xr-x | your_bittorrent.sh | 15 |
8 files changed, 143 insertions, 0 deletions
diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..5b0130c --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +* text=auto +*.torrent binary
\ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..c625865 --- /dev/null +++ b/README.md @@ -0,0 +1,36 @@ +[](https://app.codecrafters.io/users/codecrafters-bot?r=2qF) + +This is a starting point for Go solutions to the +["Build Your Own BitTorrent" Challenge](https://app.codecrafters.io/courses/bittorrent/overview). + +In this challenge, you’ll build a BitTorrent client that's capable of parsing a +.torrent file and downloading a file from a peer. Along the way, we’ll learn +about how torrent files are structured, HTTP trackers, BitTorrent’s Peer +Protocol, pipelining and more. + +**Note**: If you're viewing this repo on GitHub, head over to +[codecrafters.io](https://codecrafters.io) to try the challenge. + +# Passing the first stage + +The entry point for your BitTorrent implementation is in +`cmd/mybittorrent/main.go`. Study and uncomment the relevant code, and push your +changes to pass the first stage: + +```sh +git add . +git commit -m "pass 1st stage" # any msg +git push origin master +``` + +Time to move on to the next stage! + +# Stage 2 & beyond + +Note: This section is for stages 2 and beyond. + +1. Ensure you have `go (1.19)` installed locally +1. Run `./your_bittorrent.sh` to run your program, which is implemented in + `cmd/mybittorrent/main.go`. +1. Commit your changes and run `git push origin master` to submit your solution + to CodeCrafters. Test output will be streamed to your terminal. diff --git a/cmd/mybittorrent/main.go b/cmd/mybittorrent/main.go new file mode 100644 index 0000000..ba70193 --- /dev/null +++ b/cmd/mybittorrent/main.go @@ -0,0 +1,63 @@ +package main + +import ( + // Uncomment this line to pass the first stage + // "encoding/json" + "fmt" + "os" + "strconv" + "unicode" + // bencode "github.com/jackpal/bencode-go" // Available if you need it! +) + +// Example: +// - 5:hello -> hello +// - 10:hello12345 -> hello12345 +func decodeBencode(bencodedString string) (interface{}, error) { + if unicode.IsDigit(rune(bencodedString[0])) { + var firstColonIndex int + + for i := 0; i < len(bencodedString); i++ { + if bencodedString[i] == ':' { + firstColonIndex = i + break + } + } + + lengthStr := bencodedString[:firstColonIndex] + + length, err := strconv.Atoi(lengthStr) + if err != nil { + return "", err + } + + return bencodedString[firstColonIndex+1 : firstColonIndex+1+length], nil + } else { + return "", fmt.Errorf("Only strings are supported at the moment") + } +} + +func main() { + // You can use print statements as follows for debugging, they'll be visible when running tests. + fmt.Println("Logs from your program will appear here!") + + command := os.Args[1] + + if command == "decode" { + // Uncomment this block to pass the first stage + // + // bencodedValue := os.Args[2] + // + // decoded, err := decodeBencode(bencodedValue) + // if err != nil { + // fmt.Println(err) + // return + // } + // + // jsonOutput, _ := json.Marshal(decoded) + // fmt.Println(string(jsonOutput)) + } else { + fmt.Println("Unknown command: " + command) + os.Exit(1) + } +} diff --git a/codecrafters.yml b/codecrafters.yml new file mode 100644 index 0000000..77886cf --- /dev/null +++ b/codecrafters.yml @@ -0,0 +1,11 @@ +# Set this to true if you want debug logs. +# +# These can be VERY verbose, so we suggest turning them off +# unless you really need them. +debug: false + +# Use this to change the Go version used to run your code +# on Codecrafters. +# +# Available versions: go-1.22 +language_pack: go-1.22 @@ -0,0 +1,13 @@ +// DON'T EDIT THIS! +// +// Codecrafters relies on this file being intact to run tests successfully. Any changes +// here will not reflect when CodeCrafters tests your code, and might even cause build +// failures. +// +// DON'T EDIT THIS! + +module github.com/codecrafters-io/bittorrent-starter-go + +go 1.22 + +require github.com/jackpal/bencode-go v1.0.0 // indirect @@ -0,0 +1,2 @@ +github.com/jackpal/bencode-go v1.0.0 h1:lzbSPPqqSfWQnqVNe/BBY1NXdDpncArxShL10+fmFus= +github.com/jackpal/bencode-go v1.0.0/go.mod h1:5FSBQ74yhCl5oQ+QxRPYzWMONFnxbL68/23eezsBI5c= diff --git a/sample.torrent b/sample.torrent new file mode 100644 index 0000000..44d1411 --- /dev/null +++ b/sample.torrent @@ -0,0 +1 @@ +d8:announce55:http://bittorrent-test-tracker.codecrafters.io/announce10:created by13:mktorrent 1.14:infod6:lengthi92063e4:name10:sample.txt12:piece lengthi32768e6:pieces60:vz*kg&-n"uvfVsnR5
z r'ee
\ No newline at end of file diff --git a/your_bittorrent.sh b/your_bittorrent.sh new file mode 100755 index 0000000..60315cc --- /dev/null +++ b/your_bittorrent.sh @@ -0,0 +1,15 @@ +#!/bin/sh +# +# DON'T EDIT THIS! +# +# CodeCrafters uses this file to test your code. Don't make any changes here! +# +# DON'T EDIT THIS! +set -e + +tmpFile=$(mktemp) + +( cd $(dirname "$0") && + go build -o "$tmpFile" ./cmd/mybittorrent ) + +exec "$tmpFile" "$@" |