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.

64 lines
2.3 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. export LC_ALL=C
  8. export PATH="/usr/sbin:/usr/bin:/sbin:/bin"
  9. set -o pipefail
  10. # source function library if necessary
  11. #
  12. if [ -z "${_C}" ]; then
  13. . "/lib/functions.sh"
  14. fi
  15. # url encoding function
  16. #
  17. urlencode()
  18. {
  19. local chr str="${1}" len="${#1}" pos=0
  20. while [ "${pos}" -lt "${len}" ]; do
  21. chr="${str:pos:1}"
  22. case "${chr}" in
  23. [a-zA-Z0-9.~_-])
  24. printf "%s" "${chr}"
  25. ;;
  26. " ")
  27. printf "%%20"
  28. ;;
  29. *)
  30. printf "%%%02X" "'${chr}"
  31. ;;
  32. esac
  33. pos=$((pos + 1))
  34. done
  35. }
  36. username="$(urlencode "${1}")"
  37. password="$(urlencode "${2}")"
  38. trm_domain="telekom.portal.fon.com"
  39. trm_useragent="$(uci_get travelmate global trm_useragent "Mozilla/5.0 (Linux x86_64; rv:90.0) Gecko/20100101 Firefox/90.0")"
  40. trm_captiveurl="$(uci_get travelmate global trm_captiveurl "http://detectportal.firefox.com")"
  41. trm_maxwait="$(uci_get travelmate global trm_maxwait "30")"
  42. trm_fetch="$(command -v curl)"
  43. # get redirect url
  44. #
  45. raw_html="$(${trm_fetch} --user-agent "${trm_useragent}" --referer "http://www.example.com" --connect-timeout $((trm_maxwait / 6)) --location --silent --show-error "${trm_captiveurl}")"
  46. 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)"
  47. if [ -z "${redirect_url}" ]; then
  48. exit 1
  49. fi
  50. # final login request
  51. #
  52. 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}")"
  53. login_url="$(printf "%s" "${raw_html}" | awk 'match(tolower($0),/<logoffurl>.*<\/logoffurl>/){printf "%s",substr($0,RSTART+11,RLENGTH-23)}' 2>/dev/null)"
  54. if [ -z "${login_url}" ]; then
  55. exit 2
  56. fi