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

Howto access dataCount for wireguard? #417

Open
MattF42 opened this issue Apr 8, 2024 · 2 comments
Open

Howto access dataCount for wireguard? #417

MattF42 opened this issue Apr 8, 2024 · 2 comments

Comments

@MattF42
Copy link

MattF42 commented Apr 8, 2024

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
Copy link
Author

MattF42 commented Apr 13, 2024

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

This comment was marked as abuse.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants