UNPKG

vpn.email.client

Version:
149 lines (143 loc) 6.35 kB
/*! * Copyright 2017 Vpn.Email network security technology Canada Inc. All Rights Reserved. * * Vpn.Email network technolog Canada Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ "use strict"; const Compress = require("./compress"); const Async = require("async"); const shortID = require("shortid"); const Crypto = require("crypto"); const execImap = require("./execImap"); const maxAclias = 2; class imapConnect { constructor(users, newMail, subImapPool, mainReconnectReady, subReconnectReady, mainImapPool, password, isServer, CallBackPool, doFock, endCall) { this.users = users; this.newMail = newMail; this.subImapPool = subImapPool; this.mainReconnectReady = mainReconnectReady; this.subReconnectReady = subReconnectReady; this.mainImapPool = mainImapPool; this.password = password; this.isServer = isServer; this.CallBackPool = CallBackPool; this.doFock = doFock; this.endCall = endCall; this.mainImap = []; this.pairConnectPool = []; this.isAliasUp = false; this.totalRead = 0; this.totalReadCount = 0; this.getPairFolder(); this.connect(users, password); } _newMail(buffer) { this.totalRead += buffer.length; this.totalReadCount++; const y = Compress.openPacket(buffer); //console.log (`======> [${this.users.imapUserName}][${this.users.serverMailBoxName}] total read count :[${this.totalReadCount}], total byte [${this.totalRead}]`) //console.log ('new Mail = ', y.command) switch (y.command) { case 0: case 1: case 2: case 5: return this.newMail(y); case 3: const CallBack = this.CallBackPool.get(y.uuid); if (!CallBack) console.log(y.uuid, 'got NewMail peer connect but cant find uuid'); this.CallBackPool.delete(y.uuid); return CallBack(); default: return console.log('imapCluster->_newMail unknow y.command = ', y.command); } } preNewMail(buffer) { const data = Compress.decrypt(buffer, this.password, (err, data) => { if (err) { return console.log('=======> preNewMail Compress.decrypt ERROR', err.message); } if (!data || !data.length) return console.log('=======> preNewMail no data ERROR'); try { const dataArray = JSON.parse(data); Async.each(dataArray, n => { this._newMail(new Buffer(n['data'], 'utf8')); }); } catch (e) { return console.log('=======> preNewMail JSON parse ERROR'); } }); } pushToPool(isAlias, _execImap) { const u = { isAlias: isAlias, execImap: _execImap, name: shortID.generate() }; if (isAlias) return this.subImapPool.push(u); return this.mainImapPool.push(u); } getPairFolder() { for (let i = 0; i < maxAclias; i++) { const _listenFolder = (this.isServer ? this.users.serverMailBoxName : this.users.uuid) + i; const _saveFolder = (this.isServer ? this.users.uuid : this.users.serverMailBoxName) + i; const ret = { saveFolder: Crypto.createHash('md5').update(_saveFolder).digest('hex'), listenFolder: Crypto.createHash('md5').update(_listenFolder).digest('hex') }; this.pairConnectPool.push(ret); } } connect(users, password) { const listenFolder = this.isServer ? users.serverMailBoxName : users.uuid; const saveFolder = !this.isServer ? users.serverMailBoxName : users.uuid; const dummyImap = new execImap.execImap(users, null, saveFolder, null, this.mainReconnectReady, this.doFock, this.endCall); this.pushToPool(false, dummyImap); /* const dummyImap1 = new execImap.execImap ( users, null, saveFolder, null, this.doFock, this.endCall ) this.pushToPool ( false, dummyImap1 ) /* const dummyImap2 = new execImap.execImap ( users, null, saveFolder, null, this.doFock, this.endCall ) this.pushToPool ( true, dummyImap2 ) /* if ( this.isServer ) { const dummyImap3 = new execImap.execImap ( users, null, saveFolder, null, this.doFock, this.endCall ) this.pushToPool ( true, dummyImap3 ) const dummyImap4 = new execImap.execImap ( users, null, saveFolder, null, this.doFock, this.endCall ) this.pushToPool ( true, dummyImap4 ) const dummyImap5 = new execImap.execImap ( users, null, saveFolder, null, this.doFock, this.endCall ) this.pushToPool ( true, dummyImap5 ) } */ this.mainImap.push(new execImap.execImap(users, listenFolder, null, buffer => { this.preNewMail(buffer); }, null, this.doFock, this.endCall)); this.ConnectPair(); } ConnectPair() { if (this.isAliasUp) return; this.isAliasUp = true; console.log('start pair length = ', this.pairConnectPool.length); Async.each(this.pairConnectPool, (n, down) => { this.mainImap.push(new execImap.execImap(this.users, n.listenFolder, null, buffer => { this.preNewMail(buffer); }, null, this.doFock, this.endCall)); const sss = new execImap.execImap(this.users, null, n.saveFolder, null, this.mainReconnectReady, this.doFock, this.endCall); this.pushToPool(true, sss); }); } } Object.defineProperty(exports, "__esModule", { value: true }); exports.default = imapConnect;