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.

69 lines
1.6KB

  1. class CheckController < ApplicationController
  2. before_action :check_host, except: %i(index)
  3. helper_method :tls_type, :type
  4. def show
  5. enqueue_host unless @result
  6. @host = SimpleIDN.to_unicode @host
  7. respond_to do |format|
  8. format.html do
  9. return render :processing if @result[:pending]
  10. @result = OpenStruct.deep @result
  11. end
  12. format.json do
  13. render json: case
  14. when @result[:pending] then
  15. :pending
  16. else
  17. JSON.pretty_generate @result
  18. end
  19. end
  20. end
  21. end
  22. def refresh
  23. unless @result.pending
  24. if Rails.env == 'production'
  25. refresh_allowed = @result.date + Rails.configuration.refresh_delay
  26. if Time.now < refresh_allowed
  27. flash[:warning] = "Merci d’attendre au moins #{l refresh_allowed} pour rafraîchir"
  28. return redirect_to action: :show, id: @host
  29. end
  30. end
  31. enqueue_host
  32. end
  33. redirect_to action: :show
  34. end
  35. protected
  36. def enqueue_host
  37. Datastore.pending self.type, @host, @port
  38. self.worker.perform_async *(@port.blank? ? [@host] : [@host, @port])
  39. @result = OpenStruct.new pending: true, date: Time.now
  40. end
  41. def check_host
  42. @id = params[:id]
  43. if @id.end_with? '.json'
  44. @id = @id.sub /\.json$/, ''
  45. request.format = :json
  46. end
  47. @host, @port = @id.split ':'
  48. @host = SimpleIDN.to_ascii @host.downcase
  49. if /[^a-zA-Z0-9.-]/ =~ @host
  50. flash[:danger] = "Hôte #{@host} invalide"
  51. redirect_to action: :index
  52. return false
  53. end
  54. @port = @port.to_i if @port
  55. # @result = Datastore.host self.type, @host, @port
  56. file = File.join Rails.root, 'config/host.dump'
  57. # open(file, 'wb') { |f| f.write Marshal.dump @result }
  58. @result = Marshal.load File.read file
  59. end
  60. end