Skip to content

Commit b6cd719

Browse files
Peter XiePeter Xie
authored andcommitted
v0.8.30
1 parent 42aad4b commit b6cd719

File tree

13 files changed

+387
-264
lines changed

13 files changed

+387
-264
lines changed

app/gateway.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ export default class gateWay {
8585

8686
const _socket = Net.createConnection ({ port: this.serverPort, host: this.serverIp }, () => {
8787
encrypt.write ( _data )
88-
console.log (`send data to remote!`)
89-
console.log(`*************\n${_data.toString ()}\n*********`)
88+
console.log ( `send data to remote!` )
89+
console.log ( `*************\n${_data.toString ()}\n*********`)
9090
})
9191

9292
_socket.once ( 'end', () => {
93-
console.log (`_socket.once end!`)
93+
console.log ( `_socket.once end!` )
9494
})
9595

9696
httpBlock.once ( 'error', err => {
@@ -103,8 +103,7 @@ export default class gateWay {
103103

104104
} )
105105
encrypt.pipe ( _socket ).pipe ( httpBlock ).pipe ( decrypt ).pipe ( finish )
106-
107-
106+
108107
}
109108

110109
public requestGetWay ( id: string, uuuu: VE_IPptpStream, userAgent: string, socket: Net.Socket ) {

app/proxyServer.ts

Lines changed: 30 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import * as res from './res'
2929
import * as Stream from 'stream'
3030
import * as Fs from 'fs'
3131
import * as Path from 'path'
32-
import socket5 from './socket5'
32+
import * as Socks from './socket5ForiOpn'
3333
import gateWay from './gateway'
3434
import * as Os from 'os'
3535
const { remote } = require ( "electron" )
@@ -54,7 +54,7 @@ const IsSslConnect = ( buffer: Buffer ) => {
5454
return /^1603(01|02|03|00)|^80..0103|^(14|15|17)03(00|01)/.test (kk)
5555
}
5656

57-
const checkDomainInBlackList = ( BlackLisk: string[], domain: string, CallBack ) => {
57+
export const checkDomainInBlackList = ( BlackLisk: string[], domain: string, CallBack ) => {
5858

5959
if ( Net.isIP ( domain )) {
6060
return CallBack ( null, BlackLisk.find ( n => { return n === domain }) ? true : false )
@@ -140,7 +140,7 @@ const closeClientSocket = ( socket: Net.Socket, status: number, body: string ) =
140140
const _connect = ( hostname: string, hostIp: string, port: number, clientSocket: Net.Socket, data: Buffer, connectHostTimeOut: number, CallBack ) => {
141141
console.log (`direct _connect!`)
142142
const socket = new Net.Socket()
143-
const ip = clientSocket.remoteAddress.split (':')[3]
143+
const ip = clientSocket.remoteAddress.split (':')[3]|| clientSocket.remoteAddress
144144
let err = null
145145
const id = `[${ ip }] => [${ hostname }:${ port }] `
146146
const hostInfo = `{${ hostIp }:${ port }}`
@@ -160,7 +160,7 @@ const _connect = ( hostname: string, hostIp: string, port: number, clientSocket:
160160
}
161161

162162
socket.on ( 'connect', () => {
163-
console.log (`${ id } socket.on connect!`)
163+
console.log ( `${ id } socket.on connect!`)
164164

165165
clearTimeout ( timeout )
166166

@@ -208,7 +208,7 @@ const _connect = ( hostname: string, hostIp: string, port: number, clientSocket:
208208
return socket.connect ( port, hostIp )
209209
}
210210

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

214214
if ( isSSLConnect ) {
@@ -264,7 +264,7 @@ const tryConnectHost = ( hostname: string, hostIp: domainData, port: number, dat
264264

265265
}
266266

267-
const isAllBlackedByFireWall = ( hostName: string, ip6: boolean, checkAgainTime: number, gatway: gateWay, userAgent: string, domainListPool: Map < string, domainData >,
267+
export const isAllBlackedByFireWall = ( hostName: string, ip6: boolean, gatway: gateWay, userAgent: string, domainListPool: Map < string, domainData >,
268268
CallBack: ( err?: Error, hostIp?: domainData ) => void ) => {
269269

270270
const hostIp = domainListPool.get ( hostName )
@@ -276,7 +276,13 @@ const isAllBlackedByFireWall = ( hostName: string, ip6: boolean, checkAgainTime:
276276
return CallBack ( null, hostIp )
277277
}
278278

279-
279+
const isSslFromBuffer = ( buffer ) => {
280+
console.log ( buffer.toString ('hex'))
281+
const ret = /^\x16[\x2c-\xff]\x01\x00[\x00-\x05].[\x00-\x09][\x00-\x1f]|^\x80[\x0f-\xff]\x01[\x00-\x09][\x00-\x1f][\x00-\x05].\x00.\x00./.test ( buffer )
282+
283+
console.log ( `ret [${ ret }]`)
284+
return ret
285+
}
280286
const httpProxy = ( clientSocket: Net.Socket, buffer: Buffer, useGatWay: boolean, ip6: boolean, connectTimeOut: number,
281287
domainListPool: Map < string, domainData >, gatway: gateWay, checkAgainTime: number, blackDomainList: string[] ) => {
282288

@@ -285,7 +291,7 @@ const httpProxy = ( clientSocket: Net.Socket, buffer: Buffer, useGatWay: boolean
285291
const userAgent = httpHead.headers [ 'user-agent' ]
286292

287293
const CallBack = ( err?: Error, _data?: Buffer ) => {
288-
294+
console.log ( `tryConnectHost callback err [${ err }], _data[${ _data }]`)
289295
if ( err ) {
290296

291297
if ( useGatWay && _data && _data.length && clientSocket.writable ) {
@@ -296,7 +302,7 @@ const httpProxy = ( clientSocket: Net.Socket, buffer: Buffer, useGatWay: boolean
296302
cmd: Rfc1928.CMD.CONNECT,
297303
ATYP: Rfc1928.ATYP.IP_V4,
298304
port: parseInt ( httpHead.Url.port || httpHead.isHttps ? '443' : '80' ),
299-
ssl: httpHead.isHttps
305+
ssl: isSslFromBuffer ( _data )
300306
}
301307

302308
const id = `[${ clientSocket.remoteAddress.split(':')[3] }:${ clientSocket.remotePort }][${ uuuu.uuid }] `
@@ -322,7 +328,7 @@ const httpProxy = ( clientSocket: Net.Socket, buffer: Buffer, useGatWay: boolean
322328

323329
if ( ! hostIp ) {
324330
console.log ( `domain connect to [${ hostName }]`)
325-
return isAllBlackedByFireWall ( hostName, ip6, checkAgainTime, gatway, userAgent, domainListPool, ( err, _hostIp ) => {
331+
return isAllBlackedByFireWall ( hostName, ip6, gatway, userAgent, domainListPool, ( err, _hostIp ) => {
326332
if ( err ) {
327333
console.log ( `[${ hostName }] Blocked!`)
328334
return closeClientSocket ( clientSocket, 504, null )
@@ -344,68 +350,16 @@ const httpProxy = ( clientSocket: Net.Socket, buffer: Buffer, useGatWay: boolean
344350
})
345351

346352
}
347-
/*
348-
const httpProxyTest = ( socket: Net.Socket, data: Buffer, gateway: gateWay ) => {
349-
const httpHead = new HttpProxyHeader ( data )
350-
const port = parseInt ( httpHead.Url.port || httpHead.isHttps ? '443' : '80' )
351-
const hostName = httpHead.Url.hostname
352-
const userAgent = httpHead.headers [ 'user-agent' ]
353-
let first = true
354-
355-
const localrequest = ( buf: Buffer ) => {
356-
if ( httpHead.isHttps && first ) {
357-
first = false
358-
console.log ('https connect!')
359-
socket.once ( 'data', _data => {
360-
return localrequest ( _data )
361-
})
362-
363-
return closeClientSocket ( socket, -200, '' )
364-
}
365-
const net = Net.createConnection ( port, hostName, () => {
366-
const ls = new Compress.printStream ('>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
367-
const ls1 = new Compress.printStream ('<<<<<<<<<<<<<<<<<<<<<<<<<<<<')
368-
net.pipe ( socket ).pipe( ls ).pipe ( net )
369-
net.write ( buf )
370-
})
371353

372-
}
373-
374-
const connectTestPort = ( buf: Buffer ) => {
375-
if ( httpHead.isHttps && first ) {
376-
first = false
377-
console.log ('https connect!')
378-
socket.once ( 'data', _data => {
379-
return connectTestPort ( _data )
380-
})
381-
382-
return closeClientSocket ( socket, -200, '' )
383-
}
384-
const uuuu : VE_IPptpStream = {
385-
uuid: Crypto.randomBytes (10).toString ('hex'),
386-
host: hostName,
387-
buffer: buf.toString ( 'base64' ),
388-
cmd: Rfc1928.CMD.CONNECT,
389-
ATYP: Rfc1928.ATYP.IP_V4,
390-
port: port
391-
}
392-
const id = `[${ socket.remoteAddress.split(':')[3] }:${ socket.remotePort }][${ uuuu.uuid }]`
393-
console.log ( uuuu.buffer )
394-
return gateway.requestGetWayTest ( id, uuuu, userAgent, socket )
395-
}
396-
397-
return connectTestPort ( data )
398-
}
399-
*/
400-
export default class proxyServer {
354+
export class proxyServer {
401355

402356
private hostLocalIpv4: { network: string, address: string } []= []
403357
private hostLocalIpv6: string = null
404358
private hostGlobalIpV4: string = null
405359
private hostGlobalIpV6: string = null
406360
private network = false
407361
private getGlobalIpRunning = false
408-
362+
public gateway = new gateWay ( this.serverIp, this.serverPort, this.password )
409363
private saveWhiteIpList () {
410364
if ( this.whiteIpList.length > 0 )
411365
Fs.writeFile ( Path.join( __dirname, whiteIpFile ), JSON.stringify( this.whiteIpList ), { encoding: 'utf8' }, err => {
@@ -456,13 +410,11 @@ export default class proxyServer {
456410
return _HTTP_200 ( FindProxyForURL )
457411
}
458412
*/
459-
constructor ( private whiteIpList: string[], private domainListPool: Map < string, domainData >,
460-
private port: number, private securityPath: string, private serverIp: string, private serverPort: number, private password: string, private checkAgainTimeOut: number,
461-
private connectHostTimeOut: number, useGatWay: boolean, domainBlackList: string[] ) {
462-
463-
const gateway = new gateWay ( serverIp, serverPort, password )
464-
465-
this.getGlobalIp ( gateway )
413+
constructor ( public whiteIpList: string[], public domainListPool: Map < string, domainData >,
414+
private port: number, private securityPath: string, private serverIp: string, private serverPort: number, private password: string, public checkAgainTimeOut: number,
415+
public connectHostTimeOut: number, public useGatWay: boolean, public domainBlackList: string[] ) {
416+
this.getGlobalIp ( this.gateway )
417+
let socks = null
466418
const server = Net.createServer ( socket => {
467419
const ip = socket.remoteAddress
468420
const isWhiteIp = this.whiteIpList.find ( n => { return n === ip }) ? true : false
@@ -483,18 +435,23 @@ export default class proxyServer {
483435
*/
484436
switch ( data.readUInt8 ( 0 )) {
485437
case 0x4:
438+
socks = new Socks.sockt4 ( socket, data, this )
486439
return console.log ( 'SOCK4 connect' )
487440
case 0x5:
488441
console.log ( 'socks5 connect' )
489-
return new socket5 ( socket )
442+
return socks = new Socks.socks5 ( socket, this )
490443
default:
491-
return httpProxy ( socket, data, useGatWay, this.hostGlobalIpV6 ? true : false, connectHostTimeOut, domainListPool, gateway, checkAgainTimeOut, domainBlackList )
444+
return httpProxy ( socket, data, useGatWay, this.hostGlobalIpV6 ? true : false, connectHostTimeOut, domainListPool, this.gateway, checkAgainTimeOut, domainBlackList )
492445
}
493446
})
494447

495448
socket.on ( 'error', err => {
449+
socks = null
496450
console.log ( `[${ip}] socket.on error`, err.message )
497451
})
452+
socket.once ('end', () => {
453+
socks = null
454+
})
498455
})
499456

500457
server.on ( 'error', err => {

app/public/scripts/home.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ const infoDefine = [
604604
GlobalIp: '本机互联网IP地址:',
605605
QTGateRegionERROR: ['发送连接请求Email到QTGate系统发生送信错误, 请检查您的IMAP账号的设定。',
606606
''],
607-
GlobalIpInfo: '注意:当您按下【QTGate连结】时您会把您的本机互联网IP提供给QTGate系统,如果您不愿意,请选择【@QTGate】技术来使用QTGate服务!没有【@QTGate】选项是因为@QTGate只能对应的iCloud邮箱。',
607+
GlobalIpInfo: '注意:当您按下【QTGate连结】时您会把您的本机互联网IP提供给QTGate系统,如果您不愿意,请选择【@QTGate】技术来使用QTGate服务!没有【@QTGate】选项是因为@QTGate技术只能对应iCloud邮箱。',
608608
sendConnectRequestMail: ['您的QTGate客户端没有和QTgate系统联机,客户端已向QTgate系统重新发出联机请求Email。和QTgate系统联机需要额外的时间,请耐心等待。',
609609
'当免费用户连续24小时内没有使用客户端,您的连接会被中断。付费用户情况下QTgate系统可保持持续联机一个月。'],
610610
cacheDatePlaceDate: [{ name: '1小时', id: 1 }, { name: '12小时', id: 12 }, { name: '1日', id: 24 }, { name: '15日', id: 360 }, { name: '1月', id: 720 }, { name: '6月', id: 4320 }, { name: '永远', id: -1 }],

app/public/scripts/home.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ const infoDefine = [
662662
GlobalIp: '本机互联网IP地址:',
663663
QTGateRegionERROR:['发送连接请求Email到QTGate系统发生送信错误, 请检查您的IMAP账号的设定。',
664664
''],
665-
GlobalIpInfo: '注意:当您按下【QTGate连结】时您会把您的本机互联网IP提供给QTGate系统,如果您不愿意,请选择【@QTGate】技术来使用QTGate服务!没有【@QTGate】选项是因为@QTGate只能对应的iCloud邮箱。',
665+
GlobalIpInfo: '注意:当您按下【QTGate连结】时您会把您的本机互联网IP提供给QTGate系统,如果您不愿意,请选择【@QTGate】技术来使用QTGate服务!没有【@QTGate】选项是因为@QTGate技术只能对应iCloud邮箱。',
666666
sendConnectRequestMail: ['您的QTGate客户端没有和QTgate系统联机,客户端已向QTgate系统重新发出联机请求Email。和QTgate系统联机需要额外的时间,请耐心等待。',
667667
'当免费用户连续24小时内没有使用客户端,您的连接会被中断。付费用户情况下QTgate系统可保持持续联机一个月。'],
668668
cacheDatePlaceDate: [{ name:'1小时', id: 1 }, { name:'12小时', id: 12 },{ name:'1日', id: 24 }, { name:'15日', id: 360 }, { name:'1月', id: 720 }, { name:'6月', id: 4320 }, { name:'永远', id: -1 }],

app/qtGate_emailClient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import * as Fs from 'fs'
1919
import * as Path from 'path'
2020
import * as Dns from 'dns'
2121
import * as Net from 'net'
22-
import socks5 from './socket5'
22+
//import socks5 from './socket5'
2323
import * as Crypto from 'crypto'
2424
import HttpProxyHeader from './httpProxy'
2525
import * as Rfc1928 from './rfc1928'
@@ -361,7 +361,7 @@ export default class imapProxyServer {
361361
return console.log ( 'SOCK4 connect' )
362362
case 0x5:
363363
console.log ( 'socks5 connect' )
364-
return new socks5 ( socket )
364+
return //new socks5 ( socket )
365365
default:
366366
return httpImapProxy ( this.imapClass, socket, data, useGatWay, this.hostGlobalIpV6 ? true : false, connectHostTimeOut,
367367
domainListPool, checkAgainTimeOut, domainBlackList )

app/rfc1928.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@
151151
if ( this.ATYP !== ATYP.IP_V4 )
152152
return
153153
const y = n.split ('.')
154-
const ret = new Buffer ('00000000','hex')
154+
const ret = new Buffer ( '00000000','hex' )
155155
for ( let i = 0; i < 4; i ++ ) {
156156
const k = parseInt (y[i])
157157
if ( !k || k < 0 || k > 255 )
@@ -171,4 +171,49 @@
171171

172172

173173
}
174+
175+
export class socket4Requests {
176+
constructor ( public buffer: Buffer ) {}
177+
public get socketVersion () {
178+
return this.buffer.readUInt8 ( 0 )
179+
}
180+
public get IsSocket4 () {
181+
return this.buffer.readUInt8 ( 0 ) === 0x04
182+
}
183+
public get cmd () {
184+
return this.buffer.readUInt8 ( 1 )
185+
}
186+
public get port () {
187+
return this.buffer.readUInt16BE ( 2 )
188+
}
189+
public get targetIp () {
190+
const uu = `${ this.buffer.readUInt8 (4).toString() }.${ this.buffer.readUInt8 (5).toString() }.${ this.buffer.readUInt8 (6).toString() }.${ this.buffer.readUInt8 (7).toString() }`
191+
if ( /^0.0.0/.test (uu))
192+
return null
193+
return uu
194+
}
195+
public get domainName () {
196+
if ( ! this.targetIp ) {
197+
return this.buffer.slice (9).toString ()
198+
}
199+
return null
200+
}
201+
202+
public request_granted ( targetIp: string, targetPort: number) {
203+
if ( !targetIp )
204+
return Buffer.from ('005a0000000000','hex')
205+
const ret = Buffer.from ('005a000000000000','hex')
206+
ret.writeUInt16BE ( targetPort, 2 )
207+
const u = targetIp.split ('.')
208+
for ( let i = 4, l = 0; i < 8; i ++, l ++ ) {
209+
210+
ret.writeUInt8 ( parseInt (u[l]), i )
211+
}
212+
return ret
213+
}
214+
public get request_failed () {
215+
return Buffer.from ('005b000000','hex')
216+
}
217+
218+
}
174219

app/server.js

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const Express = require('express');
3434
const cookieParser = require('cookie-parser');
3535
const Uuid = require('node-uuid');
3636
const { remote } = require('electron');
37-
const DEBUG = false;
37+
const DEBUG = true;
3838
const QTGateFolder = Path.join(Os.homedir(), '.QTGate');
3939
const QTGateSignKeyID = /3acbe3cbd3c1caa9/i;
4040
const configPath = Path.join(QTGateFolder, 'config.json');
@@ -48,30 +48,6 @@ const QTGatePongReplyTime = 1000 * 30;
4848
let mainWindow = null;
4949
const createWindow = () => {
5050
remote.getCurrentWindow().createWindow();
51-
/*
52-
mainWindow = new remote.BrowserWindow ({
53-
width: 850,
54-
height: 480,
55-
minWidth: 850,
56-
minHeight: 480,
57-
show: false,
58-
backgroundColor: '#ffffff',
59-
icon: process.platform === 'linux' ? Path.join ( __dirname, 'app/public/assets/images/512x512.png' ) : Path.join ( __dirname, 'app/qtgate.icns' )
60-
})
61-
62-
mainWindow.loadURL ( `http://127.0.0.1:${ port }/` )
63-
if ( debug ) {
64-
mainWindow.webContents.openDevTools()
65-
mainWindow.maximize()
66-
}
67-
68-
mainWindow.once ( 'closed', () => {
69-
mainWindow = null
70-
})
71-
mainWindow.once ('ready-to-show', () => {
72-
mainWindow.show()
73-
})
74-
*/
7551
};
7652
const _doUpdate = (tag) => {
7753
saveLog(`_doUpdate tag = [${tag}]`);

0 commit comments

Comments
 (0)