WebxdcViewController.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. import UIKit
  2. import WebKit
  3. import DcCore
  4. class WebxdcViewController: WebViewViewController {
  5. enum WebxdcHandler: String {
  6. case log = "log"
  7. case setUpdateListener = "setUpdateListener"
  8. case sendStatusUpdate = "sendStatusUpdateHandler"
  9. }
  10. let INTERNALSCHEMA = "webxdc"
  11. var messageId: Int
  12. var webxdcUpdateObserver: NSObjectProtocol?
  13. var webxdcName: String = ""
  14. var sourceCodeUrl: String?
  15. private var allowInternet: Bool = false
  16. private var shortcutManager: ShortcutManager?
  17. private lazy var moreButton: UIBarButtonItem = {
  18. let image: UIImage?
  19. if #available(iOS 13.0, *) {
  20. image = UIImage(systemName: "ellipsis.circle")
  21. } else {
  22. image = UIImage(named: "ic_more")
  23. }
  24. return UIBarButtonItem(image: image,
  25. style: .plain,
  26. target: self,
  27. action: #selector(moreButtonPressed))
  28. }()
  29. // Block just everything, except of webxdc urls
  30. let blockRules = """
  31. [
  32. {
  33. "trigger": {
  34. "url-filter": ".*"
  35. },
  36. "action": {
  37. "type": "block"
  38. }
  39. },
  40. {
  41. "trigger": {
  42. "url-filter": "webxdc://*"
  43. },
  44. "action": {
  45. "type": "ignore-previous-rules"
  46. }
  47. }
  48. ]
  49. """
  50. lazy var webxdcbridge: String = {
  51. let addr = dcContext.addr?
  52. .addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
  53. let displayname = (dcContext.displayname ?? dcContext.addr)?
  54. .addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
  55. let script = """
  56. window.webxdc = (() => {
  57. var log = (s)=>webkit.messageHandlers.log.postMessage(s);
  58. var update_listener = () => {};
  59. let should_run_again = false;
  60. let running = false;
  61. let lastSerial = 0;
  62. window.__webxdcUpdate = async () => {
  63. if (running) {
  64. should_run_again = true
  65. return
  66. }
  67. should_run_again = false
  68. running = true;
  69. try {
  70. const updates = await fetch("webxdc-update.json?"+lastSerial).then((response) => response.json())
  71. updates.forEach((update) => {
  72. update_listener(update);
  73. if (lastSerial < update["max_serial"]){
  74. lastSerial = update["max_serial"]
  75. }
  76. });
  77. } catch (e) {
  78. log("json error: "+ e.message)
  79. } finally {
  80. running = false;
  81. if (should_run_again) {
  82. await window.__webxdcUpdate()
  83. }
  84. }
  85. }
  86. return {
  87. selfAddr: decodeURI("\((addr ?? "unknown"))"),
  88. selfName: decodeURI("\((displayname ?? "unknown"))"),
  89. setUpdateListener: (cb, serial) => {
  90. update_listener = cb
  91. return window.__webxdcUpdate()
  92. },
  93. getAllUpdates: () => {
  94. console.error("deprecated 2022-02-20 all updates are returned through the callback set by setUpdateListener");
  95. return Promise.resolve([]);
  96. },
  97. sendUpdate: (payload, descr) => {
  98. // only one parameter is allowed, we we create a new parameter object here
  99. var parameter = {
  100. payload: payload,
  101. descr: descr
  102. };
  103. webkit.messageHandlers.sendStatusUpdateHandler.postMessage(parameter);
  104. },
  105. };
  106. })();
  107. """
  108. return script
  109. }()
  110. override var configuration: WKWebViewConfiguration {
  111. let config = WKWebViewConfiguration()
  112. let preferences = WKPreferences()
  113. let contentController = WKUserContentController()
  114. contentController.add(self, name: WebxdcHandler.sendStatusUpdate.rawValue)
  115. contentController.add(self, name: WebxdcHandler.setUpdateListener.rawValue)
  116. contentController.add(self, name: WebxdcHandler.log.rawValue)
  117. let scriptSource = """
  118. window.RTCPeerConnection = ()=>{};
  119. RTCPeerConnection = ()=>{};
  120. try {
  121. window.webkitRTCPeerConnection = ()=>{};
  122. webkitRTCPeerConnection = ()=>{};
  123. } catch (e){}
  124. """
  125. let script = WKUserScript(source: scriptSource, injectionTime: .atDocumentStart, forMainFrameOnly: false)
  126. contentController.addUserScript(script)
  127. config.userContentController = contentController
  128. config.setURLSchemeHandler(self, forURLScheme: INTERNALSCHEMA)
  129. config.mediaTypesRequiringUserActionForPlayback = []
  130. config.allowsInlineMediaPlayback = true
  131. if #available(iOS 13.0, *) {
  132. preferences.isFraudulentWebsiteWarningEnabled = true
  133. }
  134. if #available(iOS 14.0, *) {
  135. config.defaultWebpagePreferences.allowsContentJavaScript = true
  136. } else {
  137. preferences.javaScriptEnabled = true
  138. }
  139. preferences.javaScriptCanOpenWindowsAutomatically = false
  140. config.preferences = preferences
  141. return config
  142. }
  143. init(dcContext: DcContext, messageId: Int) {
  144. self.messageId = messageId
  145. self.shortcutManager = ShortcutManager(dcContext: dcContext, messageId: messageId)
  146. super.init(dcContext: dcContext)
  147. }
  148. required init?(coder: NSCoder) {
  149. fatalError("init(coder:) has not been implemented")
  150. }
  151. override func viewDidLoad() {
  152. super.viewDidLoad()
  153. let msg = dcContext.getMessage(id: messageId)
  154. let dict = msg.getWebxdcInfoDict()
  155. let document = dict["document"] as? String ?? ""
  156. webxdcName = dict["name"] as? String ?? "ErrName" // name should not be empty
  157. let chatName = dcContext.getChat(chatId: msg.chatId).name
  158. self.allowInternet = dict["internet_access"] as? Bool ?? false
  159. self.title = document.isEmpty ? "\(webxdcName) – \(chatName)" : "\(document) – \(chatName)"
  160. navigationItem.rightBarButtonItem = moreButton
  161. if let sourceCode = dict["source_code_url"] as? String,
  162. !sourceCode.isEmpty {
  163. sourceCodeUrl = sourceCode
  164. }
  165. }
  166. override func willMove(toParent parent: UIViewController?) {
  167. super.willMove(toParent: parent)
  168. let willBeRemoved = parent == nil
  169. navigationController?.interactivePopGestureRecognizer?.isEnabled = willBeRemoved
  170. if willBeRemoved {
  171. let nc = NotificationCenter.default
  172. if let webxdcUpdateObserver = webxdcUpdateObserver {
  173. nc.removeObserver(webxdcUpdateObserver)
  174. }
  175. shortcutManager = nil
  176. } else {
  177. addObserver()
  178. }
  179. }
  180. private func addObserver() {
  181. let nc = NotificationCenter.default
  182. webxdcUpdateObserver = nc.addObserver(
  183. forName: dcNotificationWebxdcUpdate,
  184. object: nil,
  185. queue: OperationQueue.main
  186. ) { [weak self] notification in
  187. guard let self = self else { return }
  188. guard let ui = notification.userInfo,
  189. let messageId = ui["message_id"] as? Int else {
  190. logger.error("failed to handle dcNotificationWebxdcUpdate")
  191. return
  192. }
  193. if messageId == self.messageId {
  194. self.updateWebxdc()
  195. }
  196. }
  197. }
  198. override func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
  199. if let url = navigationAction.request.url {
  200. if url.scheme == "mailto" {
  201. openChatFor(url: url)
  202. decisionHandler(.cancel)
  203. return
  204. } else if url.scheme != INTERNALSCHEMA {
  205. logger.debug("cancel loading: \(url)")
  206. decisionHandler(.cancel)
  207. return
  208. }
  209. }
  210. logger.debug("loading: \(String(describing: navigationAction.request.url))")
  211. decisionHandler(.allow)
  212. }
  213. override func viewWillAppear(_ animated: Bool) {
  214. super.viewWillAppear(animated)
  215. if allowInternet {
  216. loadHtml()
  217. } else {
  218. loadRestrictedHtml()
  219. }
  220. }
  221. override func viewDidDisappear(_ animated: Bool) {
  222. super.viewDidDisappear(animated)
  223. if #available(iOS 15.0, *) {
  224. webView.setAllMediaPlaybackSuspended(true)
  225. }
  226. }
  227. private func loadRestrictedHtml() {
  228. WKContentRuleListStore.default().compileContentRuleList(
  229. forIdentifier: "WebxdcContentBlockingRules",
  230. encodedContentRuleList: blockRules) { (contentRuleList, error) in
  231. guard let contentRuleList = contentRuleList, error == nil else {
  232. return
  233. }
  234. let configuration = self.webView.configuration
  235. configuration.userContentController.add(contentRuleList)
  236. self.loadHtml()
  237. }
  238. }
  239. private func loadHtml() {
  240. DispatchQueue.global(qos: .userInitiated).async { [weak self] in
  241. guard let self = self else { return }
  242. let url = URL(string: "\(self.INTERNALSCHEMA)://acc\(self.dcContext.id)-msg\(self.messageId).localhost/index.html")
  243. let urlRequest = URLRequest(url: url!)
  244. DispatchQueue.main.async {
  245. self.webView.load(urlRequest)
  246. }
  247. }
  248. }
  249. private func updateWebxdc() {
  250. webView.evaluateJavaScript("window.__webxdcUpdate()", completionHandler: nil)
  251. }
  252. @objc private func moreButtonPressed() {
  253. let alert = UIAlertController(title: webxdcName + " – " + String.localized("webxdc_app"),
  254. message: nil,
  255. preferredStyle: .safeActionSheet)
  256. let addToHomescreenAction = UIAlertAction(title: String.localized("add_to_home_screen"), style: .default, handler: addToHomeScreen(_:))
  257. alert.addAction(addToHomescreenAction)
  258. if sourceCodeUrl != nil {
  259. let sourceCodeAction = UIAlertAction(title: String.localized("source_code"), style: .default, handler: openUrl(_:))
  260. alert.addAction(sourceCodeAction)
  261. }
  262. let cancelAction = UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil)
  263. alert.addAction(cancelAction)
  264. self.present(alert, animated: true, completion: nil)
  265. }
  266. private func addToHomeScreen(_ action: UIAlertAction) {
  267. shortcutManager?.showShortcutLandingPage()
  268. }
  269. private func openUrl(_ action: UIAlertAction) {
  270. if let sourceCodeUrl = sourceCodeUrl,
  271. let url = URL(string: sourceCodeUrl) {
  272. UIApplication.shared.open(url)
  273. }
  274. }
  275. }
  276. extension WebxdcViewController: WKScriptMessageHandler {
  277. func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
  278. let handler = WebxdcHandler(rawValue: message.name)
  279. switch handler {
  280. case .log:
  281. guard let msg = message.body as? String else {
  282. logger.error("could not convert param \(message.body) to string")
  283. return
  284. }
  285. logger.debug("webxdc log msg: "+msg)
  286. case .sendStatusUpdate:
  287. guard let dict = message.body as? [String: AnyObject],
  288. let payloadDict = dict["payload"] as? [String: AnyObject],
  289. let payloadJson = try? JSONSerialization.data(withJSONObject: payloadDict, options: []),
  290. let payloadString = String(data: payloadJson, encoding: .utf8),
  291. let description = dict["descr"] as? String else {
  292. logger.error("Failed to parse status update parameters \(message.body)")
  293. return
  294. }
  295. _ = dcContext.sendWebxdcStatusUpdate(msgId: messageId, payload: payloadString, description: description)
  296. default:
  297. logger.debug("another method was called")
  298. }
  299. }
  300. }
  301. extension WebxdcViewController: WKURLSchemeHandler {
  302. func webView(_ webView: WKWebView, start urlSchemeTask: WKURLSchemeTask) {
  303. if let url = urlSchemeTask.request.url, let scheme = url.scheme, scheme == INTERNALSCHEMA {
  304. if url.path == "/webxdc-update.json" || url.path == "webxdc-update.json" {
  305. let lastKnownSerial = Int(url.query ?? "0") ?? 0
  306. let data = Data(
  307. dcContext.getWebxdcStatusUpdates(msgId: messageId, lastKnownSerial: lastKnownSerial).utf8)
  308. let response = URLResponse(url: url, mimeType: "application/json", expectedContentLength: data.count, textEncodingName: "utf-8")
  309. urlSchemeTask.didReceive(response)
  310. urlSchemeTask.didReceive(data)
  311. urlSchemeTask.didFinish()
  312. return
  313. }
  314. let file = url.path
  315. let dcMsg = dcContext.getMessage(id: messageId)
  316. var data: Data
  317. if url.lastPathComponent == "webxdc.js" {
  318. data = Data(webxdcbridge.utf8)
  319. } else {
  320. data = dcMsg.getWebxdcBlob(filename: file)
  321. }
  322. let mimeType = DcUtils.getMimeTypeForPath(path: file)
  323. let statusCode = (data.isEmpty ? 404 : 200)
  324. var headerFields = [
  325. "Content-Type": mimeType,
  326. "Content-Length": "\(data.count)",
  327. ]
  328. if !self.allowInternet {
  329. headerFields["Content-Security-Policy"] = """
  330. default-src 'self';
  331. style-src 'self' 'unsafe-inline' blob: ;
  332. font-src 'self' data: blob: ;
  333. script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: ;
  334. connect-src 'self' data: blob: ;
  335. img-src 'self' data: blob: ;
  336. webrtc 'block' ;
  337. """
  338. }
  339. guard let response = HTTPURLResponse(
  340. url: url,
  341. statusCode: statusCode,
  342. httpVersion: "HTTP/1.1",
  343. headerFields: headerFields
  344. ) else {
  345. return
  346. }
  347. urlSchemeTask.didReceive(response)
  348. urlSchemeTask.didReceive(data)
  349. urlSchemeTask.didFinish()
  350. } else {
  351. logger.debug("not loading \(String(describing: urlSchemeTask.request.url))")
  352. }
  353. }
  354. func webView(_ webView: WKWebView, stop urlSchemeTask: WKURLSchemeTask) {
  355. }
  356. }