From d5e828d31c952c0e5d58808853ccd391cfcb5d3e Mon Sep 17 00:00:00 2001 From: JoshuaMoelans <60878493+JoshuaMoelans@users.noreply.github.com> Date: Wed, 13 Nov 2024 11:41:21 +0100 Subject: [PATCH 1/5] feat: add socks proxy support --- util/net/http_transport_mac.mm | 3 +++ util/net/url.cc | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/util/net/http_transport_mac.mm b/util/net/http_transport_mac.mm index dd28e02421..a6f75ac95c 100644 --- a/util/net/http_transport_mac.mm +++ b/util/net/http_transport_mac.mm @@ -289,6 +289,9 @@ static bool ExecuteProxyRequest(NSMutableURLRequest* request, @"HTTPSEnable" : @YES, @"HTTPSPort" : proxy_port, @"HTTPSProxy" : hostNS, + (__bridge id)kCFNetworkProxiesSOCKSEnable : @YES, + (__bridge id)kCFNetworkProxiesSOCKSPort : proxy_port, + (__bridge id)kCFNetworkProxiesSOCKSProxy : hostNS, }; sessionConfig.connectionProxyDictionary = proxyDict; NSURLSession* session = diff --git a/util/net/url.cc b/util/net/url.cc index 9d037bdcd2..0e819fae98 100644 --- a/util/net/url.cc +++ b/util/net/url.cc @@ -53,6 +53,7 @@ bool CrackURL(const std::string& url, size_t host_start; static constexpr const char kHttp[] = "http://"; static constexpr const char kHttps[] = "https://"; + static constexpr const char kSocks[] = "socks5://"; if (url.compare(0, strlen(kHttp), kHttp) == 0) { result_scheme = "http"; result_port = "80"; @@ -61,8 +62,12 @@ bool CrackURL(const std::string& url, result_scheme = "https"; result_port = "443"; host_start = strlen(kHttps); + } else if (url.compare(0, strlen(kSocks), kSocks) == 0) { + result_scheme = "socks5"; + result_port = "1080"; + host_start = strlen(kSocks); } else { - LOG(ERROR) << "expecting http or https"; + LOG(ERROR) << "expecting http, https or socks5"; return false; } From c7fa60cdd0c2d09afa36935661cf25be810b5f3d Mon Sep 17 00:00:00 2001 From: JoshuaMoelans <60878493+JoshuaMoelans@users.noreply.github.com> Date: Wed, 13 Nov 2024 11:59:23 +0100 Subject: [PATCH 2/5] fix: only add socks option on non-IOS system --- util/net/http_transport_mac.mm | 2 ++ 1 file changed, 2 insertions(+) diff --git a/util/net/http_transport_mac.mm b/util/net/http_transport_mac.mm index a6f75ac95c..6613b061e9 100644 --- a/util/net/http_transport_mac.mm +++ b/util/net/http_transport_mac.mm @@ -289,9 +289,11 @@ static bool ExecuteProxyRequest(NSMutableURLRequest* request, @"HTTPSEnable" : @YES, @"HTTPSPort" : proxy_port, @"HTTPSProxy" : hostNS, + #if !TARGET_OS_IOS (__bridge id)kCFNetworkProxiesSOCKSEnable : @YES, (__bridge id)kCFNetworkProxiesSOCKSPort : proxy_port, (__bridge id)kCFNetworkProxiesSOCKSProxy : hostNS, + #endif }; sessionConfig.connectionProxyDictionary = proxyDict; NSURLSession* session = From d34c64593eba2b7e5f929763f4c450f3416303b8 Mon Sep 17 00:00:00 2001 From: JoshuaMoelans <60878493+JoshuaMoelans@users.noreply.github.com> Date: Wed, 13 Nov 2024 14:16:30 +0100 Subject: [PATCH 3/5] fix: add runtime check for socks5 proxy scheme --- util/net/http_transport_mac.mm | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/util/net/http_transport_mac.mm b/util/net/http_transport_mac.mm index 6613b061e9..beac7cced9 100644 --- a/util/net/http_transport_mac.mm +++ b/util/net/http_transport_mac.mm @@ -289,12 +289,16 @@ static bool ExecuteProxyRequest(NSMutableURLRequest* request, @"HTTPSEnable" : @YES, @"HTTPSPort" : proxy_port, @"HTTPSProxy" : hostNS, + }; #if !TARGET_OS_IOS - (__bridge id)kCFNetworkProxiesSOCKSEnable : @YES, - (__bridge id)kCFNetworkProxiesSOCKSPort : proxy_port, - (__bridge id)kCFNetworkProxiesSOCKSProxy : hostNS, + if ([schemeNS isEqualToString:@"socks5"]) { + proxyDict = @{ + @"SOCKSEnable" : @YES, + @"SOCKSPort" : proxy_port, + @"SOCKSProxy" : hostNS, + }; + } #endif - }; sessionConfig.connectionProxyDictionary = proxyDict; NSURLSession* session = [NSURLSession sessionWithConfiguration:sessionConfig]; From 81c5c5a9725da5869f7c2e7fd084ceaf337f180e Mon Sep 17 00:00:00 2001 From: JoshuaMoelans <60878493+JoshuaMoelans@users.noreply.github.com> Date: Wed, 13 Nov 2024 14:44:41 +0100 Subject: [PATCH 4/5] fix: make all proxyDict keys strings --- util/net/http_transport_mac.mm | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/util/net/http_transport_mac.mm b/util/net/http_transport_mac.mm index beac7cced9..7fbe387068 100644 --- a/util/net/http_transport_mac.mm +++ b/util/net/http_transport_mac.mm @@ -283,14 +283,13 @@ static bool ExecuteProxyRequest(NSMutableURLRequest* request, NSNumber* proxy_port = @(std::stoi(port)); NSDictionary* proxyDict = @{ - (__bridge id)kCFNetworkProxiesHTTPEnable : @YES, - (__bridge id)kCFNetworkProxiesHTTPPort : proxy_port, - (__bridge id)kCFNetworkProxiesHTTPProxy : hostNS, + @"HTTPEnable" : @YES, + @"HTTPPort" : proxy_port, + @"HTTPProxy" : hostNS, @"HTTPSEnable" : @YES, @"HTTPSPort" : proxy_port, @"HTTPSProxy" : hostNS, }; - #if !TARGET_OS_IOS if ([schemeNS isEqualToString:@"socks5"]) { proxyDict = @{ @"SOCKSEnable" : @YES, @@ -298,7 +297,6 @@ static bool ExecuteProxyRequest(NSMutableURLRequest* request, @"SOCKSProxy" : hostNS, }; } - #endif sessionConfig.connectionProxyDictionary = proxyDict; NSURLSession* session = [NSURLSession sessionWithConfiguration:sessionConfig]; From 3224a1cf80f66c95b11f446bd7a4dae3cddc6be5 Mon Sep 17 00:00:00 2001 From: JoshuaMoelans <60878493+JoshuaMoelans@users.noreply.github.com> Date: Wed, 13 Nov 2024 15:53:00 +0100 Subject: [PATCH 5/5] apply suggestion from code review --- util/net/http_transport_mac.mm | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/util/net/http_transport_mac.mm b/util/net/http_transport_mac.mm index 7fbe387068..bfd844b19f 100644 --- a/util/net/http_transport_mac.mm +++ b/util/net/http_transport_mac.mm @@ -282,19 +282,24 @@ static bool ExecuteProxyRequest(NSMutableURLRequest* request, NSString* hostNS = base::SysUTF8ToNSString(host); NSNumber* proxy_port = @(std::stoi(port)); - NSDictionary* proxyDict = @{ - @"HTTPEnable" : @YES, - @"HTTPPort" : proxy_port, - @"HTTPProxy" : hostNS, - @"HTTPSEnable" : @YES, - @"HTTPSPort" : proxy_port, - @"HTTPSProxy" : hostNS, - }; - if ([schemeNS isEqualToString:@"socks5"]) { + NSDictionary* proxyDict; + + if ([schemeNS isEqualToString:@"http"] || [schemeNS isEqualToString:@"https"]) { + // The keys in this dictionary refer to the target URL, + // whereas `schemeNS` refers to the proxy URL. + proxyDict = @{ + @"HTTPEnable" : @YES, + @"HTTPPort" : proxy_port, + @"HTTPProxy" : hostNS, + @"HTTPSEnable" : @YES, + @"HTTPSPort" : proxy_port, + @"HTTPSProxy" : hostNS + }; + } else if ([schemeNS isEqualToString:@"socks5"]) { proxyDict = @{ @"SOCKSEnable" : @YES, @"SOCKSPort" : proxy_port, - @"SOCKSProxy" : hostNS, + @"SOCKSProxy" : hostNS }; } sessionConfig.connectionProxyDictionary = proxyDict;