summaryrefslogtreecommitdiffstats
path: root/toolkit/system/osxproxy/nsOSXSystemProxySettings.mm
blob: 27c7b47148169da09453b19cc5805c4060b47ac3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#import <Cocoa/Cocoa.h>
#import <SystemConfiguration/SystemConfiguration.h>

#include "nsISystemProxySettings.h"
#include "mozilla/Components.h"
#include "nsPrintfCString.h"
#include "nsNetCID.h"
#include "nsObjCExceptions.h"
#include "mozilla/Attributes.h"
#include "mozilla/StaticPrefs_network.h"
#include "ProxyUtils.h"
#include "ProxyConfig.h"

class nsOSXSystemProxySettings : public nsISystemProxySettings {
 public:
  NS_DECL_THREADSAFE_ISUPPORTS
  NS_DECL_NSISYSTEMPROXYSETTINGS

  nsOSXSystemProxySettings();
  nsresult Init();

  // called by OSX when the proxy settings have changed
  void ProxyHasChanged();

  // is there a PAC url specified in the system configuration
  bool IsAutoconfigEnabled() const;
  // retrieve the pac url
  nsresult GetAutoconfigURL(nsAutoCString& aResult) const;

  // Find the SystemConfiguration proxy & port for a given URI
  nsresult FindSCProxyPort(const nsACString& aScheme, nsACString& aResultHost,
                           int32_t& aResultPort, bool& aResultSocksProxy);

  // is host:port on the proxy exception list?
  bool IsInExceptionList(const nsACString& aHost) const;

 protected:
  virtual ~nsOSXSystemProxySettings();
  virtual void InitDone() {}
  virtual void OnProxyConfigChangedInternal() {}

  SCDynamicStoreContext mContext;
  SCDynamicStoreRef mSystemDynamicStore;
  NSDictionary* mProxyDict;

  // Mapping of URI schemes to SystemConfiguration keys
  struct SchemeMapping {
    const char* mScheme;
    CFStringRef mEnabled;
    CFStringRef mHost;
    CFStringRef mPort;
    bool mIsSocksProxy;
  };
  static const SchemeMapping gSchemeMappingList[];
};

NS_IMPL_ISUPPORTS(nsOSXSystemProxySettings, nsISystemProxySettings)

NS_IMETHODIMP
nsOSXSystemProxySettings::GetMainThreadOnly(bool* aMainThreadOnly) {
  *aMainThreadOnly = true;
  return NS_OK;
}

NS_IMETHODIMP
nsOSXSystemProxySettings::GetSystemWPADSetting(bool* aSystemWPADSetting) {
  *aSystemWPADSetting = false;
  return NS_OK;
}

// Mapping of URI schemes to SystemConfiguration keys
const nsOSXSystemProxySettings::SchemeMapping
    nsOSXSystemProxySettings::gSchemeMappingList[] = {
        {"http", kSCPropNetProxiesHTTPEnable, kSCPropNetProxiesHTTPProxy,
         kSCPropNetProxiesHTTPPort, false},
        {"https", kSCPropNetProxiesHTTPSEnable, kSCPropNetProxiesHTTPSProxy,
         kSCPropNetProxiesHTTPSPort, false},
        {"ftp", kSCPropNetProxiesFTPEnable, kSCPropNetProxiesFTPProxy,
         kSCPropNetProxiesFTPPort, false},
        {"socks", kSCPropNetProxiesSOCKSEnable, kSCPropNetProxiesSOCKSProxy,
         kSCPropNetProxiesSOCKSPort, true},
        {NULL, NULL, NULL, NULL, false},
};

static void ProxyHasChangedWrapper(SCDynamicStoreRef aStore,
                                   CFArrayRef aChangedKeys, void* aInfo) {
  static_cast<nsOSXSystemProxySettings*>(aInfo)->ProxyHasChanged();
}

nsOSXSystemProxySettings::nsOSXSystemProxySettings()
    : mSystemDynamicStore(NULL), mProxyDict(NULL) {
  mContext = (SCDynamicStoreContext){0, this, NULL, NULL, NULL};
}

