From 8a05a9eefc77e17fb6e938db9b969cc6f0ab4366 Mon Sep 17 00:00:00 2001 From: Vahid Farid Date: Fri, 31 May 2024 16:04:42 +0330 Subject: [PATCH] Update trojan.ts --- src/trojan.ts | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/trojan.ts b/src/trojan.ts index af6230f3..bb4d4fb6 100644 --- a/src/trojan.ts +++ b/src/trojan.ts @@ -1,7 +1,8 @@ import { connect } from 'cloudflare:sockets' -import { GetTrojanConfig, MuddleDomain, getSHA224Password } from "./helpers" +import { GetTrojanConfig, MuddleDomain, getSHA224Password, getUUID } from "./helpers" import { cfPorts, proxiesUri } from "./variables" import { RemoteSocketWrapper, CustomArrayBuffer, VlessHeader, UDPOutbound, Config, Env } from "./interfaces" +import { encodeBase64 } from 'bcryptjs' const WS_READY_STATE_OPEN: number = 1 const WS_READY_STATE_CLOSING: number = 2 @@ -13,12 +14,11 @@ let countries: Array = [] export async function GetTrojanConfigList(sni: string, addressList: Array, start: number, max: number, env: Env) { filterCountries = "" proxyList = [] - const sha224Password: string = getSHA224Password(sni) let configList: Array = [] for (let i = 0; i < max; i++) { configList.push(GetTrojanConfig( i + start, - sha224Password, + getUUID(sni), MuddleDomain(sni), addressList[Math.floor(Math.random() * addressList.length)], cfPorts[Math.floor(Math.random() * cfPorts.length)] @@ -29,7 +29,7 @@ export async function GetTrojanConfigList(sni: string, addressList: Array = Object.values(new WebSocketPair) webSocket.accept() @@ -41,8 +41,6 @@ export async function TrojanOverWSHandler(request: Request, sni: string, env: En value: null, } - let portWithRandomLog = ""; - readableWebSocketStream.pipeTo(new WritableStream({ async write(chunk, controller) { if (remoteSocketWapper.value) { @@ -56,20 +54,17 @@ export async function TrojanOverWSHandler(request: Request, sni: string, env: En message, portRemote = 443, addressRemote = "", - rawDataIndex + rawClientData, } = await ParseTrojanHeader(chunk, sha224Password); address = addressRemote; - portWithRandomLog = `${portRemote}--${Math.random()} tcp`; if (hasError) { throw new Error(message); } - const rawClientData: Uint8Array = chunk.slice(rawDataIndex) HandleTCPOutbound(remoteSocketWapper, addressRemote, portRemote, rawClientData, webSocket, env); }, })).catch((err) => { }); return new Response(null, { status: 101, - // @ts-ignore webSocket: client }); } @@ -163,11 +158,11 @@ async function ParseTrojanHeader(buffer: ArrayBuffer, sha224Password: string) { hasError: false, addressRemote: address, portRemote, - rawDataIndex: addressIndex + addressLength, + rawClientData: socks5DataBuffer.slice(portIndex + 4), }; } -async function HandleTCPOutbound(remoteSocket: RemoteSocketWrapper, addressRemote: string, portRemote: number, rawClientData: Uint8Array | undefined, webSocket: WebSocket, env: Env): Promise { +async function HandleTCPOutbound(remoteSocket: RemoteSocketWrapper, addressRemote: string, portRemote: number, rawClientData: ArrayBuffer | undefined, webSocket: WebSocket, env: Env): Promise { const maxRetryCount = 5 let retryCount = 0; @@ -178,7 +173,8 @@ async function HandleTCPOutbound(remoteSocket: RemoteSocketWrapper, addressRemot } const tcpSocket: Socket = connect(socketAddress) remoteSocket.value = tcpSocket - const writer: WritableStreamDefaultWriter = tcpSocket.writable.getWriter() + // console.log(`connected to ${address}:${port}`); + const writer: WritableStreamDefaultWriter = tcpSocket.writable.getWriter() await writer.write(rawClientData) writer.releaseLock() return tcpSocket @@ -202,7 +198,6 @@ async function HandleTCPOutbound(remoteSocket: RemoteSocketWrapper, addressRemot }) } proxyList = proxyList.map(ip => ip.split(",")[0]) - console.log(proxyList) } if (proxyList.length > 0) { proxyIP = proxyList[Math.floor(Math.random() * proxyList.length)]