स्क्रैच से सीधे इम्प्लीमेंट किया गया BitTorrent client
(github.com/piyushgupta53)- यह open source प्रोजेक्ट Go भाषा में इम्प्लीमेंट किया गया BitTorrent client है, जो फ़ाइल डाउनलोड की बुनियादी लॉजिक को खुद इम्प्लीमेंट करता है
- Bencode encoding/decoding को सीधे हैंडल करता है और मज़बूत error validation फीचर्स शामिल करता है
- .torrent फ़ाइल parsing, info hash calculation, peer-to-peer communication जैसी core functionalities को व्यापक रूप से सपोर्ट करता है
- concurrent download, file assembly, block-level storage management जैसी वास्तविक उपयोगिता बढ़ाने वाली सुविधाएँ शामिल हैं
- मौजूदा BitTorrent open source प्रोजेक्ट्स की तुलना में Go की सरलता, code structure की स्पष्टता, और modularization में इसकी मज़बूती है
अवलोकन
यह प्रोजेक्ट Go भाषा में सीधे BitTorrent client को इम्प्लीमेंट करता है
यह BitTorrent protocol का उपयोग करके फ़ाइल डाउनलोड functionality को self-developed रूप में प्रदान करता है
मुख्य रूप से यह torrent फ़ाइल parsing, peer discovery, file download सुविधाओं को संभालता है
मुख्य फीचर्स
-
Bencode encoding/decoding
- string, integer, list, dictionary सहित सभी Bencode types का समर्थन
- मज़बूत error handling और data validation लागू
-
torrent फ़ाइल प्रोसेसिंग
- single और multi-file torrent दोनों को parse कर सकता है
- info hash और प्रत्येक piece hash को extract करता है, तथा सभी standard fields का समर्थन करता है
-
peer discovery और communication
- HTTP tracker समर्थन
- peers के बीच handshake protocol इम्प्लीमेंट किया गया है
- BitTorrent message protocol और peer connection management को संभालता है
-
download फीचर्स
- piece और block-level management
- concurrent download processing
- download progress tracking और file assembly
- block-level storage management के ज़रिए बेहतर efficiency
प्रोजेक्ट संरचना
- cmd/ : command-line interface और executable files
- internal/
- bencode/ : Bencode encoding और decoding functionality
- torrent/ : torrent फ़ाइल parsing और processing
- tracker/ : tracker protocol implementation
- peer/ : peer-to-peer communication functionality
- download/ : download management functionality
- pkg/ : बाहर expose किए जा सकने वाले packages का संग्रह
आवश्यकताएँ
- Go 1.21 या उससे ऊपर आवश्यक
उपयोग
- फिलहाल प्रोजेक्ट शुरुआती development stage में है, इसलिए usage guide बाद में जोड़ी जाएगी
विकास स्थिति और योजनाएँ
- फ़िलहाल development सक्रिय रूप से जारी है
- विस्तृत development stages
checkpoint.mdफ़ाइल में दर्ज हैं - आगे की योजनाएँ:
- Magnet link समर्थन
- metadata exchange protocol
- DHT(distributed hash table) समर्थन प्रस्तावित है
संदर्भ दस्तावेज़
- BitTorrent protocol specification
- Bencode specification
प्रोजेक्ट का महत्व और मज़बूतियाँ
- यह प्रोजेक्ट Go भाषा की सरल syntax और concurrency का उपयोग करके BitTorrent client के जटिल हिस्सों को स्पष्ट रूप से modularize करके इम्प्लीमेंट करता है
- स्पष्ट संरचना के कारण scalability और maintainability के लिहाज़ से यह सीखने और वास्तविक उपयोग, दोनों में लाभ देता है
- तुलनात्मक रूप से शुरुआती चरण में होने के बावजूद इसने BitTorrent protocol की core functionalities को तेज़ी से साकार किया है
1 टिप्पणियां
Hacker News टिप्पणियाँ
यह सुझाव दिया गया कि bencode decoder में dynamic memory allocation size पर limit लगानी चाहिए। torrent file या announce से आने वाले input पर भरोसा नहीं किया जा सकता, इसलिए malicious input बहुत बड़े allocation request करके denial-of-service (DoS) करा सकता है। string parsing में उपयुक्त upper bound बची हुई input length रखी जा सकती है, क्योंकि सही torrent में file की remaining length से लंबी string नहीं हो सकती
प्रोजेक्ट साफ-सुथरा और simple लग रहा है, जो अच्छा है। Readme में एक one-liner usage example जोड़ना अच्छा रहेगा। उदाहरण के लिए
./go-torrent My-Linux-Distro-Wink-ISO.torrentजैसा usage दिखाने वाला वाक्य जोड़ने की सलाह। अगरtorrent.ParseFromUrlफीचर भी जोड़ दें तो और बेहतर। हर किसी के लिए अपनी “spiritual journey” के लिए ऐसा अनुभव करना सार्थक हैcodecrafters की तरफ से दिया जाने वाला इसी तरह का एक challenge परिचित कराया गया। इस course में process progression और test आदि में मदद मिलती है, और इसे एक महीने तक मुफ्त में आज़माया था; काफ़ी मज़ेदार था
https://app.codecrafters.io/courses/bittorrent/overview
Go developer न होने के नज़रिए से पूछा गया कि पुराना Go 1.21 version क्यों इस्तेमाल किया जा रहा है। क्या खास तौर पर पुराने version पर अड़े रहने की कोई वजह है? देखा तो 10 महीने पहले ही support बंद हो चुका था
इसकी वजह Windows 7 support है। Go 1.21.4 या उससे नीचे लिखे गए project 2009 के बाद के लगभग हर Windows और सभी कंप्यूटरों पर चलते हैं, लेकिन 1.21.5 या उससे ऊपर इस्तेमाल करने पर वे सिर्फ़ modern कंप्यूटरों और Windows 10, 11 पर चलते हैं और इसका कोई खास फ़ायदा नहीं है
https://github.com/golang/go/issues/64622
README शायद AI ने लिखा है। असली
go.modfile में Go version1.23.1दिया गया है, इसलिए नतीजे में1.23.1या उससे ऊपर चाहिएhttps://github.com/piyushgupta53/go-torrent-client/blob/6130f4e/go.mod#L3 https://go.dev/doc/modules/gomod-ref#go-notes
वाकई बहुत शानदार project है। कॉलेज के दिनों में Georgia Tech की networking class में इसे final project के रूप में किया था। कोड खो गया, लेकिन उससे सीखे हुए सबक ज़िंदगी भर साथ रहे। इस तरह के project नई language सीखने का बहुत अच्छा तरीका हैं
पूछा गया कि क्या magnet link support है।
Edit: पता चला कि यह आगे जोड़ा जाने वाला feature है
यह कैसे बनाया, इसे लेकर जिज्ञासा जताई गई। क्या protocol spec सीधे पढ़ी, या दूसरी implementations देखीं? ऐसी चीज़ें बिल्कुल शुरुआत से कैसे implement की जाती हैं, इसे लेकर हमेशा उत्सुकता रहती है
https://www.bittorrent.org/beps/bep_0003.html
अतिरिक्त extensions यहाँ हैं
https://www.bittorrent.org/beps/bep_0000.html
काम को बहुत छोटे-छोटे हिस्सों में बाँटकर हर step पर खुद result verify करना ज़रूरी है। उदाहरण के लिए मैंने
.torrentfile parsing से शुरुआत की, और bencoding खुद implement करना पड़ा। Arch Linux की.torrentfile डाउनलोड करके देखा तो वह गलत format में निकली, औरurl-listजैसी unexpected key मिली। यह क्या है, इसकी जाँच की तो पता चला कि यह bep_0019 से जुड़ी है। आखिरकार Debian Linux की.torrentfile को सफलतापूर्वक parse कर लियाउसके बाद tracker announce HTTP request और peer protocol भी implement किया। peer protocol काफ़ी कठिन था, और experimental approach ने बहुत मदद की। Debian torrent से announce URL हटा दिया ताकि peers बिल्कुल न मिलें, फिर KTorrent में अपने client को सीधे जोड़कर message exchange observe किया, और उसके आधार पर अपना code भी बदला। इसमें बहुत trial and error और debugging हुई
protocol के कुछ बारीक हिस्से official docs में बिल्कुल नहीं मिले, इसलिए कभी-कभी छोटी-मोटी बात ChatGPT से भी पूछी, और अलग-अलग clients की implementations थोड़ी अलग होने की वजह से detailed algorithms साफ़ नहीं हैं। कौन सा block लेना है, किस peer से connect करना है, choke/unchoke कैसे चलता है—ये चीज़ें ठीक से documented नहीं हैं। web search ने बहुत मदद की
इसके अलावा https://wiki.theory.org/Main_Page साइट पर भी उपयोगी जानकारी है
अभी मैं KTorrent के साथ पूरा download/upload कर सकने वाले stage तक पहुँच गया हूँ। आगे tracker से peers लेना, download करने वाले blocks चुनना, और उन्हें file में save करने वाला algorithm बनाना बाकी है
अगर और detailed process जाननी हो तो कभी भी पूछ सकते हैं
पूछा गया कि GUI जोड़ना कितना मुश्किल होगा। Go में GUI implementation के उदाहरण ज़्यादा नहीं दिखते
व्यक्तिगत रूप से मुझे ImGui wrapper https://github.com/AllenDang/giu पसंद है
सबसे feature-rich शायद unison है, लेकिन असल में इसका कितना व्यापक उपयोग होता है, पता नहीं, और docs बहुत कम लगते हैं https://github.com/richardwilkes/unison
Gio एक नए तरीके का GUI framework है, और Tailscale व gotraceui में इस्तेमाल होता है https://gioui.org
अगर web development का अनुभव हो तो Wails सीखना आसान है https://wails.io
GTK4 bindings भी ठीक लगती हैं https://github.com/diamondburned/gotk4
Cogent Core भी दिलचस्प लगता है, लेकिन मैंने इसे Go छोड़कर Odin language पर जाने से पहले बस थोड़ी देर इस्तेमाल किया था https://www.cogentcore.org/core
Fyne के साथ मुझे व्यक्तिगत रूप से कई कंप्यूटरों और OS पर performance problems हुईं, लेकिन फिर भी यही सबसे मशहूर GUI framework है https://fyne.io
ऐसे project में दिलचस्पी होने की वजह से खुद भी कभी कोशिश करने का सोचा था। यह कितना कठिन है, और इसे कितना complete मानते हैं, क्या DHT, Magnet, NAT traversal जैसी complex features भी implement की गई हैं—यह पूछा गया। यह भी साफ़ नहीं कि लगभग सभी torrents को cover करने के लिए कौन-कौन सी features अनिवार्य हैं। torrent से जुड़े protocols इतने ज़्यादा हैं कि पूरी list भी पता नहीं, और हर protocol क्या करता है, यह भी समझना मुश्किल है
कठिनाई का स्तर experience, language proficiency, और experimental mindset पर बहुत निर्भर करता है। उदाहरण के लिए मैंने भी पिछले हफ़्ते Go में Bittorrent client बनाना शुरू किया, और एक हफ़्ते में यहाँ पोस्ट किए गए project के लगभग 80% स्तर तक पहुँच गया। Go, protocols और networking की अच्छी समझ होने के कारण, और experiment करने की आदत की वजह से तेज़ी से प्रगति हुई
Bittorrent की official spec खुद बहुत छोटी है, इसलिए इसे करीब एक घंटे में पढ़कर समझा जा सकता है https://www.bittorrent.org/beps/bep_0003.html
लेकिन इसके आसपास बहुत सारे extension protocols हैं, और हर client का support level अलग है। उदाहरण के लिए कई client पहले protocol encryption (असल में obfuscation) की कोशिश करते हैं, और न होने पर plain protocol पर चले जाते हैं
अगर आप सिर्फ़ Linux distribution जैसी official
.torrentfiles से download करना चाहते हैं, तो कठिनाई निश्चित रूप से कम है। आमतौर पर file एक ही होती है, tracker मौजूद होता है, और ज़्यादातर peers standard protocol इस्तेमाल करते हैं, इसलिए NAT environment में भी बिना internal port खोले काफ़ी कुछ चल जाता हैबेशक अगर आप सामान्य, खासकर grey-area torrents चाहते हैं, तो magnet link parsing, peer discovery के लिए DHT, और automatic port mapping के लिए UPnP जैसी चीज़ें धीरे-धीरे जोड़नी पड़ेंगी। लेकिन इन्हें भी feature-by-feature चरणों में implement किया जा सकता है। जितनी ज़्यादा features होंगी, उतने ज़्यादा peers मिलेंगे और सफल exchange की संभावना बढ़ेगी
यह काफ़ी चुनौतीपूर्ण था, और protocol व bencoding तरीका सीखने, पूरे architecture को दिमाग़ में बैठाने, और फिर code लिखने में लगभग एक महीना लगा। Magnet और DHT का support अभी नहीं है
पूछा गया कि क्या v2 और mutable torrents implement किए गए हैं। साथ ही यह भी कहा गया कि mutable torrents ज़रूर implement किए जाने चाहिए