You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
3.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #import "ARDTURNClient+Internal.h"
  11. #import "ARDUtilities.h"
  12. #import "RTCIceServer+JSON.h"
  13. // TODO(tkchin): move this to a configuration object.
  14. static NSString *kTURNRefererURLString = @"https://appr.tc";
  15. static NSString *kARDTURNClientErrorDomain = @"ARDTURNClient";
  16. static NSInteger kARDTURNClientErrorBadResponse = -1;
  17. @implementation ARDTURNClient {
  18. NSURL *_url;
  19. }
  20. - (instancetype)initWithURL:(NSURL *)url {
  21. NSParameterAssert([url absoluteString].length);
  22. if (self = [super init]) {
  23. _url = url;
  24. }
  25. return self;
  26. }
  27. - (void)requestServersWithCompletionHandler:
  28. (void (^)(NSArray *turnServers, NSError *error))completionHandler {
  29. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:_url];
  30. [NSURLConnection sendAsyncRequest:request
  31. completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  32. if (error) {
  33. completionHandler(nil, error);
  34. return;
  35. }
  36. NSDictionary *responseDict = [NSDictionary dictionaryWithJSONData:data];
  37. NSString *iceServerUrl = responseDict[@"ice_server_url"];
  38. [self makeTurnServerRequestToURL:[NSURL URLWithString:iceServerUrl]
  39. WithCompletionHandler:completionHandler];
  40. }];
  41. }
  42. #pragma mark - Private
  43. - (void)makeTurnServerRequestToURL:(NSURL *)url
  44. WithCompletionHandler:(void (^)(NSArray *turnServers,
  45. NSError *error))completionHandler {
  46. NSMutableURLRequest *iceServerRequest = [NSMutableURLRequest requestWithURL:url];
  47. iceServerRequest.HTTPMethod = @"POST";
  48. [iceServerRequest addValue:kTURNRefererURLString forHTTPHeaderField:@"referer"];
  49. [NSURLConnection sendAsyncRequest:iceServerRequest
  50. completionHandler:^(NSURLResponse *response,
  51. NSData *data,
  52. NSError *error) {
  53. if (error) {
  54. completionHandler(nil, error);
  55. return;
  56. }
  57. NSDictionary *turnResponseDict = [NSDictionary dictionaryWithJSONData:data];
  58. NSMutableArray *turnServers = [NSMutableArray array];
  59. [turnResponseDict[@"iceServers"] enumerateObjectsUsingBlock:
  60. ^(NSDictionary *obj, NSUInteger idx, BOOL *stop){
  61. [turnServers addObject:[RTCIceServer serverFromJSONDictionary:obj]];
  62. }];
  63. if (!turnServers) {
  64. NSError *responseError =
  65. [[NSError alloc] initWithDomain:kARDTURNClientErrorDomain
  66. code:kARDTURNClientErrorBadResponse
  67. userInfo:@{
  68. NSLocalizedDescriptionKey: @"Bad TURN response.",
  69. }];
  70. completionHandler(nil, responseError);
  71. return;
  72. }
  73. completionHandler(turnServers, nil);
  74. }];
  75. }
  76. @end