[PATCH] Always show "Last Handshake" and "Rx / Tx Bytes"

Kevin Seidel k.seidel at q1.eu
Fri Mar 18 12:34:26 UTC 2022


From: moogle19 <k.seidel at q1.eu>

---
 Sources/WireGuardApp/UI/TunnelViewModel.swift | 37 +++++++++----------
 1 file changed, 18 insertions(+), 19 deletions(-)

diff --git a/Sources/WireGuardApp/UI/TunnelViewModel.swift b/Sources/WireGuardApp/UI/TunnelViewModel.swift
index b65c8cc..1c578c5 100644
--- a/Sources/WireGuardApp/UI/TunnelViewModel.swift
+++ b/Sources/WireGuardApp/UI/TunnelViewModel.swift
@@ -309,7 +309,7 @@ class TunnelViewModel {
                 scratchpad[.preSharedKey] = preSharedKey
             }
             if !config.allowedIPs.isEmpty {
-                scratchpad[.allowedIPs] = config.allowedIPs.map { $0.stringRepresentation }.joined(separator: ", ")
+                scratchpad[.allowedIPs] =  config.allowedIPs.map(\.stringRepresentation).joined(separator: ", ")
             }
             if let endpoint = config.endpoint {
                 scratchpad[.endpoint] = endpoint.stringRepresentation
@@ -317,15 +317,10 @@ class TunnelViewModel {
             if let persistentKeepAlive = config.persistentKeepAlive {
                 scratchpad[.persistentKeepAlive] = String(persistentKeepAlive)
             }
-            if let rxBytes = config.rxBytes {
-                scratchpad[.rxBytes] = prettyBytes(rxBytes)
-            }
-            if let txBytes = config.txBytes {
-                scratchpad[.txBytes] = prettyBytes(txBytes)
-            }
-            if let lastHandshakeTime = config.lastHandshakeTime {
-                scratchpad[.lastHandshakeTime] = prettyTimeAgo(timestamp: lastHandshakeTime)
-            }
+            scratchpad[.rxBytes] = config.rxBytes?.prettyBytes() ?? "-"
+            scratchpad[.txBytes] = config.txBytes?.prettyBytes() ?? "-"
+            scratchpad[.lastHandshakeTime] = config.lastHandshakeTime?.prettyTimeAgo() ?? "-"
+
             return scratchpad
         }
 
@@ -624,30 +619,34 @@ class TunnelViewModel {
     }
 }
 
-private func prettyBytes(_ bytes: UInt64) -> String {
-    switch bytes {
+private extension UInt64 {
+  func prettyBytes() -> String {
+    switch self {
     case 0..<1024:
-        return "\(bytes) B"
+      return "\(self) B"
     case 1024 ..< (1024 * 1024):
-        return String(format: "%.2f", Double(bytes) / 1024) + " KiB"
+      return String(format: "%.2f", Double(self) / 1024) + " KiB"
     case 1024 ..< (1024 * 1024 * 1024):
-        return String(format: "%.2f", Double(bytes) / (1024 * 1024)) + " MiB"
+      return String(format: "%.2f", Double(self) / (1024 * 1024)) + " MiB"
     case 1024 ..< (1024 * 1024 * 1024 * 1024):
-        return String(format: "%.2f", Double(bytes) / (1024 * 1024 * 1024)) + " GiB"
+      return String(format: "%.2f", Double(self) / (1024 * 1024 * 1024)) + " GiB"
     default:
-        return String(format: "%.2f", Double(bytes) / (1024 * 1024 * 1024 * 1024)) + " TiB"
+      return String(format: "%.2f", Double(self) / (1024 * 1024 * 1024 * 1024)) + " TiB"
     }
+  }
 }
 
-private func prettyTimeAgo(timestamp: Date) -> String {
+private extension Date {
+  func prettyTimeAgo() -> String {
     let now = Date()
-    let timeInterval = Int64(now.timeIntervalSince(timestamp))
+    let timeInterval = Int64(now.timeIntervalSince(self))
     switch timeInterval {
     case ..<0: return tr("tunnelHandshakeTimestampSystemClockBackward")
     case 0: return tr("tunnelHandshakeTimestampNow")
     default:
         return tr(format: "tunnelHandshakeTimestampAgo (%@)", prettyTime(secondsLeft: timeInterval))
     }
+  }
 }
 
 private func prettyTime(secondsLeft: Int64) -> String {
-- 
2.35.1



More information about the WireGuard mailing list