You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
2.2 KiB

  1. #!/bin/sh
  2. # captive portal auto-login script for telekom hotspots (DE)
  3. # Copyright (c) 2021 Dirk Brenken (dev@brenken.org)
  4. # This is free software, licensed under the GNU General Public License v3.
  5. # set (s)hellcheck exceptions
  6. # shellcheck disable=1091,3040,3043,3057
  7. . "/lib/functions.sh"
  8. # url encoding function
  9. #
  10. urlencode()
  11. {
  12. local chr str="${1}" len="${#1}" pos=0
  13. while [ "${pos}" -lt "${len}" ]; do
  14. chr="${str:pos:1}"
  15. case "${chr}" in
  16. [a-zA-Z0-9.~_-])
  17. printf "%s" "${chr}"
  18. ;;
  19. " ")
  20. printf "%%20"
  21. ;;
  22. *)
  23. printf "%%%02X" "'${chr}"
  24. ;;
  25. esac
  26. pos=$((pos + 1))
  27. done
  28. }
  29. export LC_ALL=C
  30. export PATH="/usr/sbin:/usr/bin:/sbin:/bin"
  31. set -o pipefail
  32. username="$(urlencode "${1}")"
  33. password="$(urlencode "${2}")"
  34. trm_domain="telekom.portal.fon.com"
  35. trm_useragent="$(uci_get travelmate global trm_useragent "Mozilla/5.0 (Linux x86_64; rv:90.0) Gecko/20100101 Firefox/90.0")"
  36. trm_captiveurl="$(uci_get travelmate global trm_captiveurl "http://detectportal.firefox.com")"
  37. trm_maxwait="$(uci_get travelmate global trm_maxwait "30")"
  38. trm_fetch="$(command -v curl)"
  39. # get redirect url
  40. #
  41. raw_html="$(${trm_fetch} --user-agent "${trm_useragent}" --referer "http://www.example.com" --connect-timeout $((trm_maxwait / 6)) --location --silent --show-error "${trm_captiveurl}")"
  42. redirect_url="$(printf "%s" "${raw_html}" | awk 'match(tolower($0),/<loginurl>.*<\/loginurl>/){printf "%s",substr($0,RSTART+10,RLENGTH-21)}' 2>/dev/null | awk '{gsub("&amp;","\\&");printf "%s",$0}' 2>/dev/null)"
  43. [ -z "${redirect_url}" ] && exit 1
  44. # final login request
  45. #
  46. raw_html="$("${trm_fetch}" --user-agent "${trm_useragent}" --referer "https://${trm_domain}" --connect-timeout $((trm_maxwait / 6)) --header "content-type: application/x-www-form-urlencoded" --location --silent --show-error --data "UserName=${username}&Password=${password}&FNAME=0&button=Login&OriginatingServer=http%3A%2F%2F${trm_captiveurl}" "${redirect_url}")"
  47. login_url="$(printf "%s" "${raw_html}" | awk 'match(tolower($0),/<logoffurl>.*<\/logoffurl>/){printf "%s",substr($0,RSTART+11,RLENGTH-23)}' 2>/dev/null)"
  48. [ -n "${login_url}" ] && exit 0 || exit 255