Skip to content

asminkarki012/bit-torrent-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Writing bit torrent client

Things I Learned

  • Working of bit torrent client
  • Managing UDP(dgram in node) and TCP(net.Socket in node) connection socket
  • Downloading and assembling files in block
  • big-endian byte-ordering system
  • Writing and parsing Bit torrent protocol buffers

Running program

npm i
 tsx src/index.ts <path-to-your-torrent-file>

for now default is "torrents/electronics.torrent".Other file might not work as they do not valid tracker url or have not handle other cases.

Workflow Diagram

graph TD
    A[".torrent file"] --> B["Parse using Bencode"]
    B --> C["Extract Metadata:<br/>• announce URL<br/>• piece length<br/>• pieces hash<br/>• file info<br/>• creation info"]
    C --> D["Calculate InfoHash<br/>(SHA-1 of info section)"]
    D --> E["Send UDP Connect Request<br/>to Tracker"]
    E --> F["Tracker sends back<br/>ConnectionId"]
    F --> G["Send UDP Announce Request<br/>(with ConnectionId + InfoHash)"]
    G --> H["Tracker responds with<br/>Peer List (IP:Port)"]
    H --> I["Connect to Peers via TCP"]
    I --> J["Handshake with Peers<br/>(exchange InfoHash)"]
    J --> K["Exchange Bitfield/Have messages<br/>(who has which pieces)"]
    K --> L["Request piece blocks<br/>from available peers"]
    L --> M["Receive piece data"]
    M --> N["Verify piece hash<br/>(against torrent metadata)"]
    N --> O{"Hash Valid?"}
    O -->|Yes| P["Write piece to file"]
    O -->|No| Q["Discard piece,<br/>request again"]
    Q --> L
    P --> R{"All pieces<br/>downloaded?"}
    R -->|No| L
    R -->|Yes| S["File complete"]
    
    style A fill:#e1f5fe
    style S fill:#c8e6c9
    style O fill:#fff3e0
    style R fill:#fff3e0
Loading

Enhancement Ideas

  • Support announce-list ie multiple tracker options
  • pausing and resuming the downloads
  • Reconnect when connection is dropped
  • implement distributed hash tables
  • support magnet link
  • support upload files
  • making simple ui for it
  • multiple torrent download

References

About

Building your own bit torrent client

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors