Fix issue of gateway interface detection in the case of VPN usage #122
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The gateway interface is detected with the following code :
new_gateway_interface=$(ip route get 1.2.3.4 | awk '{ print $5; }')
This code works as expected in some cases because
ip route get 1.2.3.4
returns a line where the interface name is in the 5th field, like in this first example :But in some other cases, for example when a wireguard VPN is used to make the server reachable from the internet, the network interface name is in the 3rd field, like in this second example :
(note that in the above example the actual public IP address of the VPN was replaced by
5.6.7.8
to anonymize it)Solution
replace the gateway interface detection code with the following :
new_gateway_interface=$(ip route get 1.2.3.4 | awk '$2 ~ /^dev$/ { print $3; } $4 ~ /^dev$/ { print $5; }')
I have tested that this is working in both cases described above. But I'm no awk expert. There might be a better way to do this that would find the interface name in any other position in the result of
ip route get 1.2.3.4
PR Status
Automatic tests
Automatic tests can be triggered on https://ci-apps-dev.yunohost.org/ after creating the PR, by commenting "!testme", "!gogogadgetoci" or "By the power of systemd, I invoke The Great App CI to test this Pull Request!". (N.B. : for this to work you need to be a member of the Yunohost-Apps organization)