Skip to content

Commit abfd278

Browse files
authored
browser(webkit): allow setting proxy per browser context (#2445)
1 parent a82139b commit abfd278

File tree

4 files changed

+129
-42
lines changed

4 files changed

+129
-42
lines changed

browser_patches/webkit/BUILD_NUMBER

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1256
1+
1257

browser_patches/webkit/embedder/Playwright/mac/AppDelegate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
NSMutableSet *_dialogs;
4040
NSString* _initialURL;
4141
NSString* _userDataDir;
42+
NSString* _proxyServer;
4243
IBOutlet NSMenuItem *_newWebKit2WindowItem;
4344
}
4445

browser_patches/webkit/embedder/Playwright/mac/AppDelegate.m

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ - (id)init
8989

9090
_initialURL = nil;
9191
_userDataDir = nil;
92+
_proxyServer = nil;
9293
NSArray *arguments = [[NSProcessInfo processInfo] arguments];
9394
NSRange subargs = NSMakeRange(1, [arguments count] - 1);
9495
NSArray *subArray = [arguments subarrayWithRange:subargs];
@@ -100,6 +101,10 @@ - (id)init
100101
NSRange range = NSMakeRange(16, [argument length] - 16);
101102
_userDataDir = [[argument substringWithRange:range] copy];
102103
}
104+
if ([argument hasPrefix:@"--proxy="]) {
105+
NSRange range = NSMakeRange(8, [argument length] - 8);
106+
_proxyServer = [[argument substringWithRange:range] copy];
107+
}
103108
}
104109

105110
_headless = [arguments containsObject: @"--headless"];
@@ -126,6 +131,39 @@ - (void)awakeFromNib
126131
[NSApp setAutomaticCustomizeTouchBarMenuItemEnabled:YES];
127132
}
128133

134+
135+
- (NSDictionary *)proxyConfiguration:(NSString *)proxyServer
136+
{
137+
if (!proxyServer)
138+
return nil;
139+
140+
#pragma clang diagnostic push
141+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
142+
143+
NSMutableDictionary *dictionary = [[[NSMutableDictionary alloc] init] autorelease];
144+
if ([proxyServer hasPrefix:@"socks5://"]) {
145+
NSURL *socksProxy = [NSURL URLWithString:proxyServer];
146+
[dictionary setObject:[socksProxy host] forKey:(NSString *)kCFStreamPropertySOCKSProxyHost];
147+
NSNumber *port = [socksProxy port];
148+
if (port)
149+
[dictionary setObject:port forKey:(NSString *)kCFStreamPropertySOCKSProxyPort];
150+
} else {
151+
NSURL *httpProxy = [NSURL URLWithString: [NSString stringWithFormat:@"http://%@", proxyServer]];
152+
NSString *host = [httpProxy host];
153+
NSNumber *port = [httpProxy port];
154+
[dictionary setObject:host forKey:(NSString *)kCFStreamPropertyHTTPProxyHost];
155+
[dictionary setObject:host forKey:(NSString *)kCFStreamPropertyHTTPSProxyHost];
156+
if (port) {
157+
[dictionary setObject:port forKey:(NSString *)kCFStreamPropertyHTTPProxyPort];
158+
[dictionary setObject:port forKey:(NSString *)kCFStreamPropertyHTTPSProxyPort];
159+
}
160+
}
161+
162+
#pragma clang diagnostic pop
163+
164+
return dictionary;
165+
}
166+
129167
- (WKWebsiteDataStore *)persistentDataStore
130168
{
131169
static WKWebsiteDataStore *dataStore;
@@ -166,6 +204,7 @@ - (WKWebsiteDataStore *)persistentDataStore
166204
NSURL *webSqlDirectory = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/WebSQL", _userDataDir]];
167205
[configuration _setWebSQLDatabaseDirectory:webSqlDirectory];
168206
}
207+
[configuration setProxyConfiguration:[self proxyConfiguration:_proxyServer]];
169208
dataStore = [[WKWebsiteDataStore alloc] _initWithConfiguration:configuration];
170209
}
171210

@@ -275,12 +314,16 @@ - (WKWebView *)createHeadlessPage:(WKWebViewConfiguration *)configuration withUR
275314
return [webView autorelease];
276315
}
277316

278-
- (_WKBrowserContext *)createBrowserContext
317+
- (_WKBrowserContext *)createBrowserContext:(NSString *)proxyServer
279318
{
280319
_WKBrowserContext *browserContext = [[_WKBrowserContext alloc] init];
281320
_WKProcessPoolConfiguration *processConfiguration = [[[_WKProcessPoolConfiguration alloc] init] autorelease];
282321
processConfiguration.forceOverlayScrollbars = YES;
283-
browserContext.dataStore = [WKWebsiteDataStore nonPersistentDataStore];
322+
_WKWebsiteDataStoreConfiguration *dataStoreConfiguration = [[[_WKWebsiteDataStoreConfiguration alloc] initNonPersistentConfiguration] autorelease];
323+
if (!proxyServer || ![proxyServer length])
324+
proxyServer = _proxyServer;
325+
[dataStoreConfiguration setProxyConfiguration:[self proxyConfiguration:proxyServer]];
326+
browserContext.dataStore = [[WKWebsiteDataStore alloc] _initWithConfiguration:dataStoreConfiguration];
284327
browserContext.processPool = [[[WKProcessPool alloc] _initWithConfiguration:processConfiguration] autorelease];
285328
[browserContext.processPool _setDownloadDelegate:self];
286329
[_browserContexts addObject:browserContext];

0 commit comments

Comments
 (0)