passepartoutvpn / tunnelkit

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

How to get traffic (rxBytes, txBytes) in WireGuard #323

Closed dovanvu1792 closed 11 months ago

dovanvu1792 commented 1 year ago

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

keeshux commented 1 year ago

AFAIK, the information is not accessible from WireGuardKit.

dovanvu1792 commented 1 year ago

Ref: https://github.com/WireGuard/wireguard-apple/blob/master/Sources/WireGuardApp/Tunnel/TunnelConfiguration%2BUapiConfig.swift

dovanvu1792 commented 1 year ago

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 1 year ago

@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 commented 1 year ago

@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 1 year ago

@keeshux

Are you also using .sendProviderMessage?

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

keeshux commented 1 year ago

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.

keeshux commented 1 year ago

https://github.com/passepartoutvpn/tunnelkit/blob/d8563e7f15f03fcd5ed60df4e1ff8bdda2097761/Sources/TunnelKitOpenVPNAppExtension/OpenVPNTunnelProvider.swift#L387

zooxop commented 1 year ago

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.

JohnYezub commented 11 months ago

I've opened the PR with RX/TX feature which has similar to OpenVpn implementation, please take a look!