From 4ed07ccecb8eeb18302ba229da428af484d2d882 Mon Sep 17 00:00:00 2001 From: Fijxu Date: Mon, 12 Aug 2024 23:17:04 -0400 Subject: [PATCH] 0.8.6: Alternative domains and less useless variables --- config/config.yml | 4 ++++ src/config.cr | 11 ++++++++--- src/handling/admin.cr | 2 +- src/handling/handling.cr | 12 ++---------- src/routing.cr | 33 ++++++++++++++++++++++++--------- src/utils.cr | 14 +++++++------- src/views/index.ecr | 13 ++++++++++++- 7 files changed, 58 insertions(+), 31 deletions(-) diff --git a/config/config.yml b/config/config.yml index 8f4ee4f..672f074 100644 --- a/config/config.yml +++ b/config/config.yml @@ -34,3 +34,7 @@ opengraphUseragents: - "chatterino-api-cache/" - "FFZBot/" - "Twitterbot/" + +alternativeDomains: + - "ayaya.beauty" + - "lamartina.gay" diff --git a/src/config.cr b/src/config.cr index 3aa5027..9280e42 100644 --- a/src/config.cr +++ b/src/config.cr @@ -10,7 +10,8 @@ class Config property dbTableName : String = "files" property adminEnabled : Bool = false property adminApiKey : String? = "" - property incremental_fileameLength : Bool = true + # Not implemented + property incrementalFileameLength : Bool = true property fileameLength : Int32 = 3 # In MiB property size_limit : Int16 = 512 @@ -27,11 +28,15 @@ class Config property deleteFilesCheck : Int32 = 1800 property deleteKeyLength : Int32 = 4 # Blocked extensions that are not allowed to be uploaded to the server - property blockedExtensions : Array(String) = [] of String - property opengraphUseragents : Array(String) = [] of String property siteInfo : String = "xd" property siteWarning : String? = "" property log_level : LogLevel = LogLevel::Info + property blockedExtensions : Array(String) = [] of String + property opengraphUseragents : Array(String) = [] of String + # Since this program detects the Host header of the client it can be used + # with multiple domains. You can display the domains in the frontend + # and in `/api/stats` + property alternativeDomains : Array(String) = [] of String def self.load config_file = "config/config.yml" diff --git a/src/handling/admin.cr b/src/handling/admin.cr index 820d9b0..e403873 100644 --- a/src/handling/admin.cr +++ b/src/handling/admin.cr @@ -5,7 +5,7 @@ module Handling::Admin def delete_file(env) if env.request.headers.try &.["X-Api-Key"]? != CONFIG.adminApiKey || nil - error401 "Wrong API Key" + error401 "Wrong API Key" end files = env.params.json["files"].as((Array(JSON::Any))) successfull_files = [] of String diff --git a/src/handling/handling.cr b/src/handling/handling.cr index 6a6f675..a15e8fd 100644 --- a/src/handling/handling.cr +++ b/src/handling/handling.cr @@ -21,9 +21,6 @@ module Handling checksum = "" ip_address = "" delete_key = nil - ip_address = env.request.headers.try &.["X-Forwarded-For"]? ? env.request.headers.["X-Forwarded-For"] : env.request.remote_address.to_s.split(":").first - protocol = env.request.headers.try &.["X-Forwarded-Proto"]? ? env.request.headers["X-Forwarded-Proto"] : "http" - host = env.request.headers.try &.["X-Forwarded-Host"]? ? env.request.headers["X-Forwarded-Host"] : env.request.headers["Host"] # TODO: Return the file that matches a checksum inside the database HTTP::FormData.parse(env.request) do |upload| if upload.filename.nil? || upload.filename.to_s.empty? @@ -85,9 +82,6 @@ module Handling files = env.params.json["files"].as((Array(JSON::Any))) successfull_files = [] of NamedTuple(filename: String, extension: String, original_filename: String, checksum: String, delete_key: String | Nil) failed_files = [] of String - ip_address = env.request.headers.try &.["X-Forwarded-For"]? ? env.request.headers.["X-Forwarded-For"] : env.request.remote_address.to_s.split(":").first - protocol = env.request.headers.try &.["X-Forwarded-Proto"]? ? env.request.headers["X-Forwarded-Proto"] : "http" - host = env.request.headers.try &.["X-Forwarded-Host"]? ? env.request.headers["X-Forwarded-Host"] : env.request.headers["Host"] # X-Forwarded-For if behind a reverse proxy and the header is set in the reverse # proxy configuration. if files.empty? @@ -166,8 +160,6 @@ module Handling end def retrieve_file(env) - protocol = env.request.headers.try &.["X-Forwarded-Proto"]? ? env.request.headers["X-Forwarded-Proto"] : "http" - host = env.request.headers.try &.["X-Forwarded-Host"]? ? env.request.headers["X-Forwarded-Host"] : env.request.headers["Host"] begin fileinfo = SQL.query_all("SELECT filename, original_filename, uploaded_at, extension, checksum, thumbnail FROM #{CONFIG.dbTableName} @@ -224,6 +216,7 @@ module Handling json.field "maxUploadSize", CONFIG.size_limit json.field "thumbnailGeneration", CONFIG.generateThumbnails json.field "filenameLength", CONFIG.fileameLength + json.field "alternativeDomains", CONFIG.alternativeDomains end end end @@ -265,9 +258,8 @@ module Handling end def sharex_config(env) - protocol = env.request.headers.try &.["X-Forwarded-Proto"]? ? env.request.headers["X-Forwarded-Proto"] : "http" - host = env.request.headers.try &.["X-Forwarded-Host"]? ? env.request.headers["X-Forwarded-Host"] : env.request.headers["Host"] env.response.content_type = "application/json" + # So it's able to download the file instead of displaying it env.response.headers["Content-Disposition"] = "attachment; filename=\"#{host}.sxcu\"" return %({ "Version": "14.0.1", diff --git a/src/routing.cr b/src/routing.cr index a88f3cc..c7e3209 100644 --- a/src/routing.cr +++ b/src/routing.cr @@ -1,6 +1,19 @@ require "./http-errors" +macro ip_address + env.request.headers.try &.["X-Forwarded-For"]? || env.request.remote_address.to_s.split(":").first +end + +macro protocol + env.request.headers.try &.["X-Forwarded-Proto"]? || "http" +end + +macro host + env.request.headers.try &.["X-Forwarded-Host"]? || env.request.headers["Host"] +end + module Routing + extend self @@exit_nodes = Array(String).new if CONFIG.blockTorAddresses spawn do @@ -16,17 +29,15 @@ module Routing end end before_post do |env| - ip_address = env.request.headers.try &.["X-Forwarded-For"]? ? env.request.headers.["X-Forwarded-For"] : env.request.remote_address.to_s.split(":").first if @@exit_nodes.includes?(ip_address) halt env, status_code: 401, response: error401(CONFIG.torMessage) end end end - def self.register_all + def register_all get "/" do |env| files_hosted = SQL.query_one "SELECT COUNT (filename) FROM files", as: Int32 - host = env.request.headers["Host"] render "src/views/index.ecr" end @@ -38,12 +49,6 @@ module Routing Handling.upload_url(env) end - if CONFIG.adminEnabled - post "/api/admin/delete" do |env| - Handling::Admin.delete_file(env) - end - end - get "/:filename" do |env| Handling.retrieve_file(env) end @@ -63,5 +68,15 @@ module Routing get "/sharex.sxcu" do |env| Handling.sharex_config(env) end + + self.register_admin + end + + def register_admin + if CONFIG.adminEnabled + post "/api/admin/delete" do |env| + Handling::Admin.delete_file(env) + end + end end end diff --git a/src/utils.cr b/src/utils.cr index 3737bf0..0d8358d 100644 --- a/src/utils.cr +++ b/src/utils.cr @@ -87,11 +87,11 @@ module Utils # end # end - def hash_file(file_path : String) + def hash_file(file_path : String) : String Digest::SHA1.hexdigest &.file(file_path) end - def hash_io(file_path : IO) + def hash_io(file_path : IO) : String Digest::SHA1.hexdigest &.update(file_path) end @@ -191,12 +191,12 @@ module Utils resp = HTTP::Client.get(CONFIG.torExitNodesUrl) do |res| if res.success? && res.status_code == 200 begin - File.open(CONFIG.torExitNodesFile, "w") do |output| - IO.copy(res.body_io, output) + File.open(CONFIG.torExitNodesFile, "w") do |output| + IO.copy(res.body_io, output) + end + rescue ex + LOGGER.error "Failed to write to file: #{ex.message}" end - rescue ex - LOGGER.error "Failed to write to file: #{ex.message}" - end else LOGGER.error "Failed to retrieve exit nodes list. Status Code: #{res.status_code}" end diff --git a/src/views/index.ecr b/src/views/index.ecr index a001b8c..7d54c47 100644 --- a/src/views/index.ecr +++ b/src/views/index.ecr @@ -22,11 +22,22 @@

- Chatterino Config | ShareX Config | file-uploader-crystal (BETA <%= CURRENT_TAG %> - <%= CURRENT_VERSION %> @ <%= CURRENT_BRANCH %>) + Chatterino Config | + ShareX Config | + + file-uploader-crystal (BETA <%= CURRENT_TAG %> - <%= CURRENT_VERSION %> @ <%= CURRENT_BRANCH %>) +

Archivos alojados: <%= files_hosted %>

<% if CONFIG.blockTorAddresses %>

<%= CONFIG.torMessage %>

+ <% end %> + <% if !CONFIG.alternativeDomains.empty? %> +

+ <% CONFIG.alternativeDomains.each do | domain | %> + <%= domain %> + <% end %> +

<% end %>