Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wgpeerselector: expect actual traffic flow #253

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions net/wgpeerselector/files/usr/bin/wgpeerselector
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ function WGPeer:new(o)
-- some defaults
o.rx_bytes = 0
o.tx_bytes = 0
o.prev_rx_bytes = 0
o.prev_tx_bytes = 0
o.latest_handshake = 0
o.established_at = 0
-- terminology:
Expand Down Expand Up @@ -175,10 +177,17 @@ function WGPeer:established_time()
return (time.time() - self.established_at)
end

function WGPeer:has_recent_handshake()
function WGPeer:has_recent_success()
-- WireGuard handshakes are sent at least every 2 minutes, if there is
-- payload traffic.
return (time.time() - self.latest_handshake) < 150
if 150 < (time.time() - self.latest_handshake) then return false end
-- Check if actually traffic was able to be received
if 0 == (self.rx_bytes - self.prev_rx_bytes) then return false end
self.prev_rx_bytes = self.rx_bytes
-- Check if actually traffic was able to be sent
if 0 == (self.tx_bytes - self.prev_tx_bytes) then return false end
self.prev_tx_bytes = self.tx_bytes
return true
end

local WGPeerSelector = {}
Expand Down Expand Up @@ -236,7 +245,7 @@ function WGPeerSelector:try_connect_to_peer(peer, timeout)
sleep(timeout)
peer:update_stats_from_kernel()

local connection_successful = peer:has_recent_handshake()
local connection_successful = peer:has_recent_success()

if not connection_successful then
peer:uninstall_from_kernel()
Expand Down