Update wireguard.sh

1, Added update WireGuard from source;
2, Check result when install or update WireGuard.

Signed-off-by: Teddysun <i@teddysun.com>
This commit is contained in:
Teddysun 2019-09-24 21:36:18 +09:00
parent c90af7d498
commit 3e27b32449
No known key found for this signature in database
GPG Key ID: 09BD4C080AD6C46D

View File

@ -125,6 +125,31 @@ _error_detect() {
fi fi
} }
_version_gt(){
test "$(echo "$@" | tr " " "\n" | sort -V | head -n 1)" != "$1"
}
_is_installed() {
if _exists "wg" && _exists "wg-quick"; then
if [ -s "/lib/modules/$(uname -r)/extra/wireguard.ko" ] || [ -s "/lib/modules/$(uname -r)/extra/wireguard.ko.xz" ] \
|| [ -s "/lib/modules/$(uname -r)/updates/dkms/wireguard.ko" ]; then
return 0
else
return 1
fi
else
return 2
fi
}
_get_latest_ver() {
wireguard_ver="$(wget --no-check-certificate -qO- https://api.github.com/repos/WireGuard/WireGuard/tags | grep 'name' | head -1 | cut -d\" -f4)"
if [ -z "${wireguard_ver}" ]; then
wireguard_ver="$(curl -Lso- https://api.github.com/repos/WireGuard/WireGuard/tags | grep 'name' | head -1 | cut -d\" -f4)"
fi
[ -z "${wireguard_ver}" ] && _error "Failed to get wireguard latest version from github"
}
# Check OS version # Check OS version
check_os() { check_os() {
_info "Check OS version" _info "Check OS version"
@ -197,18 +222,14 @@ install_wg_1() {
*) *)
;; # do nothing ;; # do nothing
esac esac
if ! _is_installed; then
_error "Failed to install wireguard, the kernel is most likely not configured correctly"
fi
} }
# Install from source # Install from source
install_wg_2() { install_wg_2() {
_info "Install wireguard from source" _info "Install wireguard from source"
wireguard_ver="$(wget --no-check-certificate -qO- https://api.github.com/repos/WireGuard/WireGuard/tags | grep 'name' | head -1 | cut -d\" -f4)"
if [ -z "${wireguard_ver}" ]; then
wireguard_ver="$(curl -Lso- https://api.github.com/repos/WireGuard/WireGuard/tags | grep 'name' | head -1 | cut -d\" -f4)"
fi
[ -z "${wireguard_ver}" ] && _error "Failed to get wireguard latest version from github."
wireguard_name="WireGuard-${wireguard_ver}"
wireguard_url="https://github.com/WireGuard/WireGuard/archive/${wireguard_ver}.tar.gz"
case "$(_os)" in case "$(_os)" in
ubuntu|debian) ubuntu|debian)
_error_detect "apt-get update" _error_detect "apt-get update"
@ -240,6 +261,9 @@ install_wg_2() {
*) *)
;; # do nothing ;; # do nothing
esac esac
_get_latest_ver
wireguard_name="WireGuard-${wireguard_ver}"
wireguard_url="https://github.com/WireGuard/WireGuard/archive/${wireguard_ver}.tar.gz"
_error_detect "wget --no-check-certificate -qO ${wireguard_name}.tar.gz ${wireguard_url}" _error_detect "wget --no-check-certificate -qO ${wireguard_name}.tar.gz ${wireguard_url}"
_error_detect "tar zxf ${wireguard_name}.tar.gz" _error_detect "tar zxf ${wireguard_name}.tar.gz"
_error_detect "cd ${wireguard_name}/src" _error_detect "cd ${wireguard_name}/src"
@ -247,6 +271,9 @@ install_wg_2() {
_error_detect "make module" _error_detect "make module"
_error_detect "make install" _error_detect "make install"
_error_detect "cd ${cur_dir} && rm -fr ${wireguard_name}.tar.gz ${wireguard_name}" _error_detect "cd ${cur_dir} && rm -fr ${wireguard_name}.tar.gz ${wireguard_name}"
if ! _is_installed; then
_error "Failed to install wireguard, the kernel is most likely not configured correctly"
fi
} }
# Create server interface # Create server interface
@ -550,17 +577,16 @@ list_clients() {
printf ${line} printf ${line}
} }
version() { check_version() {
if _exists "wg" && _exists "wg-quick"; then _is_installed
if [ -s "/lib/modules/$(uname -r)/extra/wireguard.ko" ] || [ -s "/lib/modules/$(uname -r)/extra/wireguard.ko.xz" ] \ rt=$?
|| [ -s "/lib/modules/$(uname -r)/updates/dkms/" ]; then if [ ${rt} -eq 0 ]; then
_exists "modinfo" && installed_wg_ver="$(modinfo -F version wireguard)" _exists "modinfo" && installed_wg_ver="$(modinfo -F version wireguard)"
[ -n "${installed_wg_ver}" ] && echo "WireGuard version: $(_green ${installed_wg_ver})" [ -n "${installed_wg_ver}" ] && echo "WireGuard version: $(_green ${installed_wg_ver})" && return 0
else elif [ ${rt} -eq 1 ]; then
_red "WireGuard kernel module does not exists\n" _red "WireGuard kernel module does not exists\n" && return 1
fi elif [ ${rt} -eq 2 ]; then
else _red "WireGuard is not installed\n" && return 2
_red "WireGuard is not installed\n"
fi fi
} }
@ -571,6 +597,7 @@ Options:
-h, --help Print this help text and exit -h, --help Print this help text and exit
-r, --repo Install WireGuard from repository -r, --repo Install WireGuard from repository
-s, --source Install WireGuard from source -s, --source Install WireGuard from source
-u, --update Upgrade WireGuard from source
-v, --version Print WireGuard version if installed -v, --version Print WireGuard version if installed
-a, --add Add a WireGuard client -a, --add Add a WireGuard client
-d, --del Delete a WireGuard client -d, --del Delete a WireGuard client
@ -601,6 +628,27 @@ install_from_source() {
install_completed install_completed
} }
update_from_source() {
if check_version; then
_get_latest_ver
echo "WireGuard latest version: $(_green ${wireguard_ver})"
if _version_gt "${wireguard_ver}" "${installed_wg_ver}"; then
echo "Do you want to upgrade WireGuard? (y/n)"
read -p "(Default: n):" update_wg
[ -z "${update_wg}" ] && update_wg="n"
if [ "${update_wg}" = "y" -o "${update_wg}" = "Y" ]; then
install_wg_2
systemctl restart wg-quick@${SERVER_WG_NIC}
echo "Update WireGuard completed"
else
echo "Update WireGuard canceled"
fi
else
echo "No updates needed to update WireGuard"
fi
fi
}
cur_dir="$(pwd)" cur_dir="$(pwd)"
[ ${EUID} -ne 0 ] && _error "This script must be run as root" [ ${EUID} -ne 0 ] && _error "This script must be run as root"
@ -630,8 +678,11 @@ main() {
-s|--source) -s|--source)
install_from_source install_from_source
;; ;;
-u|--update)
update_from_source
;;
-v|--version) -v|--version)
version check_version
;; ;;
-a|--add) -a|--add)
add_client add_client