]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
macOS: Hide exclude private IPs when PrivateKey / PublicKey is missing
authorRoopesh Chander <roop@roopc.net>
Fri, 22 Mar 2019 10:22:39 +0000 (15:52 +0530)
committerRoopesh Chander <roop@roopc.net>
Fri, 22 Mar 2019 10:30:45 +0000 (16:00 +0530)
Signed-off-by: Roopesh Chander <roop@roopc.net>
WireGuard/WireGuard/UI/macOS/View/ConfTextStorage.swift
WireGuard/WireGuard/UI/macOS/View/ConfTextView.swift

index 2219759ee0195bac6df6830237fdfdf2b2c71b3f..6da5c98e7e107145312208096ca98b15c5ef02e5 100644 (file)
@@ -19,6 +19,7 @@ class ConfTextStorage: NSTextStorage {
     private(set) var hasOnePeer: Bool = false
     private(set) var lastOnePeerAllowedIPs = [String]()
     private(set) var lastOnePeerDNSServers = [String]()
+    private(set) var lastOnePeerHasPublicKey = false
 
     override init() {
         backingStore = NSMutableAttributedString(string: "")
@@ -88,6 +89,7 @@ class ConfTextStorage: NSTextStorage {
         hasOnePeer = false
         lastOnePeerAllowedIPs = []
         lastOnePeerDNSServers = []
+        lastOnePeerHasPublicKey = false
     }
 
     func evaluateExcludePrivateIPs(highlightSpans: UnsafePointer<highlight_span>) {
@@ -125,6 +127,8 @@ class ConfTextStorage: NSTextStorage {
                                  backingStore.attributedSubstring(from: NSRange(location: nextnext.pointee.start, length: nextnext.pointee.len)).string
                 }
                 lastOnePeerAllowedIPs.append(substring)
+            } else if span.type == HighlightPublicKey {
+                lastOnePeerHasPublicKey = true
             }
             spans = spans.successor()
         }
index 2ea8f843190449fb658342507372a91beb8cd678..6016e082ea228d9a934c2aebe0dfb0550c6a948b 100644 (file)
@@ -14,6 +14,7 @@ class ConfTextView: NSTextView {
     override var string: String {
         didSet {
             confTextStorage.highlightSyntax()
+            updateConfigData()
         }
     }
 
@@ -53,6 +54,21 @@ class ConfTextView: NSTextView {
         }
     }
 
+    private func updateConfigData() {
+        if hasError != confTextStorage.hasError {
+            hasError = confTextStorage.hasError
+        }
+        if privateKeyString != confTextStorage.privateKeyString {
+            privateKeyString = confTextStorage.privateKeyString
+        }
+        let hasSyntaxError = confTextStorage.hasError
+        let hasSemanticError = confTextStorage.privateKeyString == nil || !confTextStorage.lastOnePeerHasPublicKey
+        let updatedSinglePeerAllowedIPs = confTextStorage.hasOnePeer && !hasSyntaxError && !hasSemanticError ? confTextStorage.lastOnePeerAllowedIPs : nil
+        if singlePeerAllowedIPs != updatedSinglePeerAllowedIPs {
+            singlePeerAllowedIPs = updatedSinglePeerAllowedIPs
+        }
+    }
+
     func setConfText(_ text: String) {
         let fullTextRange = NSRange(location: 0, length: (string as NSString).length)
         if shouldChangeText(in: fullTextRange, replacementString: text) {
@@ -66,16 +82,7 @@ extension ConfTextView: NSTextViewDelegate {
 
     func textDidChange(_ notification: Notification) {
         confTextStorage.highlightSyntax()
-        if hasError != confTextStorage.hasError {
-            hasError = confTextStorage.hasError
-        }
-        if privateKeyString != confTextStorage.privateKeyString {
-            privateKeyString = confTextStorage.privateKeyString
-        }
-        let updatedSinglePeerAllowedIPs = confTextStorage.hasOnePeer && !confTextStorage.hasError ? confTextStorage.lastOnePeerAllowedIPs : nil
-        if singlePeerAllowedIPs != updatedSinglePeerAllowedIPs {
-            singlePeerAllowedIPs = updatedSinglePeerAllowedIPs
-        }
+        updateConfigData()
         needsDisplay = true
     }