Skip to content

Commit 12031e1

Browse files
committed
added filter
1 parent 2a7ded9 commit 12031e1

19 files changed

+337
-166
lines changed

package.json

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

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

66
"license": "UNLICENSED",
77
"files": [

src/localServer/define.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ interface requestObj {
693693
interface VE_IPptpStream {
694694
type?: string
695695
buffer: string
696-
host: string|null
696+
host: string
697697
port: number
698698
cmd: string
699699
//ATYP: number
@@ -868,4 +868,9 @@ interface assetPrice {
868868
interface assetOracle {
869869
lastUpdate: number
870870
assets: assetPrice[]
871+
}
872+
873+
type filterRule = {
874+
DOMAIN: string[]
875+
IP: string[]
871876
}

src/localServer/localGateway.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,5 @@ start()
6363
// curl -v -x http://127.0.0.1:8888 "https://www.google.com"
6464
// curl -v -x socks4a://localhost:8888 "https://www.google.com"
6565
// curl -v -x socks4://localhost:8888 "https://www.google.com"
66-
// curl -v -x socks5h://localhost:8888 "https://www.google.com"
66+
// curl -v -x socks5h://localhost:8888 "https://www.google.com"
67+
// curl -v -x socks5h://localhost:3002 "https://www.google.com"

src/localServer/localServer.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ type Native_StartVPNObj = {
210210
exitNode: nodes_info[]
211211
}
212212

213+
type filterRule = {
214+
DOMAIN: string[]
215+
IP: string[]
216+
}
217+
213218
const appsPath: string = join ( __dirname )
214219
export class Daemon {
215220
private logsPool: proxyLogs[] = []
@@ -266,7 +271,21 @@ export class Daemon {
266271
return this.initialize ()
267272
})
268273

274+
app.post ( '/rule', ( req: any, res: any ) => {
275+
const vpnObj = req.body.data
276+
try {
277+
const data: filterRule = JSON.parse(vpnObj)
278+
logger(inspect(data, false, 3, true))
279+
if (_proxyServer) {
280+
_proxyServer.rule(data)
281+
}
282+
} catch (ex) {
283+
logger(`/rule JSON.parse(vpnObj) Error`)
284+
}
269285

286+
287+
return res.end()
288+
})
270289

271290
app.post ( '/postMessage', ( req: any, res: any ) => {
272291
const post_data: postData = req.body

src/localServer/proxyServer.ts

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { ethers } from 'ethers'
1616
import * as Crypto from 'crypto'
1717
import IP from 'ip'
1818
import {resolve4} from 'node:dns'
19+
import OS from 'node:os'
1920
import {createConnection} from 'node:net'
2021

2122
const _HTTP_200 = ( body: string ) => {
@@ -27,6 +28,11 @@ const _HTTP_200 = ( body: string ) => {
2728
return ret
2829
}
2930

31+
type filterRule = {
32+
DOMAIN: string[]
33+
IP: string[]
34+
}
35+
3036
const _HTTP_200V2 = `HTTP/1.1 200 Connection Established\r\n\r\n`
3137

3238

@@ -46,6 +52,8 @@ const getHostIpv4: (host: string) => Promise<string> = (host: string) => new Pro
4652
})
4753

4854

55+
56+
4957
const httpProxy = ( clientSocket: Net.Socket, _buffer: Buffer, agent: string, proxyServer: proxyServer) => {
5058
const httpHead = new HttpProxyHeader ( _buffer )
5159
const hostName = httpHead.host
@@ -280,6 +288,66 @@ const ConnectToProxyNode = (cmd : SICommandObj, SaaSnode: nodes_info, entryNode:
280288

281289
}
282290

291+
const isLocalhost = (hostname) => {
292+
if (hostname === 'localhost' || hostname === '127.0.0.1' || hostname === '::1') {
293+
return true
294+
}
295+
try {
296+
const hostIP = require('dns').lookupSync(hostname)
297+
const networkInterfaces = OS.networkInterfaces()
298+
const isPublic = IP.isPublic(hostIP)
299+
300+
if (!isPublic) {
301+
return true
302+
}
303+
for (const name of Object.keys(networkInterfaces)) {
304+
if (!networkInterfaces[name]) {
305+
continue
306+
}
307+
308+
for (const net of networkInterfaces[name]) {
309+
if (net.address === hostIP) {
310+
return true
311+
}
312+
}
313+
}
314+
return false
315+
} catch (error) {
316+
return false
317+
}
318+
}
319+
320+
321+
const ConnectViaLocal = (uuuu : VE_IPptpStream, resoestSocket: Net.Socket ) => {
322+
const port = uuuu.port
323+
const host = uuuu.host
324+
325+
const socket = createConnection ( port, host, () => {
326+
327+
socket.pipe(resoestSocket).pipe(socket)
328+
329+
const data = Buffer.from(uuuu.buffer, 'base64')
330+
if (data) {
331+
socket.write (data)
332+
}
333+
334+
resoestSocket.resume()
335+
})
336+
337+
socket.once ( 'end', () => {
338+
// logger (Colors.red(`socks5Connect host [${host}:${port}] on END!`))
339+
resoestSocket.end().destroy()
340+
})
341+
342+
socket.on ( 'error', err => {
343+
resoestSocket.end().destroy()
344+
logger (Colors.red(`socks5Connect [${host}:${port}] on Error! [${err.message}]`))
345+
})
346+
347+
348+
349+
350+
}
283351

284352
export class proxyServer {
285353

@@ -294,6 +362,7 @@ export class proxyServer {
294362
public useGatWay = true
295363
public clientSockets: Set<Net.Socket> = new Set()
296364
public currentWallet: ethers.Wallet
365+
public ruleData: filterRule|null = null
297366

298367
private startLocalProxy = async () => {
299368

@@ -363,8 +432,62 @@ export class proxyServer {
363432

364433
}
365434

435+
private checkRule = (host: string) => {
436+
437+
const isIpAddress = IP.isV4Format(host)
438+
439+
if (isIpAddress) {
440+
const isPublic = IP.isPublic(host)
441+
442+
if (!isPublic) {
443+
return true
444+
}
445+
446+
if (!this.ruleData) {
447+
return false
448+
}
449+
450+
const index = this.ruleData.IP.findIndex(n => IP.cidrSubnet(n).contains(host))
451+
if (index > -1) {
452+
return false
453+
}
454+
455+
return true
456+
}
457+
458+
const isLocal = isLocalhost (host)
459+
if (isLocal) {
460+
return true
461+
}
462+
463+
if (!this.ruleData) {
464+
return false
465+
}
466+
const hostName = host.toLowerCase()
467+
const index = this.ruleData.DOMAIN.findIndex(n => {
468+
const splitN = n.split('.').filter(n => n.length)
469+
const regExpData = splitN.join('\.')
470+
const regionRule = new RegExp(`(^|\\.)${regExpData}$`)
471+
return regionRule.test(hostName)
472+
})
473+
474+
if (index > -1 ) {
475+
return true
476+
}
477+
478+
return false
479+
}
480+
481+
366482
public requestGetWay = async (uuuu : VE_IPptpStream, socket: Net.Socket ) => {
367483

484+
485+
if (this.checkRule(uuuu.host)) {
486+
logger(`Direct to connect ${uuuu.host}:${uuuu.port} Server!`)
487+
return ConnectViaLocal(uuuu, socket)
488+
}
489+
logger(`package ${uuuu.host}:${uuuu.port} to Layer Minus Protocol!`)
490+
368491
const upChannel_SaaS_node = getRandomSaaSNode(this._egressNodes)
369492

370493

@@ -424,6 +547,11 @@ export class proxyServer {
424547

425548
}
426549
})
550+
551+
public rule = (_rule: filterRule) => {
552+
_rule.DOMAIN = _rule.DOMAIN.map(n=> n.toLowerCase())
553+
this.ruleData = _rule
554+
}
427555
}
428556

429557
// curl -v -x http://127.0.0.1:3002 "https://www.google.com"

src/localServer/socks.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,17 @@ export class socks5 {
101101
}
102102
}
103103

104-
104+
105105

106106
// PAYMENT REQUIRE
107107

108108

109109
this.socket.once ( 'data', ( _data: Buffer ) => {
110+
111+
if (!req.host) {
112+
return this.stopConnection(req)
113+
}
114+
110115
const uuuu : VE_IPptpStream = {
111116
uuid: this.uuid,
112117
host: req.host,
@@ -116,6 +121,8 @@ export class socks5 {
116121
ssl: isSslFromBuffer (_data),
117122
order: 0
118123
}
124+
125+
119126

120127
return this.proxyServer.requestGetWay ( uuuu, this.socket )
121128
})

src/localServer/workers/electron/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
<link rel="apple-touch-icon" href="/logo192.png" />
1111
<link rel="manifest" href="/manifest.json" />
1212
<title>Silent Pass</title>
13-
<script defer="defer" src="/static/js/main.f47ca665.js"></script>
14-
<link href="/static/css/main.18155f27.css" rel="stylesheet">
13+
<script defer="defer" src="/static/js/main.74dc99d2.js"></script>
14+
<link href="/static/css/main.db965d91.css" rel="stylesheet">
1515
</head>
1616

1717
<body><noscript>You need to enable JavaScript to run this app.</noscript>

src/localServer/workers/static/css/main.18155f27.css

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/localServer/workers/static/css/main.18155f27.css.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/localServer/workers/static/css/main.db965d91.css

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)