-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwg_install.sh
120 lines (107 loc) · 3.56 KB
/
wg_install.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
#!/bin/bash
function green(){
echo -e "\033[32m\033[01m $1 \033[0m"
}
function red(){
echo -e "\033[31m\033[01m $1 \033[0m"
}
function yellow(){
echo -e "\033[33m\033[01m $1 \033[0m"
}
install(){
# user input settings
read -p "Choose your main port (80, 179, 443, 3784, 4784, 8443 are included by default, select among them): " -e -i 3785 port
read -p "MTU (max is 1420, recommended for udp2raw 1200): " -e -i 1420 mtu
read -p "Purge snapd and unattended-upgrades with their dependencies (N/y): " -e -i "n" purge
case ${purge:0:1} in
y|Y )
# garbage cleanup
apt purge snapd unattended-upgrades -y
apt --purge autoremove -y
;;
* )
;;
esac
dns="1.1.1.1"
# forwarding rules
echo "net.ipv4.ip_forward=1" > /etc/sysctl.d/10-ipv4-forward.conf
echo 1 > /proc/sys/net/ipv4/ip_forward
# requirements
version=$(cat /etc/os-release | awk -F '[".]' '$1=="VERSION="{print $2}')
apt update
apt upgrade -y
apt install ifupdown wireguard resolvconf -y
# keys and settings
cd /etc/wireguard
umask 077
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)
eth=$(ls /sys/class/net | awk '/^e/{print}')
cat > /etc/wireguard/wg0.conf <<-EOF
[Interface]
PrivateKey = $s1
Address = 10.0.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; iptables -t nat -A PREROUTING -i $eth -d $serverip -p udp -m multiport --dports 80,179,443,3784,4784,8443 -j REDIRECT --to-ports $port; iptables -A PREROUTING -t nat -i $eth -p udp --dport 53 -j DNAT --to-destination $dns:53
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o $eth -j MASQUERADE; iptables -t nat -D PREROUTING -i $eth -d $serverip -p udp -m multiport --dports 80,179,443,3784,4784,8443 -j REDIRECT --to-ports $port; iptables -D PREROUTING -t nat -i $eth -p udp --dport 53 -j DNAT --to-destination $dns:53
ListenPort = $port
MTU = $mtu
[Peer]
PublicKey = $c2
AllowedIPs = 10.0.0.2/32
EOF
cat > /etc/wireguard/client.conf <<-EOF
[Interface]
PrivateKey = $c1
Address = 10.0.0.2/24
DNS = 10.0.0.1
MTU = $mtu
[Peer]
PublicKey = $s2
Endpoint = $serverip:$port
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
EOF
wg-quick up wg0
systemctl enable wg-quick@wg0
green "Running on UDP: DNS $dns, MTU $mtu"
green "Now download /etc/wireguard/client.conf"
green "It's recommended to reboot in order to finish the updates"
}
start_menu(){
clear
green " ===================================="
green " Wireguard one-click setup "
green " Requires: Ubuntu >= 18.04 + root access "
green " About: based on TunSafe one-click setup by atrandys "
green "https://github.com/atrandys/tunsafe"
green " ===================================="
echo
green " 1. Wireguard installation"
green " 2. Echo client configuration"
yellow " 0. Exit"
echo
read -p "Please enter the number: " num
case "$num" in
1)
install
;;
2)
cat /etc/wireguard/client.conf
;;
0)
exit 1
;;
*)
clear
red "Incorrect number"
sleep 1s
start_menu
;;
esac
}
start_menu