Skip to content
This repository has been archived by the owner on Sep 29, 2024. It is now read-only.

Add WireGuard RX/TX data statistics #341

Merged
merged 12 commits into from
Dec 14, 2023
Prev Previous commit
Next Next commit
tunnelKitOriginal: replace UInt64 in WireGuardDataCount
minor file consolidation
  • Loading branch information
EugeneYezub committed Nov 17, 2023
commit 35e322cc458c33872baa78bb0ca7a47155b36150
8 changes: 0 additions & 8 deletions Sources/TunnelKitWireGuardAppExtension/WgStats.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ import Foundation
import NetworkExtension
import os


extension Notification.Name {

}

open class WireGuardTunnelProvider: NEPacketTunnelProvider {
private var cfg: WireGuard.ProviderConfiguration!

Expand Down Expand Up @@ -224,36 +219,3 @@ extension WireGuardLogLevel {
}
}
}

private extension WireGuardDataCount {
init?(from string: String) {
var bytesReceived: UInt64?
var bytesSent: UInt64?

string.enumerateLines { line, stop in
if bytesReceived == nil, let value = parseValue("rx_bytes=", in: line) {
bytesReceived = value
} else if bytesSent == nil, let value = parseValue("tx_bytes=", in: line) {
bytesSent = value
}

if bytesReceived != nil, bytesSent != nil {
stop = true
}
}

guard let bytesReceived, let bytesSent else {
return nil
}

self.init(bytesReceived, bytesSent)
}
}

@inline(__always) private func parseValue(_ prefixKey: String, in line: String) -> UInt64? {
guard line.hasPrefix(prefixKey) else { return nil }

let value = line.dropFirst(prefixKey.count)

return UInt64(value)
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ extension UserDefaults {
}
}

@objc private var wireGuardDataCountArray: [UInt64]? {
@objc private var wireGuardDataCountArray: [UInt]? {
get {
return array(forKey: WireGuard.ProviderConfiguration.Keys.dataCount.rawValue) as? [UInt64]
return array(forKey: WireGuard.ProviderConfiguration.Keys.dataCount.rawValue) as? [UInt]
}
set {
set(newValue, forKey: WireGuard.ProviderConfiguration.Keys.dataCount.rawValue)
Expand All @@ -193,18 +193,3 @@ extension UserDefaults {
removeObject(forKey: WireGuard.ProviderConfiguration.Keys.dataCount.rawValue)
}
}

/// A pair of received/sent bytes count.
public struct WireGuardDataCount: Equatable {

/// Received bytes count.
public var bytesReceived: UInt64

/// Sent bytes count.
public var bytesSent: UInt64

public init(_ received: UInt64, _ sent: UInt64) {
self.bytesReceived = received
self.bytesSent = sent
}
}
55 changes: 55 additions & 0 deletions Sources/TunnelKitWireGuardManager/WireGuardDataCount.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// WireGuardDataCount.swift
//
//
// Created by Yevgeny Yezub on 17/11/23.
//
keeshux marked this conversation as resolved.
Show resolved Hide resolved

import Foundation
/// A pair of received/sent bytes count.
public struct WireGuardDataCount: Equatable {

/// Received bytes count.
public var bytesReceived: UInt
keeshux marked this conversation as resolved.
Show resolved Hide resolved

/// Sent bytes count.
public var bytesSent: UInt
keeshux marked this conversation as resolved.
Show resolved Hide resolved

public init(_ received: UInt, _ sent: UInt) {
self.bytesReceived = received
self.bytesSent = sent
}
}

extension WireGuardDataCount {
public init?(from string: String) {
var bytesReceived: UInt?
var bytesSent: UInt?

string.enumerateLines { line, stop in
if bytesReceived == nil, let value = parseValue("rx_bytes=", in: line) {
bytesReceived = value
} else if bytesSent == nil, let value = parseValue("tx_bytes=", in: line) {
bytesSent = value
}

if bytesReceived != nil, bytesSent != nil {
stop = true
}
}

guard let bytesReceived, let bytesSent else {
return nil
}

self.init(bytesReceived, bytesSent)
}
}

private func parseValue(_ prefixKey: String, in line: String) -> UInt? {
keeshux marked this conversation as resolved.
Show resolved Hide resolved
guard line.hasPrefix(prefixKey) else { return nil }
keeshux marked this conversation as resolved.
Show resolved Hide resolved

let value = line.dropFirst(prefixKey.count)

return UInt(value)
}