UNPKG

13.9 kBSource Map (JSON)View Raw
1{"version":3,"file":"message-io.js","names":["_nativeDuplexpair","_interopRequireDefault","require","tls","_interopRequireWildcard","_events","_message","_packet","_incomingMessageStream","_outgoingMessageStream","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","get","n","__proto__","a","Object","defineProperty","getOwnPropertyDescriptor","u","prototype","hasOwnProperty","call","i","set","obj","MessageIO","EventEmitter","constructor","socket","packetSize","debug","tlsNegotiationComplete","incomingMessageStream","IncomingMessageStream","incomingMessageIterator","Symbol","asyncIterator","outgoingMessageStream","OutgoingMessageStream","pipe","args","length","log","securePair","cleartext","setMaxSendFragment","startTls","credentialsDetails","hostname","trustServerCertificate","maxVersion","includes","secureContext","createSecureContext","Promise","resolve","reject","duplexpair","DuplexPair","connect","socket1","servername","rejectUnauthorized","encrypted","socket2","onSecureConnect","removeListener","onReadable","onError","once","err","destroy","cipher","getCipher","name","version","emit","unpipe","message","Message","type","TYPE","PRELOGIN","resetConnection","chunk","read","write","end","readMessage","then","response","data","catch","sendMessage","packetType","result","next","done","Error","value","_default","exports","module"],"sources":["../src/message-io.ts"],"sourcesContent":["import DuplexPair from 'native-duplexpair';\n\nimport { Duplex } from 'stream';\nimport * as tls from 'tls';\nimport { Socket } from 'net';\nimport { EventEmitter } from 'events';\n\nimport Debug from './debug';\n\nimport Message from './message';\nimport { TYPE } from './packet';\n\nimport IncomingMessageStream from './incoming-message-stream';\nimport OutgoingMessageStream from './outgoing-message-stream';\n\nclass MessageIO extends EventEmitter {\n declare socket: Socket;\n declare debug: Debug;\n\n declare tlsNegotiationComplete: boolean;\n\n declare private incomingMessageStream: IncomingMessageStream;\n declare outgoingMessageStream: OutgoingMessageStream;\n\n declare securePair?: {\n cleartext: tls.TLSSocket;\n encrypted: Duplex;\n };\n\n declare incomingMessageIterator: AsyncIterableIterator<Message>;\n\n constructor(socket: Socket, packetSize: number, debug: Debug) {\n super();\n\n this.socket = socket;\n this.debug = debug;\n\n this.tlsNegotiationComplete = false;\n\n this.incomingMessageStream = new IncomingMessageStream(this.debug);\n this.incomingMessageIterator = this.incomingMessageStream[Symbol.asyncIterator]();\n\n this.outgoingMessageStream = new OutgoingMessageStream(this.debug, { packetSize: packetSize });\n\n this.socket.pipe(this.incomingMessageStream);\n this.outgoingMessageStream.pipe(this.socket);\n }\n\n packetSize(...args: [number]) {\n if (args.length > 0) {\n const packetSize = args[0];\n this.debug.log('Packet size changed from ' + this.outgoingMessageStream.packetSize + ' to ' + packetSize);\n this.outgoingMessageStream.packetSize = packetSize;\n }\n\n if (this.securePair) {\n this.securePair.cleartext.setMaxSendFragment(this.outgoingMessageStream.packetSize);\n }\n\n return this.outgoingMessageStream.packetSize;\n }\n\n // Negotiate TLS encryption.\n startTls(credentialsDetails: tls.SecureContextOptions, hostname: string, trustServerCertificate: boolean) {\n if (!credentialsDetails.maxVersion || !['TLSv1.2', 'TLSv1.1', 'TLSv1'].includes(credentialsDetails.maxVersion)) {\n credentialsDetails.maxVersion = 'TLSv1.2';\n }\n\n const secureContext = tls.createSecureContext(credentialsDetails);\n\n return new Promise<void>((resolve, reject) => {\n const duplexpair = new DuplexPair();\n const securePair = this.securePair = {\n cleartext: tls.connect({\n socket: duplexpair.socket1 as Socket,\n servername: hostname,\n secureContext: secureContext,\n rejectUnauthorized: !trustServerCertificate\n }),\n encrypted: duplexpair.socket2\n };\n\n const onSecureConnect = () => {\n securePair.encrypted.removeListener('readable', onReadable);\n securePair.cleartext.removeListener('error', onError);\n securePair.cleartext.removeListener('secureConnect', onSecureConnect);\n\n // If we encounter any errors from this point on,\n // we just forward them to the actual network socket.\n securePair.cleartext.once('error', (err) => {\n this.socket.destroy(err);\n });\n\n const cipher = securePair.cleartext.getCipher();\n if (cipher) {\n this.debug.log('TLS negotiated (' + cipher.name + ', ' + cipher.version + ')');\n }\n\n this.emit('secure', securePair.cleartext);\n\n securePair.cleartext.setMaxSendFragment(this.outgoingMessageStream.packetSize);\n\n this.outgoingMessageStream.unpipe(this.socket);\n this.socket.unpipe(this.incomingMessageStream);\n\n this.socket.pipe(securePair.encrypted);\n securePair.encrypted.pipe(this.socket);\n\n securePair.cleartext.pipe(this.incomingMessageStream);\n this.outgoingMessageStream.pipe(securePair.cleartext);\n\n this.tlsNegotiationComplete = true;\n\n resolve();\n };\n\n const onError = (err?: Error) => {\n securePair.encrypted.removeListener('readable', onReadable);\n securePair.cleartext.removeListener('error', onError);\n securePair.cleartext.removeListener('secureConnect', onSecureConnect);\n\n securePair.cleartext.destroy();\n securePair.encrypted.destroy();\n\n reject(err);\n };\n\n const onReadable = () => {\n // When there is handshake data on the encrypted stream of the secure pair,\n // we wrap it into a `PRELOGIN` message and send it to the server.\n //\n // For each `PRELOGIN` message we sent we get back exactly one response message\n // that contains the server's handshake response data.\n const message = new Message({ type: TYPE.PRELOGIN, resetConnection: false });\n\n let chunk;\n while (chunk = securePair.encrypted.read()) {\n message.write(chunk);\n }\n this.outgoingMessageStream.write(message);\n message.end();\n\n this.readMessage().then(async (response) => {\n // Setup readable handler for the next round of handshaking.\n // If we encounter a `secureConnect` on the cleartext side\n // of the secure pair, the `readable` handler is cleared\n // and no further handshake handling will happen.\n securePair.encrypted.once('readable', onReadable);\n\n for await (const data of response) {\n // We feed the server's handshake response back into the\n // encrypted end of the secure pair.\n securePair.encrypted.write(data);\n }\n }).catch(onError);\n };\n\n securePair.cleartext.once('error', onError);\n securePair.cleartext.once('secureConnect', onSecureConnect);\n securePair.encrypted.once('readable', onReadable);\n });\n }\n\n // TODO listen for 'drain' event when socket.write returns false.\n // TODO implement incomplete request cancelation (2.2.1.6)\n sendMessage(packetType: number, data?: Buffer, resetConnection?: boolean) {\n const message = new Message({ type: packetType, resetConnection: resetConnection });\n message.end(data);\n this.outgoingMessageStream.write(message);\n return message;\n }\n\n /**\n * Read the next incoming message from the socket.\n */\n async readMessage(): Promise<Message> {\n const result = await this.incomingMessageIterator.next();\n\n if (result.done) {\n throw new Error('unexpected end of message stream');\n }\n\n return result.value;\n }\n}\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