-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathiran-docker.sh
More file actions
executable file
·291 lines (247 loc) · 8.86 KB
/
Copy pathiran-docker.sh
File metadata and controls
executable file
·291 lines (247 loc) · 8.86 KB
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
#!/bin/bash
# Iran Docker Management Script
# Author: https://github.com/Linuxmaster14
RED='\033[1;31m'
GREEN='\033[1;32m'
CYAN='\033[1;36m'
NC='\033[0m'
# Check if script is running with root privileges
if [ "$EUID" -ne 0 ]; then
echo -e "${RED}Error:${NC} Please run this script as root (use sudo)"
exit 1
fi
# Main menu options
main_menu_items=("Set DNS" "Install Docker" "Update Docker" "Set Docker Proxy" "Exit")
# Available DNS providers and their servers
dns_options=("Shecan" "Shecan Pro" "Radar" "Electro" "Begzar" "DNSPro" "403" "Bertina" "DynX" "Google" "Cloudflare" "Reset to Default")
declare -A dns_servers=(
["Shecan"]="178.22.122.100 185.51.200.2"
["Shecan Pro"]="178.22.122.101 185.51.200.1"
["Radar"]="10.202.10.10 10.202.10.11"
["Electro"]="78.157.42.100 78.157.42.101"
["Begzar"]="185.55.226.26 185.55.226.25"
["DNSPro"]="87.107.110.109 87.107.110.110"
["403"]="10.202.10.202 10.202.10.102"
["Bertina"]="193.186.32.32"
["DynX"]="10.70.95.150 10.70.95.162"
["Google"]="8.8.8.8 8.8.4.4"
["Cloudflare"]="1.1.1.1 1.0.0.1"
["Reset to Default"]="127.0.0.53"
)
# Iranian Docker registry mirrors
registry_proxies=(
"docker.kernel.ir"
"focker.ir"
"registry.docker.ir"
"docker.arvancloud.ir"
"docker.haiocloud.com"
"docker.iranserver.com"
"docker.mobinhost.com"
"hub.mecan.ir"
"docker.nrp.co"
"docker-mirror.liara.ir"
"ghcr-mirror.liara.ir"
"mirrors.pardisco.co"
"mirror2.chabokan.net"
"docker.abrha.net"
"mirror-docker.runflare.com"
)
# DNS Management Functions
backup_resolv() {
cp /etc/resolv.conf "/etc/resolv.conf.bak.$(date +%Y%m%d_%H%M%S)"
}
ping_proxy() {
local domain="$1"
local times=($(ping -c 3 -W 1 "$domain" 2>/dev/null | grep -oP 'time=\K[0-9.]+'))
if [ ${#times[@]} -gt 0 ]; then
printf '%s\n' "${times[@]}" | sort -n | head -1
else
echo "timeout"
fi
}
# Set DNS servers based on user selection
set_dns() {
echo
for i in "${!dns_options[@]}"; do
echo " $((i + 1))) ${dns_options[$i]}"
done
echo " 0) Back"
echo
read -rp "Select DNS (0-${#dns_options[@]}): " dns_choice
if [[ "$dns_choice" == "0" ]]; then return; fi
if [[ ! "$dns_choice" =~ ^[0-9]+$ ]] || (( dns_choice < 1 || dns_choice > ${#dns_options[@]} )); then
echo -e "${RED}Invalid DNS option${NC}"; return
fi
provider="${dns_options[$((dns_choice - 1))]}"
dns_list="${dns_servers[$provider]}"
# Create backup before making changes
backup_resolv
# Write new DNS configuration
{
echo "# DNS updated on $(date) by iran-docker script"
for dns in $dns_list; do
echo "nameserver $dns"
done
if [ "$provider" != "Reset to Default" ]; then
echo "options edns0 trust-ad"
grep "^search" /etc/resolv.conf 2>/dev/null
fi
} > /etc/resolv.conf
echo -e "${GREEN}DNS updated to:${NC} $provider"
}
# Docker Management Functions
install_docker() {
# Check if Docker is already installed
if command -v docker >/dev/null 2>&1; then
version=$(docker version --format '{{.Server.Version}}' 2>/dev/null)
if [ -n "$version" ]; then
echo -e "${GREEN}Docker is already installed. Version: $version${NC}"
return
fi
fi
echo -e "${CYAN}Starting Docker installation...${NC}"
# Update package lists
echo -e "${GREEN}Updating package lists...${NC}"
if ! apt-get update; then
echo -e "${RED}Error:${NC} Failed to update package lists."
return 1
fi
# Upgrade system packages
echo -e "${GREEN}Upgrading system packages...${NC}"
if ! apt-get upgrade -y; then
echo -e "${RED}Error:${NC} Failed to upgrade packages."
return 1
fi
# Install required dependencies
echo -e "${GREEN}Installing required packages (curl)...${NC}"
if ! apt-get install -y curl; then
echo -e "${RED}Error:${NC} Failed to install prerequisites."
return 1
fi
# Download and run Docker installation script
echo -e "${GREEN}Downloading and installing Docker...${NC}"
if ! curl -fsSL https://get.docker.com | sh; then
echo -e "${RED}Error:${NC} Docker installation script failed."
return 1
fi
# Verify Docker installation
if ! command -v docker >/dev/null 2>&1; then
echo -e "${RED}Error:${NC} Docker command not found after installation."
return 1
fi
# Get and display Docker version
version=$(docker version --format '{{.Server.Version}}' 2>/dev/null)
if [ -z "$version" ]; then
echo -e "${RED}Error:${NC} Could not retrieve Docker version."
return 1
fi
echo -e "${GREEN}Docker installed successfully. Version: $version${NC}"
}
# Update Docker to the latest version
update_docker() {
# Check if Docker is installed
if ! command -v docker >/dev/null 2>&1; then
echo -e "${RED}Docker is not installed.${NC} Please install it first."
return
fi
echo -e "${CYAN}Updating Docker to the latest version...${NC}"
# Download and run Docker installation script (also works for updates)
echo -e "${GREEN}Downloading and installing Docker...${NC}"
if ! curl -fsSL https://get.docker.com | sh >/dev/null 2>&1; then
echo -e "${RED}Error:${NC} Docker update script failed."
return 1
fi
# Display updated version
version=$(docker version --format '{{.Server.Version}}' 2>/dev/null)
echo -e "${GREEN}Docker updated successfully. Version: $version${NC}"
}
# Find fastest proxy automatically
find_fastest_proxy() {
echo -e "${CYAN}Testing proxies for best performance...${NC}" >&2
local best_proxy=""
local best_time="999999"
for proxy in "${registry_proxies[@]}"; do
printf "Testing %s... " "$proxy" >&2
ping_time=$(ping_proxy "$proxy")
if [[ "$ping_time" != "timeout" ]]; then
echo "${ping_time}ms" >&2
# Compare ping times using awk (standard on most systems) to handle floats
if (( $(awk -v p="$ping_time" -v b="$best_time" 'BEGIN {if (p < b) print 1; else print 0}') )); then
best_time="$ping_time"
best_proxy="$proxy"
fi
else
echo "timeout" >&2
fi
done
if [[ -n "$best_proxy" ]]; then
echo >&2
echo -e "${GREEN}Fastest proxy found:${NC} $best_proxy (${best_time}ms)" >&2
echo "$best_proxy"
else
echo -e "${RED}No responsive proxies found${NC}" >&2
return 1
fi
}
# Configure Docker to use Iranian registry mirrors
set_docker_proxy() {
echo
echo -e "${CYAN}Select Docker Registry Mirror:${NC}"
echo " 00) Auto-select fastest proxy"
for i in "${!registry_proxies[@]}"; do
echo " $((i + 1))) https://${registry_proxies[$i]}"
done
echo " 0) Back"
echo
read -rp "Select (0=Back, 00=Auto, 1-${#registry_proxies[@]}): " proxy_choice
if [[ "$proxy_choice" == "0" ]]; then return; fi
if [[ "$proxy_choice" == "00" ]]; then
# Auto-select fastest proxy
fastest_output=$(find_fastest_proxy)
mirror=$(echo "$fastest_output" | tail -n1)
if [[ -z "$mirror" ]] || [[ "$mirror" == *"No responsive proxies found"* ]]; then
echo -e "${RED}Failed to find fastest proxy${NC}"
return
fi
else
if [[ ! "$proxy_choice" =~ ^[0-9]+$ ]] || (( proxy_choice < 1 || proxy_choice > ${#registry_proxies[@]} )); then
echo -e "${RED}Invalid option${NC}"; return
fi
mirror="${registry_proxies[$((proxy_choice - 1))]}"
fi
# Create Docker configuration directory
mkdir -p /etc/docker
# Create daemon.json with registry mirror configuration
echo -e "{\n \"registry-mirrors\": [\"https://$mirror\"]\n}" > /etc/docker/daemon.json
# Restart Docker service to apply changes
echo -e "${CYAN}Restarting Docker service...${NC}"
if systemctl restart docker; then
echo -e "${GREEN}Docker service restarted successfully${NC}"
else
echo -e "${RED}Warning:${NC} Docker service restart failed. Configuration saved but may need manual restart."
echo -e "${CYAN}Try running:${NC} sudo systemctl restart docker"
fi
echo -e "${GREEN}Docker proxy set to:${NC} $mirror"
}
# Main Menu and Program Entry Point
main_menu() {
while true; do
echo
echo -e "${CYAN}Main Menu:${NC}"
for i in "${!main_menu_items[@]}"; do
echo " $((i + 1))) ${main_menu_items[$i]}"
done
echo
read -rp "Select an option (1-${#main_menu_items[@]}): " choice
case "$choice" in
1) set_dns ;;
2) install_docker ;;
3) update_docker ;;
4) set_docker_proxy ;;
5) echo -e "${CYAN}Bye.${NC}"; exit 0 ;;
*) echo -e "${RED}Invalid choice${NC}" ;;
esac
done
}
# Start the program
main_menu