-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
195 lines (164 loc) · 5.77 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"crypto/rand"
"fmt"
"github.com/Danitilahun/Bit_Torrent/bitfield"
"github.com/Danitilahun/Bit_Torrent/common"
"github.com/Danitilahun/Bit_Torrent/download"
"github.com/Danitilahun/Bit_Torrent/fileUtils"
messageutils "github.com/Danitilahun/Bit_Torrent/messageUtils"
"github.com/Danitilahun/Bit_Torrent/peer"
"github.com/Danitilahun/Bit_Torrent/peerinteraction"
"github.com/Danitilahun/Bit_Torrent/piecehandler"
"github.com/Danitilahun/Bit_Torrent/seed"
"github.com/Danitilahun/Bit_Torrent/torrentmodels"
"github.com/Danitilahun/Bit_Torrent/tracker"
"log"
mrand "math/rand"
"net"
"time"
)
func main() {
manifest := fileUtils.ReadManifestFromFile("debian-11.6.0-amd64-netinst.iso.torrent")
// Create files
blobFile := fileUtils.LoadOrCreateDownloadBlob(&manifest)
memCopy := make([]byte, manifest.TotalLength)
blobFile.ReadAt(memCopy, 0)
// Load progress from persistent storage
currentBitField, bitfieldFile := bitfield.LoadOrCreateBitFieldFromFile(&manifest)
totalDownloaded := countDownloadedPieces(currentBitField)
fmt.Println("Total downloaded", totalDownloaded)
// Get peers list
id := [20]byte{}
rand.Read(id[:])
peerAddresses, err := tracker.GetPeersList(manifest, id, common.Port)
if err != nil {
fmt.Println("Can't get peers", err)
panic(err)
}
// Create an empty peerAddresses slice
// peerAddresses := []peer.PeerAddress{}
// Append the new address
// newAddress := peer.PeerAddress{IP: net.ParseIP("192.168.137.13"), Port: 6881}
// peerAddresses = append(peerAddresses, newAddress)
// Print the slice containing only the new address
fmt.Println(peerAddresses)
// channels
workChannel := make(chan download.PieceJob, len(manifest.PieceHashes))
pieceJobResultChannel := make(chan *download.PieceJobResult)
seedRequestChannel := make(chan *seed.SeedRequest)
// Call the function to create work for each piece
createWorkForPieces(&manifest, currentBitField, &workChannel)
// create common structure for leecher and seeder
peers := make([]*peer.Peer, len(peerAddresses))
for i, peerAddress := range peerAddresses {
go peerinteraction.StartPeerWorker(peers, i, peerAddress, id, manifest, common.Port, &workChannel, currentBitField, &pieceJobResultChannel, &seedRequestChannel, nil)
}
// Listen for seeding requests
go func() {
for {
seedRequest := <-seedRequestChannel
// handle seeding request
go seed.HandleSeedingRequest(seedRequest, memCopy, currentBitField, &manifest)
}
}()
// Start the seeding server in a separate goroutine
go startSeedingServer(&peers, id, manifest, common.Port, &workChannel, currentBitField, &pieceJobResultChannel, &seedRequestChannel)
// Optimistic Unchoking
go optimisticUnchoking(peers)
// process results
for {
pieceJobResult := <-pieceJobResultChannel
if pieceJobResult == nil {
continue
}
copy(memCopy[pieceJobResult.PieceIndex*int(manifest.PieceLength):], pieceJobResult.PieceData)
// write piece to file
piecehandler.WritePieceToFile(&manifest, pieceJobResult, blobFile)
// update bitfield
currentBitField.MarkPiece(pieceJobResult.PieceIndex)
currentBitField.WriteToFile(&manifest, bitfieldFile)
// update progress
totalDownloaded++
fmt.Printf("Downloaded %v/%v pieces\n", totalDownloaded, len(manifest.PieceHashes))
// send have message to all peers
for _, peer := range peers {
if peer != nil {
messageutils.SendHaveMessage(peer, pieceJobResult.PieceIndex)
}
}
// check if download is finished
if totalDownloaded == len(manifest.PieceHashes) {
fmt.Println("Download finished")
fileUtils.WriteBlobToFiles(&manifest)
}
}
}
func optimisticUnchoking(peers []*peer.Peer) {
for {
if len(peers) != 0 {
// unchoke random peer
peerIndex := mrand.Intn(len(peers))
if peers[peerIndex] != nil {
if peers[peerIndex].IsChoked {
peers[peerIndex].IsChoked = false
go messageutils.SendUnchokeMessage(peers[peerIndex])
}
}
}
time.Sleep(31 * time.Second)
}
}
func startSeedingServer(peers *[]*peer.Peer, id [20]byte, manifest torrentmodels.TorrentManifest, port int, workChannel *chan download.PieceJob, currentBitField *bitfield.Bitfield, pieceJobResultChannel *chan *download.PieceJobResult, seedRequestChannel *chan *seed.SeedRequest) {
ListenAddr := ":" + fmt.Sprint(port)
listener, err := net.Listen("tcp", ListenAddr)
if err != nil {
log.Fatal(err)
}
defer listener.Close()
log.Printf("Listening on %s...\n", ListenAddr)
for {
conn, err := listener.Accept()
if err != nil {
log.Println(err)
continue
}
peerInstance := peer.Peer{
Address: peer.PeerAddress{
IP: conn.RemoteAddr().(*net.TCPAddr).IP,
Port: uint16(port),
},
Conn: conn,
Interested: false,
IsChoked: true,
IsChoking: false,
BitField: make([]byte, len(manifest.PieceHashes)),
}
*peers = append(*peers, &peerInstance)
go peerinteraction.StartPeerWorker(*peers, len(*peers)-1, peerInstance.Address, id, manifest, port, workChannel, currentBitField, pieceJobResultChannel, seedRequestChannel, &conn)
}
}
// createWorkForPieces generates download jobs for each piece in the manifest that hasn't been downloaded yet.
func createWorkForPieces(manifest *torrentmodels.TorrentManifest, currentBitField *bitfield.Bitfield, workChannel *chan download.PieceJob) {
for index, hash := range manifest.PieceHashes {
// Ignore already downloaded pieces
if !currentBitField.HasPiece(index) {
*workChannel <- download.PieceJob{
PieceIndex: index,
PieceHash: hash,
PieceLength: piecehandler.GetPieceLength(index, int(manifest.PieceLength), int(manifest.TotalLength)),
}
}
}
}
func countDownloadedPieces(bitField *bitfield.Bitfield) int {
totalDownloaded := 0
for _, piece := range *bitField {
for i := 0; i < 8; i++ {
if piece&(1<<uint(i)) != 0 {
totalDownloaded++
}
}
}
return totalDownloaded
}