Repository navigation
Refactor tunnel startup logic similar to WireGuard #2467
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e6e81dc
Refactor tunnel startup logic similar to WireGuard
usa- 309679f
Treat short-lived vtund sessions as failed starts
usa- 0e5e2fd
Merge branch 'master' into improve-tunnel
usa- d693007
Use monotonic time and increase VTun startup threshold
usa- 9c36a8c
Align startup log messages
usa- 43c1cf8
Align startup log messages
usa- File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,103 +1,99 @@ | ||
| #!/bin/sh | ||
|
|
||
| vtund_server=${1:-vtun.localhost} | ||
| vtund_port="5000" | ||
| server=${1:-vtun.localhost} | ||
| port="5000" | ||
| vtund_iface="tunnel" | ||
| device_name="IPC-VTUND" | ||
| working_dir="/tmp" | ||
| identity_cfg="$working_dir/vtund.conf" | ||
| udhcpc_pid="$working_dir/udhcpc-$vtund_iface.pid" | ||
| config_path="$working_dir/vtund.conf" | ||
| udhcpc_pid_path="$working_dir/udhcpc-$vtund_iface.pid" | ||
| delay_start=0 | ||
| delay_step=10 | ||
| delay_max=300 | ||
|
|
||
| . /usr/sbin/common | ||
|
|
||
| ready() { | ||
| identity_src=$(ip r | awk '/default/ {print $5}' | head -n 1) | ||
| start() { | ||
| read_subshell_pid | ||
| prog="${0##*/}[$subshell_pid]" | ||
| default_iface=$(ip r | awk '/default/ {print $5}' | head -n 1) | ||
|
|
||
| [ -n "$identity_src" ] || { | ||
| [ -n "$default_iface" ] || { | ||
| log_warning "Unable to determine the default network interface" | ||
| return 1 | ||
| } | ||
|
|
||
| identity_mac=$(cat "/sys/class/net/$identity_src/address" 2>/dev/null | tr 'a-z' 'A-Z') | ||
| default_mac=$(cat "/sys/class/net/$default_iface/address" 2>/dev/null | tr 'a-z' 'A-Z') | ||
|
|
||
| [ -n "$identity_mac" ] || { | ||
| [ -n "$default_mac" ] || { | ||
| log_warning "Unable to read the MAC address of the default network interface" | ||
| return 1 | ||
| } | ||
|
|
||
| echo "$vtund_server" | grep -qE '^((^|\.)(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){4}$' && return 0 | ||
|
|
||
| nslookup "$vtund_server" >/dev/null 2>&1 || { | ||
| log_warning "Unable to resolve the VTun server address" | ||
| return 1 | ||
| } | ||
| echo "$server" | grep -qE '^((^|\.)(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){4}$' || | ||
| nslookup -timeout=3 -retry=1 "$server" >/dev/null 2>&1 || { | ||
| log_warning "Unable to resolve the VTun server hostname" | ||
| return 1 | ||
| } | ||
|
|
||
| if [ ! -L "/sys/class/net/$vtund_iface" ]; then | ||
| run_cmd "" "" false daemon.err modprobe tun || return 1 | ||
| run_cmd "" "" false daemon.err tunctl -t "$vtund_iface" || return 1 | ||
| fi | ||
|
|
||
| kill_pid_file -9 "$udhcpc_pid_path" udhcpc | ||
|
|
||
| password=$(echo "$default_mac" | sha1sum | awk '{print $1}') | ||
| profile=$(echo "$default_mac" | tr -d ':') | ||
|
|
||
| { | ||
| echo "options {" | ||
| echo " port $port;" | ||
| echo " ifconfig /sbin/ifconfig;" | ||
| echo "}" | ||
| echo "$profile {" | ||
| echo " password $password;" | ||
| echo " device $vtund_iface;" | ||
| echo " stat no;" | ||
| echo " persist yes;" | ||
| echo " keepalive 10:5;" | ||
| echo " timeout 10;" | ||
| echo " up {" | ||
| echo " ifconfig \"$vtund_iface hw ether $default_mac mtu 1500 -multicast up\";" | ||
| echo " program \"udhcpc -T 1 -t 5 -R -b -O staticroutes -S -s tapip -p '$udhcpc_pid_path' -i $vtund_iface -x hostname:$device_name-$profile\";" | ||
| echo " };" | ||
| echo " down {" | ||
| echo " program \"kill_pid_file -9 '$udhcpc_pid_path' udhcpc\";" | ||
| echo " ifconfig \"$vtund_iface down\";" | ||
| echo " };" | ||
| echo "}" | ||
| } >"$config_path" | ||
|
|
||
| # Workaround for VTun bug described in https://bugzilla.redhat.com/show_bug.cgi?id=1462458#c26 | ||
| run_cmd "" "" false daemon.err ifconfig $vtund_iface hw ether $default_mac mtu 1500 -multicast up || return 1 | ||
|
|
||
| log_info "Starting VTun connection to $server" | ||
|
|
||
| start_time=$(cat /proc/uptime | cut -d " " -f 1 | tr -d .) | ||
| vtund -n -f "$config_path" "$profile" "$server" >/dev/null 2>&1 | ||
| end_time=$(cat /proc/uptime | cut -d " " -f 1 | tr -d .) | ||
|
|
||
| # Treat a session that ends quickly as a failed attempt regardless of status | ||
| [ $(( end_time - start_time )) -ge 1000 ] | ||
| } | ||
|
|
||
| { | ||
| read_subshell_pid | ||
| prog="${0##*/}[$subshell_pid]" | ||
| restart_delay=$delay_start | ||
|
|
||
| while true; do | ||
| while true; do | ||
| if ! ready; then | ||
| ready_delay=$delay_start | ||
|
|
||
| while true; do | ||
| [ $ready_delay -lt $delay_max ] && ready_delay=$(( ready_delay + delay_step )) | ||
| sleep $ready_delay | ||
| ready && break | ||
| done | ||
|
|
||
| log_info "Starting the tunnel to $vtund_server" | ||
| fi | ||
|
|
||
| if [ ! -L "/sys/class/net/$vtund_iface" ]; then | ||
| run_cmd "" "" false daemon.err modprobe tun || break | ||
| run_cmd "" "" false daemon.err tunctl -t "$vtund_iface" || break | ||
| fi | ||
|
|
||
| kill_pid_file -9 "$udhcpc_pid" udhcpc | ||
|
|
||
| identity_pas=$(echo "$identity_mac" | sha1sum | awk '{print $1}') | ||
| identity_tid=$(echo "$identity_mac" | tr -d ':') | ||
|
|
||
| { | ||
| echo "options {" | ||
| echo " port $vtund_port;" | ||
| echo " ifconfig /sbin/ifconfig;" | ||
| echo "}" | ||
| echo "$identity_tid {" | ||
| echo " password $identity_pas;" | ||
| echo " device $vtund_iface;" | ||
| echo " stat no;" | ||
| echo " persist yes;" | ||
| echo " keepalive 10:5;" | ||
| echo " timeout 10;" | ||
| echo " up {" | ||
| echo " ifconfig \"$vtund_iface hw ether $identity_mac mtu 1500 -multicast up\";" | ||
| echo " program \"udhcpc -T 1 -t 5 -R -b -O staticroutes -S -s tapip -p '$udhcpc_pid' -i $vtund_iface -x hostname:$device_name-$identity_tid\";" | ||
| echo " };" | ||
| echo " down {" | ||
| echo " program \"kill_pid_file -9 '$udhcpc_pid' udhcpc\";" | ||
| echo " ifconfig \"$vtund_iface down\";" | ||
| echo " };" | ||
| echo "}" | ||
| } >"$identity_cfg" | ||
|
|
||
| # workaround for VTun bug described in https://bugzilla.redhat.com/show_bug.cgi?id=1462458#c26 | ||
| run_cmd "" "" false daemon.err ifconfig $vtund_iface hw ether $identity_mac mtu 1500 -multicast up || break | ||
|
|
||
| vtund -n -f "$identity_cfg" "$identity_tid" "$vtund_server" >/dev/null 2>&1 && | ||
| restart_delay=$delay_start | ||
|
|
||
| break | ||
| done | ||
|
|
||
| [ $restart_delay -lt $delay_max ] && restart_delay=$(( restart_delay + delay_step )) | ||
| sleep $restart_delay | ||
| if ! start; then | ||
| delay=$delay_start | ||
|
|
||
| while true; do | ||
| [ $delay -lt $delay_max ] && delay=$(( delay + delay_step )) | ||
| sleep $delay | ||
| start && break | ||
|
openipc-ai marked this conversation as resolved.
|
||
| done | ||
| fi | ||
|
|
||
| sleep 10 | ||
| done | ||
| } & | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.