Skip to content

Commit f14a2f1

Browse files
committed
Improved DNS Hostname Resolution And Caching
Resolves #325 - implement new caching system via cronjob - update dr_gateways DNS names to resolve to all available IP's - update uacreg DNS names to resolve to all available IP's - update DNS names every 5 minutes - update backend to transparently access/store JSON in description/tag fields - update all other tables to use new schema for JSON storage - move local address to cron updated entry in address table - add FLT_INTERNAL flag for internal use addresses - add/update a few utility functions to `dsip_lib.sh` - update default imports to use new JSON structure
1 parent 301cab8 commit f14a2f1

30 files changed

+955
-444
lines changed

CHANGELOG.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
[//]: # (END_SECTION HEADER)
77
[//]: # (START_SECTION COMMITS
8+
7287564593dd788bdfc5e3472cca0bd89b84a4bc
89
d8ac84aa08a89a604320df81870c18c734f8fdf0
910
3893533afbfaf9d5aedb02b45688ac94920cbd4f
1011
f8bfe45b3cc64e49d3decc0adb7a10f493ff22a0
@@ -1969,6 +1970,32 @@ a72121b9551921aa3dced32d943c6034ba318f82
19691970
ce6c5aac0db5476dc496c34388e4f9ce2c4b86e5
19701971
b46b1e64f06f448bde78b98e3ae8228ce5f96067
19711972
END_SECTION COMMITS)
1973+
[//]: # (START_SECTION 7287564593dd788bdfc5e3472cca0bd89b84a4bc)
1974+
### Improved DNS Hostname Resolution And Caching
1975+
1976+
> Commit: [7287564593dd788bdfc5e3472cca0bd89b84a4bc](https://github.com/dOpensource/dsiprouter/commit/7287564593dd788bdfc5e3472cca0bd89b84a4bc)
1977+
> Date: Thu, 1 Jul 2021 12:25:12 -0400
1978+
> Author: Tyler Moore (tmoore@goflyball.com)
1979+
> Committer: Tyler Moore (tmoore@goflyball.com)
1980+
> Signed: Tyler Moore (devopsec) <tmoore@goflyball.com>
1981+
1982+
1983+
- Resolves [#325](https://github.com/dOpensource/dsiprouter/issues/325)
1984+
- implement new caching system via cronjob
1985+
- update dr_gateways DNS names to resolve to all available IP's
1986+
- update uacreg DNS names to resolve to all available IP's
1987+
- update DNS names every 5 minutes
1988+
- update backend to transparently access/store JSON in description/tag fields
1989+
- update all other tables to use new schema for JSON storage
1990+
- move local address to cron updated entry in address table
1991+
- add FLT_INTERNAL flag for internal use addresses
1992+
- add/update a few utility functions to `dsip_lib.sh`
1993+
- update default imports to use new JSON structure
1994+
1995+
1996+
---
1997+
1998+
[//]: # (END_SECTION 7287564593dd788bdfc5e3472cca0bd89b84a4bc)
19721999
[//]: # (START_SECTION d8ac84aa08a89a604320df81870c18c734f8fdf0)
19732000
### Fix Bug In Commit 9e7949a
19742001

dsiprouter.sh

Lines changed: 210 additions & 18 deletions
Large diffs are not rendered by default.

dsiprouter/dsip_lib.sh

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,25 +170,25 @@ function decryptConfigAttrib() {
170170
}
171171
export -f decryptConfigAttrib
172172

173-
# $1 == attribute name
173+
# $1 == feature name
174174
# $2 == kamailio config file
175-
function enableKamailioConfigAttrib() {
175+
function enableKamailioConfigFeature() {
176176
local NAME="$1"
177177
local CONFIG_FILE="$2"
178178

179179
sed -i -r -e "s~#+(!(define|trydef|redefine)[[:space:]]? $NAME)~#\1~g" ${CONFIG_FILE}
180180
}
181-
export -f enableKamailioConfigAttrib
181+
export -f enableKamailioConfigFeature
182182

183-
# $1 == attribute name
183+
# $1 == feature name
184184
# $2 == kamailio config file
185-
function disableKamailioConfigAttrib() {
185+
function disableKamailioConfigFeature() {
186186
local NAME="$1"
187187
local CONFIG_FILE="$2"
188188

189189
sed -i -r -e "s~#+(!(define|trydef|redefine)[[:space:]]? $NAME)~##\1~g" ${CONFIG_FILE}
190190
}
191-
export -f disableKamailioConfigAttrib
191+
export -f disableKamailioConfigFeature
192192

193193
# $1 == name of defined url to change
194194
# $2 == value to change url to
@@ -204,6 +204,24 @@ function setKamailioConfigDburl() {
204204
}
205205
export -f setKamailioConfigDburl
206206

207+
# $1 == name of define to change
208+
# $2 ==
209+
# $3 == kamailio config file
210+
# $4 == -q (quote as string)
211+
function setKamailioConfigDef() {
212+
local NAME="$1"
213+
local VALUE="$2"
214+
local CONFIG_FILE="$3"
215+
216+
if [[ "$4" == "-q" ]]; then
217+
VALUE='"'"${VALUE}"'"'
218+
fi
219+
220+
perl -e "\$name='${NAME}'; \$value='${VALUE}';" \
221+
-i -pe 's%(#+\!)(define|trydef|redefine)([ \t]+${name}[ \t]+).*%\1\2\3${value}%g' ${CONFIG_FILE}
222+
}
223+
export -f setKamailioConfigDef
224+
207225
# $1 == name of substdef to change
208226
# $2 == value to change substdef to
209227
# $3 == kamailio config file
@@ -388,6 +406,8 @@ export -f ipv6Test
388406
# notes: prints internal ip, or empty string if not available
389407
# notes: tries ipv4 first then ipv6
390408
function getInternalIP() {
409+
local IPV6_ENABLED=${IPV6_ENABLED:-0}
410+
391411
local IP=$(ip -4 route get $GOOGLE_DNS_IPV4 2>/dev/null | head -1 | grep -oP 'src \K([^\s]+)')
392412
if (( ${IPV6_ENABLED} == 1 )) && [[ -z "$IP" ]]; then
393413
IP=$(ip -6 route get $GOOGLE_DNS_IPV6 2>/dev/null | head -1 | grep -oP 'src \K([^\s]+)')
@@ -470,11 +490,14 @@ export -f getInternalFQDN
470490
# notes: will use EXTERNAL_IP if available or look it up dynamically
471491
# notes: tries ipv4 first then ipv6
472492
function getExternalFQDN() {
493+
local IPV6_ENABLED=${IPV6_ENABLED:-0}
494+
473495
local EXTERNAL_IP=${EXTERNAL_IP:-$(getExternalIP)}
474496
local EXTERNAL_FQDN=$(dig @${GOOGLE_DNS_IPV4} +short -x ${EXTERNAL_IP} 2>/dev/null | head -1 | sed 's/\.$//')
475497
if (( ${IPV6_ENABLED} == 1 )) && [[ -z "$EXTERNAL_FQDN" ]]; then
476498
EXTERNAL_FQDN=$(dig @${GOOGLE_DNS_IPV6} +short -x ${EXTERNAL_IP} 2>/dev/null | head -1 | sed 's/\.$//')
477499
fi
500+
478501
printf '%s' "$EXTERNAL_FQDN"
479502
}
480503
export -f getExternalFQDN
@@ -483,6 +506,7 @@ export -f getExternalFQDN
483506
# notes: prints internal CIDR address, or empty string if not available
484507
# notes: tries ipv4 first then ipv6
485508
function getInternalCIDR() {
509+
local IPV6_ENABLED=${IPV6_ENABLED:-0}
486510
local PREFIX_LEN="" DEF_IFACE=""
487511
local IP=$(ip -4 route get $GOOGLE_DNS_IPV4 2>/dev/null | head -1 | grep -oP 'src \K([^\s]+)')
488512

@@ -505,6 +529,26 @@ function getInternalCIDR() {
505529
}
506530
export -f getInternalCIDR
507531

532+
# $1 == host to resolve
533+
# $2 == -a (return all resolved IPs)
534+
# output: IP address(es) of host
535+
function hostToIP() {
536+
local IPV6_ENABLED=${IPV6_ENABLED:-0}
537+
local HOST="$1"
538+
539+
local IP_ADDR=$(dig @${GOOGLE_DNS_IPV4} +short A ${HOST} 2>/dev/null)
540+
if (( ${IPV6_ENABLED} == 1 )) && [[ -z "$EXTERNAL_FQDN" ]]; then
541+
IP_ADDR=$(dig @${GOOGLE_DNS_IPV6} +short AAAA ${HOST} 2>/dev/null | head -1 | sed 's/\.$//')
542+
fi
543+
544+
if [[ "$2" == "-a" ]]; then
545+
echo -n "$IP_ADDR"
546+
else
547+
echo -n "$IP_ADDR" | head -1
548+
fi
549+
}
550+
export -f hostToIP
551+
508552
# $1 == cmd as executed in systemd (by ExecStart=)
509553
# notes: take precaution when adding long running functions as they will block startup in boot order
510554
# notes: adding init commands on an AMI instance must not be long running processes, otherwise they will fail

0 commit comments

Comments
 (0)