diff --git a/README.md b/README.md index e75ba2f..7cae690 100644 --- a/README.md +++ b/README.md @@ -8,17 +8,19 @@ Client config file and qrcode are generated. ### config The wireguard default config directory is /etc/wireguard. + The script config file is wg.def, create and edit it according to wg.def.sample. + You can generate the public key and private key with command `wg genkey | tee prikey | wg pubkey > pubkey`. ### usage Running as root. -#### start wireguard +#### init wireguard server ```bash -wg-quick up wg0 +./user.sh -i ``` #### add a user diff --git a/user.sh b/user.sh index a421712..a474dd3 100755 --- a/user.sh +++ b/user.sh @@ -84,7 +84,7 @@ add_user() { exit 1 fi - echo "$user $_VPN_IP $public_key" >> ${SAVED_FILE} + echo "$user $_VPN_IP $public_key" >> ${SAVED_FILE} && echo "use $user is added. config dir is $userdir" } del_user() { @@ -105,7 +105,7 @@ del_user() { if [[ -n "$ip" ]]; then echo "$ip" >> ${AVAILABLE_IP_FILE} fi - rm -rf $userdir + rm -rf $userdir && echo "use $user is deleted" } generate_and_install_server_config_file() { @@ -125,7 +125,7 @@ EOF \cp -f $WG_TMP_CONF_FILE $WG_CONF_FILE } -do_clear() { +clear_all() { local interface=$_INTERFACE wg-quick down $interface > $WG_CONF_FILE @@ -148,8 +148,29 @@ do_user() { generate_and_install_server_config_file } +init_server() { + local interface=$_INTERFACE + local template_file=${SERVER_TPL_FILE} + + if [[ -s $WG_CONF_FILE ]]; then + echo "$WG_CONF_FILE exist" + exit 1 + fi + generate_cidr_ip_file_if + eval "echo \"$(cat "${template_file}")\"" > $WG_CONF_FILE + chmod 600 $WG_CONF_FILE + wg-quick up $interface +} + usage() { - echo "usage: $0 [-a|-d|-c|-g] [username]" + echo "usage: $0 [-a|-d|-c|-g|-i] [username] + + -i: init server conf + -a: add user + -d: del user + -c: clear all + -g: generate ip file + " } # main @@ -161,8 +182,10 @@ fi action=$1 user=$2 -if [[ $action == "-c" ]]; then - do_clear +if [[ $action == "-i" ]]; then + init_server +elif [[ $action == "-c" ]]; then + clear_all elif [[ $action == "-g" ]]; then generate_cidr_ip_file_if elif [[ ! -z "$user" && ( $action == "-a" || $action == "-d" ) ]]; then diff --git a/wg.def.sample b/wg.def.sample index ad6a1a1..78e5c37 100644 --- a/wg.def.sample +++ b/wg.def.sample @@ -1,6 +1,6 @@ _INTERFACE=wg0 _VPN_NET=192.168.88.0/24 _SERVER_PORT=2000 -_SERVER_LISTEN=wg.example.com:$_SERVER_PORT +_SERVER_LISTEN=wg.example.com:$_SERVER_PORT # client use _SERVER_PUBLIC_KEY= _SERVER_PRIVATE_KEY=