Skip to content

feat: add support to socks proxy #1793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

henrybarreto
Copy link

@henrybarreto henrybarreto commented Aug 1, 2025

Summary by Sourcery

Enable socks proxy support by extending makeProxyAgent to choose between HTTP and SOCKS agents based on proxy URL protocol and update dependencies accordingly

New Features:

  • Add support for SOCKS proxy in makeProxyAgent

Enhancements:

  • Refactor makeProxyAgent to delegate protocol-based agent creation to selectProxyAgent helper

Build:

  • Add socks-proxy-agent dependency to package.json

Copy link
Contributor

sourcery-ai bot commented Aug 1, 2025

Reviewer's Guide

This PR refactors makeProxyAgent by extracting a selectProxyAgent helper that parses the proxy URL, chooses between HttpsProxyAgent and SocksProxyAgent based on protocol (http: or socks:), and throws on unsupported protocols. makeProxyAgent now delegates both string and object inputs to this helper, and the new socks-proxy-agent dependency is added.

Class diagram for makeProxyAgent and selectProxyAgent changes

classDiagram
    class makeProxyAgent {
        +makeProxyAgent(proxy: Proxy | string): HttpsProxyAgent | SocksProxyAgent
    }
    class selectProxyAgent {
        +selectProxyAgent(proxyUrl: string): HttpsProxyAgent | SocksProxyAgent
    }
    class Proxy {
        +host: string
        +port: number
        +protocol: string
        +username?: string
        +password?: string
    }
    class HttpsProxyAgent
    class SocksProxyAgent

    makeProxyAgent --> selectProxyAgent : uses
    makeProxyAgent --> Proxy : accepts
    selectProxyAgent --> HttpsProxyAgent : returns
    selectProxyAgent --> SocksProxyAgent : returns
Loading

Class diagram for new dependency: socks-proxy-agent

classDiagram
    class SocksProxyAgent {
    }
    class HttpsProxyAgent {
    }
    class selectProxyAgent {
        +selectProxyAgent(proxyUrl: string): HttpsProxyAgent | SocksProxyAgent
    }
    selectProxyAgent --> SocksProxyAgent : instantiates
    selectProxyAgent --> HttpsProxyAgent : instantiates
Loading

File-Level Changes

Change Details Files
Extract proxy agent selection into a helper
  • Introduce selectProxyAgent function that parses the proxy URL
  • Define HTTP and SOCKS protocol constants
  • Use a switch statement to return HttpsProxyAgent or SocksProxyAgent
  • Throw an error for unsupported proxy protocols
src/utils/makeProxyAgent.ts
Refactor makeProxyAgent to use the helper
  • Replace direct HttpsProxyAgent instantiation with selectProxyAgent for string inputs
  • Construct proxyUrl from object inputs and delegate to selectProxyAgent
  • Remove redundant agent construction code
src/utils/makeProxyAgent.ts
Add SOCKS proxy support and dependency
  • Import and utilize SocksProxyAgent for socks:// URLs
  • Add socks-proxy-agent to project dependencies
src/utils/makeProxyAgent.ts
package.json

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @henrybarreto - I've reviewed your changes - here's some feedback:

  • Consider using a common base Agent type (e.g. from 'http' or 'https') as the return signature instead of the specific HttpsProxyAgent or SocksProxyAgent to simplify downstream usage.
  • The protocol parsing branch only handles 'socks'; you may want to support variants like 'socks5'/'socks4' or normalize protocol strings to cover more proxy types.
  • Rename the 'splited' variable to something like 'parts' for clarity and correct grammar.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider using a common base Agent type (e.g. from 'http' or 'https') as the return signature instead of the specific HttpsProxyAgent or SocksProxyAgent to simplify downstream usage.
- The protocol parsing branch only handles 'socks'; you may want to support variants like 'socks5'/'socks4' or normalize protocol strings to cover more proxy types.
- Rename the 'splited' variable to something like 'parts' for clarity and correct grammar.

## Individual Comments

### Comment 1
<location> `src/utils/makeProxyAgent.ts:12` </location>
<code_context>
 };

