From d7a00f22a45b9239ff45783f3655391ade206746 Mon Sep 17 00:00:00 2001 From: Alameenpd <167607669+Alameenpd@users.noreply.github.com> Date: Sun, 1 Sep 2024 23:53:39 +0000 Subject: [PATCH] new --- package.json | 4 ++- src/Platform.ts | 15 ++++++++ src/PlatformAPI.ts | 4 ++- src/PlatformInfo.ts | 20 +++++++++++ src/index.ts | 28 ++++++++++++++- yarn.lock | 85 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 153 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 1545cde..53d0e55 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,9 @@ }, "dependencies": { "emoji-regex": "^10.1.0", - "qrcode.react": "^3.1.0" + "http-proxy-agent": "^7.0.2", + "qrcode.react": "^3.1.0", + "socks-proxy-agent": "^8.0.4" }, "optionalDependencies": { "lodash": "^4.17.21", diff --git a/src/Platform.ts b/src/Platform.ts index ee25f56..1bfd2fd 100644 --- a/src/Platform.ts +++ b/src/Platform.ts @@ -3,6 +3,7 @@ import type { PhoneNumber } from './PhoneNumber' import type { PlatformAPI, LoginCreds, LoginResult } from './PlatformAPI' import type { PlatformInfo } from './PlatformInfo' + export type AuthProps = { isReauthing?: boolean api?: PlatformAPI @@ -22,4 +23,18 @@ export interface Platform { info: PlatformInfo api: PlatformAPI auth?: React.LazyExoticComponent | React.ComponentClass> + proxyConfig?: ProxyConfig; +} +export interface ProxyConfig { + host: string; + port: number; + protocol: 'http' | 'https' | 'socks4' | 'socks5'; + auth?: { + username: string; + password: string; + }; } + +export interface PlatformConfig { + proxyConfig?: ProxyConfig; +} \ No newline at end of file diff --git a/src/PlatformAPI.ts b/src/PlatformAPI.ts index 13b8ea7..a29b5a4 100644 --- a/src/PlatformAPI.ts +++ b/src/PlatformAPI.ts @@ -12,6 +12,7 @@ import type { NotificationsInfo } from './Notifications' import type { StickerPack, StickerPackID } from './StickerPack' import type { Attachment, AttachmentID } from './Attachment' import type { OverridablePlatformInfo } from './PlatformInfo' +import type { PlatformConfig } from './Platform' export type OnLoginEventCallback = (data: any) => void @@ -130,8 +131,9 @@ export interface PlatformAPI { * Called after new PlatformAPI() * @param session - return value of `serializeSession`, `undefined` if not logged in */ - init: (session: SerializedSession, context: ClientContext, prefs?: Record) => Awaitable + init(session?: any, prefs?: Record, config?: PlatformConfig): Awaitable; + /** `dispose` disconnects all network connections and cleans up. Called when user disables account and when app exits. */ dispose: () => Awaitable diff --git a/src/PlatformInfo.ts b/src/PlatformInfo.ts index 1bb685a..08e6014 100644 --- a/src/PlatformInfo.ts +++ b/src/PlatformInfo.ts @@ -58,6 +58,7 @@ export type Pref = { default: boolean | string } + type HTMLSVG = `` type HexColor = `#${string}` @@ -97,6 +98,15 @@ export interface OverridablePlatformInfo { } } } +export interface ProxyConfig { + host: string; + port: number; + protocol: 'http' | 'https' | 'socks4' | 'socks5'; + auth?: { + username: string; + password: string; + }; +} export interface PlatformInfo extends OverridablePlatformInfo, ExtraProp { name: string @@ -131,6 +141,16 @@ export interface PlatformInfo extends OverridablePlatformInfo, ExtraProp { prefs?: Record + proxyConfig?: { + host: string; + port: number; + protocol: 'http' | 'https' | 'socks4' | 'socks5'; + auth?: { + username: string; + password: string; + }; + }; + getUserProfileLink?: (participant: Participant) => string generateUniqueMessageID?: () => string } diff --git a/src/index.ts b/src/index.ts index bd929c8..206f438 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,9 @@ import type { TextsNodeGlobals, TextsRendererGlobals } from './TextsGlobals' +import { SocksProxyAgent } from 'socks-proxy-agent' export * from './enums' export * from './errors' export * from './constants' - export * from './fetch' export * from './generic' export * from './CustomEmoji' @@ -22,6 +22,32 @@ export * from './User' export * from './StickerPack' export * from './IAsyncSqlite' export * from './util' +export type { ProxyConfig, PlatformConfig } from './Platform'; +// Ensure the globalThis texts is of correct type export const texts = (globalThis as any).texts as TextsNodeGlobals export const textsRenderer = (globalThis as any).texts as TextsRendererGlobals + + + +// // Wrap the original createHttpClient method +// const originalCreateHttpClient = texts.createHttpClient.bind(texts) + +// // Define a new createHttpClient method with proxy support +// texts.createHttpClient = (options: any & { proxy?: ProxyConfig }) => { +// // Check if proxy options are provided +// if (options.proxy) { +// const { protocol, host, port, auth } = options.proxy +// const proxyUrl = `${protocol}://${auth ? `${auth.username}:${auth.password}@` : ''}${host}:${port}` + +// // Set the appropriate agent based on the protocol +// if (protocol === 'socks4' || protocol === 'socks5') { +// options.agent = new SocksProxyAgent(proxyUrl) +// } else { +// options.agent = new HttpsProxyAgent(proxyUrl) +// } +// } + +// // Call the original createHttpClient with the updated options +// // return originalCreateHttpClient(options) +// } diff --git a/yarn.lock b/yarn.lock index 35a7bc2..faa6536 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1612,8 +1612,10 @@ __metadata: emoji-regex: ^10.1.0 eslint: ^8.30.0 form-data: ^4.0.0 + http-proxy-agent: ^7.0.2 lodash: ^4.17.21 qrcode.react: ^3.1.0 + socks-proxy-agent: ^8.0.4 typescript: ^5.0.2 ws: ^8.15.1 dependenciesMeta: @@ -1908,6 +1910,15 @@ __metadata: languageName: node linkType: hard +"agent-base@npm:^7.1.0, agent-base@npm:^7.1.1": + version: 7.1.1 + resolution: "agent-base@npm:7.1.1" + dependencies: + debug: ^4.3.4 + checksum: 51c158769c5c051482f9ca2e6e1ec085ac72b5a418a9b31b4e82fe6c0a6699adb94c1c42d246699a587b3335215037091c79e0de512c516f73b6ea844202f037 + languageName: node + linkType: hard + "ajv@npm:^6.10.0, ajv@npm:^6.12.4": version: 6.12.6 resolution: "ajv@npm:6.12.6" @@ -2368,6 +2379,18 @@ __metadata: languageName: node linkType: hard +"debug@npm:^4.3.4": + version: 4.3.6 + resolution: "debug@npm:4.3.6" + dependencies: + ms: 2.1.2 + peerDependenciesMeta: + supports-color: + optional: true + checksum: 1630b748dea3c581295e02137a9f5cbe2c1d85fea35c1e6597a65ca2b16a6fce68cec61b299d480787ef310ba927dc8c92d3061faba0ad06c6a724672f66be7f + languageName: node + linkType: hard + "deep-is@npm:^0.1.3": version: 0.1.4 resolution: "deep-is@npm:0.1.4" @@ -3142,6 +3165,16 @@ __metadata: languageName: node linkType: hard +"http-proxy-agent@npm:^7.0.2": + version: 7.0.2 + resolution: "http-proxy-agent@npm:7.0.2" + dependencies: + agent-base: ^7.1.0 + debug: ^4.3.4 + checksum: 670858c8f8f3146db5889e1fa117630910101db601fff7d5a8aa637da0abedf68c899f03d3451cac2f83bcc4c3d2dabf339b3aa00ff8080571cceb02c3ce02f3 + languageName: node + linkType: hard + "ignore@npm:^5.1.4, ignore@npm:^5.1.8": version: 5.1.9 resolution: "ignore@npm:5.1.9" @@ -3201,6 +3234,16 @@ __metadata: languageName: node linkType: hard +"ip-address@npm:^9.0.5": + version: 9.0.5 + resolution: "ip-address@npm:9.0.5" + dependencies: + jsbn: 1.1.0 + sprintf-js: ^1.1.3 + checksum: aa15f12cfd0ef5e38349744e3654bae649a34c3b10c77a674a167e99925d1549486c5b14730eebce9fea26f6db9d5e42097b00aa4f9f612e68c79121c71652dc + languageName: node + linkType: hard + "is-arrayish@npm:^0.2.1": version: 0.2.1 resolution: "is-arrayish@npm:0.2.1" @@ -3383,6 +3426,13 @@ __metadata: languageName: node linkType: hard +"jsbn@npm:1.1.0": + version: 1.1.0 + resolution: "jsbn@npm:1.1.0" + checksum: 944f924f2bd67ad533b3850eee47603eed0f6ae425fd1ee8c760f477e8c34a05f144c1bd4f5a5dd1963141dc79a2c55f89ccc5ab77d039e7077f3ad196b64965 + languageName: node + linkType: hard + "jsesc@npm:^2.5.1": version: 2.5.2 resolution: "jsesc@npm:2.5.2" @@ -4165,6 +4215,34 @@ __metadata: languageName: node linkType: hard +"smart-buffer@npm:^4.2.0": + version: 4.2.0 + resolution: "smart-buffer@npm:4.2.0" + checksum: b5167a7142c1da704c0e3af85c402002b597081dd9575031a90b4f229ca5678e9a36e8a374f1814c8156a725d17008ae3bde63b92f9cfd132526379e580bec8b + languageName: node + linkType: hard + +"socks-proxy-agent@npm:^8.0.4": + version: 8.0.4 + resolution: "socks-proxy-agent@npm:8.0.4" + dependencies: + agent-base: ^7.1.1 + debug: ^4.3.4 + socks: ^2.8.3 + checksum: b2ec5051d85fe49072f9a250c427e0e9571fd09d5db133819192d078fd291276e1f0f50f6dbc04329b207738b1071314cee8bdbb4b12e27de42dbcf1d4233c67 + languageName: node + linkType: hard + +"socks@npm:^2.8.3": + version: 2.8.3 + resolution: "socks@npm:2.8.3" + dependencies: + ip-address: ^9.0.5 + smart-buffer: ^4.2.0 + checksum: 7a6b7f6eedf7482b9e4597d9a20e09505824208006ea8f2c49b71657427f3c137ca2ae662089baa73e1971c62322d535d9d0cf1c9235cf6f55e315c18203eadd + languageName: node + linkType: hard + "source-map@npm:^0.5.0": version: 0.5.7 resolution: "source-map@npm:0.5.7" @@ -4172,6 +4250,13 @@ __metadata: languageName: node linkType: hard +"sprintf-js@npm:^1.1.3": + version: 1.1.3 + resolution: "sprintf-js@npm:1.1.3" + checksum: a3fdac7b49643875b70864a9d9b469d87a40dfeaf5d34d9d0c5b1cda5fd7d065531fcb43c76357d62254c57184a7b151954156563a4d6a747015cfb41021cad0 + languageName: node + linkType: hard + "string-natural-compare@npm:^3.0.1": version: 3.0.1 resolution: "string-natural-compare@npm:3.0.1"