-
Notifications
You must be signed in to change notification settings - Fork 0
/
crepe-internal.coffee
504 lines (426 loc) · 16.3 KB
/
crepe-internal.coffee
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
###
# crepe-internal.coffee - This file handles all gnutella-specific internals
# and exposes a few useful methods as UI hooks
###
# Imports
net = require('net')
fs = require('fs')
gp = require('./gnutella-packet.js')
http = require('http')
assert = require('assert')
path = require('path')
shared_folder = process.cwd()
debugging = console.log
# Debugging output is off by default. To enable debugging, type 'debug'
# into the REPL. To turn it off, type 'nodebug'
console.log = ->
return
# Enable exports
root = exports ? this # http://stackoverflow.com/questions/4214731/coffeescript-global-variables
################################################################################
# Internal data structures and related methods
################################################################################
# Internal tables used for routing
origin = {} # Stores {packet.id -> packet} mappings
mitm = {} # Stores {packet.id -> socket} mappings
query_hit = {} #Stores {packet.id -> callback} mappings
downloadables = [] # An array of download objects
class DownloadObject
constructor : (address, port, fileName) ->
@address ?= address
@port ?= port
@fileName ?= fileName
crepeServer = null # The actual crepeServer instance
fileServer = null # The actual fileServer instance
shareFolder = null # The share folder.
crepeServerPort = 0
fileServerPort = 0
# Methods to make the crepeServer and fileServer accessible in this file
root.setCrepeServer = (s) ->
crepeServer = s
root.setFileServer = (s) ->
fileServer = s
fileServerPort = fileServer.port()
root.setShareFolder = (s) ->
shareFolder = s
################################################################################
# REPL API Methods
################################################################################
# This method attempts to add the address/port to our neighborhood.
# Args:
# address: String - IP address
# port: Number - port
root.connect = (address, port) ->
if !port?
port = address
address = '127.0.0.1'
socket = new net.Socket()
socket.setNoDelay()
# Error handler
socket.on 'error', (error) ->
console.log "CONNECT Error: #{error}"
# Handle connection handshake
socket.on 'connect', ->
# Send connect request
console.log "Sending CONNECT to #{socket.remoteAddress}:#{socket.remotePort}"
connectPacket = new gp.ConnectPacket()
try
socket.write(connectPacket.serialize())
catch error
console.log "Sending connect request FAILED!"
# Outgoing data handler for socket
# Note: The outbound socket connections only handle the following packets
# - PONG
# - CONNECTOK
# - QUERYHIT
# - PING: special case because we want to add an immediate peer using this
socket.on 'data', (data) ->
console.log "Received Buffer:"
console.log data
packet = gp.deserialize(data)
console.log "Deserialized packet: ", packet
switch packet.type
# handle connect ok
when gp.PacketType.CONNECTOK
console.log "received CONNECTOK #{socket.remoteAddress}:#{socket.remotePort}"
if nh.ableToAdd(socket.remoteAddress, socket.remotePort)
nh.add(socket.remoteAddress, socket.remotePort, socket)
break
# handle special direct ping from peer wanting to connect
when gp.PacketType.PING
console.log "received direct PING:#{packet.id} from #{socket.remoteAddress}:#{socket.remotePort}"
# Send pong in response to the ping
console.log "replying direct PONG:#{packet.id} to #{@remoteAddress}:#{@remotePort}"
pong = new gp.PongPacket()
pong.id = packet.id
pong.address = socket.address().address
pong.port = crepeServerPort
pong.ttl = 1
try
socket.write(pong.serialize())
catch error
console.log "replying direct PONG FAILED"
break
# handle pong
when gp.PacketType.PONG
console.log "received PONG:#{packet.id} from #{packet.address}:#{packet.port}"
# Try to connect to peer if the pong is intended for this node
# TODO: decide if we are able to add any more peers(Kevin)
if origin[packet.id] && !nh.at(packet.address, packet.port) &&
origin[packet.id].type == gp.PacketType.PING
console.log "Trying to connect to #{packet.address}:#{packet.port}"
root.connect(packet.address, packet.port)
# Forward the pong back to where it originated
else if mitm[packet.id]?
console.log "Forwarding PONG:#{packet.id}"
try
mitm[packet.id].write(data)
catch error
console.log "Forwarding PONG FAILED!"
break
# handle query hit
when gp.PacketType.QUERYHIT
console.log "received QUERYHIT:#{packet.id}"
if origin[packet.id]? && query_hit[packet.id]?
query_hit[packet.id] packet
#console.log "HITS: #{packet.address}:#{packet.port}"
#for result in packet.resultSet
# result_string = "filename:#{result.fileName}, "
# result_string += "size:#{result.fileSize}, "
# result_string += "index:#{result.fileIndex}, "
# result_string += "serventID:#{packet.serventIdentifier}"
# console.log result_string
else if mitm[packet.id]?
console.log "forwarding QUERYHIT:#{packet.id}"
try
mitm[packet.id].write(data)
catch error
console.log "forwarding QUERHIT failed"
break
# default
else
console.log "Unknown command:"
console.log data
socket.connect(port, address)
# This method issues Query packets and calls resultCallback for every
# Query Hit packet it recieves
# Args:
# query: String - the string to search for
# resultCallback: function - called for every QueryHit packet
# Args:
# queryHit - A QueryHit packet object representing the result
root.search = (query, resultCallback) ->
q = new gp.QueryPacket()
q.searchCriteria = query
origin[q.id] = q
query_hit[q.id] = (packet) ->
console.log "HITS: #{packet.address}:#{packet.port}"
for result in packet.resultSet
result_string = "##{downloadables.length} filename:#{result.fileName}, "
result_string += "size:#{result.fileSize}, "
result_string += "index:#{result.fileIndex}, "
result_string += "serventID:#{packet.serventIdentifier}"
downloadables[downloadables.length] = new DownloadObject(packet.address, packet.port, result.fileName)
console.info result_string
nh.sendToAll(q.serialize())
root.list = ->
for result, j in downloadables
result_string = "##{j} filename:#{result.fileName}, "
result_string += "address:#{result.address}, "
result_string += "port:#{result.port}, "
console.info result_string
# Create utility function for saving files. This function is used in
# root.download
saveFile = (filePath, data) ->
fs.writeFile filePath, data, 'binary', (err) ->
if (err)
console.log "Error saving the #{filePath}: #{err}"
else
console.log "#{filePath} save success!"
# This method attempts to download the file identified by fileIdentifier
# Args:
# fileIdentifier: a locally-unique identifier that uniquely identifies
# a given QueryHit entry.
# downloadStatusCallback: function - called periodically during the
# download process. (TODO: args)
root.download = (fileIdentifier, downloadStatusCallback) ->
downItem = downloadables[fileIdentifier]
# Make sure that downItem is defined.
if (not downItem?)
console.log "This index is invalid: #{fileIdentifier}"
return
options =
host: downItem.address
port: downItem.port
path: "/#{downItem.fileName}"
console.log "Downloading: #{fileIdentifier}:#{downItem.fileName}"
console.log options
http.get options, (res) ->
console.log "Get File response: #{res.statusCode}"
if (res.statusCode != 200)
console.log "Bad File Response!!"
return
res.on 'data', (data) ->
# Takea look at the current items in teh folder
fs.readdir shareFolder, (err, files) ->
fileName = downItem.fileName
console.log 'Files!'
# Populate object for instant lookup.
temp = {}
for file in files
temp[file] = true
console.log temp
filePath = "#{shareFolder}/#{fileName}"
# Check to see if the file already exists
if (not temp[fileName])
# Save to the filename if the file doesn't already exist.
saveFile filePath, data
else
# Save the file and append a number to indicate it is a copy
groups = fileName.match /(.*)\.(.*)/
fileMain = ''
fileExtension = null
if (groups)
fileMain = groups[1]
fileExtension = groups[2]
else
fileMain = fileName
i = 0
while true
if (fileExtension)
fileName = "#{fileMain}_(#{i}).#{fileExtension}"
else
fileName = "#{fileMain}_(#{i})"
if (not temp[fileName])
saveFile "#{shareFolder}/#{fileName}", data
break
else
i = i + 1
assert.ok 1
root.setDebug = (debug) ->
if debug
console.log = debugging
else
console.log = ->
return
################################################################################
# Socket Handler Methods
################################################################################
# This method handles inbound sockets by classifying incoming packets and
# handling them accordingly.
# Args:
# socket: The socket between this node and the incoming node
# Note: The inbound socket connections only handle the following packets
# - PING
# - CONNECT
# - QUERY
# - PONG: special case because we want to add an immediate peer using this
root.connectionHandler = (socket) ->
# Error handler
socket.on 'error', (error) ->
console.log "HANDLER Error: #{error}"
# Inbound data handler
socket.on 'data', (data) ->
console.log "Received Buffer:"
console.log data
packet = gp.deserialize(data)
switch packet.type
# handle connect
when gp.PacketType.CONNECT
console.log "received CONNECT from #{socket.remoteAddress}:#{socket.remotePort}"
connectOKPacket = new gp.ConnectOKPacket()
ping = new gp.PingPacket()
ping.ttl = 1
origin[ping.id] = ping
try
console.log "sending CONNECTOK to #{socket.remoteAddress}:#{socket.remotePort}"
socket.write connectOKPacket.serialize(), 'binary', ->
# TODO(Kevin): decide if we want to send ping or not. Might not want to add
# another peer because we have reached the peer limit
console.log "sending direct PING:#{ping.id} to #{socket.remoteAddress}:#{socket.remotePort}"
#TODO: FIX THIS UGLY HACK
# The data buffer on the other end of this socket queues up the CONNECTOK
# and PING packets. This causes the packet to be misinterpreted.
write_serialize = ->
try
socket.write(ping.serialize())
catch error
console.log "failed to send DIRECT PING"
setTimeout(write_serialize, 2000)
catch error
console.log "sending CONNECTOK FAILED!"
break
# handle ping
when gp.PacketType.PING
console.log "received PING:#{packet.id}"
if origin[packet.id]? || mitm[packet.id]?
console.log "drop PING because already seen"
break
# Send pong in response to the ping
console.log "replying PONG:#{packet.id} to #{@remoteAddress}:#{@remotePort}"
pong = new gp.PongPacket()
pong.id = packet.id
pong.address = socket.address().address
pong.port = crepeServerPort
pong.numFiles = 1337 # TODO(advait): Fix this
pong.numKbShared = 1337 # TODO(advait): Fix
try
socket.write(pong.serialize())
catch error
console.log "replying PONG FAILED!"
nh.remove(socket.remoteAddress, socket.remotePort)
# Forward ping to other neighbors
packet.ttl--
packet.hops++
if packet.ttl > 0
console.log "forwarding PING:#{packet.id} to all neighbors"
mitm[packet.id] = socket
nh.sendToAll(data)
break
# handle special pong because immediate peer wants to connect
when gp.PacketType.PONG
console.log "received direct PONG:#{packet.id} #{packet.address}:#{packet.port}"
if origin[packet.id]? && origin[packet.id].type == gp.PacketType.PING &&
nh.ableToAdd(packet.address, packet.port) && packet.ttl == 1 && packet.hops == 0
root.connect(packet.address, packet.port)
break
# handle push
when gp.PacketType.PUSH
console.log "received PUSH:#{packet.id}"
break
# handle query
when gp.PacketType.QUERY
console.log "received QUERY:#{packet.id} search:#{packet.searchCriteria}"
if origin[packet.id]? || mitm[packet.id]?
console.log "drop QUERY because already seen"
break
# Forward query to all neighbors
console.log "forwarding Query:#{packet.id}"
mitm[packet.id] = socket
nh.sendToAll(data)
# Send a Query Hit if we find a match
searchResults = fileServer.search(packet.searchCriteria)
if searchResults.length > 0
queryHit = new gp.QueryHitPacket()
queryHit.address = socket.address().address
queryHit.port = fileServerPort
queryHit.id = packet.id
for result in searchResults
queryHit.addResult(result)
console.log "sending QUERYHIT:#{queryHit.id}"
try
socket.write(queryHit.serialize())
catch error
console.log "Sending QUERYHIT failed: #{error}"
else
console.log "Can't send directory:#{packet.searchCriteria}"
break
# default
else
console.log "Unknown command:"
console.log data
# This method sets up a listening port for the server as well as saving the
# address and port on which the server is listening.
root.listeningHandler = ->
crepeServer = this
crepeServerPort = this.address().port
console.info "server is now listening port #{crepeServerPort}"
console.info "CTRL+C to exit"
setInterval(updateNeighborhood, 15000)
################################################################################
# Neighborhood utility methods
################################################################################
# Neighborhood object to handle peers in the neighborhood set
nh =
MAX_PEERS : 10
neighbors : {}
count : 0
# Add new peer to the neighborhood set
add: (ip, port, socket) ->
if @count < @MAX_PEERS
console.log "Added new peer #{ip}:#{port}"
@neighbors["#{ip}:#{port}"] = socket
@count++
# Remove peer from the neighborhood set
remove: (ip, port) ->
if @neighbors["#{ip}:#{port}"]
@neighbors["#{ip}:#{port}"] = undefined
@count--
# Return the socket associated with the peer at ip and port
at: (ip, port) ->
return @neighbors["#{ip}:#{port}"]
# Return true if a new peer can be added to the neighborhood
ableToAdd: (ip, port) ->
if !@at(ip, port)? && @count < @MAX_PEERS
return true
else
return false
# Send data to all peers in the neighborhood but do not send to the peer
# associated with the "exclude" socket. The "exclude" paramter is used to exclude
# the peer that sent the incoming ping or query.
sendToAll: (data) ->
for node, socket of @neighbors
# ignore the excluded socket and undefined sockets
if socket?
try
socket.write data
catch error
console.log "PEER ERROR: #{error}"
console.log "peer #{node} died. Removing #{node}"
@neighbors[node] = undefined
@count--
# Print the list of neighbors
printAll: ->
console.info "List of neighbors:"
for node, socket of @neighbors
if socket?
console.info "#{node}"
root.printNeighbors = ->
nh.printAll()
updateNeighborhood = ->
# Indicate that this node sent the original ping
ping = new gp.PingPacket()
origin[ping.id] = ping
# Flood neighbors with pings
console.log "Flood neighbors with ping:#{ping.id}"
nh.sendToAll(ping.serialize())