Skip to content

Commit

Permalink
Improved DNS Hostname Resolution And Caching
Browse files Browse the repository at this point in the history
  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`
  • Loading branch information
devopsec committed May 3, 2021
1 parent dccb7db commit df8487e
Show file tree
Hide file tree
Showing 27 changed files with 816 additions and 305 deletions.
34 changes: 30 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

[//]: # (END_SECTION HEADER)
[//]: # (START_SECTION COMMITS
4de26cfe35cbff4ed9ada409a27f029534338e9d
bc4571b985af26248a5362932963b5c6cc5b3326
dccb7db03c6f0f3ae7b2564174c848a262c15d92
2c1e9d5bd1ae3c8bb20829b50831a43314fb5546
3860b0e3a3d786bb94cdeb7d03b6535540514367
70f7542b32a5df58c7d04bbdf2100d91950f8bc2
Expand Down Expand Up @@ -1938,10 +1939,35 @@ a72121b9551921aa3dced32d943c6034ba318f82
ce6c5aac0db5476dc496c34388e4f9ce2c4b86e5
b46b1e64f06f448bde78b98e3ae8228ce5f96067
END_SECTION COMMITS)
[//]: # (START_SECTION 4de26cfe35cbff4ed9ada409a27f029534338e9d)
[//]: # (START_SECTION bc4571b985af26248a5362932963b5c6cc5b3326)
### Improved DNS Hostname Resolution And Caching

> Commit: [bc4571b985af26248a5362932963b5c6cc5b3326](https://github.com/dOpensource/dsiprouter/commit/bc4571b985af26248a5362932963b5c6cc5b3326)
> Date: Mon, 3 May 2021 17:17:41 -0400
> Author: Tyler Moore ([email protected])
> Committer: Tyler Moore ([email protected])
> Signed: Tyler Moore (devopsec) <[email protected]>


- Resolves [#325](https://github.com/dOpensource/dsiprouter/issues/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`


---

