#!/bin/bash debug=0 BOLD=$( tput bold) NORMAL=$( tput sgr0) RESET=$( tput sgr0) NC=$( tput sgr0) # No color BOLD=$( tput bold) BLACK=$( tput setaf 0) RED=$( tput setaf 1) GREEN=$( tput setaf 2) YELLOW=$( tput setaf 3) BLUE=$( tput setaf 4) MAGENTA=$( tput setaf 5) CYAN=$( tput setaf 6) WHITE=$( tput setaf 7) DEFAULT=$( tput setaf 9) #---ini file parameters list unset PARAMS; PARAMS=( Rtr_Addr_Public Rrt_Port Rtr_Interface Rtr_Addr_Private Rtr_CIDR_Mask Rtr_PUB_KEY ) export RouterName="" export Company="" export CORP="" #========== INTERNAL FUNCTIONS ================================================ #---------- function Info ----------------------------------------------------- # # With date / time prefix # Info() { printf "${GREEN}%s ${NC} %s\n" "$( date +%F_%T )" "$*" } #---------- function Message -------------------------------------------------- # # Send to STDOUT # function Message() { printf "\n${GREEN}[i] ${BLUE}%s${NC}\n" "$*" } #---------- ip2int ------------------------------------------------------------ # function ip2int() { local a b c d { IFS=. read a b c d; } <<< $1 echo $(((((((a << 8) | b) << 8) | c) << 8) | d)) } #---------- int2ip ------------------------------------------------------------ # function int2ip() { local ui32=$1; shift local ip n for n in 1 2 3 4; do ip=$((ui32 & 0xff))${ip:+.}$ip ui32=$((ui32 >> 8)) done echo $ip } #---------- CreateUser -------------------------------------------------------- # function CreateUser() { local CORP=$1 local RouterCfg=$2 local UserNumber=$3 local NameUser=$4 local debug=1 #---Read values from config file for PARAM in "${PARAMS[@]}" do eval local ${PARAM}=$(sed -nr "/^\[${CORP}\]/ { :l /^${PARAM}[ ]*=/ { s/[^=]*=[ ]*//; p; q;}; n; b l;}" $RouterCfg) done Digits=000 Temp="${Digits}${UserNumber}" ClientNumPad=$(echo ${Temp:(-${#Digits})}) IFS=. read -r octet1 octet2 octet3 octet4 <<< "$Rtr_Addr_Private" Subnet="${octet1}.${octet2}.${octet3}" Message "Subnet : $Subnet" Message "ClientNumPad : $ClientNumPad" ((debug)) && echo -e " DEBUG - CreateUser UserNumber = $1 NameUser = $2 Company = $3 Rtr_Addr_Public = $Rtr_Addr_Public Rrt_Port = $Rrt_Port Rtr_Interface = $Rtr_Interface Rtr_Addr_Private = $Rtr_Addr_Private Rtr_CIDR_Mask = $Rtr_CIDR_Mask Rtr_PUB_KEY = $Rtr_PUB_KEY Subnet = $Subnet " && exit CLIENT_PRIV_KEY=$(wg genkey) CLIENT_PUB_KEY=$(echo "${CLIENT_PRIV_KEY}" | wg pubkey) CLIENT_PRE_SHARED_KEY=$(wg genpsk) ROUTER_PUB_KEY="$RouterPubKey" CLIENT_FILE_PREFIX="${ClientNumPad}-${NameUser}" CLIENT_FILE_WIN="${CLIENT_FILE_PREFIX}.conf" CLIENT_FILE_RTR="${CLIENT_FILE_PREFIX}.Peer.rsc" echo -e "\nClient: ${GREEN}---------------------------------------------------------${NC}" echo -e "[Interface] PrivateKey = ${CLIENT_PRIV_KEY} ListenPort = 51821 Address = ${Subnet}.${UserNumber}/32 DNS = 1.1.1.1,8.8.8.8 [Peer] PublicKey = ${ROUTER_PUB_KEY} PresharedKey = ${CLIENT_PRE_SHARED_KEY} AllowedIPs = 0.0.0.0/0 Endpoint = ${RouterAddressPub}:${RouterPort} PersistentKeepalive = 25 " | tee "${CLIENT_FILE_WIN}" echo -e "\nRouter: ${GREEN}---------------------------------------------------------${NC}" echo -e "/interface wireguard peers add allowed-address=${Subnet}.${UserNumber}/32 disabled=no name=\"${NameUser}\" interface=${Rtr_Interface} \\ preshared-key=\"${CLIENT_PRE_SHARED_KEY}\" public-key=\"${CLIENT_PUB_KEY}\""| tee "${CLIENT_FILE_RTR}" Message "QR Code:" qrencode -t ansiutf8 -l L < "${CLIENT_FILE_WIN}" qrencode -l L -s 6 -d 225 -o "${CLIENT_FILE_WIN}.png" < "${CLIENT_FILE_WIN}" ls -1 ${ClientNumPad}* } function GetRouter_Infos() { local RouterName="$1" local IniFile="${1}.cfg" local debug=0 ((debug)) && echo -e "\nIniFile = ${IniFile}\n" read -p "Entrer CORP: " CORP echo -e "[${CORP}]" | tee ${IniFile} for PARAM in "${PARAMS[@]}" do #eval ${PARAM}=$(sed -nr "/^\[${CORP}\]/ { :l /^${PARAM}[ ]*=/ { s/[^=]*=[ ]*//; p; q;}; n; b l;}" ${IniFile}) echo -e "\nPARAM = $PARAM" eval 'read -p "Entrer ${PARAM} " Variable' eval 'echo ${PARAM}=${Variable} | tee -a ${IniFile}' done ((debug)) && echo "GetRouter_Infos exit" } Help() { cat << EOF usage: $(basename "$0") [OPTIONS] -c Company name -h Show this message -i Interactive -u User Name -n User number EOF } ((!$#)) && Help && exit while getopts c:dhin:r:u: option do case "${option}" in c) CORP=${OPTARG} ;; d) debug=1 ;; h) Help exit ;; i) Interactive exit ;; n) UserNumber="${OPTARG}" ;; r) RouterName="${OPTARG}" # à enlever ;; u) NameUser="${OPTARG}" ;; *) echo -e "Usage (bad argument: $OPTARG) \n" exit 1;; esac done #---Init global variables #for PARAM in "${PARAMS[@]}" #do # eval export '${PARAM}=""' # done CfgNum=$(find . -maxdepth 1 -iname "*.cfg" |wc -l) #Message "Found $CfgNum config files" if [[ $CfgNum -eq 1 ]] then RouterCfg=$(find . -maxdepth 1 -iname "*.cfg" -printf "%f") else read -p "Entrer Nom du Router: " RouterName ((debug)) && echo -e "Router Name = ${RouterName}" GetRouter_Infos "${RouterName}" RouterCfg=${RouterName}.cfg fi ((debug)) &&echo -e " RouterCfg : $RouterCfg CORP : $CORP " #((debug)) && echo -e "Avant PARAM" for PARAM in "${PARAMS[@]}" do eval ${PARAM}=$(sed -nr "/^\[${CORP}\]/ { :l /^${PARAM}[ ]*=/ { s/[^=]*=[ ]*//; p; q;}; n; b l;}" $RouterCfg) done echo -e " Avant Create User UserNumber = $UserNumber NameUser = $NameUser CORP = $CORP Rtr_Addr_Public = $Rtr_Addr_Public Rrt_Port = $Rrt_Port Rtr_Interface = $Rtr_Interface Rtr_Addr_Private = $Rtr_Addr_Private Rtr_CIDR_Mask = $Rtr_CIDR_Mask Rtr_PUB_KEY = $Rtr_PUB_KEY " exit CreateUser ${CORP} ${RouterCfg} ${UserNumber} ${NameUser} #${Rtr_Addr_Public} ${Rrt_Port} ${Rtr_Interface} ${Rtr_Addr_Private} ${Rtr_CIDR_Mask} "${Rtr_PUB_KEY}" exit CLIENT_PRIV_KEY=$(wg genkey) CLIENT_PUB_KEY=$(echo "${CLIENT_PRIV_KEY}" | wg pubkey) CLIENT_PRE_SHARED_KEY=$(wg genpsk) echo -e " CLIENT_PRIV_KEY $CLIENT_PRIV_KEY CLIENT_PUB_KEY $CLIENT_PUB_KEY CLIENT_PRE_SHARED_KEY $CLIENT_PRE_SHARED_KEY "