Optimized login function

This commit is contained in:
Jinhill 2021-10-12 14:38:06 +08:00
parent 910649c5f5
commit 0d6864ca4a

View File

@ -1,4 +1,4 @@
#! /usr/bin/env sh #!/usr/bin/env sh
####################################################### #######################################################
# GeoScaling DNS2 hook script for acme.sh # GeoScaling DNS2 hook script for acme.sh
# #
@ -15,39 +15,38 @@ COOKIE_FILE="/tmp/.geos.cookie"
#Add cookie to request #Add cookie to request
export _CURL="curl -s -c ${COOKIE_FILE} -b ${COOKIE_FILE}" export _CURL="curl -s -c ${COOKIE_FILE} -b ${COOKIE_FILE}"
SESSION_TIMEOUT=300 SESSION_TIMEOUT=300
log(){ log() {
echo "$@" 1>&2 echo "$@" 1>&2
} }
#$1:url #$1:url
url_encode() { url_encode() {
echo "$1" | awk -v ORS="" '{ gsub(/./,"&\n") ; print }' | while read -r l; echo "$1" | awk -v ORS="" '{ gsub(/./,"&\n") ; print }' | while read -r l; do
do case "$l" in
case "$l" in [-_.~a-zA-Z0-9]) printf '%s' "$l" ;;
[-_.~a-zA-Z0-9] ) printf '%s' "$l" ;; "") printf '%%20' ;;
"" ) printf '%%20' ;; *) printf '%%%02X' "'$l" ;;
* ) printf '%%%02X' "'$l" esac
esac done
done
} }
#$1:string,$2:char, if $2 not set return array len,$ret:count #$1:string,$2:char, if $2 not set return array len,$ret:count
count() { count() {
if [ -n "$2" ];then if [ -n "$2" ]; then
echo "$1" | awk -F"$2" '{print NF-1}' echo "$1" | awk -F"$2" '{print NF-1}'
else else
echo "$1" | wc -w echo "$1" | wc -w
fi fi
} }
#$1:seesion mode,$2:username,$3:password #$1:seesion mode,$2:username,$3:password
login() { login() {
if [ -n "$1" ] && [ "$1" = "1" ] && [ -f "${COOKIE_FILE}" ];then if [ -n "$1" ] && [ "$1" = "1" ] && [ -f "${COOKIE_FILE}" ]; then
c_t=$(date -r "${COOKIE_FILE}" "+%s") c_t=$(date -r "${COOKIE_FILE}" "+%s")
now=$(date "+%s") now=$(date "+%s")
s_t=$(( now - c_t )) s_t=$((now - c_t))
if [ ${s_t} -lt ${SESSION_TIMEOUT} ];then if [ ${s_t} -lt ${SESSION_TIMEOUT} ]; then
return 0 return 0
fi fi
fi fi
@ -69,6 +68,7 @@ login() {
if [ "${http_code}" = "302" ]; then if [ "${http_code}" = "302" ]; then
return 0 return 0
fi fi
rm -rf "${COOKIE_FILE}"
_err "geoscaling login failed for user ${GEOS_Username} bad RC from post" _err "geoscaling login failed for user ${GEOS_Username} bad RC from post"
return 1 return 1
} }
@ -79,11 +79,11 @@ login() {
# zone_id=xxxxxx # zone_id=xxxxxx
get_zone() { get_zone() {
resp=$($_CURL "https://www.geoscaling.com/dns2/index.php?module=domains") resp=$($_CURL "https://www.geoscaling.com/dns2/index.php?module=domains")
table=$(echo "${resp}" | grep -oE "<table[^>]+ class=\"threecolumns\">.*</table>") table=$(echo "${resp}" | grep -oE "<table[^>]+ class=\"threecolumns\">.*</table>")
items=$(echo "${table}" | grep -oE '<a [^>]+><b>[^>]+>') items=$(echo "${table}" | grep -oE '<a [^>]+><b>[^>]+>')
i=1 i=1
c=$(count "$1" ".") c=$(count "$1" ".")
while [ $i -le $c ]; do while [ $i -le "$c" ]; do
d=$(echo "$1" | cut -d . -f $i-) d=$(echo "$1" | cut -d . -f $i-)
if [ -z "$d" ]; then if [ -z "$d" ]; then
return 1 return 1
@ -93,7 +93,7 @@ get_zone() {
sub_domain=$(echo "$1" | sed "s/.$d//") sub_domain=$(echo "$1" | sed "s/.$d//")
return 0 return 0
fi fi
i=$(( i + 1 )) i=$((i + 1))
done done
return 1 return 1
} }
@ -101,7 +101,7 @@ get_zone() {
#$1:domain id,$2:dns fullname #$1:domain id,$2:dns fullname
get_record_id() { get_record_id() {
resp=$($_CURL "https://www.geoscaling.com/dns2/index.php?module=domain&id=$1") resp=$($_CURL "https://www.geoscaling.com/dns2/index.php?module=domain&id=$1")
ids=$(echo "${resp}" | tr -d "\n" | grep -oE "<table id='records_table'.*</a></td></tr></table>" | grep -oE "id=\"[0-9]*.name\">$2" | cut -d '"' -f 2 | cut -d '.' -f 1) ids=$(echo "${resp}" | tr -d "\n" | grep -oE "<table id='records_table'.*</a></td></tr></table>" | grep -oE "id=\"[0-9]*.name\">$2" | cut -d '"' -f 2 | cut -d '.' -f 1)
if [ -z "${ids}" ]; then if [ -z "${ids}" ]; then
_err "DNS record $2 not found." _err "DNS record $2 not found."
return 1 return 1