From 15802d96dff9f8daae2267475affd8f5661fe4ea Mon Sep 17 00:00:00 2001 From: Swen van Zanten Date: Sun, 7 Oct 2018 15:54:29 +0200 Subject: [PATCH] Make sure the Tor controller connects --- VergeiOS/AppDelegate.swift | 6 +-- VergeiOS/Http/TorClient.swift | 48 ++++++++++++------- .../Setup/LoadingTorViewController.swift | 8 +++- .../Views/Setup/WelcomeViewController.swift | 5 +- .../TransactionTableViewController.swift | 4 ++ 5 files changed, 47 insertions(+), 24 deletions(-) diff --git a/VergeiOS/AppDelegate.swift b/VergeiOS/AppDelegate.swift index 057383e4..5619d1e9 100644 --- a/VergeiOS/AppDelegate.swift +++ b/VergeiOS/AppDelegate.swift @@ -28,15 +28,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate { registerAppforDetectLockState() setupListeners() - + // Start the tor client TorClient.shared.start { DispatchQueue.main.asyncAfter(deadline: .now() + 0.75) { // Start the price ticker. PriceTicker.shared.start() - - let loadingViewController = self.window?.rootViewController as! LoadingTorViewController - loadingViewController.completeLoading() } } @@ -233,4 +230,3 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } } - diff --git a/VergeiOS/Http/TorClient.swift b/VergeiOS/Http/TorClient.swift index 6f9f3f41..bce3f679 100644 --- a/VergeiOS/Http/TorClient.swift +++ b/VergeiOS/Http/TorClient.swift @@ -77,24 +77,20 @@ class TorClient { NotificationCenter.default.post(name: .didStartTorThread, object: self) } - DispatchQueue.main.asyncAfter(deadline: .now() + 4) { - do { - if !self.controller.isConnected { - try self.controller?.connect() - - NotificationCenter.default.post(name: .didConnectTorController, object: self) - } - - try self.authenticateController { - print("Tor tunnel started! 🤩") - - NotificationCenter.default.post(name: .didEstablishTorConnection, object: self) + DispatchQueue.main.asyncAfter(deadline: .now() + 2) { + // Connect Tor controller. + self.connectController(completion: completion) - completion() + // Make sure the controller connects. + var interval: Timer! + interval = setInterval(4) { + if !self.controller.isConnected { + print("Retry tor controller connection") + self.connectController(completion: completion) + } else { + print("Remove tor controller connection interval") + interval.invalidate() } - } catch { - print(error.localizedDescription) - completion() } } } @@ -119,6 +115,26 @@ class TorClient { } } + private func connectController(completion: @escaping () -> Void) { + do { + if !self.controller.isConnected { + try self.controller?.connect() + NotificationCenter.default.post(name: .didConnectTorController, object: self) + } + + try self.authenticateController { + print("Tor tunnel started! 🤩") + + NotificationCenter.default.post(name: .didEstablishTorConnection, object: self) + + completion() + } + } catch { + print(error.localizedDescription) + completion() + } + } + private func authenticateController(completion: @escaping () -> Void) throws -> Void { let cookie = try Data( contentsOf: config.dataDirectory!.appendingPathComponent("control_auth_cookie"), diff --git a/VergeiOS/Views/Setup/LoadingTorViewController.swift b/VergeiOS/Views/Setup/LoadingTorViewController.swift index 3f866a84..ea68a516 100644 --- a/VergeiOS/Views/Setup/LoadingTorViewController.swift +++ b/VergeiOS/Views/Setup/LoadingTorViewController.swift @@ -14,10 +14,14 @@ class LoadingTorViewController: UIViewController { return .lightContent } - func completeLoading() { + override func viewDidLoad() { + super.viewDidLoad() + let identifier = WalletManager.default.setup ? "showWallet" : "showWelcomeView" - self.performSegue(withIdentifier: identifier, sender: self) + DispatchQueue.main.async { + self.performSegue(withIdentifier: identifier, sender: self) + } } // MARK: - Navigation diff --git a/VergeiOS/Views/Setup/WelcomeViewController.swift b/VergeiOS/Views/Setup/WelcomeViewController.swift index 27a764c9..f63c74e5 100644 --- a/VergeiOS/Views/Setup/WelcomeViewController.swift +++ b/VergeiOS/Views/Setup/WelcomeViewController.swift @@ -16,9 +16,12 @@ class WelcomeViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - + + // TODO: Create a Tor setup page and move this over. // Set Tor enabled as default. WalletManager.default.useTor = true + // Now start Tor. + TorClient.shared.start {} } } diff --git a/VergeiOS/Views/Transactions/TransactionTableViewController.swift b/VergeiOS/Views/Transactions/TransactionTableViewController.swift index e1294781..543fd075 100644 --- a/VergeiOS/Views/Transactions/TransactionTableViewController.swift +++ b/VergeiOS/Views/Transactions/TransactionTableViewController.swift @@ -164,6 +164,10 @@ class TransactionTableViewController: UIViewController, UITableViewDelegate, UIT } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + if indexPath.section == 0 { + return + } + if items[indexPath.row].txid == transaction?.txid { return }