passepartoutvpn / tunnelkit

VPN client library for Apple platforms.
GNU General Public License v3.0
9 stars 8 forks source link

Howto access dataCount for wireguard? #417

Open MattF42 opened 7 months ago

MattF42 commented 7 months ago

Summary

Howto access dataCount for wireguard connections?

Steps to reproduce

import TunnelKitManager
import TunnelKitWireGuard
import TunnelKitWireGuardAppExtension
#if os(iOS) || os(watchOS) || os(tvOS)
import SVProgressHUD
#endif
import Combine

struct WireGuardView: View {
    let vpn: NetworkExtensionVPN

    var body: some View {       
 [SNIP]
}

private extension WireGuardView {
    var formView: some View {
   [SNIP]
}
var buttonView: some View {
[SNIP]
}

func connect(sub: String, usa: Int) {
        if(self.vpnStatus == .disconnected) {
            if let index = subscrArr.firstIndex(of: sub) {
                let parsed = sub.replacingOccurrences(of: ".conf", with: "")
                var title = "MyVPN " + parsed
                var serverArr = endPointArr[index].components(separatedBy: ":")

                guard let cfg = WireGuard.DemoConfiguration.make(params: .init(
                    title: title,
                    appGroup: appGroup,
                    clientPrivateKey: privKeyArr[index],
                    clientAddress: addressArr[index],
                    serverPublicKey: pubKeyArr[index],
                    serverAddress: serverArr[0],
                    serverPort: serverArr[1]
                )) else {
                    print("Configuration incomplete")
                    return
                }

                Task {
                    try await vpn.reconnect(
                        TunnelIdentifier.wireGuard,
                        configuration: cfg,
                        extra: nil,
                        after: .seconds(2)
                    )
                }
                executeRepeatedly()
            } else {
                print("Element not found")
            }
        } else {
            Task {
                await vpn.disconnect()
            }
        }

    }

    private func executeRepeatedly() {
        DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) { [weak self] in
            print(self?.cfg.dataCount ?? "error1")  // **Value of type 'WireGuardView' has no member 'cfg'**
            // self.TunnelKitProvider.Configuration.dataCount(in:)
            // print(self?.Configuration.DataCount ?? "error1")
            self?.executeRepeatedly()
        }
    }

    func disconnect(counter: Int) {
       //  statusString = "Disconnected"
        Task {
            await vpn.disconnect()
        }
    }
}

What is the current bug behavior?

print(self?.cfg.dataCount ?? "error1") // <-- Value of type 'WireGuardView' has no member 'cfg'

The VPN connects successfully. appGroup is correctly updated to reflect my fork of the Demo codebase. It's not a bug, but I can not seem to work out the correct way to call dataCount for the wireguard connection :(

What is the expected correct behavior?

Please help providing the correct method to access dataCount for wireguard connection

Relevant logs and/or screenshots

N/A

Possible fixes suggested remediation

Documented sample code

MattF42 commented 7 months ago

So I've worked out that we need to extend WireGuardTunnelProvider:

import Foundation

class PacketTunnelProvider: WireGuardTunnelProvider {
    override func startTunnel(options: [String : NSObject]? = nil) async throws {
        print("In startTunnel")
        dataCountInterval = 3000
            try await super.startTunnel(options: options)
        }
}

However startTunnel never seems to get called.

Any hints on howto record and access the wireguard tunnel stats would be much appreciated

NasrullahKhan commented 6 months ago

hey did you manage to do it?