nsresult nsOSXSystemProxySettings::Init() {
  NS_OBJC_BEGIN_TRY_BLOCK_RETURN;

  // Register for notification of proxy setting changes
  // See:
  // http://developer.apple.com/documentation/Networking/Conceptual/CFNetwork/CFStreamTasks/chapter_4_section_5.html
  mSystemDynamicStore = SCDynamicStoreCreate(NULL, CFSTR("Mozilla"),
                                             ProxyHasChangedWrapper, &mContext);
  if (!mSystemDynamicStore) return NS_ERROR_FAILURE;

  // Set up the store to monitor any changes to the proxies
  CFStringRef proxiesKey = SCDynamicStoreKeyCreateProxies(NULL);
  if (!proxiesKey) return NS_ERROR_FAILURE;

  CFArrayRef keyArray = CFArrayCreate(NULL, (const void**)(&proxiesKey), 1,
                                      &kCFTypeArrayCallBacks);
  CFRelease(proxiesKey);
  if (!keyArray) return NS_ERROR_FAILURE;

  SCDynamicStoreSetNotificationKeys(mSystemDynamicStore, keyArray, NULL);
  CFRelease(keyArray);

  // Add the dynamic store to the run loop
  CFRunLoopSourceRef storeRLSource =
      SCDynamicStoreCreateRunLoopSource(NULL, mSystemDynamicStore, 0);
  if (!storeRLSource) return NS_ERROR_FAILURE;
  CFRunLoopAddSource(CFRunLoopGetCurrent(), storeRLSource,
                     kCFRunLoopCommonModes);
  CFRelease(storeRLSource);

  // Load the initial copy of proxy info
  mProxyDict = (NSDictionary*)SCDynamicStoreCopyProxies(mSystemDynamicStore);
  if (!mProxyDict) return NS_ERROR_FAILURE;

  InitDone();

  return NS_OK;

  NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
}

nsOSXSystemProxySettings::~nsOSXSystemProxySettings() {
  NS_OBJC_BEGIN_TRY_IGNORE_BLOCK;

  [mProxyDict release];

  if (mSystemDynamicStore) {
    // Invalidate the dynamic store's run loop source
    // to get the store out of the run loop
    CFRunLoopSourceRef rls =
        SCDynamicStoreCreateRunLoopSource(NULL, mSystemDynamicStore, 0);
    if (rls) {
      CFRunLoopSourceInvalidate(rls);
      CFRelease(rls);
    }
    CFRelease(mSystemDynamicStore);
  }

  NS_OBJC_END_TRY_IGNORE_BLOCK;
}

void nsOSXSystemProxySettings::ProxyHasChanged() {
  NS_OBJC_BEGIN_TRY_IGNORE_BLOCK;

  [mProxyDict release];
  mProxyDict = (NSDictionary*)SCDynamicStoreCopyProxies(mSystemDynamicStore);

  NS_OBJC_END_TRY_IGNORE_BLOCK;

  OnProxyConfigChangedInternal();
}

nsresult nsOSXSystemProxySettings::FindSCProxyPort(const nsACString& aScheme,
                                                   nsACString& aResultHost,
                                                   int32_t& aResultPort,
                                                   bool& aResultSocksProxy) {
  NS_OBJC_BEGIN_TRY_BLOCK_RETURN;

  NS_ENSURE_TRUE(mProxyDict != NULL, NS_ERROR_FAILURE);

  for (const SchemeMapping* keys = gSchemeMappingList; keys->mScheme != NULL;
       ++keys) {
    // Check for matching scheme (when appropriate)
    if (strcasecmp(keys->mScheme, PromiseFlatCString(aScheme).get()) &&
        !keys->mIsSocksProxy)
      continue;

    // Check the proxy is enabled
    NSNumber* enabled = [mProxyDict objectForKey:(NSString*)keys->mEnabled];
    NS_ENSURE_TRUE(enabled == NULL || [enabled isKindOfClass:[NSNumber class]],
                   NS_ERROR_FAILURE);
    if ([enabled intValue] == 0) continue;

    // Get the proxy host
    NSString* host = [mProxyDict objectForKey:(NSString*)keys->mHost];
    if (host == NULL) break;
    NS_ENSURE_TRUE([host isKindOfClass:[NSString class]], NS_ERROR_FAILURE);
    aResultHost.Assign([host UTF8String]);

    // Get the proxy port
    NSNumber* port = [mProxyDict objectForKey:(NSString*)keys->mPort];
    NS_ENSURE_TRUE([port isKindOfClass:[NSNumber class]], NS_ERROR_FAILURE);
    aResultPort = [port intValue];

    aResultSocksProxy = keys->mIsSocksProxy;

    return NS_OK;
  }

  return NS_ERROR_FAILURE;

  NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
}

