-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeer.js
More file actions
177 lines (166 loc) · 6.56 KB
/
peer.js
File metadata and controls
177 lines (166 loc) · 6.56 KB
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
// peer.js
// node js libraries
const WebSocket = require("ws");
const natpmp = require("nat-pmp");
const axios = require("axios");
// tracker ip and port #
const trackerUrl = "ws://<INSERTIPHERE>:<INSERTTRACKPORTHERE>";
// peer id is randomly generated, peer chunk data in title and data format
const peerId = Math.random().toString(36).substring(7);
const fileChunks = { "chunk<INSERTNUMBERHERE>": `This is the chunk data from peer ${peerId}.`};
// NAT-PMP client for port forwarding
const client = new natpmp.Client();
const externalPort = 4000;
// re-registration with tracker to prevent idle timeout, time in milliseconds
const reRegTime = 30000; // 30 seconds
// func to get user public ip to give to tracker on connection using axios
async function getPublicIP() {
try {
const res = await axios.get("https://api.ipify.org?format=json");
const publicIp = res.data.ip;
console.log(`Public IP fetched: ${publicIp}`);
return publicIp;
}
catch (err) {
console.error("Failed to fetch public IP:", err.message);
return null;
}
}
console.log(`Peer server running on port ${externalPort}`);
// websocket to use communincate with tracker
const ws = new WebSocket(trackerUrl);
ws.on("open", async () => {
try {
// connect to peer and get public ip for initial registration
console.log(`Connected to tracker as peer ${peerId}`);
const publicIp = await getPublicIP();
if (!publicIp) {
console.error("Cannot proceed without a valid public IP.");
return;
}
registerWithTracker(ws, publicIp);
// timer for re-registration, on reset send request for peer list
setInterval(() => {
registerWithTracker(ws, publicIp);
}, reRegTime);
setTimeout(() => {
ws.send(JSON.stringify({ type: "getPeers", id: peerId }));
}, 1000);
}
catch (err) {
console.error("Error during WebSocket open:", err.message);
}
});
// peer receiving differing message types from tracker
ws.on("message", (message) => {
try {
// parse message from tracker with JSON
const data = JSON.parse(message);
// if peerList print out list and start chunk download from other peers
if (data.type === "peerList") {
console.log(`Received peer list:`, data.peers);
downloadChunk(data.peers);
}
else if (data.type === "error") {
console.error(`Tracker error: ${data.message}`);
}
else {
console.warn(`Unknown message type received: ${data.type}`);
}
}
catch (err) {
console.error("Error processing tracker message:", err.message, "Message content:", message);
}
});
// create new websocketserver for other peer connections
const { WebSocketServer } = require("ws");
const peerServer = new WebSocketServer({ port: externalPort });
// chunk request from fellow peer, starting with connection establishment
peerServer.on("connection", (socket) => {
try {
console.log("Connection request from peer");
// connection made, chunk request received
socket.on("message", (message) => {
try {
// parse request with JSON, if request send chunk data under JSON format
const data = JSON.parse(message);
if (data.type === "requestChunk" && fileChunks[data.chunkId]) {
socket.send(JSON.stringify({
type: "chunkData",
chunkId: data.chunkId,
data: fileChunks[data.chunkId],
}));
}
// if unknown chunk request, print warning and tell requesting peer
else {
console.log(data);
console.warn(`Chunk ${data.chunkId} not found.`);
socket.send(JSON.stringify({ type: "error", message: "Chunk not found." }));
}
}
catch (err) {
console.error("Error processing peer message:", err.message);
}
});
}
catch (err) {
console.error("Error handling peer connection:", err.message);
}
});
// func for first register with tracker
function registerWithTracker(ws, publicIp) {
try {
// send to tracker peer data as JSON data
ws.send(JSON.stringify({
type: "register",
id: peerId,
ip: publicIp,
port: externalPort,
}));
console.log(`Reintroduced to tracker as peer ${peerId}`);
}
catch (err) {
console.error("Error during registration with tracker:", err.message);
}
}
// func for requesting other peers in received peer list for their chunk data
function downloadChunk(peerList) {
peerList.forEach((peer) => {
const peerUrl = `ws://${peer.ip}:${peer.port}`;
const peerSocket = new WebSocket(peerUrl);
// on connection to other peer, send chunk request
peerSocket.on("open", () => {
try {
console.log(`Connected to peer ${peer.id}`);
// loop and try to get current connected peer for their chunk via matching chunkId
for(let i = 0; i < peerList.length(); i++) {
peerSocket.send(JSON.stringify({ type: "requestChunk", chunkId: `chunk${i}` }));
}
}
catch (err) {
console.error("Error during chunk request:", err.message);
}
});
// on receiving message from current connected peer
peerSocket.on("message", (message) => {
try {
// if message chunk data add to fileChunks list after JSON parsing
const data = JSON.parse(message);
if (data.type === "chunkData") {
fileChunks[data.chunkId] = data.data;
console.log(`Received chunk: ${data.chunkId}`);
console.log(`Chunk data: ${data.data}`);
}
else {
console.warn(`Unknown chunk message type: ${data.type}`);
}
}
catch (err) {
console.error("Error processing chunk message:", err.message);
}
});
peerSocket.on("error", (err) => {
console.error(`Error connecting to peer ${peer.id}:`, err.message);
});
});
}