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.4KB

  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. return render :no_tls if @result.no_tls
  11. end
  12. format.json do
  13. render json: case
  14. when @result.pending then :pending
  15. when @result.no_tls then :no_tls
  16. else @result
  17. end
  18. end
  19. end
  20. end
  21. def refresh
  22. unless @result.pending
  23. refresh_allowed = @result.date + Rails.configuration.refresh_delay
  24. if Time.now < refresh_allowed
  25. flash[:warning] = "Merci d’attendre au moins #{l refresh_allowed} pour rafraîchir"
  26. return redirect_to action: :show, id: @host
  27. end
  28. enqueue_host
  29. end
  30. redirect_to action: :show
  31. end
  32. protected
  33. def enqueue_host
  34. Datastore.pending self.type, @id, @port
  35. self.worker.perform_async *(@port.blank? ? [@host] : [@host, @port])
  36. @result = OpenStruct.new pending: true , date: Time.now
  37. end
  38. def check_host
  39. @id = params[:id]
  40. if @id.end_with? '.json'
  41. @id = @id.sub /\.json$/, ''
  42. request.format = :json
  43. end
  44. @host, @port = @id.split ':'
  45. @host = SimpleIDN.to_ascii @host.downcase
  46. if /[^a-zA-Z0-9.-]/.match @host
  47. flash[:danger] = "Hôte #{@host} invalide"
  48. redirect_to action: :index
  49. return false
  50. end
  51. @result = Datastore.host self.type, @host, @port
  52. end
  53. end