-
Notifications
You must be signed in to change notification settings - Fork 536
/
wg_mult.sh.bak
298 lines (279 loc) · 9.29 KB
/
wg_mult.sh.bak
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/bin/bash
#wireguard onekey script for centos7+/ubuntu/debian
function blue(){
echo -e "\033[34m\033[01m$1\033[0m"
}
function green(){
echo -e "\033[32m\033[01m$1\033[0m"
}
function red(){
echo -e "\033[31m\033[01m$1\033[0m"
}
function rand(){
min=$1
max=$(($2-$min+1))
num=$(cat /dev/urandom | head -n 10 | cksum | awk -F ' ' '{print $1}')
echo $(($num%$max+$min))
}
function check_selinux(){
CHECK=$(grep SELINUX= /etc/selinux/config | grep -v "#")
if [ "$CHECK" == "SELINUX=enforcing" ]; then
red "======================================================================="
red "检测到SELinux为开启状态,为防止wireguard连接失败,请先重启VPS后,再执行本脚本"
red "======================================================================="
read -p "是否现在重启 ?请输入 [Y/n] :" yn
[ -z "${yn}" ] && yn="y"
if [[ $yn == [Yy] ]]; then
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
echo -e "VPS 重启中..."
reboot
fi
exit
fi
if [ "$CHECK" == "SELINUX=permissive" ]; then
red "======================================================================="
red "检测到SELinux为宽容状态,为防止wireguard连接失败,请先重启VPS后,再执行本脚本"
red "======================================================================="
read -p "是否现在重启 ?请输入 [Y/n] :" yn
[ -z "${yn}" ] && yn="y"
if [[ $yn == [Yy] ]]; then
sed -i 's/SELINUX=permissive/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
echo -e "VPS 重启中..."
reboot
fi
exit
fi
}
function check_release(){
source /etc/os-release
RELEASE=$ID
VERSION=$VERSION_ID
}
function install_wg(){
check_release
if [ "$RELEASE" == "centos" ] && [ "$VERSION" == "7" ]; then
yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install -y "kernel-devel-uname-r == $(uname -r)"
curl -o /etc/yum.repos.d/jdoss-wireguard-epel-7.repo https://copr.fedorainfracloud.org/coprs/jdoss/wireguard/repo/epel-7/jdoss-wireguard-epel-7.repo
yum install -y wireguard-dkms wireguard-tools qrencode iptables-services
systemctl stop firewalld
systemctl disable firewalld
systemctl enable iptables
systemctl start iptables
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -F
service iptables save
service iptables restart
echo 1 > /proc/sys/net/ipv4/ip_forward
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p
elif [ "$RELEASE" == "centos" ] && [ "$VERSION" == "8" ]; then
yum install -y epel-release
yum install -y "kernel-devel-uname-r == $(uname -r)"
yum config-manager --set-enabled PowerTools
yum copr enable -y jdoss/wireguard
yum install -y wireguard-dkms wireguard-tools qrencode iptables-services
systemctl stop firewalld
systemctl disable firewalld
systemctl enable iptables
systemctl start iptables
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -F
service iptables save
service iptables restart
echo 1 > /proc/sys/net/ipv4/ip_forward
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p
elif [ "$RELEASE" == "ubuntu" ] && [ "$VERSION" == "19.04" ]; then
red "==================="
red "暂未支持ubuntu19.04系统"
red "==================="
elif [ "$RELEASE" == "ubuntu" ] && [ "$VERSION" == "19.10" ]; then
red "==================="
red "暂未支持ubuntu19.10系统"
red "==================="
elif [ "$RELEASE" == "ubuntu" ] && [ "$VERSION" == "16.04" ]; then
systemctl stop ufw
systemctl disable ufw
apt-get -y update
add-apt-repository -y ppa:wireguard/wireguard
apt-get update
apt-get install -y wireguard qrencode iptables
systemctl enable iptables
systemctl start iptables
echo 1 > /proc/sys/net/ipv4/ip_forward
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p
elif [ "$RELEASE" == "ubuntu" ] && [ "$VERSION" == "18.04" ]; then
systemctl stop ufw
systemctl disable ufw
apt-get -y update
apt-get install -y software-properties-common
apt-get install -y openresolv
add-apt-repository -y ppa:wireguard/wireguard
apt-get -y update
apt-get install -y wireguard qrencode iptables
systemctl enable iptables
systemctl start iptables
echo 1 > /proc/sys/net/ipv4/ip_forward
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p
elif [ "$RELEASE" == "debian" ]; then
echo "deb http://deb.debian.org/debian/ unstable main" > /etc/apt/sources.list.d/unstable.list
printf 'Package: *\nPin: release a=unstable\nPin-Priority: 90\n' > /etc/apt/preferences.d/limit-unstable
apt update
apt install -y wireguard qrencode iptables
systemctl enable iptables
systemctl start iptables
echo 1 > /proc/sys/net/ipv4/ip_forward
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p
else
red "================="
red "您当前系统暂未支持"
red "================="
fi
}
function config_wg(){
mkdir /etc/wireguard
cd /etc/wireguard
wg genkey | tee sprivatekey | wg pubkey > spublickey
wg genkey | tee cprivatekey | wg pubkey > cpublickey
s1=$(cat sprivatekey)
s2=$(cat spublickey)
c1=$(cat cprivatekey)
c2=$(cat cpublickey)
serverip=$(curl ipv4.icanhazip.com)
port=$(rand 10000 60000)
eth=$(ls /sys/class/net| awk 'NR==1&&/^e/{print $1}')
chmod 777 -R /etc/wireguard
cat > /etc/wireguard/wg0.conf <<-EOF
[Interface]
PrivateKey = $s1
Address = 10.77.0.1/24
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o $eth -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o $eth -j MASQUERADE
ListenPort = $port
DNS = 8.8.8.8
MTU = 1420
[Peer]
PublicKey = $c2
AllowedIPs = 10.77.0.2/32
EOF
cat > /etc/wireguard/client.conf <<-EOF
[Interface]
PrivateKey = $c1
Address = 10.77.0.2/24
DNS = 8.8.8.8
MTU = 1420
[Peer]
PublicKey = $s2
Endpoint = $serverip:$port
AllowedIPs = 0.0.0.0/0, ::0/0
PersistentKeepalive = 25
EOF
wg-quick up wg0
systemctl enable wg-quick@wg0
content=$(cat /etc/wireguard/client.conf)
green "电脑端请下载/etc/wireguard/client.conf文件,手机端可直接使用软件扫码"
green "${content}" | qrencode -o - -t UTF8
}
function add_user(){
green "=================================="
green "给新用户起个名字,不能和已有用户重复"
green "=================================="
read -p "请输入用户名:" newname
cd /etc/wireguard/
if [ ! -f "/etc/wireguard/$newname.conf" ]; then
cp client.conf $newname.conf
wg genkey | tee temprikey | wg pubkey > tempubkey
ipnum=$(grep Allowed /etc/wireguard/wg0.conf | tail -1 | awk -F '[ ./]' '{print $6}')
newnum=$((10#${ipnum}+1))
sed -i 's%^PrivateKey.*$%'"PrivateKey = $(cat temprikey)"'%' $newname.conf
sed -i 's%^Address.*$%'"Address = 10.77.0.$newnum\/24"'%' $newname.conf
cat >> /etc/wireguard/wg0.conf <<-EOF
[Peer]
PublicKey = $(cat tempubkey)
AllowedIPs = 10.77.0.$newnum/32
EOF
wg set wg0 peer $(cat tempubkey) allowed-ips 10.77.0.$newnum/32
green "============================================="
green "添加完成,文件:/etc/wireguard/$newname.conf"
green "============================================="
rm -f temprikey tempubkey
else
red "======================"
red "用户名已存在,请更换名称"
red "======================"
fi
}
function remove_wg(){
check_release
if [ -d "/etc/wireguard" ]; then
wg-quick down wg0
if [ "$RELEASE" == "centos" ]; then
yum remove -y wireguard-dkms wireguard-tools
rm -rf /etc/wireguard/
green "卸载完成"
elif [ "$RELEASE" == "ubuntu" ]; then
apt-get remove -y wireguard
rm -rf /etc/wireguard/
green "卸载完成"
elif [ "$RELEASE" == "debian" ]; then
apt remove -y wireguard
rm -rf /etc/wireguard/
green "卸载完成"
else
red "系统不符合要求"
fi
else
red "未检测到wireguard"
fi
}
function start_menu(){
clear
green "=========================================="
green " Info : For Centos7+/Ubuntu16+/Debian9+"
green " Author : A"
green "=========================================="
green "1. Install wireguard"
red "2. Remove wireguard"
green "3. Show client QRcode"
green "4. Add user"
red "0. Exit"
echo
read -p "Please enter a number:" num
case "$num" in
1)
check_selinux
install_wg
config_wg
;;
2)
remove_wg
;;
3)
content=$(cat /etc/wireguard/client.conf)
echo "${content}" | qrencode -o - -t UTF8
;;
4)
add_user
;;
0)
exit 1
;;
*)
clear
red "Please enter the correct number!"
sleep 1s
start_menu
;;
esac
}
start_menu