Skip to content

Commit 72265e6

Browse files
committed
update http proxy
1 parent 3ed157c commit 72265e6

File tree

3 files changed

+58
-82
lines changed

3 files changed

+58
-82
lines changed

src/localServer/proxyServer.ts

Lines changed: 53 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,68 @@ import { TransformCallback } from 'stream'
1515
import { ethers } from 'ethers'
1616
import EthCrypto from 'eth-crypto'
1717
import * 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

2132
const isSslFromBuffer = ( buffer ) => {
2233

2334
const request = buffer.toString()
2435
return /^CONNECT\ /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 (/^CONNECT /.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

5282
const 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

81109
const 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

130149
const 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-
232223
const 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"

src/localServer/socks.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616

1717
import * as Net from 'net'
1818
import * as Rfc1928 from './rfc1928'
19-
import * as res from './res'
2019
import * as Crypto from 'crypto'
21-
import httpProxyHeader from './httpProxy'
2220
import {proxyServer } from './proxyServer'
2321
import * as Util from 'util'
2422
import { logger, hexDebug } from './logger'
@@ -36,22 +34,6 @@ const isSslFromBuffer = ( buffer: Buffer ) => {
3634
return ret
3735
}
3836

39-
const getHostNameFromSslConnection = ( buffer: Buffer ) => {
40-
41-
if (!isSslFromBuffer(buffer)) {
42-
return null
43-
}
44-
const lengthPoint = buffer.readInt16BE(0x95)
45-
const serverName = buffer.slice (0x97, 0x97 +lengthPoint)
46-
// 00000090 00 02 01 00 00 0A 00 08 00 06 00 1D 00 17 00 18 ................
47-
// use IP address
48-
if (lengthPoint === 0x0A00 && serverName[0] === 0x8 && serverName[1] === 0x0) {
49-
return null
50-
}
51-
hexDebug(serverName)
52-
logger(`getHostNameFromSslConnection lengthPoint[${lengthPoint.toString(16)}] === 0x0A ${lengthPoint === 0x0A00} serverName[0] [${serverName[0].toString(16)}] serverName[0] === 0x8 ${serverName[0] === 0x8} && serverName[1] [${serverName[1].toString(16)}] === 0x06 [${serverName[1] === 0x0}] `)
53-
return serverName.toString()
54-
}
5537

5638
export class socks5 {
5739
private host
@@ -134,6 +116,7 @@ export class socks5 {
134116
ssl: isSslFromBuffer (_data),
135117
order: 0
136118
}
119+
137120
return this.proxyServer.requestGetWay ( uuuu, this.socket )
138121
})
139122

@@ -277,6 +260,7 @@ export class sockt4 {
277260
}
278261

279262
}
263+
280264
if ( ! this.keep ) {
281265
this.debug ? logger (colors.red(`STOP session`)): null
282266
this.socket.end ( this.req.request_failed )

src/localServer/userMining.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,12 +393,14 @@ export class miningV2_Class {
393393
private init = (privateKeyArmor: string) => {
394394
start (privateKeyArmor)
395395
}
396+
396397

397398
constructor(privateKeyArmor: string) {
398399
this.init(privateKeyArmor)
399400
}
400401

401402
public changeUsedNodes (region: string) {
403+
402404
SaaSNodes = new Map()
403405
const nodes = Guardian_Nodes.filter(n => n.region.split('.')[1] === region)
404406

@@ -415,6 +417,7 @@ export class miningV2_Class {
415417
if (node && isExisting < 0) {
416418
exportNodes.push(node)
417419
}
420+
418421
} while (exportNodes.length < 10 && exportNodes.length < nodes.length)
419422

420423
exportNodes.forEach(n => {

0 commit comments

Comments
 (0)