@@ -15,38 +15,68 @@ import { TransformCallback } from 'stream'
1515import { ethers } from 'ethers'
1616import EthCrypto from 'eth-crypto'
1717import * as Crypto from 'crypto'
18- import async from 'async'
19- import CONET_Guardian_NodeInfo_ABI from './abi/CONET_Guardian_NodeInfo.json'
18+ import IP from 'ip'
19+ import { resolve4 } from 'node:dns'
20+ import { createConnection } from 'node:net'
21+
22+ const _HTTP_200 = ( body : string ) => {
23+ const ret = `HTTP/1.1 200 OK\r\n` +
24+ `Content-Type: text/html; charset=UTF-8\r\n` +
25+ `Connection: keep-alive\r\n` +
26+ `Content-Length: ${ body . length } \r\n\r\n` +
27+ `${ body } `
28+ return ret
29+ }
30+
2031
2132const isSslFromBuffer = ( buffer ) => {
2233
2334 const request = buffer . toString ( )
2435 return / ^ C O N N E C T \ / i. test ( request )
2536}
37+ const getHostIpv4 : ( host : string ) => Promise < string > = ( host : string ) => new Promise ( resolve => {
38+ return resolve4 ( host , ( err , ipv4s ) => {
39+ if ( err || ! ipv4s ?. length ) {
40+ return resolve ( '' )
41+ }
2642
27- const httpProxy = ( clientSocket : Net . Socket , buffer : Buffer , proxyServer : proxyServer ) => {
28-
29-
30- const httpHead = new HttpProxyHeader ( buffer )
31- const hostName = httpHead . host
43+ return resolve ( ipv4s [ 0 ] )
44+ } )
45+ } )
3246
3347
34- const connect = ( _ , _data ?: Buffer ) => {
48+ const httpProxy = ( clientSocket : Net . Socket , _buffer : Buffer , agent : string , proxyServer : proxyServer ) => {
49+ const httpHead = new HttpProxyHeader ( _buffer )
50+ const hostName = httpHead . host
51+ const ssl = isSslFromBuffer ( _buffer )
52+ const connect = ( _data : Buffer ) => {
53+
3554 const uuuu : VE_IPptpStream = {
3655 uuid : Crypto . randomBytes ( 10 ) . toString ( 'hex' ) ,
3756 host : hostName ,
38- buffer : buffer . toString ( 'base64' ) ,
57+ buffer : _data . toString ( 'base64' ) ,
3958 cmd : httpHead . methods ,
4059 port : httpHead . Port ,
41- ssl : isSslFromBuffer ( _data ) ,
60+ ssl,
4261 order : 0
4362 }
63+ // socks5Connect(uuuu, clientSocket)
64+ // clientSocket.resume ()
4465 return proxyServer . requestGetWay ( uuuu , clientSocket )
4566 }
46-
47- return connect ( null , buffer )
48-
4967
68+ const reqtest = _buffer . toString ( )
69+ if ( / ^ C O N N E C T / . test ( reqtest ) ) {
70+
71+ clientSocket . once ( 'data' , data => {
72+ return connect ( data )
73+ } )
74+
75+ const response = _HTTP_200 ( '' )
76+ return clientSocket . write ( response )
77+ }
78+
79+ return connect ( _buffer )
5080}
5181
5282const getRandomSaaSNode = ( saasNodes : nodes_info [ ] ) => {
@@ -75,8 +105,6 @@ const getRandomNode = (activeNodes: nodes_info[], saasNode: nodes_info) => {
75105 return ret
76106}
77107
78- const CoNET_SI_Network_Domain = 'openpgp.online'
79- const conet_DL_getSINodes = `https://${ CoNET_SI_Network_Domain } :4001/api/conet-si-list`
80108
81109const getPac = ( hostIp : string , port : string , http : boolean , sock5 : boolean ) => {
82110
@@ -117,15 +145,6 @@ const makePrivateKeyObj = async ( privateArmor: string, password = '' ) => {
117145 return privateKey
118146}
119147
120- const getNodeByIpaddress = ( ipaddress : string , activeNodes : nodes_info [ ] ) => {
121-
122- const index = activeNodes . findIndex ( n => n . ip_addr === ipaddress )
123- if ( index > - 1 ) {
124- return activeNodes [ index ]
125- }
126- logger ( Colors . red ( `proxyServer getNodeByIpaddress [${ ipaddress } ] hasn't include at activeNodes list!` ) )
127-
128- }
129148
130149const encrypt_Message = async ( encryptionKeys , message : any ) => {
131150 const encryptObj = {
@@ -159,7 +178,9 @@ const createSock5ConnectCmd = async (currentProfile: profile, SaaSnode: nodes_in
159178 walletAddress : currentProfile . keyID . toLowerCase ( )
160179 }
161180
162- logger ( Colors . blue ( `createSock5ConnectCmd` ) )
181+
182+ logger ( Colors . blue ( `createSock5ConnectCmd data = ${ inspect ( requestData , false , 3 , true ) } ` ) )
183+
163184 const message = JSON . stringify ( command )
164185 const messageHash = ethers . id ( message )
165186 const signMessage = EthCrypto . sign ( currentProfile . privateKeyArmor , messageHash )
@@ -199,36 +220,6 @@ class transferCount extends Transform {
199220}
200221
201222
202- const sendTransferDataToLocalHost = ( infoData : ITypeTransferCount ) => {
203- return new Promise ( resolve => {
204- const option : RequestOptions = {
205- host : 'localhost' ,
206- method : 'POST' ,
207- port : '3001' ,
208- path : '/proxyusage' ,
209- headers : {
210- 'Content-Type' : 'application/json;charset=UTF-8' ,
211- 'Connection' : 'close' ,
212- }
213- }
214- const req = requestHttp ( option , res => {
215- if ( res . statusCode !== 200 ) {
216- return logger ( Colors . red ( `sendTransferDataToLocalHost res.statusCode [${ res . statusCode } ] !== 200 ERROR` ) )
217- }
218- logger ( Colors . blue ( `sendTransferDataToLocalHost SUCCESS` ) )
219- res . destroy ( )
220- resolve ( null )
221- } )
222-
223- req . on ( 'error' , err => {
224- logger ( Colors . red ( `sendTransferDataToLocalHost on Error!` ) , err )
225- } )
226-
227- req . end ( JSON . stringify ( { data :infoData } ) )
228- } )
229-
230- }
231-
232223const ConnectToProxyNode = ( cmd : SICommandObj , SaaSnode : nodes_info , entryNode : nodes_info , socket : Net . Socket , uuuu : VE_IPptpStream , server : proxyServer ) => {
233224
234225
@@ -344,10 +335,7 @@ export class proxyServer {
344335 }
345336
346337 default : {
347- const logStream = Colors . magenta ( `unsupport proxy protocol!` )
348- loggerToStream ( this . logStream , logStream )
349- logger ( logStream )
350- return socket . end ( )
338+ return httpProxy ( socket , data , agent , this )
351339 }
352340 }
353341 } )
@@ -393,7 +381,8 @@ export class proxyServer {
393381 if ( ! upChannel_SaaS_node ) {
394382 return logger ( Colors . red ( `proxyServer makeUpChannel upChannel_SaaS_node Null Error!` ) )
395383 }
396- logger ( inspect ( upChannel_SaaS_node , false , 3 , true ) )
384+
385+
397386
398387 const cmd = await createSock5ConnectCmd ( this . currentProfile , upChannel_SaaS_node , [ uuuu ] )
399388 if ( ! cmd ) {
@@ -404,9 +393,9 @@ export class proxyServer {
404393
405394 const streamString = Colors . blue ( `Create gateway request, Layer minus random SaaS node [${ Colors . magenta ( upChannel_SaaS_node . ip_addr ) } ] entry node [${ Colors . magenta ( entryNode . ip_addr ) } ]\n` )
406395
407- loggerToStream ( this . logStream , streamString )
408- logger ( streamString )
409-
396+ // loggerToStream(this.logStream, streamString)
397+ // logger(streamString)
398+ // logger(Colors.blue(`entryNode [${entryNode.ip_addr}] => Saas[${upChannel_SaaS_node.ip_addr}]`))
410399
411400 ConnectToProxyNode ( cmd , upChannel_SaaS_node , entryNode , socket , uuuu , this )
412401 }
@@ -443,4 +432,4 @@ export class proxyServer {
443432 } )
444433}
445434
446-
435+ // curl -v -x http://10.0.0.252:3002 "https://www.google.com"
0 commit comments