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.

55 lines
2.1 KiB

  1. #!/bin/sh
  2. # captive portal auto-login script for telekom hotspots (DE)
  3. # Copyright (c) 2021-2022 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. username="$(urlencode "${1}")"
  32. password="$(urlencode "${2}")"
  33. trm_domain="telekom.portal.fon.com"
  34. trm_useragent="$(uci_get travelmate global trm_useragent "Mozilla/5.0 (Linux x86_64; rv:90.0) Gecko/20100101 Firefox/90.0")"
  35. trm_captiveurl="$(uci_get travelmate global trm_captiveurl "http://detectportal.firefox.com")"
  36. trm_maxwait="$(uci_get travelmate global trm_maxwait "30")"
  37. trm_fetch="$(command -v curl)"
  38. # get redirect url
  39. #
  40. raw_html="$(${trm_fetch} --user-agent "${trm_useragent}" --referer "http://www.example.com" --connect-timeout $((trm_maxwait / 6)) --location --silent --show-error "${trm_captiveurl}")"
  41. 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)"
  42. [ -z "${redirect_url}" ] && exit 1
  43. # final login request
  44. #
  45. 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}")"
  46. login_url="$(printf "%s" "${raw_html}" | awk 'match(tolower($0),/<logoffurl>.*<\/logoffurl>/){printf "%s",substr($0,RSTART+11,RLENGTH-23)}' 2>/dev/null)"
  47. [ -n "${login_url}" ] && exit 0 || exit 255