-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspawn.sh
76 lines (68 loc) · 2.46 KB
/
spawn.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
#!/bin/bash
vpn_location=$1
vpn_provider=$2
starting_port=$3
vpn_username=$4
vpn_password=$5
container_restart=$6
network_cidr=$7
ovpn_list=./ovpn_list
existing_containers=$(docker ps -a --filter "name=haugene-transmission-openvpn" --format "{{.Names}}")
main() {
vpn_location=$(trim_extension "$vpn_location") # location with the extension ".ovpn" removed
if [[ "$vpn_location" = "list" ]]; then
if [ -e "$ovpn_list" ]; then
dos2unix "$ovpn_list" # convert possible file crlf to lf
sed -i '/^$/d' "$ovpn_list" # remove all empty lines
sed -i 's/\.ovpn//g' "$ovpn_list" # remove all ".ovpn" substrings
echo "" >>"$ovpn_list" # Add EOF
echo "Found a list with $(wc -l <"$ovpn_list") vpn's."
cat "$ovpn_list"
while read line; do
if ! [[ "$existing_containers" =~ $line ]]; then
echo ""
echo "Creating container for $line"
create_container "$line"
else
echo "Skipping creation of $line. Equally named container already exists."
fi
done <"$ovpn_list"
else
echo "No ovpn_list file found. Exiting."
return 1
fi
else
echo "Creating proxy container for location $vpn_location"
create_container "$vpn_location"
fi
}
create_container() {
ports_in_use=$(docker ps --format "{{.Ports}}" | cut -d ':' -f2 | cut -d '-' -f 1 | cut -d '/' -f 1)
while [[ $ports_in_use =~ $starting_port ]]; do
echo "Port $starting_port already in use, trying next port."
((starting_port++))
done
vpn_name=$1
echo "Configuring container for $vpn_provider"
docker run \
--cap-add=NET_ADMIN \
-d \
-e "LOCAL_NETWORK=$network_cidr" \
-e "OPENVPN_USERNAME=$vpn_username" \
-e "OPENVPN_PASSWORD=$vpn_password" \
-e "OPENVPN_PROVIDER=$vpn_provider" \
-e "OPENVPN_CONFIG=$vpn_name" \
-e "WEBPROXY_ENABLED=true" \
-e "WEBPROXY_PORT=8118" \
--name="haugene-transmission-openvpn-proxy-$vpn_name" \
-p "$starting_port:8118" \
--restart "$container_restart" \
haugene/transmission-openvpn:latest
echo "Port mapped to this http proxy: $starting_port"
((starting_port++))
}
trim_extension() {
stripped_name=$(echo "$1" | sed 's/.ovpn$//')
echo "$stripped_name"
}
main