dnk: add option Interface

This commit is contained in:
Daniil Firsov 2022-08-31 18:36:12 +03:00
parent 9d23f62325
commit 5d78701fc1
2 changed files with 19 additions and 16 deletions

View File

@ -18,6 +18,7 @@ echo "EVENT=$EVENT"
case $EVENT in
INIT)
SERVER_HOST="{{ server.settings.host_name }}"
SERVER_INTERFACE="{{ server.settings.host_interface }}"
if [ -z $SERVER_HOST ]; then
echo "ERROR: set variable 'host_name' to server settings"
exit 1
@ -40,7 +41,11 @@ case $EVENT in
cd /etc/wireguard
$CURL -s --fail-with-body https://danuk.github.io/wg-manager/wg-manager.sh > $WG_MANAGER
chmod 700 $WG_MANAGER
$WG_MANAGER -i -s $SERVER_HOST
if [ $SERVER_INTERFACE ]; then
$WG_MANAGER -i -s $SERVER_HOST -I $SERVER_INTERFACE
else
$WG_MANAGER -i -s $SERVER_HOST
fi
;;
CREATE)
USER_CFG=$($WG_MANAGER -u "{{ us.id }}" -c -p)

View File

@ -21,6 +21,7 @@ function usage {
echo " -q : Print user QR code"
echo " -u <user> : User identifier (uniq field for vpn account)"
echo " -s <server> : Server host for user connection"
echo " -I : Interface (default eth0)"
echo " -h : Usage"
exit 1
}
@ -28,7 +29,13 @@ function usage {
unset USER
umask 0077
while getopts ":icdpqhLUu:s:" opt; do
HOME_DIR="/etc/wireguard"
SERVER_NAME="wg-server"
SERVER_IP_PREFIX="10.10.10"
SERVER_PORT=39547
SERVER_INTERFACE="eth0" # ens4
while getopts ":icdpqhLUu:I:s:" opt; do
case $opt in
i) INIT=1 ;;
c) CREATE=1 ;;
@ -38,6 +45,7 @@ while getopts ":icdpqhLUu:s:" opt; do
p) PRINT_USER_CONFIG=1 ;;
q) PRINT_QR_CODE=1 ;;
u) USER="$OPTARG" ;;
I) SERVER_INTERFACE="$OPTARG" ;;
h) usage ;;
s) SERVER_ENDPOINT="$OPTARG" ;;
\?) echo "Invalid option: -$OPTARG" ; exit 1 ;;
@ -47,12 +55,6 @@ done
[ $# -lt 1 ] && usage
HOME_DIR="/etc/wireguard"
SERVER_NAME="wg-server"
SERVER_IP_PREFIX="10.10.10"
SERVER_PORT=39547
SERVER_INTERFACE="eth0" # ens4
function reload_server {
wg syncconf ${SERVER_NAME} <(wg-quick strip ${SERVER_NAME})
}
@ -110,15 +112,11 @@ function init {
mkdir -p "keys/${SERVER_NAME}"
echo -n "$SERVER_ENDPOINT" > "keys/.server"
if [ -f "keys/${SERVER_NAME}/private.key" ]; then
echo "Server has already been initialized"
exit 0
if [ ! -f "keys/${SERVER_NAME}/private.key" ]; then
echo -n "1" > "keys/.last_ip"
wg genkey | tee "keys/${SERVER_NAME}/private.key" | wg pubkey > "keys/${SERVER_NAME}/public.key"
fi
echo -n "1" > "keys/.last_ip"
wg genkey | tee "keys/${SERVER_NAME}/private.key" | wg pubkey > "keys/${SERVER_NAME}/public.key"
SERVER_PVT_KEY=$(cat "keys/$SERVER_NAME/private.key")
cat <<EOF > "$SERVER_NAME.conf"
@ -136,7 +134,7 @@ EOF
sysctl -p
systemctl enable wg-quick@${SERVER_NAME}
wg-quick up ${SERVER_NAME}
wg-quick up ${SERVER_NAME} || true
echo "Server initialized successfully"
exit 0