-export function makeProxyAgent(proxy: Proxy | string) {
+function selectProxyAgent(protocol: string, uri: string):  HttpsProxyAgent<string> | SocksProxyAgent {
+  switch (protocol) {
+    case 'http':
+      return new HttpsProxyAgent(uri);
+    case 'socks':
+      return new SocksProxyAgent(uri);
+    default:
+      throw new Error(`Unsupported proxy protocol: ${protocol}`);
+    }
</code_context>

<issue_to_address>
The protocol check in selectProxyAgent is too strict and may not handle variants like 'https' or 'socks5'.

The switch should account for protocol variants like 'https' and 'socks5' by normalizing the input and mapping common variants to the correct agent.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
function selectProxyAgent(protocol: string, uri: string):  HttpsProxyAgent<string> | SocksProxyAgent {
  switch (protocol) {
    case 'http':
      return new HttpsProxyAgent(uri);
    case 'socks':
      return new SocksProxyAgent(uri);
    default:
      throw new Error(`Unsupported proxy protocol: ${protocol}`);
    }
}
=======
function selectProxyAgent(protocol: string, uri: string):  HttpsProxyAgent<string> | SocksProxyAgent {
  // Normalize protocol and map variants
  const normalized = protocol.trim().toLowerCase();
  let mapped: string;
  switch (normalized) {
    case 'http':
    case 'https':
      mapped = 'http';
      break;
    case 'socks':
    case 'socks5':
    case 'socks4':
      mapped = 'socks';
      break;
    default:
      throw new Error(`Unsupported proxy protocol: ${protocol}`);
  }

  switch (mapped) {
    case 'http':
      return new HttpsProxyAgent(uri);
    case 'socks':
      return new SocksProxyAgent(uri);
    default:
      throw new Error(`Unsupported proxy protocol: ${protocol}`);
  }
}
>>>>>>> REPLACE

</suggested_fix>

### Comment 2
<location> `src/utils/makeProxyAgent.ts:25` </location>
<code_context>
+export function makeProxyAgent(proxy: Proxy | string): HttpsProxyAgent<string> | SocksProxyAgent {
   if (typeof proxy === 'string') {
-    return new HttpsProxyAgent(proxy);
+    const splited = proxy.split('://');
+    if (splited.length !== 2) {
+      throw new Error('Invalid proxy string format. Expected format: "protocol://host:port"');
+    }
</code_context>

<issue_to_address>
Splitting on '://' may not be robust for all valid proxy URIs.

Using 'split' may not handle all valid proxy URIs, especially those with embedded credentials or multiple '://'. Using the URL constructor is recommended for more reliable parsing.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines 12 to 29
function selectProxyAgent(protocol: string, uri: string): HttpsProxyAgent<string> | SocksProxyAgent {
switch (protocol) {
case 'http':
return new HttpsProxyAgent(uri);
case 'socks':
return new SocksProxyAgent(uri);
default:
throw new Error(`Unsupported proxy protocol: ${protocol}`);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: The protocol check in selectProxyAgent is too strict and may not handle variants like 'https' or 'socks5'.

The switch should account for protocol variants like 'https' and 'socks5' by normalizing the input and mapping common variants to the correct agent.

Suggested change
function selectProxyAgent(protocol: string, uri: string): HttpsProxyAgent<string> | SocksProxyAgent {
switch (protocol) {
case 'http':
return new HttpsProxyAgent(uri);
case 'socks':
return new SocksProxyAgent(uri);
default:
throw new Error(`Unsupported proxy protocol: ${protocol}`);
}
}
function selectProxyAgent(protocol: string, uri: string): HttpsProxyAgent<string> | SocksProxyAgent {
// Normalize protocol and map variants
const normalized = protocol.trim().toLowerCase();
let mapped: string;
switch (normalized) {
case 'http':
case 'https':
mapped = 'http';
break;
case 'socks':
case 'socks5':
case 'socks4':
mapped = 'socks';
break;
default:
throw new Error(`Unsupported proxy protocol: ${protocol}`);
}
switch (mapped) {
case 'http':
return new HttpsProxyAgent(uri);
case 'socks':
return new SocksProxyAgent(uri);
default:
throw new Error(`Unsupported proxy protocol: ${protocol}`);
}
}

Comment on lines 25 to 26
const splited = proxy.split('://');
if (splited.length !== 2) {
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Splitting on '://' may not be robust for all valid proxy URIs.

Using 'split' may not handle all valid proxy URIs, especially those with embedded credentials or multiple '://'. Using the URL constructor is recommended for more reliable parsing.

@henrybarreto henrybarreto force-pushed the feat/add-support-socks-proxy branch from 2588cac to 27006d8 Compare August 1, 2025 22:52
@henrybarreto henrybarreto marked this pull request as draft August 1, 2025 22:53
@henrybarreto henrybarreto force-pushed the feat/add-support-socks-proxy branch from 27006d8 to 6b2a87e Compare August 2, 2025 11:18
@henrybarreto henrybarreto marked this pull request as ready for review August 2, 2025 11:18
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @henrybarreto - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@DavidsonGomes DavidsonGomes changed the base branch from main to develop August 4, 2025 21:52
@henrybarreto henrybarreto force-pushed the feat/add-support-socks-proxy branch from 6b2a87e to 3390958 Compare August 5, 2025 10:08
@DavidsonGomes DavidsonGomes merged commit af423ce into EvolutionAPI:develop Aug 7, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants