Skip to content

Commit 80e9ada

Browse files
Peter XiePeter Xie
authored andcommitted
bate01
1 parent 56e3162 commit 80e9ada

File tree

14 files changed

+201
-139
lines changed

14 files changed

+201
-139
lines changed

app/@Opn.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head></head>
4+
<body></body>
5+
<script>
6+
require ('./qtGate_emailClient')
7+
</script>
8+
</html>

app/compress.ts

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -143,29 +143,34 @@ export class encryptStream extends Stream.Transform {
143143
private BlockBuffer ( _buf: Buffer ) {
144144
return Buffer.from( _buf.length.toString( 16 ).toUpperCase() + '\r\n', 'utf8' )
145145
}
146-
147-
constructor ( private password: string, private random: number, private httpHeader : ( str: string ) => Buffer, CallBack ) {
148-
super ()
149-
Async.waterfall ([
146+
private init ( callback ) {
147+
return Async.waterfall ([
150148
next => crypto.randomBytes ( 64, next ),
151149
( _salt, next ) => {
152150
this.salt = _salt
153151
crypto.randomBytes ( 12, next )
154152
},
155153
( _iv, next ) => {
156154
this.iv = _iv
157-
console.log (password)
158-
crypto.pbkdf2 ( password, this.salt, 2145, 32, 'sha512', next )
155+
crypto.pbkdf2 ( this.password, this.salt, 2145, 32, 'sha512', next )
159156
}
160157
], ( err, derivedKey ) => {
161158

162159
this.derivedKey = derivedKey
163-
return CallBack ( err )
160+
return callback ( err )
164161
})
165162
}
163+
164+
constructor ( private password: string, private random: number, private httpHeader : ( str: string ) => Buffer ) {
165+
super ()
166+
}
166167

167168
public _transform ( chunk: Buffer, encode, cb ) {
168-
169+
if ( !this.derivedKey) {
170+
return this.init (() => {
171+
return this._transform ( chunk, encode, cb )
172+
})
173+
}
169174
const cipher = crypto.createCipheriv ( 'aes-256-gcm', this.derivedKey, this.iv )
170175

171176
let _text = Buffer.concat ([ Buffer.alloc ( 4, 0 ) , chunk ])
@@ -201,7 +206,6 @@ export class encryptStream extends Stream.Transform {
201206
}
202207

203208
export class decryptStream extends Stream.Transform {
204-
private first = true
205209
private salt: Buffer
206210
private iv: Buffer
207211
private bufferLength = 8196
@@ -213,21 +217,20 @@ export class decryptStream extends Stream.Transform {
213217
decipher.setAuthTag ( _text.slice ( 0, 16 ))
214218
try {
215219
const _buf = Buffer.concat ([ decipher.update ( _text.slice ( 16 )), decipher.final () ])
216-
217-
const leng = _buf.slice( 4, 4 + _buf.readUInt32BE ( 0 ))
220+
221+
const leng = _buf.slice ( 4, 4 + _buf.readUInt32BE ( 0 ))
218222
if ( leng && leng.length ) {
219223
return leng
220224
}
221-
225+
222226
return Buffer.allocUnsafe ( 0 )
223227
} catch ( e ) {
224-
console.log ('class decryptStream _decrypt error:', e.message )
228+
console.log ( 'class decryptStream _decrypt error:', e.message )
225229
return Buffer.allocUnsafe ( 0 )
226230
}
227231
}
228232

229233
public _First ( chunk: Buffer, CallBack: ( err?: Error, text?: Buffer ) => void ) {
230-
this.first = false
231234
this.salt = chunk.slice ( 0, 64 );
232235
this.iv = chunk.slice ( 64, 76 );
233236
return crypto.pbkdf2 ( this.password, this.salt , 2145, 32, 'sha512', ( err, derivedKey ) => {
@@ -247,27 +250,17 @@ export class decryptStream extends Stream.Transform {
247250
super ()
248251
}
249252

250-
private blockData ( chunk: Buffer, cb ) {
251-
this.push ( chunk )
252-
cb ()
253-
}
254253

255254
public _transform ( chunk: Buffer, encode, cb ) {
256-
257-
let first = 0
258-
if ( this.first ) {
259-
return this._First ( chunk, ( err, text ) => {
260-
if ( err )
261-
return cb ( err )
262-
return this.blockData ( text, cb )
263-
264-
})
255+
256+
if ( !this.derivedKey ) {
257+
return this._First ( chunk, cb )
265258
}
266259

267-
const text = this._decrypt ( chunk.slice ( 0 ))
260+
const text = this._decrypt ( chunk )
268261
if ( ! text.length )
269262
return cb ( new Error ( 'lenth = 0'))
270-
return this.blockData ( text, cb )
263+
return cb ( null, text )
271264

272265
}
273266
}

app/gateway.ts

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const otherRequestForNet = ( path: string, host: string, port: number, UserAgent
2424
class hostLookupResponse extends Stream.Writable {
2525
constructor ( private CallBack: ( err?: Error, dns?: domainData ) => void ) { super ()}
2626
public _write ( chunk: Buffer, enc, next ) {
27+
console.log ( `hostLookupResponse _write come [${ chunk.toString()}]`)
2728
const ns = chunk.toString ( 'utf8' )
2829
try {
2930
const _ret = JSON.parse ( ns )
@@ -58,26 +59,34 @@ export default class gateWay {
5859

5960
const encrypt = new Compress.encryptStream ( this.password, 0, ( str: string ) => {
6061
return this.request ( str )
61-
}, err => {
62-
if ( err ) {
63-
return CallBack ( err )
64-
}
65-
const finish = new hostLookupResponse ( CallBack )
66-
const httpBlock = new Compress.getDecryptClientStreamFromHttp ()
67-
const decrypt = new Compress.decryptStream ( this.password )
68-
62+
})
63+
64+
const finish = new hostLookupResponse ( CallBack )
65+
const httpBlock = new Compress.getDecryptClientStreamFromHttp ()
66+
const decrypt = new Compress.decryptStream ( this.password )
67+
6968

70-
const _socket = Net.connect ({ port: this.serverPort, host: this.serverIp }, () => {
71-
httpBlock.on ( 'error', err => {
72-
_socket.end ( res._HTTP_502 )
73-
return CallBack ( err )
74-
})
75-
encrypt.pipe ( _socket ).pipe ( httpBlock ).pipe ( decrypt ).pipe ( finish )
76-
encrypt.write ( _data )
77-
console.log (`send data to remote!`)
78-
console.log(`*************\n${_data.toString ()}\n*********`)
79-
})
69+
const _socket = Net.createConnection ({ port: this.serverPort, host: this.serverIp }, () => {
70+
encrypt.write ( _data )
71+
console.log (`send data to remote!`)
72+
console.log(`*************\n${_data.toString ()}\n*********`)
8073
})
74+
75+
_socket.once ( 'end', () => {
76+
console.log (`_socket.once end!`)
77+
})
78+
79+
httpBlock.once ( 'error', err => {
80+
console.log (`httpBlock.on error`, err )
81+
_socket.end ( res._HTTP_502 )
82+
return CallBack ( err )
83+
})
84+
85+
decrypt.once ( 'err', err=> {
86+
87+
} )
88+
encrypt.pipe ( _socket ).pipe ( httpBlock ).pipe ( decrypt ).pipe ( finish )
89+
8190

8291
}
8392

@@ -86,25 +95,18 @@ export default class gateWay {
8695
const decrypt = new Compress.decryptStream ( this.password )
8796
const encrypt = new Compress.encryptStream ( this.password, 0, ( str: string ) => {
8897
return this.request ( str )
89-
}, err => {
90-
91-
if ( err ) {
92-
return console.log ( 'requestGetWay new Compress.encryptStream got ERROR: ', err.message )
93-
}
94-
95-
const httpBlock = new Compress.getDecryptClientStreamFromHttp ()
96-
httpBlock.once ( 'error', err => {
97-
socket.end ( res._HTTP_404 )
98-
})
99-
const _socket = Net.connect ({ port: this.serverPort, host: this.serverIp }, () => {
100-
console.log ( 'requestGetWay connect:', uuuu.host, uuuu.port )
101-
encrypt.pipe ( _socket ).pipe ( httpBlock ).pipe ( decrypt ).pipe ( socket ).pipe ( encrypt )
102-
encrypt.write ( Buffer.from ( JSON.stringify ( uuuu ), 'utf8' ))
103-
})
104-
10598
})
106-
107-
99+
const httpBlock = new Compress.getDecryptClientStreamFromHttp ()
100+
httpBlock.once ( 'error', err => {
101+
socket.end ( res._HTTP_404 )
102+
})
103+
const _socket = Net.createConnection ({ port: this.serverPort, host: this.serverIp }, () => {
104+
console.log ( 'requestGetWay connect:', uuuu.host, uuuu.port )
105+
106+
encrypt.write ( Buffer.from ( JSON.stringify ( uuuu ), 'utf8' ))
107+
})
108+
encrypt.pipe ( _socket ).pipe ( httpBlock ).pipe ( decrypt ).pipe ( socket ).pipe ( encrypt )
109+
108110
}
109111

110112
public requestGetWayTest ( id: string, uuuu: VE_IPptpStream, userAgent: string, socket: Net.Socket ) {

app/imap.js

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const crypto = require("crypto");
1515
const path_1 = require("path");
1616
const os_1 = require("os");
1717
const MAX_INT = 9007199254740992;
18-
const debug = true;
18+
const debug = false;
1919
const QTGateFolder = path_1.join(os_1.homedir(), '.QTGate');
2020
const ErrorLogFile = path_1.join(QTGateFolder, 'imap.log');
2121
let flag = 'w';
@@ -58,14 +58,16 @@ class ImapServerSwitchStream extends Stream.Transform {
5858
this.runningCommand = null;
5959
this.nextRead = true;
6060
this.idleNextStop = null;
61-
this.imapServer.on('nextNewMail', () => {
62-
this.nextRead = true;
63-
if (this.runningCommand != 'idle')
64-
return;
65-
if (this.imapServer.idleSupport) {
66-
return this.idleStop();
67-
}
68-
});
61+
if (eachMail) {
62+
this.imapServer.on('nextNewMail', () => {
63+
this.nextRead = true;
64+
if (this.runningCommand !== 'idle')
65+
return;
66+
if (this.imapServer.idleSupport) {
67+
return this.idleStop();
68+
}
69+
});
70+
}
6971
}
7072
commandProcess(text, cmdArray, next, callback) { }
7173
serverCommandError(err, CallBack) {
@@ -199,7 +201,8 @@ class ImapServerSwitchStream extends Stream.Transform {
199201
console.log ( _buf1.length )
200202
console.log ( _buf1.toString ())
201203
*/
202-
this.nextRead = false;
204+
if (this.eachMail)
205+
this.nextRead = false;
203206
this.imapServer.newMail(_buf1);
204207
}
205208
return __CallBack();
@@ -269,11 +272,11 @@ class ImapServerSwitchStream extends Stream.Transform {
269272
this.flagsDeleted(newMailIds, next);
270273
},
271274
next => this.expunge(next)
272-
], err => {
275+
], (err, newMail) => {
273276
this.runningCommand = null;
274277
if (err)
275278
return this.imapServer.destroyAll(err);
276-
if (this.nextRead && (haveMoreNewMail || havemore))
279+
if (this.nextRead && (haveMoreNewMail || havemore || newMail))
277280
return this.doNewMail();
278281
return this.idleNoop();
279282
});
@@ -282,8 +285,9 @@ class ImapServerSwitchStream extends Stream.Transform {
282285
}
283286
checkLogout(CallBack) {
284287
if (this.waitLogout) {
285-
if (!this.canDoLogout)
288+
if (!this.canDoLogout) {
286289
return this.logout_process(CallBack);
290+
}
287291
if (this.exitWithDeleteBox) {
288292
return this.deleteBox(() => {
289293
return this.logout_process(CallBack);
@@ -644,14 +648,19 @@ class ImapServerSwitchStream extends Stream.Transform {
644648
return this.imapServer.destroyAll(null);
645649
}
646650
logout(callback) {
651+
if (this.waitLogout)
652+
return callback;
647653
this.waitLogout = true;
648-
this.waitLogoutCallBack = callback;
649654
this.checkLogout(callback);
650655
}
651656
logout_process(callback) {
652-
if (!this.writable)
657+
console.log(`logout_process typeof callback = [${typeof callback}]`);
658+
if (!this.writable) {
659+
console.log(`logout_process [! this.writable] run return callback ()`);
653660
return callback();
661+
}
654662
const doLogout = () => {
663+
console.log(`logout_process doLogout()`);
655664
if (this.imapServer.listenFolder && this.imapServer.deleteBoxWhenEnd) {
656665
return Async.series([
657666
next => this.deleteBox(next),
@@ -661,6 +670,7 @@ class ImapServerSwitchStream extends Stream.Transform {
661670
return this._logout(callback);
662671
};
663672
if (this.imapServer.listenFolder && this.runningCommand) {
673+
console.log(`logout_process [this.imapServer.listenFolder && this.runningCommand], doing this.idleStop ()`);
664674
this.idleCallBack = doLogout;
665675
return this.idleStop();
666676
}
@@ -687,7 +697,7 @@ class ImapServerSwitchStream extends Stream.Transform {
687697
this.commandProcess = (text, cmdArray, next, _callback) => {
688698
switch (cmdArray[0]) {
689699
case '*': {
690-
if (/^RECENT$/i.test(cmdArray[2])) {
700+
if (/^EXPUNGE$/i.test(cmdArray[2])) {
691701
if (parseInt(cmdArray[1])) {
692702
newSwitchRet = true;
693703
}

0 commit comments

Comments
 (0)