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.

74 line
1.8KB

  1. require 'erb'
  2. require 'logging'
  3. require 'parallel'
  4. module CryptCheck
  5. module Tls
  6. module Https
  7. MAX_ANALYSIS_DURATION = 600
  8. PARALLEL_ANALYSIS = 10
  9. @@log = ::Logging.logger[Https]
  10. def self.grade(hostname, port=443)
  11. timeout MAX_ANALYSIS_DURATION do
  12. Grade.new Server.new hostname, port
  13. end
  14. rescue ::Exception => e
  15. @@log.error { "Error during #{hostname}:#{port} analysis : #{e}" }
  16. TlsNotSupportedGrade.new TlsNotSupportedServer.new hostname, port
  17. end
  18. def self.analyze(hosts, output, groups = nil)
  19. results = {}
  20. semaphore = ::Mutex.new
  21. ::Parallel.each hosts, progress: 'Analysing', in_threads: PARALLEL_ANALYSIS, finish: lambda { |item, _, _| puts item[1] } do |description, host|
  22. result = grade host.strip
  23. semaphore.synchronize do
  24. if results.include? description
  25. results[description] << result
  26. else
  27. results[description] = [result]
  28. end
  29. end
  30. end
  31. results = ::Hash[groups.collect { |g| [g, results[g]] }] if groups
  32. results.each do |d, _|
  33. results[d].sort! do |a, b|
  34. cmp = score(a) <=> score(b)
  35. if cmp == 0
  36. cmp = b.score <=> a.score
  37. if cmp == 0
  38. cmp = a.server.hostname <=> b.server.hostname
  39. end
  40. end
  41. cmp
  42. end
  43. end
  44. ::File.write output, ::ERB.new(::File.read('output/https.erb')).result(binding)
  45. end
  46. def self.analyze_from_file(file, output)
  47. config = ::YAML.load_file file
  48. hosts = []
  49. groups = []
  50. config.each do |c|
  51. d, hs = c['description'], c['hostnames']
  52. groups << d
  53. hs.each { |host| hosts << [d, host] }
  54. end
  55. self.analyze hosts, output, groups
  56. end
  57. private
  58. SCORES = %w(A+ A A- B C D E F T M X)
  59. def self.score(a)
  60. SCORES.index a.grade
  61. end
  62. end
  63. end
  64. end