bool nsOSXSystemProxySettings::IsAutoconfigEnabled() const {
  NS_OBJC_BEGIN_TRY_BLOCK_RETURN;

  NSNumber* value = [mProxyDict
      objectForKey:(NSString*)kSCPropNetProxiesProxyAutoConfigEnable];
  NS_ENSURE_TRUE(value == NULL || [value isKindOfClass:[NSNumber class]],
                 false);
  return ([value intValue] != 0);

  NS_OBJC_END_TRY_BLOCK_RETURN(false);
}

nsresult nsOSXSystemProxySettings::GetAutoconfigURL(
    nsAutoCString& aResult) const {
  NS_OBJC_BEGIN_TRY_BLOCK_RETURN;

  NSString* value = [mProxyDict
      objectForKey:(NSString*)kSCPropNetProxiesProxyAutoConfigURLString];
  if (value != NULL) {
    NS_ENSURE_TRUE([value isKindOfClass:[NSString class]], NS_ERROR_FAILURE);
    aResult.Assign([value UTF8String]);
    return NS_OK;
  }

  return NS_ERROR_FAILURE;

  NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
}

bool nsOSXSystemProxySettings::IsInExceptionList(
    const nsACString& aHost) const {
  NS_OBJC_BEGIN_TRY_BLOCK_RETURN;

  NS_ENSURE_TRUE(mProxyDict != NULL, false);

  NSArray* exceptionList =
      [mProxyDict objectForKey:(NSString*)kSCPropNetProxiesExceptionsList];
  NS_ENSURE_TRUE(
      exceptionList == NULL || [exceptionList isKindOfClass:[NSArray class]],
      false);

  NSEnumerator* exceptionEnumerator = [exceptionList objectEnumerator];
  NSString* currentValue = NULL;
  while ((currentValue = [exceptionEnumerator nextObject])) {
    NS_ENSURE_TRUE([currentValue isKindOfClass:[NSString class]], false);
    nsAutoCString overrideStr([currentValue UTF8String]);
    if (mozilla::toolkit::system::IsHostProxyEntry(aHost, overrideStr))
      return true;
  }

  NS_OBJC_END_TRY_BLOCK_RETURN(false);
}

nsresult nsOSXSystemProxySettings::GetPACURI(nsACString& aResult) {
  NS_OBJC_BEGIN_TRY_BLOCK_RETURN;

  NS_ENSURE_TRUE(mProxyDict != NULL, NS_ERROR_FAILURE);

  nsAutoCString pacUrl;
  if (IsAutoconfigEnabled() && NS_SUCCEEDED(GetAutoconfigURL(pacUrl))) {
    aResult.Assign(pacUrl);
    return NS_OK;
  }

  return NS_ERROR_FAILURE;

  NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
}

nsresult nsOSXSystemProxySettings::GetProxyForURI(const nsACString& aSpec,
                                                  const nsACString& aScheme,
                                                  const nsACString& aHost,
                                                  const int32_t aPort,
                                                  nsACString& aResult) {
  NS_OBJC_BEGIN_TRY_BLOCK_RETURN;

  int32_t proxyPort;
  nsAutoCString proxyHost;
  bool proxySocks;
  nsresult rv = FindSCProxyPort(aScheme, proxyHost, proxyPort, proxySocks);

  if (NS_FAILED(rv) || IsInExceptionList(aHost)) {
    aResult.AssignLiteral("DIRECT");
  } else if (proxySocks) {
    aResult.Assign("SOCKS "_ns + proxyHost + nsPrintfCString(":%d", proxyPort));
  } else {
    aResult.Assign("PROXY "_ns + proxyHost + nsPrintfCString(":%d", proxyPort));
  }

  return NS_OK;

  NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
}

using namespace mozilla::net;

class OSXSystemProxySettingsAsync final : public nsOSXSystemProxySettings {
 public:
  NS_INLINE_DECL_REFCOUNTING_INHERITED(OSXSystemProxySettingsAsync,
                                       nsOSXSystemProxySettings)
  NS_DECL_NSISYSTEMPROXYSETTINGS

  OSXSystemProxySettingsAsync();

 protected:
  virtual void OnProxyConfigChangedInternal() override;
  void InitDone() override;

 private:
  virtual ~OSXSystemProxySettingsAsync();

  ProxyConfig mConfig;
};

