Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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