Skip to content

Commit a799f3d

Browse files
authored
WarpServer
1 parent 5359300 commit a799f3d

File tree

1 file changed

+372
-0
lines changed

1 file changed

+372
-0
lines changed

install.sh

Lines changed: 372 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,372 @@
1+
#!/bin/bash
2+
3+
red='\033[0;31m'
4+
green='\033[0;32m'
5+
yellow='\033[0;33m'
6+
purple='\033[0;35m'
7+
cyan='\033[0;36m'
8+
white='\033[0;37m'
9+
rest='\033[0m'
10+
11+
root_access() {
12+
# Check if the script is running as root
13+
if [ "$EUID" -ne 0 ]; then
14+
echo "This script requires root access. please run as root."
15+
exit 1
16+
fi
17+
}
18+
19+
detect_distribution() {
20+
# Detect the Linux distribution
21+
local supported_distributions=("ubuntu" "debian" "centos" "fedora")
22+
23+
if [ -f /etc/os-release ]; then
24+
source /etc/os-release
25+
if [[ "${ID}" = "ubuntu" || "${ID}" = "debian" || "${ID}" = "centos" || "${ID}" = "fedora" ]]; then
26+
pm="apt"
27+
[ "${ID}" = "centos" ] && pm="yum"
28+
[ "${ID}" = "fedora" ] && pm="dnf"
29+
else
30+
echo "Unsupported distribution!"
31+
exit 1
32+
fi
33+
else
34+
echo "Unsupported distribution!"
35+
exit 1
36+
fi
37+
}
38+
39+
check_dependencies() {
40+
root_access
41+
detect_distribution
42+
43+
local dependencies=("curl" "gnupg" "iptables")
44+
45+
for dep in "${dependencies[@]}"; do
46+
if ! command -v "${dep}" &> /dev/null; then
47+
sudo "${pm}" update -y
48+
echo "${dep} is not installed. Installing..."
49+
sudo "${pm}" install "${dep}" -y
50+
fi
51+
done
52+
}
53+
54+
Install_wgcf() {
55+
curl -fsSL git.io/wgcf.sh | bash
56+
}
57+
58+
WGCF_conf="/etc/wireguard/wgcf.conf"
59+
Profile_conf="/etc/warp/wgcf-profile.conf"
60+
Wgcf_account="/etc/warp/wgcf-account.toml"
61+
IPv4_addr=$(hostname -I | awk '{print $1}')
62+
IPv6_addr=$(hostname -I | awk '{ for(i=1;i<=NF;i++) if($i~/^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{1,4}$/) {print $i; exit} }')
63+
TestIPv4_1='1.0.0.1'
64+
TestIPv4_2='9.9.9.9'
65+
TestIPv6_1='2606:4700:4700::1001'
66+
TestIPv6_2='2620:fe::fe'
67+
CF_Trace_URL='https://www.cloudflare.com/cdn-cgi/trace'
68+
Warp_ipv4=$(curl -s4 ${CF_Trace_URL} --connect-timeout 2 | grep ip | cut -d= -f2)
69+
Warp_ipv6=$(curl -s6 ${CF_Trace_URL} --connect-timeout 2 | grep ip | cut -d= -f2)
70+
WireGuard_Peer_Endpoint_IP4='162.159.192.1'
71+
WireGuard_Peer_Endpoint_IP6='2606:4700:d0::a29f:c001'
72+
WireGuard_Peer_Endpoint_IPv4="${WireGuard_Peer_Endpoint_IP4}:2408"
73+
WireGuard_Peer_Endpoint_IPv6="[${WireGuard_Peer_Endpoint_IP6}]:2408"
74+
WireGuard_Peer_Endpoint_Domain='engage.cloudflareclient.com:2408'
75+
WireGuard_Peer_AllowedIPs_IPv4='0.0.0.0/0'
76+
WireGuard_Peer_AllowedIPs_IPv6='::/0'
77+
WireGuard_Peer_AllowedIPs_DualStack='0.0.0.0/0,::/0'
78+
SysInfo_OS_Ver_major="$(rpm -E '%{rhel}')"
79+
80+
install_cloudflare_warp_packages() {
81+
os=$(uname -s)
82+
83+
if [ "$os" == "Linux" ]; then
84+
if [ -f /etc/os-release ]; then
85+
source /etc/os-release
86+
if [ "$ID" == "ubuntu" ] || [ "$ID" == "debian" ]; then
87+
curl -fsSL https://pkg.cloudflareclient.com/pubkey.gpg | sudo gpg --yes --dearmor --output /usr/share/keyrings/cloudflare-warp-archive-keyring.gpg
88+
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/cloudflare-warp-archive-keyring.gpg] https://pkg.cloudflareclient.com/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/cloudflare-client.list
89+
sudo apt update -y
90+
sudo apt install cloudflare-warp -y
91+
sudo apt install iproute2 openresolv -y
92+
sudo apt install wireguard-tools -y
93+
elif [ "$ID" == "centos" ]; then
94+
curl -fsSl https://pkg.cloudflareclient.com/cloudflare-warp-ascii.repo | sudo tee /etc/yum.repos.d/cloudflare-warp.repo
95+
sudo yum update -y
96+
yum install epel-release -y || yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-${SysInfo_OS_Ver_major}.noarch.rpm -y
97+
sudo yum install cloudflare-warp -y
98+
yum install iproute wireguard-tools -y
99+
else
100+
echo "ERROR: This operating system is not supported."
101+
exit 1
102+
fi
103+
else
104+
echo "ERROR: /etc/os-release not found. Unable to determine the operating system."
105+
exit 1
106+
fi
107+
else
108+
echo "ERROR: This script is intended for Linux systems only."
109+
exit 1
110+
fi
111+
}
112+
113+
Register_NEW_Account() {
114+
mkdir /etc/warp
115+
cd /etc/warp
116+
while [[ ! -f wgcf-account.toml ]]; do
117+
Install_wgcf
118+
echo "Cloudflare WARP Account registration in progress..."
119+
yes | wgcf register
120+
sleep 5
121+
done
122+
}
123+
124+
Generate_WireGuard_profile() {
125+
wgcf generate
126+
}
127+
128+
Read_WGCF_Profile() {
129+
PrivateKey=$(cat ${Profile_conf} | grep ^PrivateKey | cut -d= -f2- | awk '$1=$1')
130+
Address=$(cat ${Profile_conf} | grep ^Address | cut -d= -f2- | awk '$1=$1' | sed ":a;N;s/\n/,/g;ta")
131+
PublicKey=$(cat ${Profile_conf} | grep ^PublicKey | cut -d= -f2- | awk '$1=$1')
132+
}
133+
134+
Get_MTU() {
135+
echo "Getting the best MTU value for WireGuard..."
136+
MTU_Preset=1500
137+
MTU_Increment=10
138+
if [[ ${IPv4Status} = off && ${IPv6Status} = on ]]; then
139+
CMD_ping='ping6'
140+
MTU_TestIP_1="${TestIPv6_1}"
141+
MTU_TestIP_2="${TestIPv6_2}"
142+
else
143+
CMD_ping='ping'
144+
MTU_TestIP_1="${TestIPv4_1}"
145+
MTU_TestIP_2="${TestIPv4_2}"
146+
fi
147+
while true; do
148+
if ${CMD_ping} -c1 -W1 -s$((${MTU_Preset} - 28)) -Mdo ${MTU_TestIP_1} >/dev/null 2>&1 || ${CMD_ping} -c1 -W1 -s$((${MTU_Preset} - 28)) -Mdo ${MTU_TestIP_2} >/dev/null 2>&1; then
149+
MTU_Increment=1
150+
MTU_Preset=$((${MTU_Preset} + ${MTU_Increment}))
151+
else
152+
MTU_Preset=$((${MTU_Preset} - ${MTU_Increment}))
153+
if [[ ${MTU_Increment} = 1 ]]; then
154+
break
155+
fi
156+
fi
157+
if [[ ${MTU_Preset} -le 1360 ]]; then
158+
echo "MTU is set to the lowest value."
159+
MTU_Preset='1360'
160+
break
161+
fi
162+
done
163+
Get_MTU=$((${MTU_Preset} - 80))
164+
echo "WireGuard MTU: ${Get_MTU}"
165+
}
166+
167+
Generate_Wgcf_config() {
168+
[ -d "/etc/wireguard" ] || mkdir -p "/etc/wireguard"
169+
Read_WGCF_Profile
170+
Get_MTU
171+
echo "WireGuard profile generation in progress..."
172+
cat <<EOF >${WGCF_conf}
173+
[Interface]
174+
PrivateKey = ${PrivateKey}
175+
Address = ${Address}
176+
DNS = 8.8.8.8,8.8.4.4,2001:4860:4860::8888,2001:4860:4860::8844
177+
MTU = ${Get_MTU}
178+
EOF
179+
case $user_choice in
180+
1)
181+
AllowedIP=${WireGuard_Peer_AllowedIPs_IPv4}
182+
End=${WireGuard_Peer_Endpoint_IPv4}
183+
IPv4_Global_srcIP
184+
;;
185+
2)
186+
AllowedIP=${WireGuard_Peer_AllowedIPs_IPv6}
187+
End=${WireGuard_Peer_Endpoint_IPv6}
188+
IPv6_Global_srcIP
189+
;;
190+
3)
191+
AllowedIP=${WireGuard_Peer_AllowedIPs_DualStack}
192+
End=${WireGuard_Peer_Endpoint_Domain}
193+
IPv4_Global_srcIP
194+
IPv6_Global_srcIP
195+
;;
196+
*)
197+
echo "Invalid choice. Please choose 1, 2, 3, or 0."
198+
;;
199+
esac
200+
Generate_Wgcf_config_Peer
201+
}
202+
203+
IPv4_Global_srcIP() {
204+
cat <<EOF >>${WGCF_conf}
205+
PostUp = ip -4 rule add from ${IPv4_addr} lookup main prio 18
206+
PostDown = ip -4 rule delete from ${IPv4_addr} lookup main prio 18
207+
EOF
208+
}
209+
210+
IPv6_Global_srcIP() {
211+
cat <<EOF >>${WGCF_conf}
212+
PostUp = ip -6 rule add from ${IPv6_addr} lookup main prio 18
213+
PostDown = ip -6 rule delete from ${IPv6_addr} lookup main prio 18
214+
EOF
215+
}
216+
217+
Generate_Wgcf_config_Peer() {
218+
cat <<EOF >>${WGCF_conf}
219+
220+
[Peer]
221+
PublicKey = ${PublicKey}
222+
AllowedIPs = ${AllowedIP}
223+
Endpoint = ${End}
224+
EOF
225+
}
226+
227+
228+
Install() {
229+
echo -e "${cyan}----------------------------------${rest}"
230+
echo -e "${yellow}Install warp on:${rest}"
231+
echo -e "${purple}1) ${green}IPV4${rest}"
232+
echo -e "${purple}2) ${green}IPV6${rest}"
233+
echo -e "${purple}3) ${green}Both [IPV4 & IPV6]${rest}"
234+
235+
read -p "Choose an option: " user_choice
236+
check_dependencies
237+
Install_wgcf
238+
install_cloudflare_warp_packages
239+
Register_NEW_Account
240+
Generate_WireGuard_profile
241+
Generate_Wgcf_config
242+
(crontab -l ; echo "0 4 * * * systemctl restart wg-quick@wgcf;systemctl restart warp-svc") | sort - | uniq - | crontab -
243+
systemctl enable --now wg-quick@wgcf
244+
echo "Please Wait ..."
245+
sleep 1
246+
echo ""
247+
echo -e "${cyan}----------------------------------${rest}"
248+
WireGuard_Status
249+
echo -e "${cyan}----------------------------------${rest}"
250+
echo ""
251+
}
252+
253+
Uninstall() {
254+
if sudo systemctl is-enabled --quiet [email protected]; then
255+
echo "Uninstalling . . ."
256+
apt purge cloudflare-warp -y > /dev/null 2>&1
257+
yum purge cloudflare-warp -y > /dev/null 2>&1
258+
rm -f /etc/apt/sources.list.d/cloudflare-client.list /usr/share/keyrings/cloudflare-warp-archive-keyring.gpg > /dev/null 2>&1
259+
rm -f /etc/yum.repos.d/cloudflare-warp.repo > /dev/null 2>&1
260+
rm -rf /etc/warp
261+
rm -rf /etc/wireguard
262+
systemctl stop wg-quick@wgcf
263+
systemctl disable wg-quick@wgcf
264+
echo -e "${green}Uninstallation completed successfully.${rest}"
265+
read -p "Please press [Enter] to reboot the server or press [Ctrl+C] to cancel." userInput
266+
[ -z "$userInput" ] && reboot
267+
else
268+
echo "Warp is not installed."
269+
fi
270+
}
271+
272+
Warp_plus() {
273+
echo -e "${cyan}----------------------------------${rest}"
274+
read -p "Enter Warp[+] License Key: " new_license
275+
echo -e "${cyan}----------------------------------${rest}"
276+
echo -e "${yellow}Install warp ${purple}+ ${yellow}on:${rest}"
277+
echo -e "${purple}1) ${green}IPV4${rest}"
278+
echo -e "${purple}2) ${green}IPV6${rest}"
279+
echo -e "${purple}3) ${green}Both [IPV4 & IPV6]${rest}"
280+
281+
read -p "Choose an option: " user_choice
282+
check_dependencies
283+
Install_wgcf
284+
install_cloudflare_warp_packages
285+
Register_NEW_Account
286+
sed -i "s/\(license_key = \).*/\1'${new_license}'/" "wgcf-account.toml"
287+
wgcf update
288+
Generate_WireGuard_profile
289+
Generate_Wgcf_config
290+
(crontab -l ; echo "0 4 * * * systemctl restart wg-quick@wgcf;systemctl restart warp-svc") | sort - | uniq - | crontab -
291+
systemctl enable wg-quick@wgcf
292+
systemctl start wg-quick@wgcf
293+
echo ""
294+
echo -e "${cyan}----------------------------------${rest}"
295+
WireGuard_Status
296+
echo -e "${cyan}----------------------------------${rest}"
297+
echo ""
298+
}
299+
300+
Check_WARP_WireGuard_Status() {
301+
WARP_IPv4_Status=$(curl -s4 ${CF_Trace_URL} --connect-timeout 2 | grep warp | cut -d= -f2)
302+
WARP_IPv6_Status=$(curl -s6 ${CF_Trace_URL} --connect-timeout 2 | grep warp | cut -d= -f2)
303+
304+
if [[ ${WARP_IPv4_Status} = on ]]; then
305+
echo -e "${cyan}IPV4: ${purple}WARP --> ${yellow}${Warp_ipv4}${rest}"
306+
elif [[ ${WARP_IPv4_Status} = plus ]]; then
307+
echo -e "${cyan}IPV4: ${purple}WARP+ --> ${yellow}${Warp_ipv4}${rest}"
308+
else
309+
echo -e "${cyan}IPv4: Normal (off)${rest}"
310+
fi
311+
312+
if [[ ${WARP_IPv6_Status} = on ]]; then
313+
echo -e "${cyan}IPV6: ${purple}WARP --> ${yellow}${Warp_ipv6}${rest}"
314+
elif [[ ${WARP_IPv6_Status} = plus ]]; then
315+
echo -e "${cyan}IPV6: ${purple}WARP+ --> ${yellow}${Warp_ipv6}${rest}"
316+
else
317+
echo -e "${cyan}IPV6: Normal (off)${rest}"
318+
fi
319+
}
320+
321+
WireGuard_Status() {
322+
WARP_IPv4_Status=$(curl -s4 ${CF_Trace_URL} --connect-timeout 2 | grep warp | cut -d= -f2)
323+
WARP_IPv6_Status=$(curl -s6 ${CF_Trace_URL} --connect-timeout 2 | grep warp | cut -d= -f2)
324+
325+
if [[ ${WARP_IPv4_Status} = on ]]; then
326+
echo -e "${cyan}IPV4: ${purple}WARP${rest}"
327+
elif [[ ${WARP_IPv4_Status} = plus ]]; then
328+
echo -e "${cyan}IPV4: ${purple}WARP+${rest}"
329+
else
330+
echo -e "${cyan}IPv4: Normal (off)${rest}"
331+
fi
332+
333+
if [[ ${WARP_IPv6_Status} = on ]]; then
334+
echo -e "${cyan}IPV6: ${purple}WARP ${rest}"
335+
elif [[ ${WARP_IPv6_Status} = plus ]]; then
336+
echo -e "${cyan}IPV6: ${purple}WARP+${rest}"
337+
else
338+
echo -e "${cyan}IPV6: Normal (off)${rest}"
339+
fi
340+
}
341+
342+
343+
clear
344+
echo "********************************"
345+
Check_WARP_WireGuard_Status
346+
echo "********************************"
347+
echo -e "${yellow}By --> Peyman * Github.com/Ptechgithub *${rest}"
348+
echo ""
349+
echo -e "${green}Select an option${rest}: ${rest}"
350+
echo -e "${purple}1) ${green}Install WARP${rest}"
351+
echo -e "${purple}2) ${green}Install [WARP${purple} +${green}]${rest}"
352+
echo -e "${purple}3) ${red}Uninstall${rest}"
353+
echo -e "${purple}0) ${yellow}Exit${rest}"
354+
read -p "Enter your choice: " choice
355+
case "$choice" in
356+
1)
357+
Install
358+
;;
359+
2)
360+
Warp_plus
361+
;;
362+
3)
363+
echo "Exiting..."
364+
Uninstall
365+
;;
366+
0)
367+
exit
368+
;;
369+
*)
370+
echo "Invalid choice. Please select a valid option."
371+
;;
372+
esac

0 commit comments

Comments
 (0)