-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.js
152 lines (132 loc) · 3.32 KB
/
connection.js
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
const EventEmitter = require('events').EventEmitter
const debug = require('debug')
const pull = require('pull-stream')
const toPull = require('stream-to-pull-stream')
const lp = require('pull-length-prefixed')
const Pushable = require('pull-pushable')
const IDENTIFY_PROTOCOL = '/identify/0.0.0'
const MAX_BUFFER_SIZE = 1024 * 1024 * 10
class Connection extends EventEmitter {
constructor({ socket, node, remoteId }) {
super()
this.socket = socket
this.node = node
this.isIdentified = false
this.observedAddress = `${socket.remoteAddress}:${socket.remotePort}`
this.remoteId = remoteId
this.writer = null
this.started = false
this.destroyed = false
this.heartbeatTimer = null
this.heartbeatInterval = 60000
this.log = debug('fastp2p:connection')
}
start() {
if (this.started) {
this.log('already started')
return
}
this.started = true
const socket = this.socket
this.writer = new Pushable()
const duplex = toPull.duplex(socket)
pull(
this.writer,
lp.encode(),
duplex,
lp.decode({ maxLength: MAX_BUFFER_SIZE }),
pull.drain(
(data) => this.processMessage(data),
(error) => {
if (error) {
this.emit('error', 'pull drain error: ' + error)
}
}
)
)
socket.on('error', (error) => {
this.emit('error', error)
})
socket.on('end', () => {
this.emitClose('socket end')
})
socket.on('close', () => {
this.emitClose('socket closed')
})
// TODO use asymmetric encryption to identify
if (!this.remoteId) {
this.identify()
}
setTimeout(() => {
if (!this.isIdentified) {
this.emitClose('identify timeout')
}
}, 4000)
}
send(msg) {
this.writer.push(Buffer.from(JSON.stringify(msg)))
}
getRemotePeerId() {
return this.remoteId
}
destroy() {
if (this.destroyed) return
this.destroyed = true
this.stopHeartbeat()
this.socket.destroy()
this.socket.unref()
this.log('connection destroyed')
}
//********************************************
// Private methods
//********************************************
startHeartbeat() {
if (this.heartbeatTimer) return
this.heartbeatTimer = setInterval(() => {
this.send({
protocol: '/_hb_',
})
}, this.heartbeatInterval)
}
stopHeartbeat() {
if (this.heartbeatTimer) clearInterval(this.heartbeatTimer)
}
processMessage(data) {
try {
const msg = JSON.parse(data.toString())
if (msg.protocol === IDENTIFY_PROTOCOL) {
if (this.remoteId) {
if (this.remoteId !== msg.id) {
this.emit('identify:failed')
this.destroy()
return
}
this.identify()
}
this.remoteId = msg.id
this.isIdentified = true
this.emit('identified')
this.startHeartbeat()
} else {
this.emit('message', msg)
}
} catch (e) {
this.log('invalid message format:', data)
}
}
identify() {
this.send({
protocol: IDENTIFY_PROTOCOL,
id: this.node.id,
})
}
emitClose(reason) {
if (!this.closed) {
this.closed = true
this.destroy()
this.stopHeartbeat()
this.emit('close', reason)
}
}
}
module.exports = Connection