Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
"socks-proxy-agent": "^8.0.5",
"swagger-ui-express": "^5.0.1",
"tsup": "^8.3.5",
"undici": "^7.16.0",
"uuid": "^13.0.0"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
import { Instance, Message } from '@prisma/client';
import { createJid } from '@utils/createJid';
import { fetchLatestWaWebVersion } from '@utils/fetchLatestWaWebVersion';
import { makeProxyAgent } from '@utils/makeProxyAgent';
import {makeProxyAgent, makeProxyAgentUndici} from '@utils/makeProxyAgent';

Check failure on line 85 in src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Replace `makeProxyAgent,Β·makeProxyAgentUndici` with `Β·makeProxyAgent,Β·makeProxyAgentUndiciΒ·`
import { getOnWhatsappCache, saveOnWhatsappCache } from '@utils/onWhatsappCache';
import { status } from '@utils/renderStatus';
import { sendTelemetry } from '@utils/sendTelemetry';
Expand Down Expand Up @@ -594,7 +594,7 @@
const proxyUrls = text.split('\r\n');
const rand = Math.floor(Math.random() * Math.floor(proxyUrls.length));
const proxyUrl = 'http://' + proxyUrls[rand];
options = { agent: makeProxyAgent(proxyUrl), fetchAgent: makeProxyAgent(proxyUrl) };
options = { agent: makeProxyAgent(proxyUrl), fetchAgent: makeProxyAgentUndici(proxyUrl) };
} catch {
this.localProxy.enabled = false;
}
Expand All @@ -607,7 +607,7 @@
username: this.localProxy.username,
password: this.localProxy.password,
}),
fetchAgent: makeProxyAgent({
fetchAgent: makeProxyAgentUndici({
host: this.localProxy.host,
port: this.localProxy.port,
protocol: this.localProxy.protocol,
Expand Down
39 changes: 39 additions & 0 deletions src/utils/makeProxyAgent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { HttpsProxyAgent } from 'https-proxy-agent';

Check failure on line 1 in src/utils/makeProxyAgent.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Run autofix to sort these imports!
import { SocksProxyAgent } from 'socks-proxy-agent';

import { ProxyAgent } from 'undici'

Check failure on line 4 in src/utils/makeProxyAgent.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Insert `;`

type Proxy = {
host: string;
password?: string;
Expand Down Expand Up @@ -42,3 +44,40 @@

return selectProxyAgent(proxyUrl);
}

export function makeProxyAgentUndici(proxy: Proxy | string): ProxyAgent {
let proxyUrl: string

Check failure on line 49 in src/utils/makeProxyAgent.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Insert `;`
let protocol: string

Check failure on line 50 in src/utils/makeProxyAgent.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Insert `;`

if (typeof proxy === 'string') {
const url = new URL(proxy)

Check failure on line 53 in src/utils/makeProxyAgent.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Insert `;`
protocol = url.protocol.replace(':', '')

Check failure on line 54 in src/utils/makeProxyAgent.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Insert `;`
proxyUrl = proxy

Check failure on line 55 in src/utils/makeProxyAgent.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Insert `;`
} else {
const { host, password, port, protocol: proto, username } = proxy

Check failure on line 57 in src/utils/makeProxyAgent.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Insert `;`
protocol = (proto || 'http').replace(':', '')

Check failure on line 58 in src/utils/makeProxyAgent.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Insert `;`

if (protocol === 'socks') {
protocol = 'socks5'
}
Comment on lines +58 to +62
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Protocol normalization for 'socks' is hardcoded to 'socks5'.

Some proxies use 'socks4', so please handle 'socks4' explicitly or document that 'socks' defaults to 'socks5'.

Suggested change
protocol = (proto || 'http').replace(':', '')
if (protocol === 'socks') {
protocol = 'socks5'
}
protocol = (proto || 'http').replace(':', '')
// Normalize SOCKS protocol: 'socks' defaults to 'socks5', but handle 'socks4' and 'socks5' explicitly
if (protocol === 'socks') {
// Default to socks5 if protocol is 'socks'
protocol = 'socks5'
} else if (protocol === 'socks4' || protocol === 'socks5') {
// Leave as is for explicit socks4/socks5
// No change needed
}


const auth = username && password ? `${username}:${password}@` : ''
proxyUrl = `${protocol}://${auth}${host}:${port}`
}

const PROXY_HTTP_PROTOCOL = 'http'
const PROXY_HTTPS_PROTOCOL = 'https'
const PROXY_SOCKS4_PROTOCOL = 'socks4'
const PROXY_SOCKS5_PROTOCOL = 'socks5'

switch (protocol) {
case PROXY_HTTP_PROTOCOL:
case PROXY_HTTPS_PROTOCOL:
case PROXY_SOCKS4_PROTOCOL:
case PROXY_SOCKS5_PROTOCOL:
return new ProxyAgent(proxyUrl)

default:
throw new Error(`Unsupported proxy protocol: ${protocol}`)
Comment on lines +80 to +81
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Error message for unsupported protocol could be more informative.

Including the full proxy input in the error message will make it easier to trace issues when user input is involved.

Suggested change
default:
throw new Error(`Unsupported proxy protocol: ${protocol}`)
default:
throw new Error(
`Unsupported proxy protocol: ${protocol}. Full proxy input: ${typeof proxy === 'string' ? proxy : JSON.stringify(proxy)}`
)

}
}