-
-
Notifications
You must be signed in to change notification settings - Fork 238
/
Copy pathclient.js
239 lines (205 loc) · 7.65 KB
/
client.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
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
'use strict'
const EventEmitter = require('events').EventEmitter
const debug = require('debug')('minecraft-protocol')
const compression = require('./transforms/compression')
const framing = require('./transforms/framing')
const states = require('./states')
const createSerializer = require('./transforms/serializer').createSerializer
const createDeserializer = require('./transforms/serializer').createDeserializer
const createCipher = require('./transforms/encryption').createCipher
const createDecipher = require('./transforms/encryption').createDecipher
const closeTimeout = 30 * 1000
class Client extends EventEmitter {
constructor (isServer, version, customPackets, hideErrors = false) {
super()
this.customPackets = customPackets
this.version = version
this.isServer = !!isServer
this.splitter = framing.createSplitter()
this.packetsToParse = {}
this.compressor = null
this.framer = framing.createFramer()
this.cipher = null
this.decipher = null
this.decompressor = null
this.ended = true
this.latency = 0
this.hideErrors = hideErrors
this.closeTimer = null
this.state = states.HANDSHAKING
}
get state () {
return this.protocolState
}
setSerializer (state) {
this.serializer = createSerializer({ isServer: this.isServer, version: this.version, state: state, customPackets: this.customPackets })
this.deserializer = createDeserializer({
isServer: this.isServer,
version: this.version,
state: state,
packetsToParse:
this.packetsToParse,
customPackets: this.customPackets,
noErrorLogging: this.hideErrors
})
this.splitter.recognizeLegacyPing = state === states.HANDSHAKING
this.serializer.on('error', (e) => {
let parts
if (e.field) {
parts = e.field.split('.')
parts.shift()
} else { parts = [] }
const serializerDirection = !this.isServer ? 'toServer' : 'toClient'
e.field = [this.protocolState, serializerDirection].concat(parts).join('.')
e.message = `Serialization error for ${e.field} : ${e.message}`
if (!this.compressor) { this.serializer.pipe(this.framer) } else { this.serializer.pipe(this.compressor) }
this.emit('error', e)
})
this.deserializer.on('error', (e) => {
let parts
if (e.field) {
parts = e.field.split('.')
parts.shift()
} else { parts = [] }
const deserializerDirection = this.isServer ? 'toServer' : 'toClient'
e.field = [this.protocolState, deserializerDirection].concat(parts).join('.')
e.message = `Deserialization error for ${e.field} : ${e.message}`
if (!this.compressor) { this.splitter.pipe(this.deserializer) } else { this.decompressor.pipe(this.deserializer) }
this.emit('error', e)
})
this.deserializer.on('data', (parsed) => {
parsed.metadata.name = parsed.data.name
parsed.data = parsed.data.params
parsed.metadata.state = state
debug('read packet ' + state + '.' + parsed.metadata.name)
if (debug.enabled) {
const s = JSON.stringify(parsed.data, null, 2)
debug(s && s.length > 10000 ? parsed.data : s)
}
this.emit('packet', parsed.data, parsed.metadata, parsed.buffer, parsed.fullBuffer)
this.emit(parsed.metadata.name, parsed.data, parsed.metadata)
this.emit('raw.' + parsed.metadata.name, parsed.buffer, parsed.metadata)
this.emit('raw', parsed.buffer, parsed.metadata)
})
}
set state (newProperty) {
const oldProperty = this.protocolState
this.protocolState = newProperty
if (this.serializer) {
if (!this.compressor) {
this.serializer.unpipe()
this.splitter.unpipe(this.deserializer)
} else {
this.serializer.unpipe(this.compressor)
this.decompressor.unpipe(this.deserializer)
}
this.serializer.removeAllListeners()
this.deserializer.removeAllListeners()
}
this.setSerializer(this.protocolState)
if (!this.compressor) {
this.serializer.pipe(this.framer)
this.splitter.pipe(this.deserializer)
} else {
this.serializer.pipe(this.compressor)
this.decompressor.pipe(this.deserializer)
}
this.emit('state', newProperty, oldProperty)
}
get compressionThreshold () {
return this.compressor == null ? -2 : this.compressor.compressionThreshold
}
set compressionThreshold (threshold) {
this.setCompressionThreshold(threshold)
}
setSocket (socket) {
this.ended = false
// TODO : A lot of other things needs to be done.
const endSocket = () => {
if (this.ended) return
this.ended = true
clearTimeout(this.closeTimer)
this.socket.removeListener('close', endSocket)
this.socket.removeListener('end', endSocket)
this.socket.removeListener('timeout', endSocket)
this.emit('end', this._endReason || 'SocketClosed')
}
const onFatalError = (err) => {
this.emit('error', err)
endSocket()
}
const onError = (err) => this.emit('error', err)
this.socket = socket
if (this.socket.setNoDelay) { this.socket.setNoDelay(true) }
this.socket.on('connect', () => this.emit('connect'))
this.socket.on('error', onFatalError)
this.socket.on('close', endSocket)
this.socket.on('end', endSocket)
this.socket.on('timeout', endSocket)
this.framer.on('error', onError)
this.splitter.on('error', onError)
this.socket.pipe(this.splitter)
this.framer.pipe(this.socket)
}
end (reason) {
this._endReason = reason
/* ending the serializer will end the whole chain
serializer -> framer -> socket -> splitter -> deserializer */
if (this.serializer) {
this.serializer.end()
} else {
if (this.socket) this.socket.end()
}
if (this.socket) {
this.closeTimer = setTimeout(
this.socket.destroy.bind(this.socket),
closeTimeout
)
}
}
setEncryption (sharedSecret) {
if (this.cipher != null) { this.emit('error', new Error('Set encryption twice!')) }
this.cipher = createCipher(sharedSecret)
this.cipher.on('error', (err) => this.emit('error', err))
this.framer.unpipe(this.socket)
this.framer.pipe(this.cipher).pipe(this.socket)
this.decipher = createDecipher(sharedSecret)
this.decipher.on('error', (err) => this.emit('error', err))
this.socket.unpipe(this.splitter)
this.socket.pipe(this.decipher).pipe(this.splitter)
}
setCompressionThreshold (threshold) {
if (this.compressor == null) {
this.compressor = compression.createCompressor(threshold)
this.compressor.on('error', (err) => this.emit('error', err))
this.serializer.unpipe(this.framer)
this.serializer.pipe(this.compressor).pipe(this.framer)
this.decompressor = compression.createDecompressor(threshold, this.hideErrors)
this.decompressor.on('error', (err) => this.emit('error', err))
this.splitter.unpipe(this.deserializer)
this.splitter.pipe(this.decompressor).pipe(this.deserializer)
} else {
this.decompressor.threshold = threshold
this.compressor.threshold = threshold
}
}
write (name, params) {
if (!this.serializer.writable) { return }
debug('writing packet ' + this.state + '.' + name)
debug(params)
this.serializer.write({ name, params })
}
writeRaw (buffer) {
const stream = this.compressor === null ? this.framer : this.compressor
if (!stream.writable) { return }
stream.write(buffer)
}
// TCP/IP-specific (not generic Stream) method for backwards-compatibility
connect (port, host) {
const options = { port, host }
if (!this.options) this.options = options
require('./client/tcp_dns')(this, options)
options.connect(this)
}
}
module.exports = Client