cli: move to android_kernel_wireguard
These tools are now part of the ROM builder's toolkit at: https://git.zx2c4.com/android_kernel_wireguard/about/ Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
bdfb319854
commit
69d4fe9a81
@ -1,21 +0,0 @@
|
||||
# Tools for Android
|
||||
|
||||
This currently contains a version of wg-quick.bash that works with
|
||||
Android 7's `ndc` command. It requires the WireGuard module to be
|
||||
part of your kernel, but after that, the usual `wg-quick up` and
|
||||
`wg-quick down` commands work normally.
|
||||
|
||||
## Installation
|
||||
|
||||
Build a `wg` binary for Android and place it in this folder. Then
|
||||
copy this folder some place on your phone, and run `sh ./install.sh`
|
||||
as root. It should survive ROM flashes.
|
||||
|
||||
## Usage
|
||||
|
||||
Compared to the ordinary wg-quick, this one does not support SaveConfig
|
||||
and {Pre,Post}{Up,Down}.
|
||||
|
||||
Put your configuration files into `/data/misc/wireguard/`. After that,
|
||||
the normal `wg-quick up|down` commands will work.
|
||||
|
@ -1,37 +0,0 @@
|
||||
#!/sbin/sh
|
||||
|
||||
. /tmp/backuptool.functions
|
||||
|
||||
list_files() {
|
||||
cat <<_EOF
|
||||
xbin/wg
|
||||
xbin/wg-quick
|
||||
_EOF
|
||||
}
|
||||
|
||||
case "$1" in
|
||||
backup)
|
||||
list_files | while read FILE DUMMY; do
|
||||
backup_file $S/"$FILE"
|
||||
done
|
||||
;;
|
||||
restore)
|
||||
list_files | while read FILE REPLACEMENT; do
|
||||
R=""
|
||||
[ -n "$REPLACEMENT" ] && R="$S/$REPLACEMENT"
|
||||
[ -f "$C/$S/$FILE" ] && restore_file $S/"$FILE" "$R"
|
||||
done
|
||||
;;
|
||||
pre-backup)
|
||||
# Stub
|
||||
;;
|
||||
post-backup)
|
||||
# Stub
|
||||
;;
|
||||
pre-restore)
|
||||
# Stub
|
||||
;;
|
||||
post-restore)
|
||||
# Stub
|
||||
;;
|
||||
esac
|
@ -1,14 +0,0 @@
|
||||
#!/bin/sh
|
||||
mount -o rw,remount /system
|
||||
|
||||
cp -v ./addonsd.sh /system/addon.d/40-wireguard.sh
|
||||
if [ -f ./wg ]; then
|
||||
cp -v ./wg /system/xbin/wg
|
||||
else
|
||||
echo "Warning: this directory does not contain wg. You may have forgotten to compile it yourself?" >&2
|
||||
fi
|
||||
cp -v ./wg-quick.bash /system/xbin/wg-quick
|
||||
chmod 755 /system/xbin/wg /system/xbin/wg-quick /system/addon.d/40-wireguard.sh
|
||||
mkdir -pvm 700 /data/misc/wireguard
|
||||
|
||||
mount -o ro,remount /system
|
@ -1,59 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
ARCH="arm64"
|
||||
ANDROID_PLATFORM="24"
|
||||
WIREGUARD_VERSION="0.0.20171111"
|
||||
LIBMNL_VERSION="1.0.4"
|
||||
|
||||
case "$ARCH" in
|
||||
arm) ANDROID_MACHINE="arm-linux-androideabi"; ;;
|
||||
arm64) ANDROID_MACHINE="aarch64-linux-android"; ;;
|
||||
mips) ANDROID_MACHINE="mipsel-linux-android"; ;;
|
||||
mips64) ANDROID_MACHINE="mips64el-linux-android"; ;;
|
||||
x86) ANDROID_MACHINE="x86-linux-android"; ;;
|
||||
x86_64) ANDROID_MACHINE="x86_64-linux-android"; ;;
|
||||
*) echo "Error: unknown architecture" >&2; exit 1; ;;
|
||||
esac
|
||||
|
||||
GCC_VERSION="4.9"
|
||||
ANDROID_PLATFORM="/opt/android-ndk/platforms/android-$ANDROID_PLATFORM/arch-$ARCH/usr"
|
||||
ANDROID_TOOLCHAIN="/opt/android-ndk/toolchains/$ANDROID_MACHINE-$GCC_VERSION/prebuilt/linux-$(uname -m)/bin"
|
||||
|
||||
export PATH="$ANDROID_TOOLCHAIN:$PATH"
|
||||
export CC="$ANDROID_MACHINE-gcc --sysroot $ANDROID_PLATFORM"
|
||||
export LD="$ANDROID_MACHINE-ld --sysroot $ANDROID_PLATFORM"
|
||||
export CFLAGS="-O3 -fomit-frame-pointer -I$ANDROID_PLATFORM/include -fPIE"
|
||||
export LDFLAGS="-pie"
|
||||
|
||||
trap 'cd /; rm -rf "$where"' EXIT
|
||||
where="$(mktemp -d)"
|
||||
here="$PWD"
|
||||
cd "$where"
|
||||
|
||||
wget "http://ftp.netfilter.org/pub/libmnl/libmnl-$LIBMNL_VERSION.tar.bz2"
|
||||
wget "https://git.zx2c4.com/WireGuard/snapshot/WireGuard-$WIREGUARD_VERSION.tar.xz"
|
||||
|
||||
tar xjf "libmnl-$LIBMNL_VERSION.tar.bz2"
|
||||
tar xJf "WireGuard-$WIREGUARD_VERSION.tar.xz"
|
||||
|
||||
cd "libmnl-$LIBMNL_VERSION"
|
||||
./configure --enable-static --disable-shared --host="$ANDROID_MACHINE"
|
||||
make -j$(nproc)
|
||||
|
||||
cd ..
|
||||
|
||||
cd "WireGuard-$WIREGUARD_VERSION/src/tools"
|
||||
export CFLAGS="$CFLAGS -I../../../libmnl-$LIBMNL_VERSION/include"
|
||||
export LDFLAGS="$LDFLAGS -L../../../libmnl-$LIBMNL_VERSION/src/.libs"
|
||||
make -j$(nproc)
|
||||
"$ANDROID_MACHINE-strip" wg
|
||||
mv wg "$here/wg"
|
||||
|
||||
echo
|
||||
echo
|
||||
echo ===============================================
|
||||
echo Build complete:
|
||||
ls -l "$here/wg"
|
||||
echo ===============================================
|
@ -1,187 +0,0 @@
|
||||
#!/system/xbin/bash
|
||||
#
|
||||
# Copyright (C) 2016-2017 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
||||
#
|
||||
|
||||
set -e -o pipefail
|
||||
shopt -s extglob
|
||||
export LC_ALL=C
|
||||
|
||||
SELF="$(readlink -f "${BASH_SOURCE[0]}")"
|
||||
export PATH="${SELF%/*}:$PATH"
|
||||
|
||||
WG_CONFIG=""
|
||||
INTERFACE=""
|
||||
NETID=0
|
||||
ADDRESSES=( )
|
||||
MTU=""
|
||||
DNS=( )
|
||||
CONFIG_FILE=""
|
||||
PROGRAM="${0##*/}"
|
||||
ARGS=( "$@" )
|
||||
|
||||
parse_options() {
|
||||
local interface_section=0 line key value
|
||||
CONFIG_FILE="$1"
|
||||
[[ $CONFIG_FILE =~ ^[a-zA-Z0-9_=+.-]{1,16}$ ]] && CONFIG_FILE="/data/misc/wireguard/$CONFIG_FILE.conf"
|
||||
[[ -e $CONFIG_FILE ]] || die "\`$CONFIG_FILE' does not exist"
|
||||
[[ $CONFIG_FILE =~ /?([a-zA-Z0-9_=+.-]{1,16})\.conf$ ]] || die "The config file must be a valid interface name, followed by .conf"
|
||||
((($(stat -c '%#a' "$CONFIG_FILE") & 0007) == 0)) || echo "Warning: \`$CONFIG_FILE' is world accessible" >&2
|
||||
INTERFACE="${BASH_REMATCH[1]}"
|
||||
shopt -s nocasematch
|
||||
while read -r line || [[ -n $line ]]; do
|
||||
key="${line%%=*}"; key="${key##*( )}"; key="${key%%*( )}"
|
||||
value="${line#*=}"; value="${value##*( )}"; value="${value%%*( )}"
|
||||
[[ $key == "["* ]] && interface_section=0
|
||||
[[ $key == "[Interface]" ]] && interface_section=1
|
||||
if [[ $interface_section -eq 1 ]]; then
|
||||
case "$key" in
|
||||
Address) ADDRESSES+=( ${value//,/ } ); continue ;;
|
||||
MTU) MTU="$value"; continue ;;
|
||||
DNS) DNS+=( ${value//,/ } ); continue ;;
|
||||
esac
|
||||
fi
|
||||
WG_CONFIG+="$line"$'\n'
|
||||
done < "$CONFIG_FILE"
|
||||
shopt -u nocasematch
|
||||
}
|
||||
|
||||
cmd() {
|
||||
echo "[#] $*" >&2
|
||||
"$@"
|
||||
}
|
||||
|
||||
cndc() {
|
||||
local out="$(cmd ndc "$@")"
|
||||
[[ $out == *200\ 0* ]] || { echo "$out"; return 1; }
|
||||
}
|
||||
|
||||
die() {
|
||||
echo "$PROGRAM: $*" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
auto_su() {
|
||||
[[ $UID == 0 ]] || exec su -p -c "'$SELF' ${ARGS[*]}"
|
||||
}
|
||||
|
||||
add_if() {
|
||||
cmd ip link add "$INTERFACE" type wireguard
|
||||
}
|
||||
|
||||
del_if() {
|
||||
cmd ip link del "$INTERFACE"
|
||||
[[ $(ip rule show) =~ 0xc([0-9a-f]+)/0xcffff\ lookup\ $INTERFACE ]] && cndc network destroy $(( 0x${BASH_REMATCH[1]} ));
|
||||
}
|
||||
|
||||
up_if() {
|
||||
while [[ $NETID -lt 4096 ]]; do
|
||||
NETID="$RANDOM"
|
||||
done
|
||||
cmd wg set "$INTERFACE" fwmark 0x20000
|
||||
cndc interface setcfg "$INTERFACE" up
|
||||
cndc network create "$NETID" vpn 1 1
|
||||
cndc network interface add "$NETID" "$INTERFACE"
|
||||
cndc network users add "$NETID" 0-99999
|
||||
}
|
||||
|
||||
set_dns() {
|
||||
[[ ${#DNS[@]} -eq 0 ]] || cndc resolver setnetdns "$NETID" "" "${DNS[@]}"
|
||||
}
|
||||
|
||||
add_addr() {
|
||||
if [[ $1 == *:* ]]; then
|
||||
cndc interface ipv6 "$INTERFACE" enable
|
||||
cmd ip -6 addr add "$1" dev "$INTERFACE"
|
||||
else
|
||||
local ip="${1%%/*}" mask=32
|
||||
[[ $1 == */* ]] && mask="${1##*/}"
|
||||
cndc interface setcfg "$INTERFACE" "$ip" "$mask"
|
||||
fi
|
||||
}
|
||||
|
||||
set_mtu() {
|
||||
local mtu=0 endpoint output
|
||||
if [[ -n $MTU ]]; then
|
||||
cndc interface setmtu "$INTERFACE" "$MTU"
|
||||
return
|
||||
fi
|
||||
while read -r _ endpoint; do
|
||||
[[ $endpoint =~ ^\[?([a-z0-9:.]+)\]?:[0-9]+$ ]] || continue
|
||||
output="$(ip route get "${BASH_REMATCH[1]}" || true)"
|
||||
[[ ( $output =~ mtu\ ([0-9]+) || ( $output =~ dev\ ([^ ]+) && $(ip link show dev "${BASH_REMATCH[1]}") =~ mtu\ ([0-9]+) ) ) && ${BASH_REMATCH[1]} -gt $mtu ]] && mtu="${BASH_REMATCH[1]}"
|
||||
done < <(wg show "$INTERFACE" endpoints)
|
||||
if [[ $mtu -eq 0 ]]; then
|
||||
read -r output < <(ip route show default || true) || true
|
||||
[[ ( $output =~ mtu\ ([0-9]+) || ( $output =~ dev\ ([^ ]+) && $(ip link show dev "${BASH_REMATCH[1]}") =~ mtu\ ([0-9]+) ) ) && ${BASH_REMATCH[1]} -gt $mtu ]] && mtu="${BASH_REMATCH[1]}"
|
||||
fi
|
||||
[[ $mtu -gt 0 ]] || mtu=1500
|
||||
cndc interface setmtu "$INTERFACE" $(( mtu - 80 ))
|
||||
}
|
||||
|
||||
add_route() {
|
||||
cndc network route add "$NETID" "$INTERFACE" "$1"
|
||||
}
|
||||
|
||||
set_config() {
|
||||
cmd wg setconf "$INTERFACE" <(echo "$WG_CONFIG")
|
||||
}
|
||||
|
||||
cmd_usage() {
|
||||
cat >&2 <<-_EOF
|
||||
Usage: $PROGRAM [ up | down ] [ CONFIG_FILE | INTERFACE ]
|
||||
|
||||
CONFIG_FILE is a configuration file, whose filename is the interface name
|
||||
followed by \`.conf'. Otherwise, INTERFACE is an interface name, with
|
||||
configuration found at /data/misc/wireguard/INTERFACE.conf. It is to be readable
|
||||
by wg(8)'s \`setconf' sub-command, with the exception of the following additions
|
||||
to the [Interface] section, which are handled by $PROGRAM:
|
||||
|
||||
- Address: may be specified one or more times and contains one or more
|
||||
IP addresses (with an optional CIDR mask) to be set for the interface.
|
||||
- MTU: an optional MTU for the interface; if unspecified, auto-calculated.
|
||||
- DNS: an optional DNS server to use while the device is up.
|
||||
|
||||
See wg-quick(8) for more info and examples.
|
||||
_EOF
|
||||
}
|
||||
|
||||
cmd_up() {
|
||||
local i
|
||||
[[ -z $(ip link show dev "$INTERFACE" 2>/dev/null) ]] || die "\`$INTERFACE' already exists"
|
||||
trap 'del_if; exit' INT TERM EXIT
|
||||
add_if
|
||||
set_config
|
||||
set_mtu
|
||||
for i in "${ADDRESSES[@]}"; do
|
||||
add_addr "$i"
|
||||
done
|
||||
up_if
|
||||
set_dns
|
||||
for i in $(while read -r _ i; do for i in $i; do [[ $i =~ ^[0-9a-z:.]+/[0-9]+$ ]] && echo "$i"; done; done < <(wg show "$INTERFACE" allowed-ips) | sort -nr -k 2 -t /); do
|
||||
[[ $(ip route get "$i" 2>/dev/null) == *dev\ $INTERFACE\ * ]] || add_route "$i"
|
||||
done
|
||||
trap - INT TERM EXIT
|
||||
}
|
||||
|
||||
cmd_down() {
|
||||
[[ -n $(ip link show dev "$INTERFACE" type wireguard 2>/dev/null) ]] || die "\`$INTERFACE' is not a WireGuard interface"
|
||||
del_if
|
||||
}
|
||||
|
||||
if [[ $# -eq 1 && ( $1 == --help || $1 == -h || $1 == help ) ]]; then
|
||||
cmd_usage
|
||||
elif [[ $# -eq 2 && $1 == up ]]; then
|
||||
auto_su
|
||||
parse_options "$2"
|
||||
cmd_up
|
||||
elif [[ $# -eq 2 && $1 == down ]]; then
|
||||
auto_su
|
||||
parse_options "$2"
|
||||
cmd_down
|
||||
else
|
||||
cmd_usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
exit 0
|
Loading…
Reference in New Issue
Block a user