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.

153 lines
2.0KB

  1. require 'openssl'
  2. class String
  3. alias :colorize_old :colorize
  4. COLORS = {
  5. critical: { color: :white, background: :red },
  6. error: :red,
  7. warning: :light_red,
  8. good: :green,
  9. perfect: :blue,
  10. best: :magenta,
  11. unknown: { background: :black }
  12. }
  13. def colorize(state)
  14. color = COLORS[state] || state
  15. self.colorize_old color
  16. end
  17. end
  18. class Exception
  19. BACKTRACE_REGEXP = /^(.*):(\d+):in `(.*)'$/
  20. def colorize
  21. $stderr.puts self.message.colorize(:red)
  22. self.backtrace.each do |line|
  23. line = BACKTRACE_REGEXP.match line
  24. line = '%s:%s:in `%s\'' % [
  25. line[1].colorize(:yellow),
  26. line[2].colorize(:blue),
  27. line[3].colorize(:magenta)
  28. ]
  29. $stderr.puts line
  30. end
  31. end
  32. end
  33. class Integer
  34. def humanize
  35. secs = self
  36. [[60, :second],
  37. [60, :minute],
  38. [24, :hour],
  39. [30, :day],
  40. [12, :month]].map do |count, name|
  41. if secs > 0
  42. secs, n = secs.divmod count
  43. n = n.to_i
  44. n > 0 ? "#{n} #{name}#{n > 1 ? 's' : ''}" : nil
  45. end
  46. end.compact.reverse.join ' '
  47. end
  48. end
  49. class ::OpenSSL::PKey::EC
  50. def type
  51. :ecc
  52. end
  53. def size
  54. self.group.degree
  55. end
  56. def to_s
  57. "ECC #{self.size} bits"
  58. end
  59. def status
  60. case self.size
  61. when 0...160
  62. :critical
  63. when 160...192
  64. :error
  65. when 192...256
  66. :warning
  67. when 256...364
  68. else
  69. :good
  70. end
  71. end
  72. end
  73. class ::OpenSSL::PKey::RSA
  74. def type
  75. :rsa
  76. end
  77. def size
  78. self.n.num_bits
  79. end
  80. def to_s
  81. "RSA #{self.size} bits"
  82. end
  83. def status
  84. case self.size
  85. when 0...1024
  86. :critical
  87. when 1024...2048
  88. :error
  89. when 2048...4096
  90. else
  91. :good
  92. end
  93. end
  94. end
  95. class ::OpenSSL::PKey::DSA
  96. def type
  97. :dsa
  98. end
  99. def size
  100. self.p.num_bits
  101. end
  102. def to_s
  103. "DSA #{self.size} bits"
  104. end
  105. def status
  106. return :critical
  107. end
  108. end
  109. class ::OpenSSL::PKey::DH
  110. def type
  111. :dh
  112. end
  113. def size
  114. self.p.num_bits
  115. end
  116. def to_s
  117. "DH #{self.size} bits"
  118. end
  119. def status
  120. case self.size
  121. when 0...1024
  122. :critical
  123. when 1024...2048
  124. :error
  125. when 2048...4096
  126. else
  127. :good
  128. end
  129. end
  130. end