-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinstal-wg-ui
255 lines (206 loc) · 6.98 KB
/
instal-wg-ui
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
#!/bin/bash
# Função para obter a última versão do WireGuard UI
get_latest_version() {
curl --silent "https://api.github.com/repos/ngoduykhanh/wireguard-ui/releases/latest" | \
grep '"tag_name":' | \
sed -E 's/.*"([^"]+)".*/\1/'
}
# Função para gerar chaves WireGuard
generate_wg_keys() {
umask 077
wg genkey | tee privatekey | wg pubkey > publickey
private_key=$(cat privatekey)
public_key=$(cat publickey)
rm -f privatekey publickey
}
# Função para criar a interface wg0
create_wg0_conf() {
generate_wg_keys
read -p "Digite o IP local para wg0 (exemplo: 10.0.0.1/24): " wg0_ip
read -p "Digite a porta para wg0 (exemplo: 51820): " wg0_port
cat <<EOF > /etc/wireguard/wg0.conf
[Interface]
PrivateKey = $private_key
Address = $wg0_ip
ListenPort = $wg0_port
SaveConfig = true
# Adicione aqui os peers se necessário
#[Peer]
#PublicKey = <chave_do_peer>
#AllowedIPs = <IP_do_peer>
EOF
chmod 600 /etc/wireguard/wg0.conf
}
# Função para verificar e aplicar net.ipv4.ip_forward
apply_ip_forwarding() {
if sysctl -n net.ipv4.ip_forward | grep -q 1; then
echo "IP forwarding já está ativado."
else
echo "Ativando IP forwarding..."
echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf
sysctl -p
fi
}
# Função para instalação do WireGuard e configuração do sistema
install_wireguard() {
echo "Atualizando listas de pacotes..."
apt update -y
echo "Atualizando pacotes instalados..."
apt upgrade -y
# Verificando e ativando IP Forwarding
apply_ip_forwarding
echo "Instalando WireGuard..."
apt install wireguard -y
# Criando e configurando a interface wg0
echo "Criando a interface wg0..."
create_wg0_conf
# Pedindo ao usuário para definir o nome de usuário e senha do WireGuard UI
read -p "Digite o nome de usuário para o WireGuard UI: " ui_username
read -sp "Digite a senha para o WireGuard UI: " ui_password
echo
# Obtendo a última versão do WireGuard UI
latest_version=$(get_latest_version)
echo "Versão mais recente do WireGuard UI: $latest_version"
# Corrigindo a URL de download para evitar o erro 404
ui_version="${latest_version#v}" # Remove o 'v' do início
download_url="https://github.com/ngoduykhanh/wireguard-ui/releases/download/${latest_version}/wireguard-ui-v${ui_version}-linux-amd64.tar.gz"
echo "Baixando WireGuard UI versão $ui_version..."
wget -P /tmp "$download_url"
if [[ $? -ne 0 ]]; then
echo "Erro ao baixar WireGuard UI. Verifique a versão e o link."
exit 1
fi
echo "Extraindo WireGuard UI..."
tar -xzvf /tmp/wireguard-ui-*.tar.gz -C /tmp
echo "Criando a pasta wireguard-ui..."
mkdir -p /opt/wireguard-ui
mv /tmp/wireguard-ui /opt/wireguard-ui
# Verificação e aplicação de regras do Iptables
if ! iptables -C INPUT -p tcp --dport 443 -j ACCEPT 2>/dev/null; then
echo "Aplicando regra para porta 443..."
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
else
echo "Regra para porta 443 já existe."
fi
if ! iptables -C INPUT -p tcp --dport 80 -j ACCEPT 2>/dev/null; then
echo "Aplicando regra para porta 80..."
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
else
echo "Regra para porta 80 já existe."
fi
if ! iptables -C INPUT -p tcp --dport 5000 -j ACCEPT 2>/dev/null; then
echo "Aplicando regra para porta 5000..."
iptables -A INPUT -p tcp --dport 5000 -j ACCEPT
else
echo "Regra para porta 5000 já existe."
fi
if ! iptables -C INPUT -p udp --dport 51820 -j ACCEPT 2>/dev/null; then
echo "Aplicando regra para porta 51820..."
iptables -A INPUT -p udp --dport 51820 -j ACCEPT
else
echo "Regra para porta 51820 já existe."
fi
# Salvando regras do iptables
iptables-save > /etc/iptables/rules.v4
# Criação e configuração do arquivo .env
echo "WGUI_USERNAME=$ui_username" > /opt/wireguard-ui/.env
echo "WGUI_PASSWORD=$ui_password" >> /opt/wireguard-ui/.env
# Criando scripts postdown.sh e postup.sh
cat <<EOF > /opt/wireguard-ui/postdown.sh
#!/usr/bin/bash
iptables -t nat -D POSTROUTING -o <INTERFACE> -j MASQUERADE
EOF
cat <<EOF > /opt/wireguard-ui/postup.sh
#!/usr/bin/bash
iptables -t nat -A POSTROUTING -o <INTERFACE> -j MASQUERADE
EOF
chmod +x /opt/wireguard-ui/post*.sh
# Criando serviço para WireGuard UI Daemon
cat <<EOF > /etc/systemd/system/wireguard-ui-daemon.service
[Unit]
Description=WireGuard UI Daemon
Wants=network-online.target
After=network-online.target
[Service]
User=root
Group=root
Type=simple
WorkingDirectory=/opt/wireguard-ui
EnvironmentFile=/opt/wireguard-ui/.env
ExecStart=/opt/wireguard-ui/wireguard-ui -bind-address "0.0.0.0:5000"
[Install]
WantedBy=multi-user.target
EOF
cat <<EOF > /etc/systemd/system/wgui.service
[Unit]
Description=Restart WireGuard
After=network.target
[Service]
Type=oneshot
ExecStart=/usr/bin/systemctl restart wg-quick@wg0.service
[Install]
RequiredBy=wgui.path
EOF
cat <<EOF > /etc/systemd/system/wgui.path
[Unit]
Description=Watch /etc/wireguard/wg0.conf for changes
[Path]
PathModified=/etc/wireguard/wg0.conf
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable wg-quick@wg0
systemctl start wg-quick@wg0
systemctl enable wireguard-ui-daemon.service
systemctl start wireguard-ui-daemon.service
systemctl enable wgui.{path,service}
systemctl start wgui.{path,service}
echo "Instalação e configuração concluídas."
}
# Função para desinstalação do WireGuard e remoção de configurações
uninstall_wireguard() {
echo "Removendo WireGuard..."
apt remove --purge wireguard -y
echo "Removendo WireGuard UI..."
rm -rf /opt/wireguard-ui
echo "Removendo serviços WireGuard UI Daemon..."
systemctl stop wgui.{path,service}
systemctl disable wgui.{path,service}
rm /etc/systemd/system/wireguard-ui-daemon.service
rm /etc/systemd/system/wgui.{path,service}
echo "Removendo regras do iptables..."
iptables -D INPUT -p tcp --dport 443 -j ACCEPT
iptables -D INPUT -p tcp --dport 80 -j ACCEPT
iptables -D INPUT -p tcp --dport 5000 -j ACCEPT
iptables -D INPUT -p udp --dport 51820 -j ACCEPT
iptables-save > /etc/iptables/rules.v4
echo "Removendo configuração da interface wg0..."
systemctl stop wg-quick@wg0
systemctl disable wg-quick@wg0
rm /etc/wireguard/wg0.conf
echo "Remoção concluída."
}
# Menu interativo
while true; do
echo "Escolha uma opção:"
echo "1. Instalar WireGuard e configurar"
echo "2. Desinstalar WireGuard e remover configurações"
echo "3. Sair"
read -p "Opção: " option
case $option in
1)
install_wireguard
;;
2)
uninstall_wireguard
;;
3)
echo "Saindo..."
exit 0
;;
*)
echo "Opção inválida. Tente novamente."
;;
esac
done