forked from sirpasha/Backhaul-Installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackhaul_installer_v1.0.sh
196 lines (166 loc) · 5.14 KB
/
backhaul_installer_v1.0.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
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
#!/bin/bash
rm backhaul_installer_v1.0.sh
# Install required packages
sudo apt update && apt install wget -qq
clear
echo -e "\e[1;34m===================================\e[0m"
echo -e "\e[1;34m Backhaul Installation Script \e[0m"
echo -e "\e[1;34m By @PooryaSho \e[0m"
echo -e "\e[1;34m===================================\e[0m"
# Function to show loading spinner
show_loading() {
echo -e "Loading"
pid=$!
while ps -p $pid > /dev/null; do
echo -e "."
sleep 1
done
echo -e "\nDone!"
}
# Detect CPU architecture
ARCH=$(dpkg --print-architecture)
if [[ "$ARCH" == "arm64" ]]; then
DOWNLOAD_URL="https://github.com/Musixal/Backhaul/releases/download/v0.1.1/backhaul_linux_arm64.tar.gz"
elif [[ "$ARCH" == "amd64" ]]; then
DOWNLOAD_URL="https://github.com/Musixal/Backhaul/releases/download/v0.1.1/backhaul_linux_amd64.tar.gz"
else
echo "Unsupported architecture!"
exit 1
fi
# Server/Client Selection
echo -e "\e[1;33mSelect Server Type or Remove Backhaul:\e[0m"
echo -e "\e[1;36m1. Client [Kharej]\e[0m"
echo -e "\e[1;36m2. Server [Iran]\e[0m"
echo -e "\e[1;36m3. Remove Backhaul \e[0m"
echo -e "\e[1;36m0. Exit \e[0m"
read -p "Enter choice [1-3]: " server_type
srv_dir="/etc/systemd/system"
# Validate input
if [ "$server_type" == "0" ]; then
exit 1
fi
if [[ "$server_type" != "1" && "$server_type" != "2" && "$server_type" != "3" ]]; then
echo "Invalid choice!"
exit 1
fi
if [ "$server_type" == "3" ]; then
if find "$srv_dir" -type f -name "*.service" | grep -q .; then
rm backhaul config.toml /etc/systemd/system/backhaul.service &
show_loading
echo "Backhaul Removed"
exit 1
else
echo "Backhaul is not installed."
exit 1
fi
fi
# Prompt for details based on server type
if [ "$server_type" == "1" ]; then
# Download Backhaul
wget -q $DOWNLOAD_URL -O backhaul.tar.gz
# Decompress and move Backhaul binary to PATH
tar -zxvf backhaul.tar.gz -C /root/ >/dev/null
rm backhaul.tar.gz
chmod +x ./backhaul
client_server="client"
read -p "Enter remote address with port [0.0.0.0:3080]: " remote_addr
else
# Download Backhaul
wget -q $DOWNLOAD_URL -O backhaul.tar.gz
# Decompress and move Backhaul binary to PATH
tar -zxvf backhaul.tar.gz -C /root/ >/dev/null
rm backhaul.tar.gz
chmod +x ./backhaul
client_server="server"
read -p "Enter bind port: " bind_port
fi
# Transport Selection
echo -e "\e[1;33mSelect Transport Type:\e[0m"
echo -e "\e[1;36m1. TCP\e[0m"
echo -e "\e[1;36m2. TCPMUX\e[0m"
echo -e "\e[1;36m3. WebSocket (WS)\e[0m"
read -p "Enter choice [1-3]: " transport_choice
# Validate transport input
if [[ "$transport_choice" != "1" && "$transport_choice" != "2" && "$transport_choice" != "3" ]]; then
echo "Invalid transport type!"
exit 1
fi
# Set transport and parameters
case $transport_choice in
1) transport="tcp" ;;
2) transport="tcpmux" ;;
3) transport="ws" ;;
esac
# Prompt for token
read -p "Enter secure token: " token
# Channel pool (for TCP and WS)
if [[ "$transport" != "tcpmux" ]]; then
read -p "Enter channel pool size (default 8): " channel_pool
channel_pool=${channel_pool:-8}
fi
# Nodelay
read -p "Enable nodelay? (y/n): " nodelay_choice
if [[ "$nodelay_choice" == "y" ]]; then
nodelay=true
else
nodelay=false
fi
# Ports Configuration (for server)
if [ "$client_server" == "server" ]; then
ports=()
while true; do
read -p "Enter local port (or type 'done' to finish): " local_port
if [[ "$local_port" == "done" ]]; then
break
fi
read -p "Enter remote port: " remote_port
ports+=("\"$local_port=$remote_port\"")
done
fi
# Generate config.toml based on user inputs
config_file="/root/config.toml"
echo -e "\e[1;32mGenerating configuration...\e[0m"
if [[ "$client_server" == "client" ]]; then
echo "[client]" > $config_file
echo "remote_addr = \"$remote_addr\"" >> $config_file
else
echo "[server]" > $config_file
echo "bind_addr = \"0.0.0.0:$bind_port\"" >> $config_file
echo "ports = [" >> $config_file
for port_pair in "${ports[@]}"; do
echo " $port_pair," >> $config_file
done
echo "]" >> $config_file
fi
echo "transport = \"$transport\"" >> $config_file
echo "token = \"$token\"" >> $config_file
echo "nodelay = $nodelay" >> $config_file
if [[ "$transport" == "tcpmux" ]]; then
echo "mux_session = 1" >> $config_file
else
echo "channel_size = 2048" >> $config_file
echo "connection_pool = $channel_pool" >> $config_file
fi
# Create service file
service_file="/etc/systemd/system/backhaul.service"
echo -e "\e[1;32mCreating service file...\e[0m"
cat <<EOF > $service_file
[Unit]
Description=Backhaul Reverse Tunnel Service
After=network.target
[Service]
Type=simple
ExecStart=/root/backhaul -c /root/config.toml
Restart=always
RestartSec=3
LimitNOFILE=1048576
[Install]
WantedBy=multi-user.target
EOF
# Reload systemd and start service
echo -e "\e[1;32mSetting up Backhaul service...\e[0m"
sudo systemctl daemon-reload
sudo systemctl enable backhaul.service
sudo systemctl start backhaul.service
# Show service status
sudo systemctl status backhaul.service