make downloads and likes unique to ips

This commit is contained in:
Jill 2023-01-07 19:31:40 +03:00
parent ad03ea6a94
commit 573892dbd0
7 changed files with 41 additions and 3 deletions

View File

@ -16,6 +16,9 @@ append_path = ""
# for dev environments, "localhost:8080" will do
# otherwise, try something like "gdpstest.oat.zone"
hostname = "localhost:8080"
# if you're putting the server behind nginx or cloudflare,
# keep this on to keep ips accurate
trust_proxy = true
[formatting]
# whether to format dates as relative or absolute

View File

@ -0,0 +1,9 @@
-- +migrate up
CREATE TABLE ip_actions (
action TEXT NOT NULL,
value TEXT NOT NULL,
ip TEXT NOT NULL DEFAULT 0
);
-- +migrate down
DROP TABLE ip_actions;

View File

@ -24,6 +24,7 @@ require "./lib/templates"
require "./lib/reupload"
require "./lib/creator_points"
require "./lib/versions"
require "./lib/ips"
if File.exists?(".env")
Dotenv.load

View File

@ -153,7 +153,11 @@ CrystalGauntlet.endpoints["/downloadGJLevel22.php"] = ->(context : HTTP::Server:
end
if level_exists
DATABASE.exec "update levels set downloads = downloads + 1 where id = ?", level_id
ip = IPs.get_real_ip(context.request)
if DATABASE.scalar("select count(*) from ip_actions where action = ? and value = ? and ip = ? limit 1", "download", level_id, ip).as?(Int64) == 0
DATABASE.exec("update levels set downloads = downloads + 1 where id = ?", level_id)
DATABASE.exec("insert into ip_actions (action, value, ip) values (?, ?, ?)", "download", level_id, ip)
end
end
response.join("#")

View File

@ -27,12 +27,19 @@ CrystalGauntlet.endpoints["/likeGJItem211.php"] = ->(context : HTTP::Server::Con
when 3 # account comments
table = "account_comments"
column = "id"
else
return "-1"
end
is_like = (params["isLike"]? || "1").to_i
sign = is_like == 1 ? '+' : '-'
DATABASE.exec "update #{table} set likes = likes #{sign} 1 where #{column} = ?", item_id
ip = IPs.get_real_ip(context.request)
if DATABASE.scalar("select count(*) from ip_actions where action = ? and value = ? and ip = ? limit 1", "like_#{type}", item_id, ip).as?(Int64) == 0
DATABASE.exec("update #{table} set likes = likes #{sign} 1 where #{column} = ?", item_id)
DATABASE.exec("insert into ip_actions (action, value, ip) values (?, ?, ?)", "like_#{type}", item_id, ip)
end
"1"
}

14
src/lib/ips.cr Normal file
View File

@ -0,0 +1,14 @@
include CrystalGauntlet
module CrystalGauntlet::IPs
extend self
# todo: this could be better
def get_real_ip(req : HTTP::Request)
if config_get("trust_proxy").as?(Bool) && req.headers.has_key?("X-Forwarded-For")
req.headers.get("X-Forwarded-For").first.split(",").first.strip
else
req.remote_address.to_s.split(":").first
end
end
end

View File

@ -11,7 +11,7 @@ module CrystalGauntlet::Versions
when 10
SemanticVersion.new(1, 7, 0)
else
SemanticVersion.new(game_version[0].to_i, game_version[1].to_i, game_version[2..].to_i)
SemanticVersion.new(game_version[0].to_i? || 0, game_version[1].to_i? || 0, game_version[2..].to_i? || 0)
end
end