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.

60 lines
1.3KB

  1. require 'erb'
  2. require 'logging'
  3. require 'parallel'
  4. require 'thread'
  5. module SSLCheck
  6. module SSLLabs
  7. autoload :API, 'sslcheck/ssllabs/api'
  8. end
  9. autoload :Server, 'sslcheck/server'
  10. autoload :Grade, 'sslcheck/grade'
  11. @@log = Logging.logger[SSLCheck]
  12. def self.grade(hostname, port=443)
  13. timeout 600 do
  14. Grade.new Server.new hostname, port
  15. end
  16. rescue Exception => e
  17. @@log.error { "Error during #{hostname}:#{port} analysis : #{e}" }
  18. NoSslTlsGrade.new NoSslTlsServer.new hostname, port
  19. end
  20. def self.analyze(hosts, output)
  21. results = {}
  22. semaphore = Mutex.new
  23. Parallel.each hosts, progress: 'Testing', in_threads: 10 do |description, host|
  24. result = SSLCheck.grade host.strip
  25. semaphore.synchronize do
  26. if results.include? description
  27. results[description] << result
  28. else
  29. results[description] = [result]
  30. end
  31. end
  32. end
  33. results.each do |d, _|
  34. results[d].sort! do |a, b|
  35. cmp = score(a) <=> score(b)
  36. if cmp == 0
  37. cmp = a.score <=> b.score
  38. if cmp == 0
  39. cmp = a.server.hostname <=> b.server.hostname
  40. end
  41. end
  42. cmp
  43. end
  44. end
  45. File.write "output/#{output}.html", ERB.new(File.read('output/sslcheck.erb')).result(binding)
  46. end
  47. private
  48. SCORES = %w(A+ A A- B C D E F T M X)
  49. def self.score(a)
  50. SCORES.index a.grade
  51. end
  52. end