@@ -29,7 +29,7 @@ import * as res from './res'
2929import * as Stream from 'stream'
3030import * as Fs from 'fs'
3131import * as Path from 'path'
32- import socket5 from './socket5 '
32+ import * as Socks from './socket5ForiOpn '
3333import gateWay from './gateway'
3434import * as Os from 'os'
3535const { remote } = require ( "electron" )
@@ -54,7 +54,7 @@ const IsSslConnect = ( buffer: Buffer ) => {
5454 return / ^ 1 6 0 3 ( 0 1 | 0 2 | 0 3 | 0 0 ) | ^ 8 0 ..0 1 0 3 | ^ ( 1 4 | 1 5 | 1 7 ) 0 3 ( 0 0 | 0 1 ) / . 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 ) =
140140const _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+ }
280286const 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 => {
0 commit comments