fetch-all-functions 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/bin/bash
  2. # function used by fetch-all* to download server descriptors and
  3. # extra info documents
  4. # Copyright (c) 2005, 2006, 2007, 2008 Peter Palfrader
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in
  14. # all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. fetch_digest() {
  24. local digest
  25. local objecttype
  26. local urlpart
  27. local pathpart
  28. local target
  29. local targetdir
  30. local dirserver
  31. local ei
  32. digest="$1"
  33. objecttype="$2"
  34. if [ "$objecttype" = "server-descriptor" ] ; then
  35. urlpart="server"
  36. pathpart="server-descriptor"
  37. elif [ "$objecttype" = "extra-info" ] ; then
  38. urlpart="extra"
  39. pathpart="extra-info"
  40. else
  41. echo "Called fetch_digest with illegal objecttype '$objecttype'" >&2
  42. exit 1
  43. fi
  44. target=$( echo $digest | sed -e 's#^\(.\)\(.\)#'"$pathpart"'/\1/\2/\1\2#' )
  45. targetdir=$( dirname $target )
  46. [ -d "$targetdir" ] || mkdir -p "$targetdir"
  47. if ! [ -e "$target" ]; then
  48. for dirserver in $DIRSERVERS; do
  49. wget -q -O "$target" http://$dirserver/tor/$urlpart/d/"$digest" || rm -f "$target"
  50. if [ -s "$target" ]; then
  51. if egrep '^opt extra-info-digest ' "$target" > /dev/null; then
  52. ei=$( egrep '^opt extra-info-digest ' "$target" | awk '{print $3}' | tr 'A-F' 'a-f' )
  53. fetch_digest "$ei" "extra-info"
  54. elif egrep '^extra-info-digest ' "$target" > /dev/null; then
  55. ei=$( egrep '^extra-info-digest ' "$target" | awk '{print $2}' | tr 'A-F' 'a-f' )
  56. fetch_digest "$ei" "extra-info"
  57. fi
  58. break
  59. else
  60. rm -f "$target"
  61. fi
  62. done
  63. fi
  64. #if ! [ -e "$target" ]; then
  65. # echo "$objecttype $digest" >> failed
  66. #fi
  67. }
  68. if [ -x /usr/bin/base64 ] ; then
  69. base64-decode() {
  70. /usr/bin/base64 -d
  71. }
  72. else
  73. base64-decode() {
  74. perl -MMIME::Base64 -e 'print decode_base64(<>)'
  75. }
  76. fi