[//]: # (END_SECTION bc4571b985af26248a5362932963b5c6cc5b3326)
[//]: # (START_SECTION dccb7db03c6f0f3ae7b2564174c848a262c15d92)
### Permissions And Sources Bug Fixes

> Commit: [4de26cfe35cbff4ed9ada409a27f029534338e9d](https://github.com/dOpensource/dsiprouter/commit/4de26cfe35cbff4ed9ada409a27f029534338e9d)
> Commit: [dccb7db03c6f0f3ae7b2564174c848a262c15d92](https://github.com/dOpensource/dsiprouter/commit/dccb7db03c6f0f3ae7b2564174c848a262c15d92)
> Date: Mon, 26 Apr 2021 21:22:18 -0400
> Author: Tyler Moore ([email protected])
> Committer: Tyler Moore ([email protected])
Expand All @@ -1961,7 +1987,7 @@ END_SECTION COMMITS)

---

[//]: # (END_SECTION 4de26cfe35cbff4ed9ada409a27f029534338e9d)
[//]: # (END_SECTION dccb7db03c6f0f3ae7b2564174c848a262c15d92)
[//]: # (START_SECTION 2c1e9d5bd1ae3c8bb20829b50831a43314fb5546)
### Misc Bug Fixes

Expand Down
220 changes: 206 additions & 14 deletions dsiprouter.sh

Large diffs are not rendered by default.

56 changes: 50 additions & 6 deletions dsiprouter/dsip_lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -170,25 +170,25 @@ function decryptConfigAttrib() {
}
export -f decryptConfigAttrib

# $1 == attribute name
# $1 == feature name
# $2 == kamailio config file
function enableKamailioConfigAttrib() {
function enableKamailioConfigFeature() {
local NAME="$1"
local CONFIG_FILE="$2"

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

# $1 == attribute name
# $1 == feature name
# $2 == kamailio config file
function disableKamailioConfigAttrib() {
function disableKamailioConfigFeature() {
local NAME="$1"
local CONFIG_FILE="$2"

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

# $1 == name of defined url to change
# $2 == value to change url to
Expand All @@ -204,6 +204,24 @@ function setKamailioConfigDburl() {
}
export -f setKamailioConfigDburl

# $1 == name of define to change
# $2 ==
# $3 == kamailio config file
# $4 == -q (quote as string)
function setKamailioConfigDef() {
local NAME="$1"
local VALUE="$2"
local CONFIG_FILE="$3"

if [[ "$4" == "-q" ]]; then
VALUE='"'"${VALUE}"'"'
fi

perl -e "\$name='${NAME}'; \$value='${VALUE}';" \
-i -pe 's%(#+\!)(define|trydef|redefine)([ \t]+${name}[ \t]+).*%\1\2\3${value}%g' ${CONFIG_FILE}
}
export -f setKamailioConfigDef

# $1 == name of substdef to change
# $2 == value to change substdef to
# $3 == kamailio config file
Expand Down Expand Up @@ -388,6 +406,8 @@ export -f ipv6Test
# notes: prints internal ip, or empty string if not available
# notes: tries ipv4 first then ipv6
function getInternalIP() {
local IPV6_ENABLED=${IPV6_ENABLED:-0}

local IP=$(ip -4 route get $GOOGLE_DNS_IPV4 2>/dev/null | head -1 | grep -oP 'src \K([^\s]+)')
if (( ${IPV6_ENABLED} == 1 )) && [[ -z "$IP" ]]; then
IP=$(ip -6 route get $GOOGLE_DNS_IPV6 2>/dev/null | head -1 | grep -oP 'src \K([^\s]+)')
Expand Down Expand Up @@ -470,11 +490,14 @@ export -f getInternalFQDN
# notes: will use EXTERNAL_IP if available or look it up dynamically
# notes: tries ipv4 first then ipv6
function getExternalFQDN() {
local IPV6_ENABLED=${IPV6_ENABLED:-0}

local EXTERNAL_IP=${EXTERNAL_IP:-$(getExternalIP)}
local EXTERNAL_FQDN=$(dig @${GOOGLE_DNS_IPV4} +short -x ${EXTERNAL_IP} 2>/dev/null | head -1 | sed 's/\.$//')
if (( ${IPV6_ENABLED} == 1 )) && [[ -z "$EXTERNAL_FQDN" ]]; then
EXTERNAL_FQDN=$(dig @${GOOGLE_DNS_IPV6} +short -x ${EXTERNAL_IP} 2>/dev/null | head -1 | sed 's/\.$//')
fi

printf '%s' "$EXTERNAL_FQDN"
}
export -f getExternalFQDN
Expand All @@ -483,6 +506,7 @@ export -f getExternalFQDN
# notes: prints internal CIDR address, or empty string if not available
# notes: tries ipv4 first then ipv6
function getInternalCIDR() {
local IPV6_ENABLED=${IPV6_ENABLED:-0}
local PREFIX_LEN="" DEF_IFACE=""
local IP=$(ip -4 route get $GOOGLE_DNS_IPV4 2>/dev/null | head -1 | grep -oP 'src \K([^\s]+)')

Expand All @@ -505,6 +529,26 @@ function getInternalCIDR() {
}
export -f getInternalCIDR

# $1 == host to resolve
# $2 == -a (return all resolved IPs)
# output: IP address(es) of host
function hostToIP() {
local IPV6_ENABLED=${IPV6_ENABLED:-0}
local HOST="$1"

local IP_ADDR=$(dig @${GOOGLE_DNS_IPV4} +short A ${HOST} 2>/dev/null)
if (( ${IPV6_ENABLED} == 1 )) && [[ -z "$EXTERNAL_FQDN" ]]; then
IP_ADDR=$(dig @${GOOGLE_DNS_IPV6} +short AAAA ${HOST} 2>/dev/null | head -1 | sed 's/\.$//')
fi

if [[ "$2" == "-a" ]]; then
echo -n "$IP_ADDR"
else
echo -n "$IP_ADDR" | head -1
fi
}
export -f hostToIP

# $1 == cmd as executed in systemd (by ExecStart=)
# notes: take precaution when adding long running functions as they will block startup in boot order
# notes: adding init commands on an AMI instance must not be long running processes, otherwise they will fail
Expand Down
Loading

0 comments on commit df8487e

Please sign in to comment.