Add limit to prevent abuse
This commit is contained in:
parent
a892f7f972
commit
40b9093337
@ -1 +1,2 @@
|
|||||||
/data/logs/festival/server.log
|
/data/logs/festival/server.log
|
||||||
|
150
|
||||||
|
@ -34,6 +34,7 @@ enum Log {
|
|||||||
|
|
||||||
static func write(_ message: String) {
|
static func write(_ message: String) {
|
||||||
guard let f = file else {
|
guard let f = file else {
|
||||||
|
print(message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
f.write(message.data(using: .utf8)!)
|
f.write(message.data(using: .utf8)!)
|
||||||
|
@ -10,6 +10,8 @@ private var guestListPath: URL!
|
|||||||
|
|
||||||
private var declinedListPath: URL!
|
private var declinedListPath: URL!
|
||||||
|
|
||||||
|
private var maximumGuestCount = 100
|
||||||
|
|
||||||
private let df: DateFormatter = {
|
private let df: DateFormatter = {
|
||||||
let df = DateFormatter()
|
let df = DateFormatter()
|
||||||
df.dateFormat = "dd.MM. HH:mm"
|
df.dateFormat = "dd.MM. HH:mm"
|
||||||
@ -20,6 +22,7 @@ func guestCount() -> Int {
|
|||||||
registeredGuests
|
registeredGuests
|
||||||
.reduce([]) { $0 + $1.components(separatedBy: "+") }
|
.reduce([]) { $0 + $1.components(separatedBy: "+") }
|
||||||
.reduce([]) { $0 + $1.components(separatedBy: " - ") }
|
.reduce([]) { $0 + $1.components(separatedBy: " - ") }
|
||||||
|
.reduce([]) { $0 + $1.components(separatedBy: ",") }
|
||||||
.reduce([]) { $0 + $1.components(separatedBy: " und ") }
|
.reduce([]) { $0 + $1.components(separatedBy: " und ") }
|
||||||
.reduce([]) { $0 + $1.components(separatedBy: "&") }
|
.reduce([]) { $0 + $1.components(separatedBy: "&") }
|
||||||
//.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
|
//.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
|
||||||
@ -28,6 +31,9 @@ func guestCount() -> Int {
|
|||||||
|
|
||||||
|
|
||||||
func add(guest: String) -> String {
|
func add(guest: String) -> String {
|
||||||
|
guard registeredGuests.count < maximumGuestCount else {
|
||||||
|
return "Too many requests"
|
||||||
|
}
|
||||||
registeredGuests.insert(guest)
|
registeredGuests.insert(guest)
|
||||||
declinedGuests.remove(guest)
|
declinedGuests.remove(guest)
|
||||||
defer { saveLists() }
|
defer { saveLists() }
|
||||||
@ -35,6 +41,9 @@ func add(guest: String) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func remove(guest: String) -> String {
|
func remove(guest: String) -> String {
|
||||||
|
guard declinedGuests.count < maximumGuestCount else {
|
||||||
|
return "Too many requests"
|
||||||
|
}
|
||||||
registeredGuests.remove(guest)
|
registeredGuests.remove(guest)
|
||||||
declinedGuests.insert(guest)
|
declinedGuests.insert(guest)
|
||||||
defer { saveLists() }
|
defer { saveLists() }
|
||||||
@ -98,31 +107,43 @@ private func createFileIfNeeded(at path: URL) throws {
|
|||||||
try Data().write(to: path)
|
try Data().write(to: path)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func readConfig(at path: URL) throws -> URL {
|
private func readConfig(at path: URL) throws -> String {
|
||||||
do {
|
do {
|
||||||
let logPath = try String(contentsOf: path)
|
let content = try String(contentsOf: path)
|
||||||
.trimmingCharacters(in: .whitespacesAndNewlines)
|
.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
return URL(fileURLWithPath: logPath)
|
return content
|
||||||
} catch {
|
} catch {
|
||||||
log("Failed to read configuration file at \(path.path): \(error)")
|
log("Failed to read configuration file at \(path.path): \(error)")
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// configures your application
|
private func configureFromFile(at configPath: URL) throws {
|
||||||
|
let config = try readConfig(at: configPath)
|
||||||
|
.components(separatedBy: "\n")
|
||||||
|
.map { $0.trimmingCharacters(in: .whitespaces) }
|
||||||
|
.filter { !$0.isEmpty }
|
||||||
|
guard config.count == 2 else {
|
||||||
|
log("Invalid configuration file at \(configPath.path)")
|
||||||
|
throw FestivalError.invalidConfiguration
|
||||||
|
}
|
||||||
|
try Log.set(logFile: config[0])
|
||||||
|
guard let count = Int(config[1]) else {
|
||||||
|
log("Invalid maximum guest count '\(config[1])', using default")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
maximumGuestCount = count
|
||||||
|
}
|
||||||
|
|
||||||
public func configure(_ app: Application) throws {
|
public func configure(_ app: Application) throws {
|
||||||
// uncomment to serve files from /Public folder
|
|
||||||
// app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))
|
|
||||||
app.http.server.configuration.port = 9001
|
app.http.server.configuration.port = 9001
|
||||||
|
|
||||||
let configPath = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
let configPath = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
||||||
.appendingPathComponent("paths.conf")
|
.appendingPathComponent("paths.conf")
|
||||||
let logFile = try readConfig(at: configPath)
|
try configureFromFile(at: configPath)
|
||||||
|
|
||||||
let listDirectory = URL(fileURLWithPath: app.directory.publicDirectory)
|
let listDirectory = URL(fileURLWithPath: app.directory.publicDirectory)
|
||||||
.appendingPathComponent("lists")
|
.appendingPathComponent("lists")
|
||||||
|
|
||||||
try Log.set(logFile: logFile.path)
|
|
||||||
|
|
||||||
let eventLog = listDirectory.appendingPathComponent("events.txt")
|
let eventLog = listDirectory.appendingPathComponent("events.txt")
|
||||||
guestListPath = listDirectory.appendingPathComponent("registered.txt")
|
guestListPath = listDirectory.appendingPathComponent("registered.txt")
|
||||||
declinedListPath = listDirectory.appendingPathComponent("declined.txt")
|
declinedListPath = listDirectory.appendingPathComponent("declined.txt")
|
||||||
|
Loading…
Reference in New Issue
Block a user