tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ARDTURNClient.m (3393B)


      1 /*
      2 *  Copyright 2014 The WebRTC Project Authors. All rights reserved.
      3 *
      4 *  Use of this source code is governed by a BSD-style license
      5 *  that can be found in the LICENSE file in the root of the source
      6 *  tree. An additional intellectual property rights grant can be found
      7 *  in the file PATENTS.  All contributing project authors may
      8 *  be found in the AUTHORS file in the root of the source tree.
      9 */
     10 
     11 #import "ARDTURNClient+Internal.h"
     12 
     13 #import "ARDUtilities.h"
     14 #import "RTCIceServer+JSON.h"
     15 
     16 // TODO(tkchin): move this to a configuration object.
     17 static NSString *kTURNRefererURLString = @"https://appr.tc";
     18 static NSString *kARDTURNClientErrorDomain = @"ARDTURNClient";
     19 static NSInteger kARDTURNClientErrorBadResponse = -1;
     20 
     21 @implementation ARDTURNClient {
     22  NSURL *_url;
     23 }
     24 
     25 - (instancetype)initWithURL:(NSURL *)url {
     26  NSParameterAssert([url absoluteString].length);
     27  self = [super init];
     28  if (self) {
     29    _url = url;
     30  }
     31  return self;
     32 }
     33 
     34 - (void)requestServersWithCompletionHandler:
     35    (void (^)(NSArray *turnServers, NSError *error))completionHandler {
     36  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:_url];
     37  [NSURLConnection
     38       sendAsyncRequest:request
     39      completionHandler:^(
     40          NSURLResponse *response __unused, NSData *data, NSError *error) {
     41        if (error) {
     42          completionHandler(nil, error);
     43          return;
     44        }
     45        NSDictionary *responseDict = [NSDictionary dictionaryWithJSONData:data];
     46        NSString *iceServerUrl = responseDict[@"ice_server_url"];
     47        [self makeTurnServerRequestToURL:[NSURL URLWithString:iceServerUrl]
     48                   WithCompletionHandler:completionHandler];
     49      }];
     50 }
     51 
     52 #pragma mark - Private
     53 
     54 - (void)makeTurnServerRequestToURL:(NSURL *)url
     55             WithCompletionHandler:(void (^)(NSArray *turnServers,
     56                                             NSError *error))completionHandler {
     57  NSMutableURLRequest *iceServerRequest =
     58      [NSMutableURLRequest requestWithURL:url];
     59  iceServerRequest.HTTPMethod = @"POST";
     60  [iceServerRequest addValue:kTURNRefererURLString
     61          forHTTPHeaderField:@"referer"];
     62  [NSURLConnection
     63       sendAsyncRequest:iceServerRequest
     64      completionHandler:^(
     65          NSURLResponse *response __unused, NSData *data, NSError *error) {
     66        if (error) {
     67          completionHandler(nil, error);
     68          return;
     69        }
     70        NSDictionary *turnResponseDict =
     71            [NSDictionary dictionaryWithJSONData:data];
     72        NSMutableArray *turnServers = [NSMutableArray array];
     73        [turnResponseDict[@"iceServers"]
     74            enumerateObjectsUsingBlock:^(NSDictionary *obj,
     75                                         NSUInteger idx __unused,
     76                                         BOOL *stop __unused) {
     77              [turnServers addObject:[RTC_OBJC_TYPE(RTCIceServer)
     78                                         serverFromJSONDictionary:obj]];
     79            }];
     80        if (!turnServers) {
     81          NSError *responseError = [[NSError alloc]
     82              initWithDomain:kARDTURNClientErrorDomain
     83                        code:kARDTURNClientErrorBadResponse
     84                    userInfo:@{
     85                      NSLocalizedDescriptionKey : @"Bad TURN response.",
     86                    }];
     87          completionHandler(nil, responseError);
     88          return;
     89        }
     90        completionHandler(turnServers, nil);
     91      }];
     92 }
     93 
     94 @end