Skip to content

Commit 448cf7c

Browse files
Peter XiePeter Xie
authored andcommitted
v0.8.72
1 parent c32a28f commit 448cf7c

File tree

12 files changed

+870
-372
lines changed

12 files changed

+870
-372
lines changed

app/proxyServer.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
* limitations under the License.
1616
*/
1717

18-
/// <reference path="../typings/types/v7/index.d.ts" />
1918
import * as Net from 'net'
2019
import * as Http from 'http'
2120
import * as Dns from 'dns'
@@ -77,26 +76,6 @@ export const checkDomainInBlackList = ( BlackLisk: string[], domain: string, Cal
7776
})
7877
}
7978

80-
const otherRespon = ( path: string, host: string, port: number, UserAgent: string ) => {
81-
82-
const option = {
83-
host: host,
84-
port: port,
85-
path: '/' + path,
86-
method: 'GET',
87-
headers: {
88-
//'Upgrade-Insecure-Requests': 1,
89-
Host: host + ':' + port,
90-
'Accept': '*/*',
91-
'Accept-Language': 'en-US',
92-
'Connection': 'keep-alive',
93-
'Accept-Encoding': 'gzip, deflate',
94-
'User-Agent': UserAgent || 'Mozilla/5.0',
95-
}
96-
}
97-
return option
98-
}
99-
10079
const testLogin = ( req: Buffer, loginUserList: string ) => {
10180

10281
const header = new HttpProxyHeader ( req )
@@ -437,7 +416,7 @@ export class proxyServer {
437416
if ( /^GET \/pac/.test ( dataStr )) {
438417
const httpHead = new HttpProxyHeader ( data )
439418
agent = httpHead.headers['user-agent']
440-
const sock5 = /Windows NT|Darwin|Firefox/i.test ( agent ) && ! /CFNetwork|WOW64/i.test ( agent )
419+
const sock5 = /Firefox/i.test (agent) || /Windows NT|Darwin|Firefox/i.test ( agent ) && ! /CFNetwork|WOW64/i.test ( agent )
441420

442421

443422
const ret = getPac ( httpHead.host, this.port, /pacHttp/.test( dataStr ), sock5 )
@@ -461,7 +440,7 @@ export class proxyServer {
461440
socks = null
462441
console.log ( `[${ip}] socket.on error`, err.message )
463442
})
464-
socket.once ('end', () => {
443+
socket.once ( 'end', () => {
465444
socks = null
466445
})
467446
})

app/public/scripts/home.js

Lines changed: 175 additions & 116 deletions
Large diffs are not rendered by default.

app/public/scripts/home.ts

Lines changed: 203 additions & 124 deletions
Large diffs are not rendered by default.

app/qtGate_emailClient.ts

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import * as Rfc1928 from './rfc1928'
2626
import * as res from './res'
2727
import * as Async from 'async'
2828
import * as imapClass from './imapClass'
29+
import * as Socks from './socket5ForiAtOpn'
30+
import { Socket } from 'net';
2931
const { remote } = require ( "electron" )
3032

3133
interface dnsAddress {
@@ -34,10 +36,6 @@ interface dnsAddress {
3436
expire: Date
3537
connect: number[]
3638
}
37-
interface domainData {
38-
dns: dnsAddress[]
39-
expire: number
40-
}
4139

4240
const whiteIpFile = 'whiteIpList.json'
4341
const testGatewayDomainName = 'www.google.com'
@@ -67,25 +65,35 @@ const closeClientSocket = ( socket: Net.Socket, status: number, body: string ) =
6765
return socket.resume ()
6866
}
6967

70-
const isAllBlackedByFireWall = ( hostName: string, ip6: boolean, checkAgainTime: number, imapClass: imapClass.imapClientControl, domainListPool: Map < string, domainData >,
68+
export const isAllBlackedByFireWall = ( hostName: string, ip6: boolean, checkAgainTime: number, imapClass: imapClass.imapClientControl, domainListPool: Map < string, domainData >,
7169
CallBack: ( err?: Error, hostIp?: domainData ) => void ) => {
7270

7371
const hostIp = domainListPool.get ( hostName )
7472
const now = new Date ().getTime ()
73+
7574
if ( ! hostIp || hostIp.expire < now ) {
76-
console.log (`imapClass.nslookupRequest [${ hostName }]`)
75+
console.log ( `imapClass.nslookupRequest [${ hostName }]`)
7776
return imapClass.nslookupRequest ( hostName, ( err, ipadd ) => {
78-
if ( err )
77+
if ( err ) {
78+
CallBack ( err )
7979
return console.log ( `imapClass.nslookupRequest callback err `, err )
80-
return CallBack ( err, ipadd )
80+
}
81+
82+
try {
83+
const ret = JSON.parse ( ipadd.toString())
84+
return CallBack ( null, ret )
85+
} catch ( ex ) {
86+
return CallBack ( ex )
87+
}
88+
8189
})
8290
}
8391

8492

8593
return CallBack ( null, hostIp )
8694
}
8795

88-
const checkDomainInBlackList = ( domainBlackList: string[], domain: string, CallBack ) => {
96+
export const checkDomainInBlackList = ( domainBlackList: string[], domain: string, CallBack ) => {
8997

9098
if ( Net.isIP ( domain )) {
9199
return CallBack ( null, domainBlackList.find ( n => { return n === domain }) ? true : false )
@@ -185,7 +193,7 @@ const _connect = ( hostname: string, hostIp: string, port: number, clientSocket:
185193
return socket.connect ( port, hostIp )
186194
}
187195

188-
const tryConnectHost = ( hostname: string, hostIp: domainData, port: number, data: Buffer, clientSocket: Net.Socket, isSSLConnect: boolean,
196+
export const tryConnectHost = ( hostname: string, hostIp: domainData, port: number, data: Buffer, clientSocket: Net.Socket, isSSLConnect: boolean,
189197
checkAgainTimeOut: number, connectTimeOut: number, gateway: boolean, CallBack ) => {
190198

191199
if ( isSSLConnect ) {
@@ -302,7 +310,23 @@ const httpImapProxy = ( imapClass: imapClass.imapClientControl, clientSocket: Ne
302310
})
303311

304312
}
305-
313+
const getPac = ( hostIp: string, port: number, http: boolean, sock5: boolean ) => {
314+
315+
const FindProxyForURL = `function FindProxyForURL ( url, host )
316+
{
317+
if ( isInNet ( dnsResolve( host ), "0.0.0.0", "255.0.0.0") ||
318+
isInNet( dnsResolve( host ), "172.16.0.0", "255.240.255.0") ||
319+
isInNet( dnsResolve( host ), "127.0.0.0", "255.255.255.0") ||
320+
isInNet ( dnsResolve( host ), "192.168.0.0", "255.255.0.0" ) ||
321+
isInNet ( dnsResolve( host ), "10.0.0.0", "255.0.0.0" )) {
322+
return "DIRECT";
323+
}
324+
return "${ http ? 'PROXY': ( sock5 ? 'SOCKS5' : 'SOCKS' ) } ${ hostIp }:${ port.toString() }";
325+
326+
}`
327+
//return "${ http ? 'PROXY': ( sock5 ? 'SOCKS5' : 'SOCKS' ) } ${ hostIp }:${ port.toString() }; ";
328+
return res.Http_Pac ( FindProxyForURL )
329+
}
306330
export default class imapProxyServer {
307331

308332
private hostLocalIpv4: { network: string, address: string } []= []
@@ -311,7 +335,7 @@ export default class imapProxyServer {
311335
private hostGlobalIpV6: string = null
312336
private network = false
313337
private getGlobalIpRunning = false
314-
private imapClass = new imapClass.imapClientControl ( this.imapData )
338+
public imapClass = new imapClass.imapClientControl ( this.imapData )
315339

316340
private saveWhiteIpList () {
317341
if ( this.whiteIpList.length > 0 )
@@ -334,15 +358,29 @@ export default class imapProxyServer {
334358
return _HTTP_200 ( FindProxyForURL )
335359
}
336360
*/
337-
constructor ( private whiteIpList: string[], private domainListPool: Map < string, domainData >,
338-
private port: number, private securityPath: string, private checkAgainTimeOut: number, private imapData: IinputData,
339-
private connectHostTimeOut: number, useGatWay: boolean, domainBlackList: string[] ) {
361+
constructor ( private whiteIpList: string[], public domainListPool: Map < string, domainData >,
362+
private port: number, private securityPath: string, public checkAgainTimeOut: number, private imapData: IinputData,
363+
public connectHostTimeOut: number, public useGatWay: boolean, public domainBlackList: string[] ) {
340364

341365
const server = Net.createServer ( socket => {
342366
const ip = socket.remoteAddress
343367
const isWhiteIp = this.whiteIpList.find ( n => { return n === ip }) ? true : false
344-
368+
let socks = null
369+
let agent = null
345370
socket.once ( 'data', ( data: Buffer ) => {
371+
const dataStr = data.toString()
372+
if ( /^GET \/pac/.test ( dataStr )) {
373+
const httpHead = new HttpProxyHeader ( data )
374+
agent = httpHead.headers['user-agent']
375+
const sock5 = /Firefox/i.test (agent) || /Windows NT|Darwin|Firefox/i.test ( agent ) && ! /CFNetwork|WOW64/i.test ( agent )
376+
377+
378+
const ret = getPac ( httpHead.host, this.port, /pacHttp/.test( dataStr ), sock5 )
379+
console.log ( `/GET \/pac from :[${ socket.remoteAddress }] sock5 [${ sock5 }] agent [${ agent }] httpHead.headers [${ Object.keys(httpHead.headers)}]`)
380+
console.log ( dataStr )
381+
console.log ( ret )
382+
return socket.end ( ret )
383+
}
346384
/*
347385
if ( ! isWhiteIp ) {
348386
console.log ('! isWhiteIp', data.toString ('utf8'))
@@ -357,14 +395,21 @@ export default class imapProxyServer {
357395
}
358396
*/
359397
switch ( data.readUInt8 ( 0 )) {
360-
case 0x4:
361-
return console.log ( 'SOCK4 connect' )
362-
case 0x5:
398+
case 0x4: {
399+
console.log ( 'SOCK4 connect' )
400+
return socks = new Socks.sockt4 ( socket, data, agent, this )
401+
}
402+
403+
case 0x5: {
363404
console.log ( 'socks5 connect' )
364-
return //new socks5 ( socket )
365-
default:
405+
return socks = new Socks.socks5 ( socket, agent, this )
406+
}
407+
408+
default: {
366409
return httpImapProxy ( this.imapClass, socket, data, useGatWay, this.hostGlobalIpV6 ? true : false, connectHostTimeOut,
367410
domainListPool, checkAgainTimeOut, domainBlackList )
411+
}
412+
368413
}
369414
})
370415

app/server.js

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const Path = require("path");
2121
const Os = require("os");
2222
const Http = require("http");
2323
const Fs = require("fs");
24-
const url_1 = require("url");
2524
const Async = require("async");
2625
const Util = require("util");
2726
const Https = require("https");
@@ -44,6 +43,7 @@ const ErrorLogFile = Path.join(QTGateFolder, 'systemError.log');
4443
const feedbackFilePath = Path.join(QTGateFolder, '.feedBack.json');
4544
const imapDataFileName = Path.join(QTGateFolder, 'imapData.pem');
4645
const sendMailAttach = Path.join(QTGateFolder, 'sendmail');
46+
const myIpServerUrl = ['https://ipinfo.io/ip', 'https://icanhazip.com/', 'https://diagnostic.opendns.com/myip', 'http://ipecho.net/plain', 'https://www.trackip.net/ip'];
4747
const keyServer = 'https://pgp.mit.edu';
4848
const QTGatePongReplyTime = 1000 * 30;
4949
let mainWindow = null;
@@ -88,9 +88,10 @@ const findPort = (port, CallBack) => {
8888
};
8989
const doUrlWithIp = (url, dns, CallBack) => {
9090
let ret = '';
91-
const option = new url_1.URL(url);
92-
option.hostname = null;
93-
option.host = dns;
91+
const option = {
92+
host: dns,
93+
path: url,
94+
};
9495
const res = res => {
9596
res.on('data', (data) => {
9697
ret += data.toString('utf8');
@@ -102,14 +103,35 @@ const doUrlWithIp = (url, dns, CallBack) => {
102103
if (/^https/i.test(option.protocol))
103104
return Https.request(option, res)
104105
.once('error', CallBack);
105-
return Http.get(option, res)
106+
return Http.request(option, res)
106107
.once('error', CallBack);
107108
};
108-
const getMyLocalIpAddress = (server, CallBack) => {
109-
saveLog(JSON.stringify(server));
109+
const doUrl = (url, CallBack) => {
110+
let ret = '';
111+
const res = res => {
112+
res.on('data', (data) => {
113+
ret += data.toString('utf8');
114+
});
115+
res.once('end', () => {
116+
return CallBack(null, ret);
117+
});
118+
};
119+
if (/^https/.test(url))
120+
return Https.get(url, res)
121+
.once('error', err => {
122+
console.log('on err ', err);
123+
return CallBack(err);
124+
});
125+
return Http.get(url, res)
126+
.once('error', err => {
127+
console.log('on err ', err);
128+
return CallBack(err);
129+
});
130+
};
131+
const myIpServer = (CallBack) => {
110132
let ret = false;
111-
Async.each(server, (n, next) => {
112-
doUrlWithIp(n.url, n.dnsName, (err, data) => {
133+
Async.each(myIpServerUrl, (n, next) => {
134+
doUrl(n, (err, data) => {
113135
if (err || !Net.isIPv4(data)) {
114136
return next();
115137
}
@@ -119,8 +141,7 @@ const getMyLocalIpAddress = (server, CallBack) => {
119141
}
120142
});
121143
}, () => {
122-
if (!ret)
123-
return CallBack(new Error('no IP'));
144+
return CallBack(new Error(''));
124145
});
125146
};
126147
const getQTGateSign = (_key) => {
@@ -381,7 +402,6 @@ class localServer {
381402
this.proxyServerWindow = null;
382403
this.connectCommand = null;
383404
this.proxyServer = null;
384-
this.myIpServer = null;
385405
this.ex_app = Express();
386406
this.ex_app.set('views', Path.join(__dirname, 'views'));
387407
this.ex_app.set('view engine', 'pug');
@@ -581,6 +601,7 @@ class localServer {
581601
return CallBack(10);
582602
}
583603
}
604+
CallBack(null);
584605
return this.doingCheck(id, imapData, socket);
585606
});
586607
socket.on('deleteImapAccount', uuid => {
@@ -607,7 +628,6 @@ class localServer {
607628
return this.QTClass.request(com, (err, res) => {
608629
saveLog(JSON.stringify(res.Args));
609630
CallBack(res.Args[0]);
610-
this.myIpServer = res.myIpServer;
611631
saveLog(`getAvaliableRegion ${JSON.stringify(res)} `);
612632
// Have gateway connect!
613633
if (res.Args[1]) {
@@ -659,7 +679,7 @@ class localServer {
659679
this.config.keypair.publicKey = key;
660680
this.config.keypair.verified = getQTGateSign(key);
661681
this.saveConfig();
662-
socket.emit('newKeyPairCallBack', this.config.keypair);
682+
socket.emit('KeyPairActiveCallBack', this.config.keypair);
663683
this.qtGateConnectEmitData.qtGateConnecting = 2;
664684
this.qtGateConnectEmitData.error = -1;
665685
return socket.emit('qtGateConnect', this.qtGateConnectEmitData);
@@ -714,7 +734,7 @@ class localServer {
714734
});
715735
};
716736
if (cmd.connectType === 2) {
717-
return getMyLocalIpAddress(this.myIpServer, (err, data) => {
737+
return myIpServer((err, data) => {
718738
saveLog(`getMyLocalIpAddress callback err [${JSON.stringify(err)}] data [${JSON.stringify(data)}]`);
719739
cmd.imapData.clientIpAddress = data;
720740
saveLog(JSON.stringify(cmd));
@@ -903,6 +923,7 @@ class localServer {
903923
}
904924
return this.emitQTGateToClient(socket, null);
905925
}
926+
saveLog('checkPemPassword');
906927
return Async.waterfall([
907928
(next) => {
908929
return this.getPbkdf2(password, next);
@@ -1209,12 +1230,12 @@ class localServer {
12091230
if (!imapData.imapCheck || !imapData.smtpCheck || !imapData.imapTestResult)
12101231
return saveLog(` emitQTGateToClient STOP with !imapData.imapCheck || !imapData.smtpCheck || !imapData.imapTestResult`);
12111232
const ret = {
1212-
qtgateConnectImapAccount: this.config.QTGateConnectImapUuid,
1233+
qtgateConnectImapAccount: imapData.uuid,
12131234
qtGateConnecting: !imapData.sendToQTGate ? 0 : 1,
12141235
isKeypairQtgateConform: this.config.keypair.verified,
12151236
error: null
12161237
};
1217-
saveLog(`doConnect!`);
1238+
saveLog(`doConnect! ret = [${JSON.stringify(ret)}]\n imapData.uuid= [${imapData.uuid}]`);
12181239
const doConnect = () => {
12191240
if (!this.imapDataPool.length)
12201241
return;
@@ -1253,12 +1274,13 @@ class localServer {
12531274
doConnect();
12541275
}
12551276
doingCheck(id, _imapData, socket) {
1256-
saveLog(`doingCheck`);
1277+
saveLog(`doingCheck id = [${id}] UUID [${_imapData.uuid}]`);
12571278
const imapData = this.imapDataPool[this.addInImapData(_imapData)];
12581279
imapData.imapCheck = imapData.smtpCheck = false;
12591280
imapData.imapTestResult = 0;
12601281
this.saveImapData();
12611282
return this.imapTest(imapData, (err, code) => {
1283+
saveLog(`imapTest finished! [${id}]`);
12621284
socket.emit(id + '-imap', err ? err : null, code);
12631285
imapData.imapTestResult = code;
12641286
imapData.imapCheck = code > 0;
@@ -1269,6 +1291,7 @@ class localServer {
12691291
socket.emit(id + '-smtp', err1 ? err1 : null);
12701292
imapData.smtpCheck = !err1;
12711293
this.saveImapData();
1294+
saveLog(`smtpVerify finished! [${id}]`);
12721295
if (err1)
12731296
return;
12741297
this.emitQTGateToClient(socket, _imapData.uuid);

0 commit comments

Comments
 (0)