1
0
mirror of https://github.com/faicker/wg-config.git synced 2020-11-18 19:53:49 -08:00

add init server

This commit is contained in:
Faicker Mo 2019-05-15 13:18:59 +08:00
parent 18d564adcf
commit c7003084f5
3 changed files with 34 additions and 9 deletions

View File

@ -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

35
user.sh
View File

@ -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

View File

@ -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=