-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
frankiejun
committed
Aug 13, 2024
1 parent
c6c93a0
commit 4bf121e
Showing
1 changed file
with
65 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,79 @@ | ||
const net = require('net'); | ||
const WebSocket = require('ws'); | ||
const logcb = (...args) => console.log.bind(this, ...args); | ||
const errcb = (...args) => console.error.bind(this, ...args); | ||
const net = require('net') | ||
const WebSocket = require('ws') | ||
const logcb = (...args) => console.log.bind(this, ...args) | ||
const errcb = (...args) => console.error.bind(this, ...args) | ||
|
||
const uuid = (process.env.UUID || '069c70e7-77a0-4850-a7b1-9af1e0120783').replace(/-/g, ''); | ||
const port = process.env.PORT || 18619; | ||
console.log(uuid); | ||
const wss = new WebSocket.Server({ port }, logcb('listen:', port)); | ||
const uuid = ( | ||
process.env.UUID || '069c70e7-77a0-4850-a7b1-9af1e0120783' | ||
).replace(/-/g, '') | ||
const port = process.env.PORT || 18619 | ||
|
||
wss.on('connection', ws => { | ||
console.log("on connection"); | ||
const wss = new WebSocket.Server({ port }, logcb('listen:', port)) | ||
|
||
let duplex, targetConnection; | ||
wss.on('connection', (ws) => { | ||
console.log('on connection') | ||
|
||
const cleanup = () => { | ||
if (duplex) { | ||
duplex.destroy(); // 销毁 duplex 流以释放资源 | ||
} | ||
if (targetConnection) { | ||
targetConnection.end(); // 结束与目标主机的连接 | ||
} | ||
ws.terminate(); // 终止 WebSocket 连接 | ||
}; | ||
let duplex, targetConnection | ||
|
||
ws.once('message', msg => { | ||
const [VERSION] = msg; | ||
const id = msg.slice(1, 17); | ||
const cleanup = () => { | ||
if (duplex) { | ||
duplex.destroy() // 销毁 duplex 流以释放资源 | ||
} | ||
if (targetConnection) { | ||
targetConnection.end() // 结束与目标主机的连接 | ||
} | ||
ws.terminate() // 终止 WebSocket 连接 | ||
} | ||
|
||
if (!id.every((v, i) => v === parseInt(uuid.substr(i * 2, 2), 16))) { | ||
ws.close(); | ||
return; | ||
} | ||
ws.once('message', (msg) => { | ||
const [VERSION] = msg | ||
const id = msg.slice(1, 17) | ||
|
||
let i = msg.slice(17, 18).readUInt8() + 19; | ||
const targetPort = msg.slice(i, i += 2).readUInt16BE(0); | ||
const ATYP = msg.slice(i, i += 1).readUInt8(); | ||
const host = ATYP === 1 ? msg.slice(i, i += 4).join('.') : // IPV4 | ||
(ATYP === 2 ? new TextDecoder().decode(msg.slice(i + 1, i += 1 + msg.slice(i, i + 1).readUInt8())) : // domain | ||
(ATYP === 3 ? msg.slice(i, i += 16).reduce((s, b, i, a) => (i % 2 ? s.concat(a.slice(i - 1, i + 1)) : s), []).map(b => b.readUInt16BE(0).toString(16)).join(':') : '')); // IPV6 | ||
if (!id.every((v, i) => v === parseInt(uuid.substr(i * 2, 2), 16))) { | ||
ws.close() | ||
return | ||
} | ||
|
||
logcb('conn:', host, targetPort); | ||
let i = msg.slice(17, 18).readUInt8() + 19 | ||
const targetPort = msg.slice(i, (i += 2)).readUInt16BE(0) | ||
const ATYP = msg.slice(i, (i += 1)).readUInt8() | ||
const host = | ||
ATYP === 1 | ||
? msg.slice(i, (i += 4)).join('.') // IPV4 | ||
: ATYP === 2 | ||
? new TextDecoder().decode( | ||
msg.slice(i + 1, (i += 1 + msg.slice(i, i + 1).readUInt8())) | ||
) // domain | ||
: ATYP === 3 | ||
? msg | ||
.slice(i, (i += 16)) | ||
.reduce( | ||
(s, b, i, a) => (i % 2 ? s.concat(a.slice(i - 1, i + 1)) : s), | ||
[] | ||
) | ||
.map((b) => b.readUInt16BE(0).toString(16)) | ||
.join(':') | ||
: '' // IPV6 | ||
|
||
ws.send(new Uint8Array([VERSION, 0])); | ||
logcb('conn:', host, targetPort) | ||
|
||
duplex = WebSocket.createWebSocketStream(ws); | ||
ws.send(new Uint8Array([VERSION, 0])) | ||
|
||
targetConnection = net.connect({ host, port: targetPort }, function () { | ||
this.write(msg.slice(i)); | ||
duplex.on('error', errcb('E1:')).pipe(this).on('error', errcb('E2:')).pipe(duplex); | ||
}).on('error', errcb('Conn-Err:', { host, port: targetPort })); | ||
duplex = WebSocket.createWebSocketStream(ws) | ||
|
||
targetConnection.on('close', cleanup); // 目标连接关闭时清理资源 | ||
}).on('error', errcb('EE:')); | ||
targetConnection = net | ||
.connect({ host, port: targetPort }, function () { | ||
this.write(msg.slice(i)) | ||
duplex | ||
.on('error', errcb('E1:')) | ||
.pipe(this) | ||
.on('error', errcb('E2:')) | ||
.pipe(duplex) | ||
}) | ||
.on('error', errcb('Conn-Err:', { host, port: targetPort })) | ||
|
||
ws.on('close', cleanup); // WebSocket 连接关闭时清理资源 | ||
}); | ||
targetConnection.on('close', cleanup) // 目标连接关闭时清理资源 | ||
}).on('error', errcb('EE:')) | ||
|
||
ws.on('close', cleanup) // WebSocket 连接关闭时清理资源 | ||
}) |