Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 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 "ARDCEODTURNClient.h" |
| 12 | |
| 13 | #import "ARDUtilities.h" |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 14 | #import "RTCIceServer+JSON.h" |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 15 | |
| 16 | // TODO(tkchin): move this to a configuration object. |
| 17 | static NSString *kTURNOriginURLString = @"https://apprtc.appspot.com"; |
| 18 | static NSString *kARDCEODTURNClientErrorDomain = @"ARDCEODTURNClient"; |
| 19 | static NSInteger kARDCEODTURNClientErrorBadResponse = -1; |
| 20 | |
| 21 | @implementation ARDCEODTURNClient { |
| 22 | NSURL *_url; |
| 23 | } |
| 24 | |
| 25 | - (instancetype)initWithURL:(NSURL *)url { |
| 26 | NSParameterAssert([url absoluteString].length); |
| 27 | if (self = [super init]) { |
| 28 | _url = url; |
| 29 | } |
| 30 | return self; |
| 31 | } |
| 32 | |
| 33 | - (void)requestServersWithCompletionHandler: |
| 34 | (void (^)(NSArray *turnServers, |
| 35 | NSError *error))completionHandler { |
| 36 | NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:_url]; |
| 37 | // We need to set origin because TURN provider whitelists requests based on |
| 38 | // origin. |
| 39 | [request addValue:@"Mozilla/5.0" forHTTPHeaderField:@"user-agent"]; |
| 40 | [request addValue:kTURNOriginURLString forHTTPHeaderField:@"origin"]; |
| 41 | [NSURLConnection sendAsyncRequest:request |
| 42 | completionHandler:^(NSURLResponse *response, |
| 43 | NSData *data, |
| 44 | NSError *error) { |
| 45 | NSArray *turnServers = [NSArray array]; |
| 46 | if (error) { |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 47 | completionHandler(nil, error); |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 48 | return; |
| 49 | } |
| 50 | NSDictionary *dict = [NSDictionary dictionaryWithJSONData:data]; |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 51 | turnServers = @[ [RTCIceServer serverFromCEODJSONDictionary:dict] ]; |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 52 | if (!turnServers) { |
| 53 | NSError *responseError = |
| 54 | [[NSError alloc] initWithDomain:kARDCEODTURNClientErrorDomain |
| 55 | code:kARDCEODTURNClientErrorBadResponse |
| 56 | userInfo:@{ |
| 57 | NSLocalizedDescriptionKey: @"Bad TURN response.", |
| 58 | }]; |
| 59 | completionHandler(turnServers, responseError); |
| 60 | return; |
| 61 | } |
| 62 | completionHandler(turnServers, nil); |
| 63 | }]; |
| 64 | } |
| 65 | |
| 66 | @end |