Skip to content

Commit

Permalink
v0.8.40
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Xie authored and Peter Xie committed Oct 25, 2017
1 parent a13143c commit e7f2298
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 67 deletions.
3 changes: 1 addition & 2 deletions app/httpProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export default class httpProxy {
}

get isConnect () {
console.log (`isConnect:`, this.commandWithLine)

return ( /^connect /i.test ( this.commandWithLine[0] ) )
}

Expand Down Expand Up @@ -169,7 +169,6 @@ export default class httpProxy {
const vv = this.commandWithLine[0].split(':')
if ( vv.length > 1 ) {
const kk = vv[1].split (' ')[0]
console.log ( `kk = [${ kk }]`)
return parseInt ( kk )
}

Expand Down
60 changes: 44 additions & 16 deletions app/proxyServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const testGatewayDomainName = 'www.google.com'
export const checkDomainInBlackList = ( BlackLisk: string[], domain: string, CallBack ) => {

if ( Net.isIP ( domain )) {
console.log ( `checkDomainInBlackList domain [${ domain }] is IP address! `)

return CallBack ( null, BlackLisk.find ( n => { return n === domain }) ? true : false )
}

Expand Down Expand Up @@ -281,7 +281,7 @@ const httpProxy = ( clientSocket: Net.Socket, buffer: Buffer, useGatWay: boolean
const userAgent = httpHead.headers [ 'user-agent' ]

const CallBack = ( err?: Error, _data?: Buffer ) => {
console.log ( `tryConnectHost callback err [${ err }], _data[${ _data }]`)

if ( err ) {

if ( useGatWay && _data && _data.length && clientSocket.writable ) {
Expand All @@ -296,7 +296,6 @@ const httpProxy = ( clientSocket: Net.Socket, buffer: Buffer, useGatWay: boolean
}

const id = `[${ clientSocket.remoteAddress.split(':')[3] }:${ clientSocket.remotePort }][${ uuuu.uuid }] `
console.log (uuuu)
return gatway.requestGetWay ( id, uuuu, userAgent, clientSocket )

}
Expand All @@ -318,7 +317,7 @@ const httpProxy = ( clientSocket: Net.Socket, buffer: Buffer, useGatWay: boolean
const hostIp: domainData = ! isIp ? domainListPool.get ( hostName ) : { dns: [{ family: isIp, address: hostName, expire: null, connect: [] }], expire: null }

if ( ! hostIp ) {
console.log ( `domain connect to [${ hostName }]`)

return isAllBlackedByFireWall ( hostName, ip6, gatway, userAgent, domainListPool, ( err, _hostIp ) => {
if ( err ) {
console.log ( `[${ hostName }] Blocked!`)
Expand All @@ -335,17 +334,40 @@ const httpProxy = ( clientSocket: Net.Socket, buffer: Buffer, useGatWay: boolean
return tryConnectHost ( hostName, _hostIp, port, buffer, clientSocket, httpHead.isConnect, checkAgainTime, connectTimeOut, useGatWay, CallBack )
})
}
console.log ( `checkDomainInBlackList CallBack hostName[${ hostName }] is IP address, now do tryConnectHost `)

return tryConnectHost ( hostName, hostIp, port, buffer, clientSocket, httpHead.isConnect, checkAgainTime, connectTimeOut, useGatWay, CallBack )

})

}


const getPac = ( hostIp: string, port: number, http: boolean ) => {

const FindProxyForURL = `function FindProxyForURL ( url, host ) { return "${ http ? 'PROXY': 'SOCKS' } ${ hostIp }:${ port.toString() }";}`
/*
declare const isInNet:( a: string, y: string, z: string ) => string
declare const dnsResolve :( a: any ) => string
function FindProxyForURL ( url, host )
{
if ( isInNet ( dnsResolve( host ), "0.0.0.0", "255.0.0.0") ||
isInNet( dnsResolve( host ), "172.16.0.0", "255.240.255.0") ||
isInNet( dnsResolve( host ), "127.0.0.0", "255.255.255.0") ||
isInNet ( dnsResolve( host ), "192.168.0.0", "255.255.0.0" ) ||
isInNet ( dnsResolve( host ), "10.0.0.0", "255.0.0.0" )) {
return "DIRECT";
}
return "${ http ? 'PROXY': ( sock5 ? 'SOCKS5' : 'SOCKS' ) } ${ hostIp }:${ port.toString() }";
}
*/
const getPac = ( hostIp: string, port: number, http: boolean, sock5: boolean ) => {

const FindProxyForURL = `function FindProxyForURL ( url, host )
{
if ( isInNet ( dnsResolve( host ), "0.0.0.0", "255.0.0.0") ||
isInNet( dnsResolve( host ), "172.16.0.0", "255.240.255.0") ||
isInNet( dnsResolve( host ), "127.0.0.0", "255.255.255.0") ||
isInNet ( dnsResolve( host ), "192.168.0.0", "255.255.0.0" ) ||
isInNet ( dnsResolve( host ), "10.0.0.0", "255.0.0.0" )) {
return "DIRECT";
}
return "${ http ? 'PROXY': ( sock5 ? 'SOCKS5' : 'SOCKS' ) } ${ hostIp }:${ port.toString() }";
}`

return res._HTTP_200 ( FindProxyForURL )
}
Expand Down Expand Up @@ -408,22 +430,28 @@ export class proxyServer {
const server = Net.createServer ( socket => {
const ip = socket.remoteAddress
const isWhiteIp = this.whiteIpList.find ( n => { return n === ip }) ? true : false

let agent = 'Mozilla/5.0'
console.log (`new socket!`)
socket.once ( 'data', ( data: Buffer ) => {
const dataStr = data.toString()
if ( /^GET \/pac/.test ( dataStr )) {
let ret = getPac ( this.localProxyServerIP, this.port, false )
const httpHead = new HttpProxyHeader ( data )
agent = httpHead.headers['user-agent']
const sock5 = /Windows NT|Darwin/i.test ( agent ) && ! /CFNetwork/i.test (agent)

let ret = getPac ( this.localProxyServerIP, this.port, false, sock5 )
if ( /pacHttp/.test( dataStr ))
ret = getPac ( this.localProxyServerIP, this.port, true )
saveLog ( `/GET \/pac from :[${ socket.remoteAddress }]`)
ret = getPac ( this.localProxyServerIP, this.port, true, sock5 )
console.log ( `/GET \/pac from :[${ socket.remoteAddress }] sock5 [${ sock5 }] agent [${ agent }] httpHead.headers [${ Object.keys(httpHead.headers)}]`)
console.log ( dataStr )
return socket.end ( ret )
}

switch ( data.readUInt8 ( 0 )) {
case 0x4:
return socks = new Socks.sockt4 ( socket, data, this )
return socks = new Socks.sockt4 ( socket, data, agent, this )
case 0x5:
return socks = new Socks.socks5 ( socket, this )
return socks = new Socks.socks5 ( socket, agent, this )
default:
return httpProxy ( socket, data, useGatWay, this.hostGlobalIpV6 ? true : false, connectHostTimeOut, domainListPool, this.gateway, checkAgainTimeOut, domainBlackList )
}
Expand Down
24 changes: 12 additions & 12 deletions app/public/scripts/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,9 +620,9 @@ const infoDefine = [
useInfoMacOS: {
title: '<p>本地代理服务器已在后台运行,MacOS和Windows用户可以关闭本窗口。</p>您的其他电子设备,可通过设置本地Proxy伺服器,来使用QTGate连接到互联网',
title1: 'MacOS 本地代理服务器设定',
proxyServerIp: '本地代理服务器地址:',
proxyServerPort: '本地代理服务器端口:',
proxyServerPassword: '本地代理服务器登陆用户名和密码:无需设定',
proxyServerIp: '<p>代理设置选择:<span style="color: red;">自动代理设置</p>',
proxyServerPort: 'HTTP和HTTPS代理设定:',
proxyServerPassword: 'SOCKS代理设定:',
info: [{
title: '打开控制面板,点击网络',
titleImage: '/images/macOsControl.jpg',
Expand Down Expand Up @@ -834,9 +834,9 @@ const infoDefine = [
useInfoMacOS: {
title: 'ローカルプロキシサーバはバックグランドで実行しています。MacoSとWindowsユーザーはこのウィンドウを閉じても構わないです。他のデバイスはローカルプロキシに設定による、QTGate利用してインターネットへアクセスができます。',
title1: 'MacOS プロキシ設定',
proxyServerIp: 'プロキシサーバアドレス:',
proxyServerPort: 'サーバポート:',
proxyServerPassword: '登録ユーザ名とパスワード:なし',
proxyServerIp: '<p>プロキシの設定に:<span style="color:red;">自動設置</span></p>',
proxyServerPort: 'HTTPとHTTPSプロキシは:',
proxyServerPassword: 'SOCKSプロキシは:',
info: [{
title: 'コントロールパネルを開いて、ネットワークをクリックしてください。',
titleImage: '/images/macOsControl.jpg',
Expand Down Expand Up @@ -1324,9 +1324,9 @@ const infoDefine = [
}]
},
useInfoMacOS: {
proxyServerIp: 'Proxy server address:',
proxyServerPort: 'Server port:',
proxyServerPassword: 'Proxy server login username and password: none',
proxyServerIp: '<p>Proxy setup: <span style="color: red;">Automatic or Auto-Config</span></p>',
proxyServerPort: 'HTTP & HTTPS proxy setup:',
proxyServerPassword: 'SOCKS proxy setup:',
title: 'Local proxy server is running at background. MacOS and windows user may close this window. All other devices can doing internet via local proxy setup use the QTGate system.',
title1: 'MacOS proxy setup',
info: [{
Expand Down Expand Up @@ -1693,9 +1693,9 @@ const infoDefine = [
useInfoMacOS: {
title: '本地代理伺服器已在後台運行,MacOS和Windows用戶可以關閉本窗口。您的其他電子設備,可通過設置本地Proxy伺服器,來使用QTGate連接到互聯網',
title1: 'MacOS 本地代理伺服器設定',
proxyServerIp: '本地代理伺服器地址:',
proxyServerPort: '本地代理伺服器端口:',
proxyServerPassword: '本地代理伺服器登陸用戶名和密碼:無需設定',
proxyServerIp: '<p>代理設定選擇:<span style="color: red;">自動設定</p>',
proxyServerPort: 'HTTP和HTTPS代理的設定為:',
proxyServerPassword: 'SOCKS代理的設定為:',
info: [{
title: '打開控制面板,點擊【網絡】',
titleImage: '/images/macOsControl.jpg',
Expand Down
24 changes: 12 additions & 12 deletions app/public/scripts/home.ts
Original file line number Diff line number Diff line change
Expand Up @@ -680,9 +680,9 @@ const infoDefine = [
useInfoMacOS: {
title:'<p>本地代理服务器已在后台运行,MacOS和Windows用户可以关闭本窗口。</p>您的其他电子设备,可通过设置本地Proxy伺服器,来使用QTGate连接到互联网',
title1:'MacOS 本地代理服务器设定',
proxyServerIp:'本地代理服务器地址:',
proxyServerPort: '本地代理服务器端口:',
proxyServerPassword: '本地代理服务器登陆用户名和密码:无需设定',
proxyServerIp:'<p>代理设置选择:<span style="color: red;">自动代理设置</p>',
proxyServerPort: 'HTTP和HTTPS代理设定:',
proxyServerPassword: 'SOCKS代理设定:',
info:[{
title:'打开控制面板,点击网络',
titleImage:'/images/macOsControl.jpg',
Expand Down Expand Up @@ -900,9 +900,9 @@ const infoDefine = [
useInfoMacOS: {
title:'ローカルプロキシサーバはバックグランドで実行しています。MacoSとWindowsユーザーはこのウィンドウを閉じても構わないです。他のデバイスはローカルプロキシに設定による、QTGate利用してインターネットへアクセスができます。',
title1:'MacOS プロキシ設定',
proxyServerIp:'プロキシサーバアドレス:',
proxyServerPort: 'サーバポート:',
proxyServerPassword: '登録ユーザ名とパスワード:なし',
proxyServerIp:'<p>プロキシの設定に:<span style="color:red;">自動設置</span></p>',
proxyServerPort: 'HTTPとHTTPSプロキシは:',
proxyServerPassword: 'SOCKSプロキシは:',
info:[{
title:'コントロールパネルを開いて、ネットワークをクリックしてください。',
titleImage:'/images/macOsControl.jpg',
Expand Down Expand Up @@ -1404,9 +1404,9 @@ const infoDefine = [
}]
},
useInfoMacOS: {
proxyServerIp:'Proxy server address:',
proxyServerPort: 'Server port:',
proxyServerPassword: 'Proxy server login username and password: none',
proxyServerIp:'<p>Proxy setup: <span style="color: red;">Automatic or Auto-Config</span></p>',
proxyServerPort: 'HTTP & HTTPS proxy setup:',
proxyServerPassword: 'SOCKS proxy setup:',
title:'Local proxy server is running at background. MacOS and windows user may close this window. All other devices can doing internet via local proxy setup use the QTGate system.',
title1:'MacOS proxy setup',
info:[{
Expand Down Expand Up @@ -1788,9 +1788,9 @@ const infoDefine = [
useInfoMacOS: {
title:'本地代理伺服器已在後台運行,MacOS和Windows用戶可以關閉本窗口。您的其他電子設備,可通過設置本地Proxy伺服器,來使用QTGate連接到互聯網',
title1:'MacOS 本地代理伺服器設定',
proxyServerIp:'本地代理伺服器地址:',
proxyServerPort: '本地代理伺服器端口:',
proxyServerPassword: '本地代理伺服器登陸用戶名和密碼:無需設定',
proxyServerIp:'<p>代理設定選擇:<span style="color: red;">自動設定</p>',
proxyServerPort: 'HTTP和HTTPS代理的設定為:',
proxyServerPassword: 'SOCKS代理的設定為:',
info:[{

title:'打開控制面板,點擊【網絡】',
Expand Down
2 changes: 1 addition & 1 deletion app/qtGate_emailClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ const httpImapProxy = ( imapClass: imapClass.imapClientControl, clientSocket: Ne
const isIp = Net.isIP ( hostName )
const hostIp: domainData = ! isIp ? domainListPool.get ( hostName ) : { dns: [{ family: isIp, address: hostName, expire: null, connect: [] }], expire: null }
if ( ! hostIp ) {
console.log ( `domain connect to [${ hostName }]`)

return isAllBlackedByFireWall ( hostName, false, checkAgainTimeOut, imapClass, domainListPool, ( err, _hostIp ) => {
if ( err ) {
console.log ( `isAllBlackedByFireWall [${ hostName }] got Error!`, err )
Expand Down
2 changes: 1 addition & 1 deletion app/rfc1928.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@

public request_4_granted ( targetIp: string, targetPort: number) {
if ( !targetIp )
return Buffer.from ('005a0000000000','hex')
return Buffer.from ('005a000000000000','hex')
const ret = Buffer.from ('005a000000000000','hex')
ret.writeUInt16BE ( targetPort, 2 )
const u = targetIp.split ('.')
Expand Down
2 changes: 1 addition & 1 deletion app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const Net = require("net");
const Imap = require("./imap");
const freePort = require("portastic");
const Stream = require("stream");
const DEBUG = false;
const DEBUG = true;
const openpgp = require('openpgp');
const Express = require('express');
const cookieParser = require('cookie-parser');
Expand Down
25 changes: 11 additions & 14 deletions app/socket5ForiOpn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class socks5 {

const id = `[${ this.clientIP }:${ this.port }][${ Util.inspect(uuuu) }] `

return this.proxyServer.gateway.requestGetWay ( id, uuuu, 'Mozilla/5.0', this.socket )
return this.proxyServer.gateway.requestGetWay ( id, uuuu, this.agent, this.socket )

}

Expand Down Expand Up @@ -108,7 +108,7 @@ export class socks5 {
return this.closeSocks5 ( retBuffer.buffer )
}
if ( this.host ) {
return proxyServer.isAllBlackedByFireWall ( this.host, false, this.proxyServer.gateway, 'Mozilla/5.0', this.proxyServer.domainListPool, ( err, _hostIp ) => {
return proxyServer.isAllBlackedByFireWall ( this.host, false, this.proxyServer.gateway, this.agent, this.proxyServer.domainListPool, ( err, _hostIp ) => {
if ( err ) {
console.log ( `[${ this.host }] Blocked!`)
retBuffer.REP = Rfc1928.Replies.CONNECTION_NOT_ALLOWED_BY_RULESET
Expand Down Expand Up @@ -150,10 +150,11 @@ export class socks5 {
//.serverIP = this.socket.localAddress.split (':')[3]

// IPv6 not support!
console.log ( this.cmd )

switch ( this.cmd ) {

case Rfc1928.CMD.CONNECT: {
console.log (`sock5 [${ this.host }]`)
this.keep = true
break
}
Expand Down Expand Up @@ -183,7 +184,7 @@ export class socks5 {
return this.connectStat2_after ( req )
}

constructor ( private socket: Net.Socket, private proxyServer: proxyServer.proxyServer ) {
constructor ( private socket: Net.Socket,private agent: string, private proxyServer: proxyServer.proxyServer ) {

this.socket.once ( 'data', ( chunk: Buffer ) => {
return this.connectStat2 ( chunk )
Expand All @@ -202,7 +203,7 @@ export class sockt4 {
private targetDomainData: domainData = null
private clientIP = this.socket
private keep = false
constructor ( private socket: Net.Socket, private buffer: Buffer, private proxyServer: proxyServer.proxyServer ) {
constructor ( private socket: Net.Socket, private buffer: Buffer, private agent: string, private proxyServer: proxyServer.proxyServer ) {
switch ( this.cmd ) {
case Rfc1928.CMD.CONNECT: {
this.keep = true
Expand Down Expand Up @@ -242,11 +243,8 @@ export class sockt4 {
port: this.port,
ssl: isSslFromBuffer ( _data )
}

const id = `[${ this.clientIP }:${ this.port }][${ uuuu.uuid }] `
console.log ( ` ${id} [${ this.host }]`, 'try use gateway\n' )
return this.proxyServer.gateway.requestGetWay ( id, uuuu, 'Mozilla/5.0', this.socket )

return this.proxyServer.gateway.requestGetWay ( id, uuuu, this.agent, this.socket )
}

return this.socket.end ( res.HTTP_403 )
Expand All @@ -255,11 +253,10 @@ export class sockt4 {
}

this.socket.once ( 'data', ( _data: Buffer ) => {
console.log ( _data.toString ())
console.log (`connectStat2 [${ this.host||this.targetIpV4 }]get data `)
proxyServer.tryConnectHost ( this.host, this.targetDomainData, this.port, _data, this.socket, false, this.proxyServer.checkAgainTimeOut,
this.proxyServer.connectHostTimeOut, this.proxyServer.useGatWay, CallBack )
})
console.log ( `this.socket.write ( this.req.request_granted )`)
const buffer = this.req.request_4_granted ( !this.host ? null: this.targetDomainData.dns[0].address, this.port )
this.socket.write ( buffer )
return this.socket.resume ()
Expand All @@ -268,15 +265,14 @@ export class sockt4 {
if ( this.host ) {
this.targetDomainData = this.proxyServer.domainListPool.get ( this.host )
}
console.log ( `connectStat1 `)
return proxyServer.checkDomainInBlackList ( this.proxyServer.domainBlackList, this.host || this.targetIpV4, ( err, result: boolean ) => {
if ( result ) {
console.log ( `[${ this.host }] Blocked!`)
return this.socket.end ( this.req.request_failed )
}
if ( this.host ) {
console.log ( `this.host [${ this.host }]`)
return proxyServer.isAllBlackedByFireWall ( this.host, false, this.proxyServer.gateway, 'Mozilla/5.0', this.proxyServer.domainListPool, ( err, _hostIp ) => {
console.log (`socks4 host [${ this.host }]`)
return proxyServer.isAllBlackedByFireWall ( this.host, false, this.proxyServer.gateway, this.agent, this.proxyServer.domainListPool, ( err, _hostIp ) => {
if ( err ) {
console.log ( `[${ this.host }] Blocked!`)
return this.socket.end ( this.req.request_failed )
Expand All @@ -292,6 +288,7 @@ export class sockt4 {
return this.connectStat2 ()
})
}
console.log (`socks4 ipaddress [${ this.targetIpV4 }]`)
return this.connectStat2 ()

})
Expand Down
Loading

0 comments on commit e7f2298

Please sign in to comment.