passepartoutvpn/tunnelkit

How to get traffic (rxBytes, txBytes) in WireGuard

dovanvu1792 opened this issue · 10 comments

I would like to get total traffic in Wireguard session, can you share show to get it?
Thank you,

AFAIK, the information is not accessible from WireGuardKit.

I have just found the way to get it.

func getTunelData(tunnelProviderSession: NETunnelProviderSession) async {
        
            do {
                try tunnelProviderSession.sendProviderMessage(Data([ UInt8(0) ])) { responseHandler in
                    guard let data = responseHandler else { return }
                    let settings = String(data: data, encoding: .utf8)
                    
                    print(settings)
                    /*
                     _`private_key=xxx\nlisten_port=62704\npublic_key=xxx\npreshared_key=xxx\nprotocol_version=1\nendpoint=40.95.12.111:123\nlast_handshake_time_sec=1683739771\nlast_handshake_time_nsec=793274000\ntx_bytes=42052\nrx_bytes=90940\npersistent_keepalive_interval=0\nallowed_ip=0.0.0.0/0\n`_
                     */
                }
            } catch {
                print(error)
            }
        
    }
zooxop commented

@keeshux

Are there any plans to modify TunnelKit to support this feature?

I recently encountered a similar issue and had to customize TunnelKit in order to implement this feature in my project.

I am curious if there are any plans for TunnelKit to officially support this feature in the future. If not, I will have to continue relying on my customized version of TunnelKit.

@keeshux

Are there any plans to modify TunnelKit to support this feature?

I recently encountered a similar issue and had to customize TunnelKit in order to implement this feature in my project.

I am curious if there are any plans for TunnelKit to officially support this feature in the future. If not, I will have to continue relying on my customized version of TunnelKit.

Are you also using .sendProviderMessage?

zooxop commented

@keeshux

Are you also using .sendProviderMessage?

Yes. I use it to call handleAppMessage(_:completionHandler:) in NETunnelProvider.

I ask because I'm using an asynchronous approach for other tunnel states, i.e. the tunnel periodically saves state snapshots to a UserDefaults object shared with the app. That's why the feature is doable, but a bit less trivial.

guard isCountingData, let session = session, let dataCount = session.dataCount() else {

zooxop commented

Oh, I apologize. I wasn't aware of the implementation approach of the OpenVPNTunnelProvider code you mentioned.
The WireGuardTunnelProvider doesn't have those functionalities implemented, so I just resorted to directly executing handleAppMessage() in my app as a temporary workaround.

If it's possible to enable similar functionality in WireGuardTunnelProvider through UserDefaults access, it would be more consistent and preferable.