blob: 5be33353188737b351cd5f910c3adacaa549ba6c [file] [log] [blame]
Donald E Curtisa8736442015-08-05 15:48:13 -07001/*
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"
hjon79858f82016-03-13 22:08:26 -070014#import "RTCIceServer+JSON.h"
Donald E Curtisa8736442015-08-05 15:48:13 -070015
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) {
hjon79858f82016-03-13 22:08:26 -070047 completionHandler(nil, error);
Donald E Curtisa8736442015-08-05 15:48:13 -070048 return;
49 }
50 NSDictionary *dict = [NSDictionary dictionaryWithJSONData:data];
hjon79858f82016-03-13 22:08:26 -070051 turnServers = @[ [RTCIceServer serverFromCEODJSONDictionary:dict] ];
Donald E Curtisa8736442015-08-05 15:48:13 -070052 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