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