VoIPIceServerSource.swift 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. import ThreemaFramework
  22. let MIN_SPARE_VALIDITY: TimeInterval = 3600
  23. struct TurnServerInfo {
  24. var turnUrls: Array<String>
  25. var turnUrlsDualStack: Array<String>
  26. var turnUsername: String
  27. var turnPassword: String
  28. var expirationDate: Date
  29. }
  30. enum TurnServerError: Error {
  31. case badServerData
  32. }
  33. class VoIPIceServerSource {
  34. private static var cachedTurnServers: TurnServerInfo?
  35. public static func prefetchIceServers() {
  36. obtainIceServers(dualStack: false) { (result) in
  37. // ignored
  38. }
  39. }
  40. public static func obtainIceServers(dualStack: Bool, completion: @escaping (Result<RTCIceServer, Error>) -> Void) {
  41. if cachedTurnServers != nil {
  42. let minExpiration = Date().addingTimeInterval(MIN_SPARE_VALIDITY)
  43. if cachedTurnServers!.expirationDate > minExpiration {
  44. completion(.success(makeIceServers(dualStack: dualStack, turnServerInfo: cachedTurnServers!)))
  45. return
  46. }
  47. }
  48. // No unexpired TURN server info in cache; must fetch
  49. ServerAPIConnector().obtainTurnServers(with: MyIdentityStore.shared()) { (response: [AnyHashable : Any]?) in
  50. guard let expiration = response?["expiration"] as? TimeInterval,
  51. let turnUrls = response?["turnUrls"] as? Array<String>,
  52. let turnUrlsDualStack = response?["turnUrlsDualStack"] as? Array<String>,
  53. let turnUsername = response?["turnUsername"] as? String,
  54. let turnPassword = response?["turnPassword"] as? String else {
  55. completion(.failure(TurnServerError.badServerData))
  56. return
  57. }
  58. let expirationDate = Date().addingTimeInterval(expiration)
  59. cachedTurnServers = TurnServerInfo(
  60. turnUrls: turnUrls,
  61. turnUrlsDualStack: turnUrlsDualStack,
  62. turnUsername: turnUsername,
  63. turnPassword: turnPassword,
  64. expirationDate: expirationDate
  65. )
  66. completion(.success(makeIceServers(dualStack: dualStack, turnServerInfo: cachedTurnServers!)))
  67. } onError: { (e: Error?) in
  68. completion(.failure(e!))
  69. }
  70. }
  71. private static func makeIceServers(dualStack: Bool, turnServerInfo: TurnServerInfo) -> RTCIceServer {
  72. let urls = dualStack ? turnServerInfo.turnUrlsDualStack : turnServerInfo.turnUrls
  73. return RTCIceServer.init(urlStrings: urls, username: turnServerInfo.turnUsername, credential: turnServerInfo.turnPassword, tlsCertPolicy: .secure)
  74. }
  75. }