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

View File

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