@@ -21,6 +21,7 @@ import * as Path from 'path'
2121import * as Os from 'os'
2222import * as Http from 'http'
2323import * as Fs from 'fs'
24+ import { URL } from 'url'
2425import * as Async from 'async'
2526import * as Util from 'util'
2627import * as Https from 'https'
@@ -47,7 +48,7 @@ const ErrorLogFile = Path.join ( QTGateFolder, 'systemError.log' )
4748const feedbackFilePath = Path . join ( QTGateFolder , '.feedBack.json' )
4849const imapDataFileName = Path . join ( QTGateFolder , 'imapData.pem' )
4950const 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+
5152const keyServer = 'https://pgp.mit.edu'
5253const 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 ( / ^ h t t p s / . 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 ( / ^ h t t p s / 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+
147144const 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-
17461726const _doUpdate1 = ( tag_name : string , port : number ) => {
17471727 let url = null
17481728
0 commit comments