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" 14#import "RTCICEServer+JSON.h" 15 16// TODO(tkchin): move this to a configuration object. 17static NSString *kTURNOriginURLString = @"https://apprtc.appspot.com"; 18static NSString *kARDCEODTURNClientErrorDomain = @"ARDCEODTURNClient"; 19static 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) { 47 completionHandler(turnServers, error); 48 return; 49 } 50 NSDictionary *dict = [NSDictionary dictionaryWithJSONData:data]; 51 turnServers = [RTCICEServer serversFromCEODJSONDictionary:dict]; 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 67