Skip to content

Commit 22d811a

Browse files
committed
added loader support
1 parent e63d67c commit 22d811a

File tree

113 files changed

+9814
-20654
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+9814
-20654
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@conet.project/conet-proxy",
33

4-
"version": "0.12.2",
4+
"version": "0.13.0",
55

66
"license": "UNLICENSED",
77
"files": [
@@ -13,7 +13,7 @@
1313
"scripts": {
1414
"lint": "echo 'no linter available'",
1515
"test": "echo 'no linter available'",
16-
"build": "tsc --project ./tsconfig.build.json && cp src/favicon.ico build/localServer/workers/ && cp -r src/localServer/workers/* build/localServer/workers/ && cp src/index.d.ts build",
16+
"build": "tsc --project ./tsconfig.build.json ; cp -r src/localServer/workers build/localServer/workers ; cp src/index.d.ts build ; cp src/favicon.ico build/localServer/workers/ ",
1717
"clean": "rm -rf ./node_modules ./build",
1818
"local": "node build/localServer/index",
1919
"buildRun": "tsc --project ./tsconfig.build.json && cp src/favicon.ico build/localServer/workers/ && cp src/localServer/workers/utilities/*.js build/localServer/workers/utilities && cp src/index.d.ts build && node build/localServer/index",

src/localServer/define.d.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ type CoNETIndexDBInit = {
591591
preferences: any
592592
}
593593

594-
type SICommandObj_Command = 'SaaS_Proxy'|'SaaS_Sock5'|'mining'|'mining_validator'
594+
type SICommandObj_Command = 'SaaS_Sock5'|'mining'|'mining_validator'|'SilentPass'
595595

596596
interface SICommandObj {
597597
command: SICommandObj_Command
@@ -695,13 +695,8 @@ interface VE_IPptpStream {
695695
buffer: string
696696
host: string
697697
port: number
698-
cmd: string
699698
//ATYP: number
700699
uuid?: string
701-
length?:number
702-
randomBuffer?: Buffer
703-
ssl: boolean
704-
order: number
705700
}
706701

707702
type proxyLogs = {
@@ -855,7 +850,7 @@ type ITypeTransferCount = {
855850
startTime: number
856851
endTime: number
857852
nodeIpaddress: string
858-
ssl: boolean
853+
ssl?: boolean
859854
}
860855

861856

src/localServer/layerMinus.ts

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import {ethers} from 'ethers'
2+
import {Socket, createConnection} from 'node:net'
3+
import {randomBytes, getRandomValues} from 'node:crypto'
4+
import * as openpgp from 'openpgp'
5+
import Colors from 'colors/safe'
6+
import {Transform, TransformCallback} from 'node:stream'
7+
import {logger} from './logger'
8+
9+
class transferCount extends Transform {
10+
public data = ''
11+
constructor(private upload: boolean, private info: ITypeTransferCount) {
12+
super()
13+
}
14+
15+
_transform(chunk: Buffer, encoding: BufferEncoding, callback: TransformCallback): void {
16+
if (this.upload) {
17+
this.info.upload += chunk.length
18+
} else {
19+
this.data += chunk.toString()
20+
this.info.download += chunk.length
21+
}
22+
callback(null, chunk)
23+
}
24+
}
25+
26+
const getRamdomNode = (nodes: nodes_info[]) => {
27+
const randomIndex = Math.floor (Math.random() * (nodes.length))
28+
return nodes[randomIndex]
29+
}
30+
31+
const encrypt_Message = async (encryptionKeys, message: any) => {
32+
const encryptObj = {
33+
message: await openpgp.createMessage({text: Buffer.from(JSON.stringify(message)).toString('base64')}),
34+
encryptionKeys,
35+
config: { preferredCompressionAlgorithm: openpgp.enums.compression.zlib}, // compress the data with zlib
36+
}
37+
return await openpgp.encrypt
38+
//@ts-ignore
39+
(encryptObj)
40+
}
41+
42+
const otherRequestForNet = ( data: string, host: string, port: number, UserAgent: string ) => {
43+
return `POST /post HTTP/1.1\r\n` +
44+
`Host: ${ host }${ port !== 80 ? ':'+ port : '' }\r\n` +
45+
`User-Agent: ${ UserAgent ? UserAgent : 'Mozilla/5.0' }\r\n` +
46+
`Content-Type: application/json;charset=UTF-8\r\n` +
47+
`Connection: keep-alive\r\n` +
48+
`Content-Length: ${ data.length }\r\n\r\n` +
49+
data + '\r\n\r\n'
50+
}
51+
52+
export class LayerMinus {
53+
54+
private wallet: ethers.Wallet
55+
constructor(private entryNodes: nodes_info[], private egressNodes: nodes_info[], privateKey: string) {
56+
this.wallet = new ethers.Wallet(privateKey)
57+
entryNodes.forEach(async n => n.publicKeyObj = await openpgp.readKey ({ armoredKey: n.armoredPublicKey }))
58+
egressNodes.forEach(async n => n.publicKeyObj = await openpgp.readKey ({ armoredKey: n.armoredPublicKey }))
59+
}
60+
61+
public connectToLayerMinus = async (
62+
client: Socket,
63+
host: string,
64+
port: number) => {
65+
logger(Colors.blue(`connectToLayerMinus ${host}:port `))
66+
const entryNode = getRamdomNode(this.entryNodes)
67+
const egressNode = getRamdomNode(this.egressNodes)
68+
const requestData : VE_IPptpStream[] = [{
69+
uuid: randomBytes(10).toString ('hex'),
70+
host,
71+
port,
72+
buffer: ''
73+
}]
74+
75+
const command: SICommandObj = {
76+
command: 'SilentPass',
77+
algorithm: 'aes-256-cbc',
78+
Securitykey: Buffer.from(getRandomValues(new Uint8Array(16))).toString('base64'),
79+
requestData,
80+
walletAddress: this.wallet.address.toLowerCase()
81+
}
82+
const hostInfo = `${host}:${port}`
83+
84+
const infoData: ITypeTransferCount = {
85+
hostInfo: hostInfo,
86+
startTime: new Date().getTime(),
87+
download: 0,
88+
upload: 0,
89+
nodeIpaddress: egressNode.ip_addr,
90+
endTime: 0
91+
}
92+
const message =JSON.stringify(command)
93+
const signMessage = await this.wallet.signMessage(message)
94+
const encryptedCommand = await encrypt_Message( egressNode.publicKeyObj, { message, signMessage })
95+
const data = otherRequestForNet(JSON.stringify({data: encryptedCommand}), entryNode.ip_addr, 80, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36')
96+
const upload = new transferCount (true, infoData)
97+
const download = new transferCount (false, infoData)
98+
99+
const remoteSocket = createConnection(80, entryNode.ip_addr, () => {
100+
remoteSocket.write(data)
101+
logger(Colors.blue(`ConnectToProxyNode connect to entryNode ${entryNode.ip_addr}`))
102+
client.pipe(upload).pipe(remoteSocket).pipe(download).pipe(client)
103+
104+
})
105+
106+
remoteSocket.on('error', err => {
107+
logger (Colors.red(`ConnectToProxyNode entry node [${entryNode.ip_addr}:${80}] on Error ${err.message} `))
108+
})
109+
110+
remoteSocket.once('close', async () => {
111+
logger (Colors.magenta(`ConnectToProxyNode entry node [${entryNode.ip_addr}:${80}] on Close `))
112+
//await sendTransferDataToLocalHost(infoData)
113+
})
114+
115+
116+
client.once ('error', err => {
117+
logger(Colors.magenta(`Proxy client on Error [${err.message}]! STOP connecting`))
118+
remoteSocket.end().destroy()
119+
})
120+
121+
}
122+
}
123+

src/localServer/localGateway.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,12 @@ const start = () => {
6060
start()
6161

6262

63+
// curl -v -x http://127.0.0.1:8888 "https://www.speedtest.net"
6364
// curl -v -x http://127.0.0.1:8888 "https://www.google.com"
6465
// curl -v -x socks4a://localhost:8888 "https://www.google.com"
6566
// curl -v -x socks4://localhost:8888 "https://www.google.com"
6667
// curl -v -x socks5h://localhost:8888 "https://www.google.com"
67-
// curl -v -x socks5h://localhost:3002 "https://www.google.com"
68+
// curl -v -x socks5h://localhost:3002 "https://www.google.com"
69+
70+
71+

src/localServer/proxy-server.ts

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
import { createServer, Socket, Server } from 'node:net'
2+
import {logger} from './logger'
3+
import {LayerMinus} from './layerMinus'
4+
export class ProxyServer {
5+
private server: Server
6+
private host = '0.0.0.0'
7+
8+
constructor(private port: number, private layerMinus: LayerMinus) {
9+
this.port = port
10+
this.server = createServer(this.handleClient)
11+
this.start()
12+
}
13+
14+
private start = () => {
15+
this.server.listen(this.port, this.host, () => {
16+
console.log(`🟢 Proxy server running at ${this.host}:${this.port}`)
17+
})
18+
}
19+
20+
private handleClient = (client: Socket) => {
21+
client.once('data', (data: Buffer) => {
22+
const firstByte = data[0]
23+
24+
if (firstByte === 0x05) {
25+
this.handleSocks5(client)
26+
} else if (firstByte === 0x04) {
27+
this.handleSocks4(client, data)
28+
} else {
29+
this.handleHttp(client, data)
30+
}
31+
})
32+
}
33+
34+
private handleSocks5 = (client: Socket) => {
35+
client.write(Buffer.from([0x05, 0x00]))
36+
37+
client.once('data', (req: Buffer) => {
38+
const addrType = req[3]
39+
let destAddr: string, destPort: number, offset: number
40+
41+
if (addrType === 0x01) {
42+
destAddr = `${req[4]}.${req[5]}.${req[6]}.${req[7]}`
43+
offset = 8
44+
} else if (addrType === 0x03) {
45+
const len = req[4]
46+
destAddr = req.slice(5, 5 + len).toString('utf8')
47+
offset = 5 + len
48+
} else {
49+
client.end()
50+
return
51+
}
52+
53+
destPort = req.readUInt16BE(offset)
54+
this.proxyConnection(client, destAddr, destPort, () => {
55+
client.write(Buffer.from([0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0, 0]))
56+
})
57+
})
58+
}
59+
60+
private handleSocks4 = (client: Socket, data: Buffer) => {
61+
const destPort = data.readUInt16BE(2)
62+
const destIP = data.slice(4, 8)
63+
const userIdEnd = data.indexOf(0x00, 8)
64+
const isSocks4a = destIP.slice(0, 3).every(b => b === 0) && destIP[3] !== 0
65+
66+
let destAddr: string
67+
68+
if (isSocks4a) {
69+
const domainStart = userIdEnd + 1
70+
const domainEnd = data.indexOf(0x00, domainStart)
71+
destAddr = data.slice(domainStart, domainEnd).toString('utf8')
72+
} else {
73+
destAddr = `${destIP[0]}.${destIP[1]}.${destIP[2]}.${destIP[3]}`
74+
}
75+
76+
this.proxyConnection(client, destAddr, destPort, () => {
77+
const reply = Buffer.alloc(8, 0x00)
78+
reply[1] = 0x5a
79+
client.write(reply)
80+
}, () => {
81+
const reply = Buffer.alloc(8, 0x00)
82+
reply[1] = 0x5b
83+
client.write(reply)
84+
client.end()
85+
})
86+
}
87+
88+
private handleHttp = (client: Socket, data: Buffer) => {
89+
const reqStr = data.toString('utf8')
90+
91+
if (reqStr.startsWith('CONNECT')) {
92+
const [_, dest] = reqStr.split(' ')
93+
const [host, port] = dest.split(':')
94+
95+
this.proxyConnection(client, host, parseInt(port), () => {
96+
client.write('HTTP/1.1 200 Connection Established\r\n\r\n')
97+
})
98+
} else {
99+
const hostLine = reqStr.split('\r\n').find(line => line.startsWith('Host:'))
100+
if (!hostLine) return client.end()
101+
102+
const host = hostLine.split(' ')[1]
103+
const port = 80
104+
105+
this.proxyConnection(client, host, port, (remote) => {
106+
remote.write(data)
107+
})
108+
}
109+
}
110+
111+
private proxyConnection = (
112+
client: Socket,
113+
host: string,
114+
port: number,
115+
onSuccess?: (remote: Socket) => void,
116+
onError?: () => void
117+
) => {
118+
119+
if (this.layerMinus) {
120+
return this.layerMinus.connectToLayerMinus(client, host, port)
121+
}
122+
const remote = new Socket()
123+
logger (`${host}:${port}`)
124+
125+
remote.connect(port, host, () => {
126+
onSuccess?.(remote)
127+
client.pipe(remote)
128+
remote.pipe(client)
129+
130+
})
131+
132+
remote.on('error', () => {
133+
onError?.()
134+
client.end()
135+
})
136+
137+
138+
}
139+
140+
private stop = () => {
141+
this.server.close(() => {
142+
console.log('🔴 Proxy server stopped')
143+
})
144+
}
145+
}
146+
147+
148+
/**
149+
* test
150+
* curl -v -x http://127.0.0.1:3002 "https://www.google.com"
151+
// curl -v -x socks4a://localhost:3002 "https://www.google.com"
152+
// curl -v -x socks4://localhost:3002 "https://www.google.com"
153+
// curl -v -x socks5h://localhost:3002 "https://www.google.com"
154+
155+
* curl -v -x http://127.0.0.1:3003 "https://www.google.com"
156+
curl -v -x http://127.0.0.1:3003 "http://www.google.com"
157+
// curl -v -x socks4a://localhost:3003 "https://www.google.com"
158+
// curl -v -x socks4://localhost:3003 "https://www.google.com"
159+
// curl -v -x socks5h://localhost:3003 "https://www.google.com"
160+
*
161+
*/
162+
163+
const test = () => {
164+
const entryNodes: nodes_info[] = [{
165+
"region": "NW.DE",
166+
"country": "DE",
167+
"ip_addr": "217.160.189.159",
168+
"armoredPublicKey": "-----BEGIN PGP PUBLIC KEY BLOCK-----\n\nxjMEZq2V5xYJKwYBBAHaRw8BAQdAhqIi6sQx/wqogD+T0Yftwsx7iBhd4Iyh\nlRCFnJKBODHNKjB4Y2JCQjEzNzE5NzNENTdlNmJENDVhQzBkZmVGRDQ5M2I1\nOUY5RDc2QsKMBBAWCgA+BYJmrZXnBAsJBwgJkArZXaLou3oNAxUICgQWAAIB\nAhkBApsDAh4BFiEExfcG2i3ma6s72VROCtldoui7eg0AAHFgAQCrT8y1Y69H\noXTHfdLuEk+XUDpq4CAvj7KkHxbPNQU+PQD/SdBbRUcvSkzzoU4tLcXxVI0Q\nST8za1hvo3RdWCglxAPOOARmrZXnEgorBgEEAZdVAQUBAQdAcLPhpj4WdcZN\nu7pP/LLYYjzg0JhyYvVpDwUoXa9WmkoDAQgHwngEGBYKACoFgmatlecJkArZ\nXaLou3oNApsMFiEExfcG2i3ma6s72VROCtldoui7eg0AADvRAQDrgO8K+hza\ntH4LTpGZ7OscC7M2ZtUV0zXshHlEnxS5NgD/ZCAHabk0Y47bANGG7KrcqsHY\n3pmfYRPFcvckAoPiagc=\n=VhCf\n-----END PGP PUBLIC KEY BLOCK-----\n",
169+
"last_online": false,
170+
"nftNumber": 100,
171+
"domain": "9977E9A45187DD80.conet.network"
172+
}]
173+
const egressNodes: nodes_info[] = [{
174+
"region": "MD.ES",
175+
"country": "ES",
176+
"ip_addr": "93.93.112.187",
177+
"armoredPublicKey": "-----BEGIN PGP PUBLIC KEY BLOCK-----\n\nxjMEZo9ITBYJKwYBBAHaRw8BAQdAtFGkXMLHSAJ3jMZAVmfMvtFF74PkpYR9\nT50s9Ndr6HnNKjB4NmJGM0FhNzI2MWUyMUJlNUZjNzgxQWMwOUY5NDc1YzhB\nMzRBZkVlYcKMBBAWCgA+BYJmj0hMBAsJBwgJkOe/gynD16TlAxUICgQWAAIB\nAhkBApsDAh4BFiEEpJqLA2EpKEPDlaCI57+DKcPXpOUAALCdAQCIFyD/LlbY\nRGWzyaS++BBNIslOoktpHxzcgS+sD7dJggEAxGvDZQiu42l7VlStvlN4J9Jr\nGWJy8opWUlghMFcZHgrOOARmj0hMEgorBgEEAZdVAQUBAQdAqtevF55R1RHW\nh3L8novWfriyXuVZJo/vwUTylQwdCggDAQgHwngEGBYKACoFgmaPSEwJkOe/\ngynD16TlApsMFiEEpJqLA2EpKEPDlaCI57+DKcPXpOUAAHHFAQCbOklWpmRw\niorLHhB99zbaNfsn9/F2uJwRs9U0/mBAhQEAg0VOc4nDfb9MD0tHTP6crD62\nFaYFiQ7vNSBo3DuXlw0=\n=XXSu\n-----END PGP PUBLIC KEY BLOCK-----\n",
178+
"last_online": false,
179+
"nftNumber": 101,
180+
"domain": "B4CB0A41352E9BDF.conet.network"
181+
}]
182+
const privateKey = '0xc3c55e163fa1ad5a08101b21eeb56756fb68605b0c8ce7b2bbbfa336f01b32c0'
183+
const layerMinus = new LayerMinus (entryNodes, egressNodes, privateKey)
184+
new ProxyServer(3002, layerMinus)
185+
}
186+
187+
test ()
188+

0 commit comments

Comments
 (0)