-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.sh
More file actions
executable file
·137 lines (124 loc) · 4.89 KB
/
Copy pathmain.sh
File metadata and controls
executable file
·137 lines (124 loc) · 4.89 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
#!/usr/bin/env bash
#
# Copyright (C) 2023 raph521
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <https://www.gnu.org/licenses/>.
#
set -e
airdcpp_username="${AIRDCPP_USERNAME:-admin}"
airdcpp_password="${AIRDCPP_PASSWORD:-password}"
airdcpp_addr="${AIRDCPP_ADDR:-http://localhost:5600}" # ex. http://10.0.1.48:5600
gtn_addr="${GTN_ADDR:-http://localhost:8000}" # ex. http://10.0.1.48:8000
log_every_check=${LOG_EVERY_CHECK:-false}
print_datetime () {
printf "\n[$(date +'%Y-%m-%d %H:%M:%S %z')]\n"
}
#
# Get public IP and forwarded port from Gluetun
#
gtn_port_number=$(curl --fail --silent --show-error ${gtn_addr}/v1/portforward | jq '.port')
gtn_ip_address=$(curl --fail --silent --show-error ${gtn_addr}/v1/publicip/ip | jq --raw-output '.public_ip')
if [ ! "$gtn_port_number" ] || [ "$gtn_port_number" = "0" ]; then
print_datetime
echo "Could not get current forwarded port from gluetun, exiting..."
exit 0
fi
if [ ! "$gtn_ip_address" ] || [ "$gtn_ip_address" = "" ]; then
print_datetime
echo "Could not get public IP address from gluetun, exiting..."
exit 0
fi
#
# Get current settings from AirDC++
#
current_airdcpp_settings=$(curl --fail --silent --show-error \
--header "Content-Type: application/json" \
--request POST \
--user ${airdcpp_username}:${airdcpp_password} \
--data-binary \
"{ \"keys\":
[\"tcp_port\",
\"udp_port\",
\"tls_port\",
\"connection_ip_v4\",
\"tls_mode\"] }" \
${airdcpp_addr}/api/v1/settings/get)
tcp_port=$(echo $current_airdcpp_settings | jq --raw-output '.tcp_port')
udp_port=$(echo $current_airdcpp_settings | jq --raw-output '.udp_port')
tls_mode=$(echo $current_airdcpp_settings | jq --raw-output '.tls_mode')
ip_address=$(echo $current_airdcpp_settings | jq --raw-output '.connection_ip_v4')
#
# Check if Gluetun's port and IP matches AirDC++'s
#
if test "$gtn_port_number" = "$tcp_port" && \
test "$gtn_port_number" = "$udp_port" && \
test "$tls_mode" = "0" && \
test "$gtn_ip_address" = "$ip_address"; then
if [ "$log_every_check" = true ]; then
print_datetime
echo "Port and address already set"
echo " - tcp_port : $gtn_port_number"
echo " - udp_port : $gtn_port_number"
echo " - tls_mode : $tls_mode (0 = Disabled, 1 = Enabled)"
echo " - connection_ip_v4 : $gtn_ip_address"
fi
# Nothing to update, exit
exit 0
fi
#
# AirDC++'s settings are out of date, update the settings
#
airdcpp_settings_update_result=$(curl --fail --silent --show-error \
--write-out '%{http_code}' \
--header "Content-Type: application/json" \
--request POST \
--user ${airdcpp_username}:${airdcpp_password} \
--data-binary \
"{ \"tcp_port\": ${gtn_port_number},
\"udp_port\": ${gtn_port_number},
\"tls_mode\": 0,
\"connection_ip_v4\": \"${gtn_ip_address}\" }" \
${airdcpp_addr}/api/v1/settings/set)
#
# Check if AirDC++'s settings were updated
#
if test "$airdcpp_settings_update_result" = "204"; then
print_datetime
echo "AirDC++ settings updated"
echo " - tcp_port : $tcp_port -> $gtn_port_number"
echo " - udp_port : $udp_port -> $gtn_port_number"
echo " - tls_mode : $tls_mode -> 0 (0 = Disabled; 1,2 = Enabled)"
echo " - connection_ip_v4 : $ip_address -> $gtn_ip_address"
if [ -n "$DISCORD_WEBHOOK_URL" ]; then
/discord-sh/discord.sh \
--webhook-url "$DISCORD_WEBHOOK_URL" \
--username "AirDC++/Gluetun Sync" \
--title "AirDC++ Settings Update SUCCESS" \
--field "tcp_port;$tcp_port -> $gtn_port_number" \
--field "udp_port;$udp_port -> $gtn_port_number" \
--field "tls_mode;$tls_mode -> 0" \
--field "connection_ip_v4;$ip_address -> $gtn_ip_address" \
--timestamp
fi
else
print_datetime
echo "AirDC++ settings were not updated"
echo " - failed with result '$airdcpp_settings_update_result'"
if [ -n "$DISCORD_WEBHOOK_URL" ]; then
/discord-sh/discord.sh \
--webhook-url "$DISCORD_WEBHOOK_URL" \
--username "AirDC++/Gluetun Sync" \
--title "AirDC++ Settings Update FAILURE" \
--field "Result Code;$airdcpp_settings_update_result" \
--timestamp
fi
fi