AppDelegate.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // AppDelegate.swift
  3. // TunneledWebView
  4. //
  5. /*
  6. Licensed under Creative Commons Zero (CC0).
  7. https://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. import UIKit
  10. import PsiphonTunnel
  11. @UIApplicationMain
  12. @objc class AppDelegate: UIResponder, UIApplicationDelegate, JAHPAuthenticatingHTTPProtocolDelegate {
  13. var window: UIWindow?
  14. var socksProxyPort: Int = 0
  15. var httpProxyPort: Int = 0
  16. // The instance of PsiphonTunnel we'll use for connecting.
  17. var psiphonTunnel: PsiphonTunnel?
  18. class func sharedDelegate() -> AppDelegate {
  19. var delegate: AppDelegate?
  20. if (Thread.isMainThread) {
  21. delegate = UIApplication.shared.delegate as? AppDelegate
  22. } else {
  23. DispatchQueue.main.sync {
  24. delegate = UIApplication.shared.delegate as? AppDelegate
  25. }
  26. }
  27. return delegate!
  28. }
  29. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  30. // Override point for customization after application launch.
  31. // Set the class delegate and register NSURL subclass
  32. // JAHPAuthenticatingHTTPProtocol with NSURLProtocol.
  33. // See comments for `setDelegate` and `start` in
  34. // JAHPAuthenticatingHTTPProtocol.h
  35. JAHPAuthenticatingHTTPProtocol.setDelegate(self)
  36. JAHPAuthenticatingHTTPProtocol.start()
  37. self.psiphonTunnel = PsiphonTunnel.newPsiphonTunnel(self)
  38. return true
  39. }
  40. func applicationWillResignActive(_ application: UIApplication) {
  41. // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
  42. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
  43. }
  44. func applicationDidEnterBackground(_ application: UIApplication) {
  45. // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
  46. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  47. }
  48. func applicationWillEnterForeground(_ application: UIApplication) {
  49. // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
  50. }
  51. func applicationDidBecomeActive(_ application: UIApplication) {
  52. // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  53. DispatchQueue.global(qos: .default).async {
  54. // Start up the tunnel and begin connecting.
  55. // This could be started elsewhere or earlier.
  56. NSLog("Starting tunnel")
  57. guard let success = self.psiphonTunnel?.start(true), success else {
  58. NSLog("psiphonTunnel.start returned false")
  59. return
  60. }
  61. // The Psiphon Library exposes reachability functions, which can be used for detecting internet status.
  62. let reachability = Reachability.forInternetConnection()
  63. let networkStatus = reachability?.currentReachabilityStatus()
  64. NSLog("Internet is reachable? \(networkStatus != NotReachable)")
  65. }
  66. }
  67. func applicationWillTerminate(_ application: UIApplication) {
  68. // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  69. // Clean up the tunnel
  70. NSLog("Stopping tunnel")
  71. self.psiphonTunnel?.stop()
  72. }
  73. }
  74. // MARK: TunneledAppDelegate implementation
  75. // See the protocol definition for details about the methods.
  76. // Note that we're excluding all the optional methods that we aren't using,
  77. // however your needs may be different.
  78. extension AppDelegate: TunneledAppDelegate {
  79. func getPsiphonConfig() -> String? {
  80. // In this example, we're going to retrieve our Psiphon config from a file in the app bundle.
  81. // Alternatively, it could be a string literal in the code, or whatever makes sense.
  82. guard let psiphonConfigUrl = Bundle.main.url(forResource: "psiphon-config", withExtension: "json") else {
  83. NSLog("Error getting Psiphon config resource file URL!")
  84. return nil
  85. }
  86. do {
  87. return try String.init(contentsOf: psiphonConfigUrl)
  88. } catch {
  89. NSLog("Error reading Psiphon config resource file!")
  90. return nil
  91. }
  92. }
  93. /// Read the Psiphon embedded server entries resource file and return the contents.
  94. /// * returns: The string of the contents of the file.
  95. func getEmbeddedServerEntries() -> String? {
  96. guard let psiphonEmbeddedServerEntriesUrl = Bundle.main.url(forResource: "psiphon-embedded-server-entries", withExtension: "txt") else {
  97. NSLog("Error getting Psiphon embedded server entries resource file URL!")
  98. return nil
  99. }
  100. do {
  101. return try String.init(contentsOf: psiphonEmbeddedServerEntriesUrl)
  102. } catch {
  103. NSLog("Error reading Psiphon embedded server entries resource file!")
  104. return nil
  105. }
  106. }
  107. func onDiagnosticMessage(_ message: String) {
  108. NSLog("onDiagnosticMessage: %@", message)
  109. }
  110. func onConnected() {
  111. NSLog("onConnected")
  112. DispatchQueue.main.sync {
  113. let urlString = "https://freegeoip.net"
  114. let url = URL.init(string: urlString)!
  115. let mainView = self.window?.rootViewController as! ViewController
  116. mainView.loadUrl(url)
  117. }
  118. }
  119. func onListeningSocksProxyPort(_ port: Int) {
  120. DispatchQueue.main.async {
  121. JAHPAuthenticatingHTTPProtocol.resetSharedDemux()
  122. self.socksProxyPort = port
  123. }
  124. }
  125. func onListeningHttpProxyPort(_ port: Int) {
  126. DispatchQueue.main.async {
  127. JAHPAuthenticatingHTTPProtocol.resetSharedDemux()
  128. self.httpProxyPort = port
  129. }
  130. }
  131. }