-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathupdateProxies.sh
89 lines (75 loc) · 2.4 KB
/
updateProxies.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
#!/bin/bash
#Script to update proxies and restart instances if proxies are updated in proxies.txt
containers_file="containernames.txt"
proxies_file="proxies.txt"
tun_containers_file="tuncontainers.txt"
updated_proxies_file="updatedproxies.txt"
if [ -f $tun_containers_file ]; then
rm $tun_containers_file
fi
if [ -f $updated_proxies_file ]; then
rm $updated_proxies_file
fi
if [ ! -f $containers_file ]; then
echo "$containers_file file does not exist. Exiting.."
exit 1
fi
if [ ! -f $proxies_file ]; then
echo "$proxies_file file does not exist. Exiting.."
exit 1
fi
# Remove special characters ^M from proxies file
sed -i 's/\r//g' $proxies_file
# Get tun containers and stop other containers
for container in `cat $containers_file`
do
if sudo docker inspect $container >/dev/null 2>&1; then
container_image=`sudo docker inspect --format='{{.Config.Image}}' $container`
if [[ $container_image == "xjasonlyu/tun2socks"* ]]; then
echo $container | tee -a $tun_containers_file
fi
fi
done
# Store formatted proxies in a new file
while IFS= read -r line || [ -n "$line" ]; do
if [[ "$line" =~ ^[^#].* ]]; then
i=`expr $i + 1`
echo $line | tee -a $updated_proxies_file
fi
done < $proxies_file
if [ ! -f $tun_containers_file ]; then
echo "$tun_containers_file file does not exist. Exiting.."
exit 1
fi
if [ ! -f $updated_proxies_file ]; then
echo "$updated_proxies_file file does not exist. Exiting.."
exit 1
fi
# Match the number of containers with proxies
if [ `cat $tun_containers_file|wc -l` == `cat $updated_proxies_file|wc -l` ]; then
echo "Updating Proxies"
while read container_id <&3 && read container_proxy <&4; do
container_image=`sudo docker inspect --format='{{.Config.Image}}' $container_id`
if [[ $container_image == "xjasonlyu/tun2socks"* ]]; then
sudo docker exec $container_id sh -c "sed -i \"\#--proxy#s#.*# --proxy ${container_proxy//\//\\\\/} \\\\\#\" entrypoint.sh"
fi
done 3<$tun_containers_file 4<$updated_proxies_file
else
echo "Number of containers do not match proxies. Exiting.."
exit 1
fi
# Stop all containers
for container in `cat $containers_file`
do
if sudo docker inspect $container >/dev/null 2>&1; then
sudo docker stop $container
fi
done
echo "Waiting for 5 seconds before starting"
sleep 5
# Restart/Start all containers
echo "Restarting Containers"
for container in `cat $containers_file`
do
sudo docker restart $container
done