-
Notifications
You must be signed in to change notification settings - Fork 1
/
wg-vpc.sh
executable file
·148 lines (125 loc) · 3.62 KB
/
wg-vpc.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/ash
# shellcheck shell=dash source=wg-vpc.env.sh
require_command() {
for c in "$@"; do
command -v "$c" >/dev/null || {
echo >&2 "required command '$c' is not installed, aborting..."
exit 1
}
done
}
splash_screen() {
cat <<-EOF
======================================
| Automated shell script |
| to setup site to site WireGuard |
======================================
EOF
}
create_dirs() {
# Create directories
printf "Creating directories and pre-defining permissions on those directories... "
mkdir -p "${peers_dir}"
printf "Done\n"
}
generate_server_keys() {
# Generate WireGuard server keys
printf "Generating WireGuard server keys for '%s' network if not exist... " "${interface}"
test -f "${config_dir}/${interface}.key" || {
umask 077
wg genkey |
tee "${config_dir}/${interface}.key" |
wg pubkey >"${config_dir}/${interface}.pub"
}
printf "Done\n"
}
generate_peer_keys() {
printf "\n"
# Create directory for storing peers
peer="${path_prefix}_${username}"
printf "Creating directory for peer '%s'... " "${peer}"
mkdir -p "${peers_dir}/${peer}"
printf "Done\n"
# Generate peer keys
printf "Generating peer keys for '%s' if not exist... " "${peer}"
test -f "${peers_dir}/${peer}/${peer}.key" || {
umask 077
wg genkey |
tee "${peers_dir}/${peer}/${peer}.key" |
wg pubkey >"${peers_dir}/${peer}/${peer}.pub"
}
printf "Done\n"
# Generate Pre-shared key
printf "Generating peer PSK for '%s'... " "${peer}"
test -f "${peers_dir}/${peer}/${peer}.psk" || {
umask 077
wg genpsk >"${peers_dir}/${peer}/${peer}.psk"
}
printf "Done\n"
}
create_server_config() {
# Create equivalent standard server configuration
printf "Creating server config for '%s'... " "${interface}"
cat <<-EOF >"${config_dir}/${interface}.conf"
[Interface]
Address = ${server_IP}/24
ListenPort = ${server_port}
PrivateKey = $(cat "${config_dir}/${interface}.key") # server's private key
EOF
printf "Done\n"
}
append_peer_to_server_config() {
# Append peer to server configuration
printf "Append '%s' config to '%s'... " "${peer}" "${interface}"
eval "peer_site_ipcidr=\${${username}_site_ipcidr}"
# shellcheck disable=SC2154
cat <<-EOF >>"${config_dir}/${interface}.conf"
[Peer] # ${peer}
PublicKey = $(cat "${peers_dir}/${peer}/${peer}.pub") # peer's public key
PresharedKey = $(cat "${peers_dir}/${peer}/${peer}.psk") # peer's pre-shared key
PersistentKeepalive = 25
AllowedIPs = ${interface_ipcidr_prefix}.${peer_IP}/32, ${peer_site_ipcidr}
EOF
printf "Done\n"
}
create_peer_config() {
# Create peer configuration
printf "Creating config for '%s'... " "${peer}"
cat <<-EOF >"${peers_dir}/${peer}/${peer}.conf"
[Interface]
Address = ${interface_ipcidr_prefix}.${peer_IP}/32
PrivateKey = $(cat "${peers_dir}/${peer}/${peer}.key") # peer's private key
MTU = ${peer_mtu}
[Peer]
PublicKey = $(cat "${config_dir}/${interface}.pub") # server's public key
PresharedKey = $(cat "${peers_dir}/${peer}/${peer}.psk") # peer's pre-shared key
PersistentKeepalive = 25
AllowedIPs = ${peer_allowed_ips}
Endpoint = ${endpoint}:${server_port}
EOF
printf "Done\n"
}
loop_peers() {
for username in ${usernames}; do
generate_peer_keys
append_peer_to_server_config
create_peer_config
peer_IP=$((peer_IP + 1))
done
}
main() {
require_command wg
self="$(readlink -f "$0")"
config_file_default="${self%.sh}.env.sh"
config_file="$1"
test -f "${config_file}" || config_file="${config_file_default}"
config_file="$(readlink -f "${config_file}")"
. "${config_file}"
umask 077
splash_screen
create_dirs
generate_server_keys
create_server_config
loop_peers
}
main "$@"