heiher / hev-socks5-tunnel

A high-performance tun2socks for Linux/Android/FreeBSD/macOS/iOS/WSL2 (IPv4/IPv6/TCP/UDP)
MIT License
636 stars 130 forks source link

Improvements for existing traffic feature #96

Closed evgenybozhko2 closed 4 months ago

evgenybozhko2 commented 4 months ago

Hello dear. Many thanks, for collecting the traffic stats feature. Now I was faced with the issue on exactly Xiaomi Android devices. After restarting the tunnel, the older stats were returned for the first time. So I suggest adding a clean stats feature into tunnel stop.

`void hev_socks5_tunnel_stop (void) { int val;

LOG_D ("socks5 tunnel stop");

if (event_fds[1] == -1)
    return;

val = write (event_fds[1], &val, sizeof (val));
if (val < 0)
    LOG_E ("socks5 tunnel write event");

stat_tx_packets = 0;
stat_rx_packets = 0;
stat_tx_bytes = 0;
stat_rx_bytes = 0;

}`

From hev-socks5-tunnel.c implementation. Maybe someone will faced with this in the future. Thanks

heiher commented 4 months ago

Why doesn't this work?

https://github.com/heiher/hev-socks5-tunnel/blob/master/src/hev-socks5-tunnel.c#L550-L553

Restart failed?

evgenybozhko2 commented 4 months ago

Why doesn't this work?

https://github.com/heiher/hev-socks5-tunnel/blob/master/src/hev-socks5-tunnel.c#L550-L553

Restart failed?

Not sure exactly. But restart success, and after the second and next iteration then coming new stats. It looks like hev_socks5_tunnel_init called after getting the successful connection. And important note, it happens only on Xiaomi devices.

heiher commented 4 months ago

To clarify, hev_socks5_tunnel_stats is not precise, as there is no strict synchronization in place. The startup of hev-socks5-tunnel is also asynchronous. Without strict synchronization, it is impossible to determine whether it has been started, as it is cleared to 0 only when the process is completed. This also does not account for corner cases caused by weak memory ordering.

I will move the zeroing process to when the service is stopped, as this involves explicit synchronization.

evgenybozhko2 commented 4 months ago

To clarify, hev_socks5_tunnel_stats is not precise, as there is no strict synchronization in place. The startup of hev-socks5-tunnel is also asynchronous. Without strict synchronization, it is impossible to determine whether it has been started, as it is cleared to 0 only when the process is completed. This also does not account for corner cases caused by weak memory ordering.

I will move the zeroing process to when the service is stopped, as this involves explicit synchronization.

Thanks a lot.

heiher commented 4 months ago

https://github.com/heiher/hev-socks5-tunnel/commit/09ff9219525c7bf9217f4a3380e83dede17a04b2