diff --git a/Gemfile b/Gemfile index 3397785..f1f87c4 100644 --- a/Gemfile +++ b/Gemfile @@ -7,3 +7,7 @@ gem "xxhash" gem "sequel" gem "json" + +gem "base64" + +gem "zlib" diff --git a/session.rb b/session.rb index 0eb8455..f46e36e 100644 --- a/session.rb +++ b/session.rb @@ -1,49 +1,62 @@ require "base64" +require "zlib" require "json" -def set_session(request, response, key, val) +def set_session(request, response, key, val, uid = nil) session = request.cookies["session"] - session = "e30=\n" if session.nil? - session = JSON.parse(Base64.decode64(session)) + session = session.nil? ? "{}" : Zlib::Inflate.inflate(Base64.decode64(session)) + session = JSON.parse(session) session[key] = val - session = Base64.encode64(JSON.generate(session)) + compressed = Zlib::Deflate.deflate(JSON.generate(session)) + encoded = Base64.encode64(compressed) response.set_cookie("session", - value: session, + value: encoded, path: "/", expires: Time.now + 360 * 24 * 60 * 60) -rescue JSON::ParserError + begin + DB["UPDATE SignedInUsers SET last_used_at = CURRENT_TIMESTAMP WHERE code = ?", uid].update if uid + rescue Sequel::Error => e + File.write("log/main.log", "DB Error: #{e.message} when updating last_used_at for #{uid}\n", mode: "a") + end +rescue JSON::ParserError, Zlib::Error response.delete_cookie("session") end -def get_session(request, response, key) +def get_session(request, response, key, uid = nil) session = request.cookies["session"] - session = "{}" if session.nil? - session = JSON.parse(Base64.decode64(session)) + session = session.nil? ? "{}" : Zlib::Inflate.inflate(Base64.decode64(session)) + session = JSON.parse(session) + begin + DB["UPDATE SignedInUsers SET last_used_at = CURRENT_TIMESTAMP WHERE code = ?", uid].update if uid + rescue Sequel::Error => e + File.write("log/main.log", "DB Error: #{e.message} when updating last_used_at for #{uid}\n", mode: "a") + end session[key] -rescue JSON::ParserError +rescue JSON::ParserError, Zlib::Error response.delete_cookie("session") "" end def get_session_all(request, response) session = request.cookies["session"] - session = "{}" if session.nil? - JSON.parse(Base64.decode64(session)) -rescue JSON::ParserError + session = session.nil? ? "{}" : Zlib::Inflate.inflate(Base64.decode64(session)) + JSON.parse(session) +rescue JSON::ParserError, Zlib::Error response.delete_cookie("session") "" end def rm_session(request, response, key) session = request.cookies["session"] - session = "{}" if session.nil? - session = JSON.parse(Base64.decode64(session)) + session = session.nil? ? "{}" : Zlib::Inflate.inflate(Base64.decode64(session)) + session = JSON.parse(session) session.delete(key) - session = Base64.encode64(JSON.generate(session)) + compressed = Zlib::Deflate.deflate(JSON.generate(session)) + encoded = Base64.encode64(compressed) response.set_cookie("session", - value: session, + value: encoded, path: "/", expires: Time.now + 360 * 24 * 60 * 60) -rescue JSON::ParserError +rescue JSON::ParserError, Zlib::Error response.delete_cookie("session") end