0.8.6: Alternative domains and less useless variables

This commit is contained in:
Fijxu 2024-08-12 23:17:04 -04:00
parent 6f17589536
commit 4ed07ccecb
Signed by: Fijxu
GPG key ID: 32C1DDF333EDA6A4
7 changed files with 58 additions and 31 deletions

View file

@ -34,3 +34,7 @@ opengraphUseragents:
- "chatterino-api-cache/"
- "FFZBot/"
- "Twitterbot/"
alternativeDomains:
- "ayaya.beauty"
- "lamartina.gay"

View file

@ -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"

View file

@ -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

View file

@ -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",

View file

@ -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

View file

@ -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

View file

@ -22,11 +22,22 @@
<div>
<div style="text-align:center;">
<p>
<a href='./chatterino.png'>Chatterino Config</a> | <a href='./sharex.sxcu'>ShareX Config</a> | <a href='https://codeberg.org/Fijxu/file-uploader-crystal'>file-uploader-crystal (BETA <%= CURRENT_TAG %> - <%= CURRENT_VERSION %> @ <%= CURRENT_BRANCH %>)</a>
<a href='./chatterino.png'>Chatterino Config</a> |
<a href='./sharex.sxcu'>ShareX Config</a> |
<a href='https://codeberg.org/Fijxu/file-uploader-crystal'>
file-uploader-crystal (BETA <%= CURRENT_TAG %> - <%= CURRENT_VERSION %> @ <%= CURRENT_BRANCH %>)
</a>
</p>
<p>Archivos alojados: <%= files_hosted %></p>
<% if CONFIG.blockTorAddresses %>
<p style="color: red"><%= CONFIG.torMessage %></p>
<% end %>
<% if !CONFIG.alternativeDomains.empty? %>
<p>
<% CONFIG.alternativeDomains.each do | domain | %>
<a href="https://<%= domain %>"><%= domain %></a>
<% end %>
</p>
<% end %>
</div>
</body>