|
| 1 | +# frozen_string_literal: true |
| 2 | +# |
| 3 | +# ronin-app - a local web app for Ronin. |
| 4 | +# |
| 5 | +# Copyright (C) 2023-2024 Hal Brodigan (postmodern.mod3@gmail.com) |
| 6 | +# |
| 7 | +# ronin-app is free software: you can redistribute it and/or modify |
| 8 | +# it under the terms of the GNU Affero General Public License as published by |
| 9 | +# the Free Software Foundation, either version 3 of the License, or |
| 10 | +# (at your option) any later version. |
| 11 | +# |
| 12 | +# ronin-app is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +# GNU Affero General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU Affero General Public License |
| 18 | +# along with ronin-app. If not, see <http://www.gnu.org/licenses/>. |
| 19 | +# |
| 20 | + |
| 21 | +# ronin libraries |
| 22 | +require 'ronin/repos' |
| 23 | + |
| 24 | +# param validations |
| 25 | +require 'ronin/app/validations/install_repo_params' |
| 26 | + |
| 27 | +# worker classes |
| 28 | +require_relative '../workers/install_repo' |
| 29 | +require_relative '../workers/update_repo' |
| 30 | +require_relative '../workers/update_repos' |
| 31 | +require_relative '../workers/remove_repo' |
| 32 | +require_relative '../workers/purge_repos' |
| 33 | + |
| 34 | +# |
| 35 | +# App class containing routes for repos. |
| 36 | +# |
| 37 | +class App < Sinatra::Base |
| 38 | + |
| 39 | + include Ronin::App |
| 40 | + include Pagy::Backend |
| 41 | + |
| 42 | + configure do |
| 43 | + enable :sessions |
| 44 | + register Sinatra::Flash |
| 45 | + helpers Sinatra::ContentFor |
| 46 | + helpers Helpers::HTML |
| 47 | + end |
| 48 | + |
| 49 | + configure :development do |
| 50 | + register Sinatra::Reloader |
| 51 | + end |
| 52 | + |
| 53 | + helpers do |
| 54 | + include Pagy::Frontend |
| 55 | + end |
| 56 | + |
| 57 | + get '/repos' do |
| 58 | + @repos = Ronin::Repos.cache_dir |
| 59 | + |
| 60 | + erb :"repos/index" |
| 61 | + end |
| 62 | + |
| 63 | + get '/repos/install' do |
| 64 | + erb :"repos/install" |
| 65 | + end |
| 66 | + |
| 67 | + post '/repos/install' do |
| 68 | + result = Validations::InstallRepoParams.call(params) |
| 69 | + |
| 70 | + if result.success? |
| 71 | + Workers::InstallRepo.perform_async(result[:uri],result[:name]) |
| 72 | + |
| 73 | + flash[:success] = "Installing repo at #{result[:uri]}" |
| 74 | + redirect '/repos' |
| 75 | + else |
| 76 | + @errors = result.errors |
| 77 | + |
| 78 | + flash[:danger] = 'Failed to install repo!' |
| 79 | + halt 400, erb(:"repos/install") |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + post '/repos/update' do |
| 84 | + Workers::UpdateRepos.perform_async |
| 85 | + |
| 86 | + flash[:success] = 'All repos will be updated' |
| 87 | + redirect '/repos' |
| 88 | + end |
| 89 | + |
| 90 | + delete '/repos' do |
| 91 | + Workers::PurgeRepos.perform_async |
| 92 | + |
| 93 | + flash[:success] = 'All repos will be purged' |
| 94 | + redirect '/repos' |
| 95 | + end |
| 96 | + |
| 97 | + get '/repos/:name' do |
| 98 | + @repos = Ronin::Repos.cache_dir |
| 99 | + |
| 100 | + begin |
| 101 | + @repo = @repos[params[:name]] |
| 102 | + |
| 103 | + erb :"repos/show" |
| 104 | + rescue Ronin::Repos::RepositoryNotFound |
| 105 | + halt 404 |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + post '/repos/:name/update' do |
| 110 | + @repo = Ronin::Repos.cache_dir[params[:name]] |
| 111 | + |
| 112 | + Workers::UpdateRepo.perform_async(@repo.name) |
| 113 | + |
| 114 | + flash[:success] = "Repo #{@repo.name} enqueued for update" |
| 115 | + redirect "/repos/#{params[:name]}" |
| 116 | + rescue Ronin::Repos::RepositoryNotFound |
| 117 | + halt 404 |
| 118 | + end |
| 119 | + |
| 120 | + delete '/repos/:name' do |
| 121 | + @repo = Ronin::Repos.cache_dir[params[:name]] |
| 122 | + |
| 123 | + Workers::RemoveRepo.perform_async(@repo.name) |
| 124 | + |
| 125 | + flash[:success] = "Repo #{@repo.name} enqueued for removal" |
| 126 | + redirect '/repos' |
| 127 | + rescue Ronin::Repos::RepositoryNotFound |
| 128 | + halt 404 |
| 129 | + end |
| 130 | +end |
0 commit comments