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 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. Logger.info ''
  32. Logger.info { "Grade : #{server.grade.to_s.colorize server.status}" }
  33. server
  34. rescue Engine::TLSException, Engine::ConnectionError, Engine::Timeout => e
  35. AnalysisFailure.new e
  36. rescue ::Timeout::Error
  37. TooLongAnalysis.new
  38. end
  39. [[@hostname, ip, @port], result]
  40. end.to_h
  41. # rescue StandardError
  42. # raise
  43. rescue => e
  44. @error = e
  45. end
  46. def to_h
  47. target = {
  48. target: { hostname: @hostname, port: @port },
  49. }
  50. if @error
  51. target[:error] = @error
  52. else
  53. target[:hosts] = @servers.collect do |host, server|
  54. hostname, ip, port = host
  55. host = {
  56. hostname: hostname,
  57. ip: ip,
  58. port: port
  59. }
  60. case server
  61. when Server
  62. host[:handshakes] = server.to_h
  63. host[:states] = server.states
  64. host[:grade] = server.grade
  65. else
  66. host[:error] = server.message
  67. end
  68. host
  69. end
  70. end
  71. target
  72. end
  73. private
  74. def resolve
  75. begin
  76. ip = IPAddr.new @hostname
  77. return [[nil, ip.to_s, ip.family]]
  78. rescue IPAddr::InvalidAddressError
  79. end
  80. ::Addrinfo.getaddrinfo(@hostname, nil, nil, :STREAM)
  81. .collect { |a| [@hostname, a.ip_address, a.afamily, @port] }
  82. end
  83. def server(*args)
  84. TcpServer.new *args
  85. end
  86. end
  87. end
  88. end