Skip to content

Commit 1dacc8e

Browse files
Peter XiePeter Xie
authored andcommitted
v0.8.56 my Ip Server support
1 parent 7dbfd4f commit 1dacc8e

File tree

4 files changed

+94
-119
lines changed

4 files changed

+94
-119
lines changed

app/server.js

Lines changed: 36 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ 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");
2425
const Async = require("async");
2526
const Util = require("util");
2627
const Https = require("https");
@@ -43,7 +44,6 @@ const ErrorLogFile = Path.join(QTGateFolder, 'systemError.log');
4344
const feedbackFilePath = Path.join(QTGateFolder, '.feedBack.json');
4445
const imapDataFileName = Path.join(QTGateFolder, 'imapData.pem');
4546
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;
@@ -86,36 +86,29 @@ const findPort = (port, CallBack) => {
8686
return findPort(port, CallBack);
8787
});
8888
};
89-
const doUrl = (url, CallBack) => {
89+
const doUrlWithIp = (url, dns, CallBack) => {
9090
let ret = '';
91-
if (/^https/.test(url))
92-
return Https.get(url, res => {
93-
res.on('data', (data) => {
94-
ret += data.toString('utf8');
95-
});
96-
res.once('end', () => {
97-
return CallBack(null, ret);
98-
});
99-
}).once('error', err => {
100-
console.log('on err ');
101-
return CallBack(err);
102-
});
103-
return Http.get(url, res => {
91+
const option = new url_1.URL(url);
92+
option.hostname = null;
93+
option.host = dns;
94+
const res = res => {
10495
res.on('data', (data) => {
10596
ret += data.toString('utf8');
10697
});
10798
res.once('end', () => {
10899
return CallBack(null, ret);
109100
});
110-
}).once('error', err => {
111-
console.log('on err ');
112-
return CallBack(err);
113-
});
101+
};
102+
if (/^https/i.test(option.protocol))
103+
return Https.request(option, res)
104+
.once('error', CallBack);
105+
return Http.get(option, res)
106+
.once('error', CallBack);
114107
};
115-
const myIpServer = (CallBack) => {
108+
const getMyLocalIpAddress = (server, CallBack) => {
116109
let ret = false;
117-
Async.each(myIpServerUrl, (n, next) => {
118-
doUrl(n, (err, data) => {
110+
Async.each(server, (n, next) => {
111+
doUrlWithIp(n.url, n.dnsName, (err, data) => {
119112
if (err || !Net.isIPv4(data)) {
120113
return next();
121114
}
@@ -125,7 +118,8 @@ const myIpServer = (CallBack) => {
125118
}
126119
});
127120
}, () => {
128-
return CallBack(new Error('no IP'));
121+
if (!ret)
122+
return CallBack(new Error('no IP'));
129123
});
130124
};
131125
const getQTGateSign = (_key) => {
@@ -386,6 +380,7 @@ class localServer {
386380
this.proxyServerWindow = null;
387381
this.connectCommand = null;
388382
this.proxyServer = null;
383+
this.myIpServer = null;
389384
this.ex_app = Express();
390385
this.ex_app.set('views', Path.join(__dirname, 'views'));
391386
this.ex_app.set('view engine', 'pug');
@@ -585,15 +580,7 @@ class localServer {
585580
return CallBack(10);
586581
}
587582
}
588-
return myIpServer((err, ip) => {
589-
if (err || !ip) {
590-
saveLog('startCheckImap isOnline false!');
591-
return CallBack(2);
592-
}
593-
CallBack(null);
594-
this.clientIpAddress = ip;
595-
return this.doingCheck(id, imapData, socket);
596-
});
583+
return this.doingCheck(id, imapData, socket);
597584
});
598585
socket.on('deleteImapAccount', uuid => {
599586
if (!uuid && !uuid.length) {
@@ -619,6 +606,8 @@ class localServer {
619606
return this.QTClass.request(com, (err, res) => {
620607
saveLog(JSON.stringify(res.Args));
621608
CallBack(res.Args[0]);
609+
this.myIpServer = res.myIpServer;
610+
saveLog(`getAvaliableRegion ${JSON.stringify(res)} `);
622611
// Have gateway connect!
623612
if (res.Args[1]) {
624613
const uu = res.Args[1];
@@ -702,19 +691,9 @@ class localServer {
702691
if (this.proxyServer) {
703692
return;
704693
}
705-
return myIpServer((err, ipAddress) => {
706-
if (err) {
707-
return saveLog(`myIpServer return error: [${err.message}]`);
708-
}
709-
if (cmd.connectType === 2) {
710-
if (!Net.isIPv4(ipAddress)) {
711-
ipAddress = ipAddress.split('\n')[0];
712-
}
713-
cmd.imapData.clientIpAddress = ipAddress;
714-
}
715-
cmd.imapData.randomPassword = Crypto1.randomBytes(15).toString('hex');
716-
cmd.account = this.config.keypair.email.toLocaleLowerCase();
717-
saveLog(`ipAddress = [${ipAddress}] Buffer [] = ${Buffer.from(ipAddress).toString('hex')}`);
694+
cmd.imapData.randomPassword = Crypto1.randomBytes(15).toString('hex');
695+
cmd.account = this.config.keypair.email.toLocaleLowerCase();
696+
const request = () => {
718697
const com = {
719698
command: 'connectRequest',
720699
Args: [cmd],
@@ -732,7 +711,15 @@ class localServer {
732711
}
733712
saveLog(`res.error [${res.error}]`);
734713
});
735-
});
714+
};
715+
if (cmd.connectType === 2) {
716+
return getMyLocalIpAddress(this.myIpServer, (err, data) => {
717+
cmd.imapData.clientIpAddress = data;
718+
saveLog(JSON.stringify(cmd));
719+
//return request ()
720+
});
721+
}
722+
return request();
736723
});
737724
socket.on('disconnectClick', () => {
738725
this.stopGetwayConnect();
@@ -1019,12 +1006,9 @@ class localServer {
10191006
createWindow();
10201007
return saveLog(`checkConfig keyPair Error! [${JSON.stringify(err)}]`);
10211008
}
1022-
return myIpServer((err, ipaddress) => {
1023-
this.config.keypair = keyPair;
1024-
this.clientIpAddress = this.config.serverGlobalIpAddress = ipaddress;
1025-
this.saveConfig();
1026-
return createWindow();
1027-
});
1009+
this.config.keypair = keyPair;
1010+
this.saveConfig();
1011+
return createWindow();
10281012
});
10291013
return createWindow();
10301014
}

app/server.ts

Lines changed: 45 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import * as Path from 'path'
2121
import * as Os from 'os'
2222
import * as Http from 'http'
2323
import * as Fs from 'fs'
24+
import { URL } from 'url'
2425
import * as Async from 'async'
2526
import * as Util from 'util'
2627
import * as Https from 'https'
@@ -47,7 +48,7 @@ const ErrorLogFile = Path.join ( QTGateFolder, 'systemError.log' )
4748
const feedbackFilePath = Path.join ( QTGateFolder,'.feedBack.json')
4849
const imapDataFileName = Path.join ( QTGateFolder, 'imapData.pem' )
4950
const sendMailAttach = Path.join ( QTGateFolder, 'sendmail')
50-
const myIpServerUrl = [ 'https://ipinfo.io/ip', 'https://icanhazip.com/', 'https://diagnostic.opendns.com/myip', 'http://ipecho.net/plain', 'https://www.trackip.net/ip' ]
51+
5152
const keyServer = 'https://pgp.mit.edu'
5253
const QTGatePongReplyTime = 1000 * 30
5354

@@ -100,37 +101,31 @@ const findPort = ( port: number, CallBack ) => {
100101
})
101102
}
102103

103-
const doUrl = ( url: string, CallBack) => {
104+
105+
const doUrlWithIp = ( url: string, dns: string, CallBack ) => {
104106
let ret = ''
105-
if ( /^https/.test( url ))
106-
return Https.get ( url, res => {
107-
res.on('data', (data: Buffer) => {
108-
ret += data.toString('utf8')
109-
})
110-
res.once ( 'end', () => {
111-
return CallBack( null, ret )
112-
})
113-
}).once ( 'error', err => {
114-
console.log('on err ')
115-
return CallBack ( err )
116-
})
117-
return Http.get ( url, res => {
118-
res.on ('data', (data: Buffer) => {
107+
const option = new URL ( url )
108+
option.hostname = null
109+
option.host = dns
110+
const res = res => {
111+
res.on ( 'data', ( data: Buffer) => {
119112
ret += data.toString('utf8')
120113
})
121-
res.once ('end', () => {
122-
return CallBack(null, ret)
114+
res.once ( 'end', () => {
115+
return CallBack ( null, ret )
123116
})
124-
}).once ( 'error', err => {
125-
console.log( 'on err ' )
126-
return CallBack ( err )
127-
})
117+
}
118+
if ( /^https/i.test( option.protocol ))
119+
return Https.request ( option, res )
120+
.once ( 'error', CallBack )
121+
return Http.get ( option, res )
122+
.once ( 'error', CallBack )
128123
}
129124

130-
const myIpServer = ( CallBack ) => {
125+
const getMyLocalIpAddress = ( server: QTGate_DnsAddress[], CallBack ) => {
131126
let ret = false
132-
Async.each ( myIpServerUrl, ( n, next ) => {
133-
doUrl( n, ( err, data ) => {
127+
Async.each ( server, ( n: QTGate_DnsAddress, next ) => {
128+
doUrlWithIp ( n.url, n.dnsName, ( err, data ) => {
134129
if ( err || ! Net.isIPv4 ( data )) {
135130
return next ()
136131
}
@@ -140,10 +135,12 @@ const myIpServer = ( CallBack ) => {
140135
}
141136
})
142137
}, () => {
143-
return CallBack ( new Error ('no IP'))
138+
if ( ! ret )
139+
return CallBack ( new Error ('no IP'))
144140
})
145141
}
146142

143+
147144
const getQTGateSign = ( _key ) => {
148145
const key = openpgp.key.readArmored (_key).keys
149146
if (! key || ! key.length )
@@ -436,6 +433,7 @@ export class localServer {
436433
private proxyServerWindow = null
437434
public connectCommand: IConnectCommand = null
438435
public proxyServer: RendererProcess = null
436+
public myIpServer: QTGate_DnsAddress [] = null
439437

440438
public saveConfig () {
441439
Fs.writeFile ( configPath, JSON.stringify ( this.config ) , { encoding: 'utf8' }, err => {
@@ -617,16 +615,8 @@ export class localServer {
617615
}
618616
}
619617

620-
return myIpServer ((err, ip ) => {
621-
622-
if ( err || !ip ) {
623-
saveLog ( 'startCheckImap isOnline false!' )
624-
return CallBack (2)
625-
}
626-
CallBack ( null )
627-
this.clientIpAddress = ip
628-
return this.doingCheck ( id, imapData, socket )
629-
})
618+
return this.doingCheck ( id, imapData, socket )
619+
630620

631621

632622
})
@@ -658,6 +648,8 @@ export class localServer {
658648

659649
saveLog ( JSON.stringify ( res.Args ))
660650
CallBack ( res.Args[0] )
651+
this.myIpServer = res.myIpServer
652+
saveLog (`getAvaliableRegion ${ JSON.stringify ( res )} `)
661653
// Have gateway connect!
662654
if ( res.Args[ 1 ]) {
663655
const uu: IConnectCommand = res.Args[1]
@@ -754,32 +746,17 @@ export class localServer {
754746
if ( this.proxyServer ) {
755747
return
756748
}
757-
758-
return myIpServer (( err, ipAddress: string ) => {
759-
if ( err ) {
760-
return saveLog ( `myIpServer return error: [${ err.message }]`)
761-
}
762749

763-
if ( cmd.connectType === 2 ) {
764-
if ( ! Net.isIPv4 ( ipAddress )) {
765-
ipAddress = ipAddress.split ('\n')[0]
766-
}
767-
cmd.imapData.clientIpAddress = ipAddress
768-
}
769-
770-
cmd.imapData.randomPassword = Crypto1.randomBytes (15).toString('hex')
771-
cmd.account = this.config.keypair.email.toLocaleLowerCase()
772-
saveLog (`ipAddress = [${ ipAddress }] Buffer [] = ${ Buffer.from ( ipAddress ).toString ('hex')}`)
773-
750+
cmd.imapData.randomPassword = Crypto1.randomBytes (15).toString('hex')
751+
cmd.account = this.config.keypair.email.toLocaleLowerCase()
752+
753+
const request = () => {
774754
const com: QTGateAPIRequestCommand = {
775755
command: 'connectRequest',
776756
Args: [ cmd ],
777757
error: null,
778-
779758
requestSerial: Crypto1.randomBytes(8).toString('hex')
780759
}
781-
782-
783760
return this.QTClass.request ( com, ( err: number, res: QTGateAPIRequestCommand ) => {
784761
const arg: IConnectCommand = res.Args[0]
785762
arg.localServerIp = getLocalInterface ()[0]
@@ -793,8 +770,17 @@ export class localServer {
793770
}
794771
saveLog ( `res.error [${ res.error }]`)
795772
})
773+
}
774+
if ( cmd.connectType === 2 ) {
775+
return getMyLocalIpAddress ( this.myIpServer, ( err, data ) => {
776+
cmd.imapData.clientIpAddress = data
777+
saveLog ( JSON.stringify ( cmd ))
778+
//return request ()
779+
})
796780

797-
})
781+
}
782+
return request ()
783+
798784

799785
})
800786

@@ -1137,14 +1123,9 @@ export class localServer {
11371123
createWindow ()
11381124
return saveLog( `checkConfig keyPair Error! [${ JSON.stringify ( err )}]`)
11391125
}
1140-
1141-
1142-
return myIpServer(( err, ipaddress ) => {
1143-
this.config.keypair = keyPair
1144-
this.clientIpAddress = this.config.serverGlobalIpAddress = ipaddress
1145-
this.saveConfig()
1146-
return createWindow ( )
1147-
})
1126+
this.config.keypair = keyPair
1127+
this.saveConfig()
1128+
return createWindow ( )
11481129
})
11491130

11501131
return createWindow ( )
@@ -1742,7 +1723,6 @@ class ImapConnect extends Imap.imapPeer {
17421723
}
17431724

17441725

1745-
17461726
const _doUpdate1 = ( tag_name: string, port: number ) => {
17471727
let url = null
17481728

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "qtgate",
3-
"version": "0.8.55",
3+
"version": "0.8.56",
44
"license": "MIT",
55
"description": "QTGate desktop client",
66
"scripts": {
@@ -18,7 +18,7 @@
1818
},
1919
"build": {
2020
"squirrelWindows": {
21-
"remoteReleases": "https://github.com/QTGate/QTGate-Desktop-Client/releases/download/v0.8.10/"
21+
"remoteReleases": "https://github.com/QTGate/QTGate-Desktop-Client/releases/download/v0.8.51/"
2222
},
2323
"appId": "com.qtgate.client",
2424
"win": {

0 commit comments

Comments
 (0)