-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
feat: add support to socks proxy #1793
Conversation
Reviewer's GuideThis 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 changesclassDiagram
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
Class diagram for new dependency: socks-proxy-agentclassDiagram
class SocksProxyAgent {
}
class HttpsProxyAgent {
}
class selectProxyAgent {
+selectProxyAgent(proxyUrl: string): HttpsProxyAgent | SocksProxyAgent
}
selectProxyAgent --> SocksProxyAgent : instantiates
selectProxyAgent --> HttpsProxyAgent : instantiates
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this 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>
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
src/utils/makeProxyAgent.ts
Outdated
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}`); | ||
} | ||
} |
There was a problem hiding this comment.
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.
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}`); | |
} | |
} |
src/utils/makeProxyAgent.ts
Outdated
const splited = proxy.split('://'); | ||
if (splited.length !== 2) { |
There was a problem hiding this comment.
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.
2588cac
to
27006d8
Compare
27006d8
to
6b2a87e
Compare
There was a problem hiding this 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!
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
6b2a87e
to
3390958
Compare
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:
Enhancements:
Build: