Skip to content

Commit

Permalink
Make sure the Tor controller connects
Browse files Browse the repository at this point in the history
  • Loading branch information
SwenVanZanten committed Oct 7, 2018
1 parent dc09a32 commit 15802d9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 24 deletions.
6 changes: 1 addition & 5 deletions VergeiOS/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -233,4 +230,3 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}

}

48 changes: 32 additions & 16 deletions VergeiOS/Http/TorClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
Expand All @@ -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"),
Expand Down
8 changes: 6 additions & 2 deletions VergeiOS/Views/Setup/LoadingTorViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion VergeiOS/Views/Setup/WelcomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 15802d9

Please sign in to comment.