2017-07-05 01:24:52 +00:00
|
|
|
#!/bin/bash
|
2017-07-21 22:48:24 +00:00
|
|
|
#/--------------------------------------------------------------------------------------------------------| www.vdm.io |------/
|
|
|
|
#
|
|
|
|
# @version 1.0.0
|
2020-02-16 23:15:34 +00:00
|
|
|
# @build 16th Feb, 2020
|
|
|
|
# @package setninal
|
2017-07-21 22:48:24 +00:00
|
|
|
# @author Llewellyn van der Merwe <https://github.com/Llewellynvdm>
|
2020-02-16 23:15:34 +00:00
|
|
|
# @copyright Copyright (C) 2020. All Rights Reserved
|
2017-07-21 22:48:24 +00:00
|
|
|
# @license GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html
|
|
|
|
#
|
|
|
|
#/-----------------------------------------------------------------------------------------------------------------------------/
|
2017-07-05 00:34:40 +00:00
|
|
|
|
2017-07-21 22:48:24 +00:00
|
|
|
############################ GLOBAL ##########################
|
2020-02-24 22:14:41 +00:00
|
|
|
ACTION="setdata"
|
|
|
|
OWNER="sentinel-mx"
|
|
|
|
NAME="client"
|
|
|
|
HOST="https://sentinel.our.mx/"
|
|
|
|
######### DUE TONOT BEING ABLE TO INCLUDE DYNAMIC ###########
|
2017-07-21 22:48:24 +00:00
|
|
|
|
|
|
|
#################### UPDATE TO YOUR NEEDS ####################
|
|
|
|
##############################################################
|
|
|
|
############## ##########
|
|
|
|
############## CONFIG ##########
|
|
|
|
############## ##########
|
|
|
|
##############################################################
|
2020-02-16 23:15:34 +00:00
|
|
|
REPOURL="https://raw.githubusercontent.com/${OWNER}/${NAME}/master/"
|
|
|
|
VDMIPSERVER="${HOST}/${ACTION}"
|
2017-07-21 22:48:24 +00:00
|
|
|
|
|
|
|
##############################################################
|
|
|
|
############## ##########
|
|
|
|
############## MAIN ##########
|
|
|
|
############## ##########
|
|
|
|
##############################################################
|
|
|
|
function main () {
|
2017-07-25 08:47:59 +00:00
|
|
|
## set time for this run
|
|
|
|
echoTweak "$ACTION on $Datetimenow"
|
|
|
|
echo "started"
|
2017-07-21 22:48:24 +00:00
|
|
|
## make sure cron is set
|
|
|
|
setCron
|
|
|
|
## get the local server key
|
|
|
|
getLocalKey
|
|
|
|
## check access (set if not ready)
|
|
|
|
setAccessToken
|
2020-02-16 23:15:34 +00:00
|
|
|
## update Data
|
|
|
|
setData
|
2017-07-05 00:34:40 +00:00
|
|
|
}
|
|
|
|
|
2017-07-21 22:48:24 +00:00
|
|
|
##############################################################
|
|
|
|
############## ##########
|
|
|
|
############## DEFAULTS ##########
|
|
|
|
############## ##########
|
|
|
|
##############################################################
|
2017-07-25 08:47:59 +00:00
|
|
|
Datetimenow=$(TZ=":ZULU" date +"%m/%d/%Y @ %R (UTC)" )
|
2017-07-21 22:48:24 +00:00
|
|
|
VDMUSER=$(whoami)
|
|
|
|
VDMHOME=~/
|
|
|
|
VDMSCRIPT="${REPOURL}$ACTION.sh"
|
|
|
|
VDMSERVERKEY=''
|
2017-07-05 00:34:40 +00:00
|
|
|
TRUE=1
|
|
|
|
|
2017-07-21 22:48:24 +00:00
|
|
|
##############################################################
|
|
|
|
############## ##########
|
|
|
|
############## FUNCTIONS ##########
|
|
|
|
############## ##########
|
|
|
|
##############################################################
|
2017-07-05 00:34:40 +00:00
|
|
|
|
2017-07-25 08:47:59 +00:00
|
|
|
# little repeater
|
|
|
|
function repeat () {
|
|
|
|
head -c $1 < /dev/zero | tr '\0' $2
|
|
|
|
}
|
|
|
|
|
|
|
|
# little echo tweak
|
|
|
|
function echoTweak () {
|
|
|
|
echoMessage="$1"
|
|
|
|
mainlen="$2"
|
|
|
|
characters="$3"
|
|
|
|
if [ $# -lt 2 ]
|
|
|
|
then
|
|
|
|
mainlen=60
|
|
|
|
fi
|
|
|
|
if [ $# -lt 3 ]
|
|
|
|
then
|
|
|
|
characters='\056'
|
|
|
|
fi
|
|
|
|
chrlen="${#echoMessage}"
|
|
|
|
increaseBy=$((mainlen-chrlen))
|
|
|
|
tweaked=$(repeat "$increaseBy" "$characters")
|
|
|
|
echo -n "$echoMessage$tweaked"
|
|
|
|
}
|
|
|
|
|
2017-07-05 00:34:40 +00:00
|
|
|
# Set cronjob without removing existing
|
2017-07-21 22:48:24 +00:00
|
|
|
function setCron () {
|
|
|
|
if [ -f $VDMHOME/$ACTION.cron ]; then
|
2017-07-25 08:47:59 +00:00
|
|
|
echoTweak "Crontab already configured for updates..."
|
|
|
|
echo "Skipping"
|
2017-07-21 22:48:24 +00:00
|
|
|
else
|
2017-07-25 08:47:59 +00:00
|
|
|
echoTweak "Adding crontab entry for continued updates..."
|
2017-07-21 22:48:24 +00:00
|
|
|
# check if user crontab is set
|
|
|
|
currentCron=$(crontab -u $VDMUSER -l 2>/dev/null)
|
|
|
|
if [[ -z "${currentCron// }" ]]; then
|
|
|
|
currentCron="# VDM crontab settings"
|
|
|
|
echo "$currentCron" > $VDMHOME/$ACTION.cron
|
|
|
|
else
|
|
|
|
echo "$currentCron" > $VDMHOME/$ACTION.cron
|
|
|
|
fi
|
|
|
|
# check if the MAILTO is already set
|
|
|
|
if [[ $currentCron != *"MAILTO"* ]]; then
|
|
|
|
echo "MAILTO=\"\"" >> $VDMHOME/$ACTION.cron
|
|
|
|
echo "" >> $VDMHOME/$ACTION.cron
|
|
|
|
fi
|
|
|
|
# check if the @reboot curl -s $VDMSCRIPT | sudo bash is already set
|
2017-07-21 23:12:17 +00:00
|
|
|
if [[ $currentCron != *"@reboot curl -s $VDMSCRIPT | bash"* ]]; then
|
|
|
|
echo "@reboot curl -s $VDMSCRIPT | bash" >> $VDMHOME/$ACTION.cron
|
2017-07-21 22:48:24 +00:00
|
|
|
fi
|
|
|
|
# check if the @reboot curl -s $VDMSCRIPT | sudo bash is already set
|
2017-07-25 11:36:58 +00:00
|
|
|
if [[ $currentCron != *"* * * * * curl -s $VDMSCRIPT | bash"* ]]; then
|
|
|
|
echo "* * * * * curl -s $VDMSCRIPT | bash" >> $VDMHOME/$ACTION.cron
|
2017-07-21 22:48:24 +00:00
|
|
|
fi
|
|
|
|
# set the user cron
|
|
|
|
crontab -u $VDMUSER $VDMHOME/$ACTION.cron
|
|
|
|
echo "Done"
|
2017-07-05 00:34:40 +00:00
|
|
|
fi
|
2017-07-21 22:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getKey () {
|
|
|
|
# simple basic random
|
|
|
|
echo $(tr -dc 'A-HJ-NP-Za-km-z2-9' < /dev/urandom | dd bs=128 count=1 status=none)
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLocalKey () {
|
|
|
|
# Set update key
|
|
|
|
if [ -f $VDMHOME/$ACTION.key ]; then
|
2017-07-25 08:47:59 +00:00
|
|
|
echoTweak "Update key already set!"
|
|
|
|
echo "continue"
|
2017-07-21 22:48:24 +00:00
|
|
|
else
|
2017-07-25 08:47:59 +00:00
|
|
|
echoTweak "Setting the update key..."
|
2017-07-21 22:48:24 +00:00
|
|
|
echo $(getKey) > $VDMHOME/$ACTION.key
|
|
|
|
echo "Done"
|
2017-07-05 00:34:40 +00:00
|
|
|
fi
|
|
|
|
|
2017-07-21 22:48:24 +00:00
|
|
|
# Get update key
|
|
|
|
VDMSERVERKEY=$(<"$VDMHOME/$ACTION.key")
|
|
|
|
}
|
2017-07-05 00:34:40 +00:00
|
|
|
|
2017-07-21 22:48:24 +00:00
|
|
|
function setAccessToken () {
|
|
|
|
# check if vdm access was set
|
|
|
|
accessToke=$(curl -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36" -H "VDM-KEY: $VDMSERVERKEY" --silent $VDMIPSERVER)
|
2017-07-05 00:34:40 +00:00
|
|
|
|
2017-07-21 22:48:24 +00:00
|
|
|
if [[ "$accessToke" != "$TRUE" ]]; then
|
|
|
|
read -s -p "Please enter your VDM access key: " vdmAccessKey
|
|
|
|
echo ""
|
2017-07-25 08:47:59 +00:00
|
|
|
echoTweak "One moment while we set your access to the VDM system..."
|
2017-07-21 22:48:24 +00:00
|
|
|
resultAccess=$(curl -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36" -H "VDM-TRUST: $vdmAccessKey" -H "VDM-KEY: $VDMSERVERKEY" --silent $VDMIPSERVER)
|
|
|
|
if [[ "$resultAccess" != "$TRUE" ]]; then
|
2017-07-25 08:47:59 +00:00
|
|
|
echo "YOUR VDM ACCESS KEY IS INCORRECT! >> $resultAccess"
|
2017-07-21 22:48:24 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
echo "Done"
|
|
|
|
else
|
|
|
|
echo "Access granted to the VDM system."
|
|
|
|
fi
|
|
|
|
}
|
2017-07-05 00:34:40 +00:00
|
|
|
|
2020-02-16 23:15:34 +00:00
|
|
|
function setData () {
|
|
|
|
# get this station data (TODO we just do the IP for now)
|
2017-07-21 22:48:24 +00:00
|
|
|
IPNOW="$(dig +short myip.opendns.com @resolver1.opendns.com)"
|
|
|
|
# store the IP in the HOSTNAME file
|
2017-07-25 08:47:59 +00:00
|
|
|
echoTweak "Setting/Update the Dynamic IP..."
|
2017-07-21 22:48:24 +00:00
|
|
|
resultUpdate=$(curl -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36" -H "VDM-KEY: $VDMSERVERKEY" -H "VDM-IP: $IPNOW" --silent $VDMIPSERVER)
|
|
|
|
if [[ "$resultUpdate" != "$TRUE" ]]; then
|
2017-07-25 08:47:59 +00:00
|
|
|
echo "YOUR SERVER KEY IS INCORRECT! >> $resultUpdate"
|
2017-07-05 00:34:40 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
echo "Done"
|
2017-07-21 22:48:24 +00:00
|
|
|
}
|
2017-07-05 00:34:40 +00:00
|
|
|
|
2017-07-21 22:48:24 +00:00
|
|
|
##############################################################
|
|
|
|
############## ##########
|
|
|
|
############## MAIN ##########
|
|
|
|
############## ##########
|
|
|
|
##############################################################
|
|
|
|
main
|