Small sh "framework" to test some server responses
sh
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

xmpp.sh 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #Copyright (C) 2016,2023 Weber Yann
  2. #
  3. #This program is free software; you can redistribute it and/or modify
  4. #it under the terms of the GNU General Public License as published by
  5. #the Free Software Foundation; either version 3 of the License, or
  6. #any later version.
  7. #
  8. #This program is distributed in the hope that it will be useful,
  9. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. #GNU General Public License for more details.
  12. #
  13. #You should have received a copy of the GNU General Public License
  14. #along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. #
  16. # Jabber XMPP checks
  17. #
  18. __xmpp_probe() {
  19. serv=$1
  20. timeout=$2
  21. type=$3
  22. payload=$(printf "%s" "<?xml version=\"1.0\"?>\n<stream:stream xmlns:stream=\"http://etherx.jabber.org/streams\" version=\"1.0\" xmlns=\"jabber:$type\" to=\"${1}\" xml:lang=\"en\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\">\n")
  23. if [ "$verbose" -gt 2 ]
  24. then
  25. echo "$payload" | sed -e "s/^/$(datetime) [ DEBUG] Sent : /" >&2
  26. fi
  27. echo "$payload"
  28. sleep "$timeout"
  29. }
  30. _xmpp_probe() {
  31. serv=$1
  32. port=$2
  33. ipv_arg=$3
  34. timeout=$4
  35. type=$5
  36. case "$ipv_arg" in
  37. ipv6)ipv=-6;;
  38. ipv4)ipv=-4;;
  39. default)ipv="";;
  40. esac
  41. tmpres=$(mktemp -t xmpp_probe.XXXXXXXXX)
  42. echo "$(__xmpp_probe "$serv" "$timeout" "$type"| nc $ipv -q2 "$serv" "$port")</stream:stream>" | xmllint --format - > "$tmpres"
  43. if [ "$verbose" -gt 2 ]
  44. then
  45. sed -e "s/^/$(datetime) [ DEBUG] Recv : /" >&2 < "$tmpres"
  46. fi
  47. cat "$tmpres"
  48. rm "$tmpres"
  49. }
  50. _check_xmpp_ns() {
  51. ns1=$1
  52. expt_ns=$2
  53. expt_port=$3
  54. dnsq="_xmpp-${4}._tcp.${ns1}"
  55. rep="$(dig "$dnsq" srv +short | cut -d" " -f3,4)"
  56. #if [ "$rep" = "$expt_port ${expt_ns}." ]
  57. if echo "$rep" | grep "${expt_ns}.$" >/dev/null
  58. then
  59. success "$dnsq = '$rep'"
  60. else
  61. fail "$dnsq = '$rep' but '$expt_port ${expt_ns}.' expected"
  62. fi
  63. }
  64. check_xmpp_serv_ns() {
  65. _check_xmpp_ns "$1" "$2" "$3" "server"
  66. }
  67. check_xmpp_client_ns() {
  68. _check_xmpp_ns "$1" "$2" "$3" "client"
  69. }
  70. _check_xmpp_server() {
  71. serv=$1
  72. port=$2
  73. ipv=$3
  74. timeout=$4
  75. type=$5
  76. if [ -z "$port" ]
  77. then
  78. port=5222
  79. fi
  80. if [ -z "$timeout" ]
  81. then
  82. timeout=2
  83. fi
  84. if [ "$verbose" -gt 1 ]
  85. then
  86. tpe="client"
  87. if [ "$type" = "server" ]
  88. then
  89. tpe="S2S"
  90. fi
  91. logdate INFO "$tc_name: Connecting to XMPP $serv $tpe port $port $ipv (timeout=${timeout}s)" 3
  92. fi
  93. stream=$(_xmpp_probe "$serv" "$port" "$ipv" "$timeout" "$type"| head -n2 | tail -n1)
  94. if [ -z "$stream" ]
  95. then
  96. fail "Empty reply from $serv:$port"
  97. return
  98. fi
  99. if [ "$type" = "client" ]
  100. then
  101. infos=$(echo "$stream" | sed -E 's/^<([^ ]+).* xmlns="([^"]+)".* from="([^"]+)" .*$/\1 \2 \3/')
  102. else
  103. infos="$(echo "$stream" | sed -E 's/^<([^ ]+).* xmlns="([^"]+)" .*$/\1 \2/') $serv"
  104. fi
  105. if echo "$infos" | grep "jabber:$type" >/dev/null
  106. then
  107. success "Successfully connected to XMPP $type $serv:$port $ipv"
  108. else
  109. fail "Unexpected reply from $serv:$port $ipv : $infos"
  110. fi
  111. }
  112. check_xmpp_server_client() {
  113. _check_xmpp_server "$1" "$2" "$3" "$4" "client"
  114. }
  115. check_xmpp_server_s2s() {
  116. _check_xmpp_server "$1" "$2" "$3" "$4" "server"
  117. }
  118. check_xmpp_ssl() {
  119. serv=$1
  120. port=$2
  121. ipv_arg=$3
  122. if [ -z "$port" ]
  123. then
  124. port=5222
  125. fi
  126. case "$ipv_arg" in
  127. ipv6)ipv=-6;;
  128. ipv4)ipv=-4;;
  129. default)ipv="";;
  130. esac
  131. openssl s_client $ipv -connect "$serv:$port" </dev/null -starttls xmpp >/dev/null 2>/dev/null
  132. rep=$?
  133. if [ "$rep" -eq 0 ]
  134. then
  135. success "Openssl successfully negociating XMPP ssl with $serv:$port $ipv_arg"
  136. else
  137. fail "Openssl failed negociating XMPP ssl with $serv:$port $ipv_arg"
  138. fi
  139. }