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.

host.rb 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. require 'timeout'
  2. module CryptCheck
  3. module Tls
  4. class AnalysisFailure
  5. attr_reader :error
  6. def initialize(error)
  7. @error = error
  8. end
  9. def to_s
  10. @error.to_s
  11. end
  12. end
  13. class TooLongAnalysis < AnalysisFailure
  14. def initialize
  15. super "Too long analysis (max #{Host::MAX_ANALYSIS_DURATION.humanize})"
  16. end
  17. end
  18. class Host
  19. MAX_ANALYSIS_DURATION = 600
  20. attr_reader :servers, :error
  21. def initialize(hostname, port)
  22. @hostname, @port = hostname, port
  23. first = true
  24. @servers = resolve.collect do |args|
  25. _, ip, _, _ = args
  26. first ? (first = false) : Logger.info { '' }
  27. result = begin
  28. server = ::Timeout.timeout MAX_ANALYSIS_DURATION do
  29. server(*args)
  30. end
  31. grade server
  32. rescue Engine::TLSException, Engine::ConnectionError, Engine::Timeout => e
  33. AnalysisFailure.new e
  34. rescue ::Timeout::Error
  35. TooLongAnalysis.new
  36. end
  37. [[@hostname, ip, @port], result]
  38. end.to_h
  39. rescue => e
  40. @error = e
  41. end
  42. def to_json
  43. JSON.generate(@servers.collect do |host, result|
  44. hostname, ip, _ = host
  45. json = {
  46. hostname: hostname,
  47. ip: ip,
  48. }
  49. case result
  50. when Grade
  51. json[:result] = result.to_json
  52. else
  53. json[:error] = result.message
  54. end
  55. json
  56. end)
  57. end
  58. private
  59. def resolve
  60. begin
  61. ip = IPAddr.new @hostname
  62. return [[nil, ip.to_s, ip.family]]
  63. rescue IPAddr::InvalidAddressError
  64. end
  65. ::Addrinfo.getaddrinfo(@hostname, nil, nil, :STREAM)
  66. .collect { |a| [@hostname, a.ip_address, a.afamily, @port] }
  67. end
  68. def server(*args)
  69. TcpServer.new *args
  70. end
  71. def grade(server)
  72. Grade.new server
  73. end
  74. end
  75. end
  76. end