OSXSystemProxySettingsAsync::OSXSystemProxySettingsAsync() = default;

OSXSystemProxySettingsAsync::~OSXSystemProxySettingsAsync() = default;

void OSXSystemProxySettingsAsync::InitDone() { OnProxyConfigChangedInternal(); }

void OSXSystemProxySettingsAsync::OnProxyConfigChangedInternal() {
  ProxyConfig config;

  // PAC
  nsAutoCString pacUrl;
  if (IsAutoconfigEnabled() && NS_SUCCEEDED(GetAutoconfigURL(pacUrl))) {
    config.SetPACUrl(pacUrl);
  }

  // proxies (for now: PROXY and SOCKS)
  for (const SchemeMapping* keys = gSchemeMappingList; keys->mScheme != NULL;
       ++keys) {
    // Check the proxy is enabled
    NSNumber* enabled = [mProxyDict objectForKey:(NSString*)keys->mEnabled];
    if (!(enabled == NULL || [enabled isKindOfClass:[NSNumber class]])) {
      continue;
    }

    if ([enabled intValue] == 0) {
      continue;
    }

    // Get the proxy host
    NSString* host = [mProxyDict objectForKey:(NSString*)keys->mHost];
    if (host == NULL) break;
    if (!([host isKindOfClass:[NSString class]])) {
      continue;
    }

    nsCString resultHost;
    resultHost.Assign([host UTF8String]);

    // Get the proxy port
    NSNumber* port = [mProxyDict objectForKey:(NSString*)keys->mPort];
    if (!([port isKindOfClass:[NSNumber class]])) {
      continue;
    }

    int32_t resultPort = [port intValue];
    ProxyServer server(ProxyConfig::ToProxyType(keys->mScheme), resultHost,
                       resultPort);
    config.Rules().mProxyServers[server.Type()] = std::move(server);
  }

  // exceptions
  NSArray* exceptionList =
      [mProxyDict objectForKey:(NSString*)kSCPropNetProxiesExceptionsList];
  if (exceptionList != NULL && [exceptionList isKindOfClass:[NSArray class]]) {
    NSEnumerator* exceptionEnumerator = [exceptionList objectEnumerator];
    NSString* currentValue = NULL;
    while ((currentValue = [exceptionEnumerator nextObject])) {
      if (currentValue != NULL &&
          [currentValue isKindOfClass:[NSString class]]) {
        nsCString overrideStr([currentValue UTF8String]);
        config.ByPassRules().mExceptions.AppendElement(std::move(overrideStr));
      }
    }
  }

  mConfig = std::move(config);
}

NS_IMETHODIMP
OSXSystemProxySettingsAsync::GetMainThreadOnly(bool* aMainThreadOnly) {
  return nsOSXSystemProxySettings::GetMainThreadOnly(aMainThreadOnly);
}

NS_IMETHODIMP
OSXSystemProxySettingsAsync::GetPACURI(nsACString& aResult) {
  aResult.Assign(mConfig.PACUrl());
  return NS_OK;
}

NS_IMETHODIMP
OSXSystemProxySettingsAsync::GetSystemWPADSetting(bool* aSystemWPADSetting) {
  return nsOSXSystemProxySettings::GetSystemWPADSetting(aSystemWPADSetting);
}

NS_IMETHODIMP
OSXSystemProxySettingsAsync::GetProxyForURI(const nsACString& aSpec,
                                            const nsACString& aScheme,
                                            const nsACString& aHost,
                                            const int32_t aPort,
                                            nsACString& aResult) {
  for (const auto& bypassRule : mConfig.ByPassRules().mExceptions) {
    if (mozilla::toolkit::system::IsHostProxyEntry(aHost, bypassRule)) {
      aResult.AssignLiteral("DIRECT");
      return NS_OK;
    }
  }

  mConfig.GetProxyString(aScheme, aResult);
  return NS_OK;
}

NS_IMPL_COMPONENT_FACTORY(nsOSXSystemProxySettings) {
  auto settings =
      mozilla::StaticPrefs::network_proxy_detect_system_proxy_changes()
          ? mozilla::MakeRefPtr<OSXSystemProxySettingsAsync>()
          : mozilla::MakeRefPtr<nsOSXSystemProxySettings>();
  if (NS_SUCCEEDED(settings->Init())) {
    return settings.forget().downcast<nsISupports>();
  }
  return nullptr;
}