@@ -89,6 +89,7 @@ - (id)init
89
89
90
90
_initialURL = nil ;
91
91
_userDataDir = nil ;
92
+ _proxyServer = nil ;
92
93
NSArray *arguments = [[NSProcessInfo processInfo ] arguments ];
93
94
NSRange subargs = NSMakeRange (1 , [arguments count ] - 1 );
94
95
NSArray *subArray = [arguments subarrayWithRange: subargs];
@@ -100,6 +101,10 @@ - (id)init
100
101
NSRange range = NSMakeRange (16 , [argument length ] - 16 );
101
102
_userDataDir = [[argument substringWithRange: range] copy ];
102
103
}
104
+ if ([argument hasPrefix: @" --proxy=" ]) {
105
+ NSRange range = NSMakeRange (8 , [argument length ] - 8 );
106
+ _proxyServer = [[argument substringWithRange: range] copy ];
107
+ }
103
108
}
104
109
105
110
_headless = [arguments containsObject: @" --headless" ];
@@ -126,6 +131,39 @@ - (void)awakeFromNib
126
131
[NSApp setAutomaticCustomizeTouchBarMenuItemEnabled: YES ];
127
132
}
128
133
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
+
129
167
- (WKWebsiteDataStore *)persistentDataStore
130
168
{
131
169
static WKWebsiteDataStore *dataStore;
@@ -166,6 +204,7 @@ - (WKWebsiteDataStore *)persistentDataStore
166
204
NSURL *webSqlDirectory = [NSURL fileURLWithPath: [NSString stringWithFormat: @" %@ /WebSQL" , _userDataDir]];
167
205
[configuration _setWebSQLDatabaseDirectory: webSqlDirectory];
168
206
}
207
+ [configuration setProxyConfiguration: [self proxyConfiguration: _proxyServer]];
169
208
dataStore = [[WKWebsiteDataStore alloc ] _initWithConfiguration: configuration];
170
209
}
171
210
@@ -275,12 +314,16 @@ - (WKWebView *)createHeadlessPage:(WKWebViewConfiguration *)configuration withUR
275
314
return [webView autorelease ];
276
315
}
277
316
278
- - (_WKBrowserContext *)createBrowserContext
317
+ - (_WKBrowserContext *)createBrowserContext : ( NSString *) proxyServer
279
318
{
280
319
_WKBrowserContext *browserContext = [[_WKBrowserContext alloc ] init ];
281
320
_WKProcessPoolConfiguration *processConfiguration = [[[_WKProcessPoolConfiguration alloc ] init ] autorelease ];
282
321
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];
284
327
browserContext.processPool = [[[WKProcessPool alloc ] _initWithConfiguration: processConfiguration] autorelease ];
285
328
[browserContext.processPool _setDownloadDelegate: self ];
286
329
[_browserContexts addObject: browserContext];
0 commit comments