Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

https.rb 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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,
  22. finish: lambda { |item, _, _| puts item[1] } do |description, host|
  23. result = grade host.strip
  24. semaphore.synchronize do
  25. if results.include? description
  26. results[description] << result
  27. else
  28. results[description] = [result]
  29. end
  30. end
  31. end
  32. results = ::Hash[groups.collect { |g| [g, results[g]] }] if groups
  33. results.each do |d, _|
  34. results[d].sort! do |a, b|
  35. cmp = score(a) <=> score(b)
  36. if cmp == 0
  37. cmp = b.score <=> a.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, ::ERB.new(::File.read('output/https.erb')).result(binding)
  46. end
  47. def self.analyze_from_file(file, output)
  48. config = ::YAML.load_file file
  49. hosts = []
  50. groups = []
  51. config.each do |c|
  52. d, hs = c['description'], c['hostnames']
  53. groups << d
  54. hs.each { |host| hosts << [d, host] }
  55. end
  56. self.analyze hosts, output, groups
  57. end
  58. private
  59. SCORES = %w(A+ A A- B C D E F T M X)
  60. def self.score(a)
  61. SCORES.index a.grade
  62. end
  63. end
  64. end
  65. end