aboutsummaryrefslogtreecommitdiffstats
path: root/standalone-tools/installer.sh
blob: 84dacdc15fb1614c558073a115ae8bcf9c60e37e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/sbin/sh
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (C) 2015-2017 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.

SCRIPT="$1"
OUTFD="$2"
ZIP="$3"
ARCH="$(getprop ro.product.cpu.abi)"

print() {
	echo "ui_print [+] $*" >&$OUTFD
}

die() {
	echo "ui_print [-] $*" >&$OUTFD
	exit 1
}

cleanup() {
	mount -o ro,remount /system
	rm -rf /tmp/wireguard
}

mount_system() {
	local slot dev

	if grep -q /system /proc/mounts; then
		print "Remounting system partition r/w"
		mount -o rw,remount /system
	else
		print "Mounting system partition"

		slot="$(getprop ro.boot.slot_suffix)"
		[ -z "$slot" ] && slot="$(getprop ro.boot.slot)"

		dev="$(find /dev/block -iname "system$slot" -print | head -n 1)"
		[ -n "$dev" ] || die "Could not find system partition"

		mount -o rw "$dev" /system || die "Could not mount system partition"
	fi
}

echo "ui_print ==================================" >&$OUTFD
echo "ui_print =          WireGuard Tools       =" >&$OUTFD
echo "ui_print =             by zx2c4           =" >&$OUTFD
echo "ui_print =         www.wireguard.com      =" >&$OUTFD
echo "ui_print ==================================" >&$OUTFD

trap cleanup INT TERM EXIT

mount_system

rm -rf /tmp/wireguard
mkdir -p /tmp/wireguard
print "Extracting files"
unzip -d /tmp/wireguard "$ZIP"
[ -d /tmp/wireguard/arch/$ARCH ] || die "Not available for device's ABI"
print "Installing WireGuard tools"
cp /tmp/wireguard/arch/$(getprop ro.product.cpu.abi)/* /system/xbin/
cp /tmp/wireguard/addon.d/40-wireguard.sh /system/addon.d/
chmod 755 /system/xbin/wg /system/xbin/wg-quick /system/addon.d/40-wireguard.sh

mkdir -pm 700 /data/misc/wireguard
print "Success! Be sure your kernel has the WireGuard module enabled."