2020-11-11 22:49:55 +00:00
|
|
|
import Vapor
|
|
|
|
import Foundation
|
2020-11-12 21:23:25 +00:00
|
|
|
import Crypto
|
2020-11-11 22:49:55 +00:00
|
|
|
import Leaf
|
|
|
|
|
|
|
|
func routes(_ app: Application) throws {
|
|
|
|
// Render home page when root domain is loaded
|
|
|
|
app.get { req -> EventLoopFuture<View> in
|
|
|
|
let fileData = try String(contentsOfFile: "\(app.directory.resourcesDirectory)/config.json").data(using: .utf8)
|
|
|
|
let config: Config = try! JSONDecoder().decode(Config.self, from: fileData!)
|
2020-11-12 21:23:25 +00:00
|
|
|
let loginStatus = req.session.data["loggedIn"] ?? "false"
|
|
|
|
let loginBool = loginStatus == "true" ? true : false
|
|
|
|
return req.view.render("home", LContext(config: config, loggedIn: loginBool))
|
2020-11-11 22:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
app.get("status", ":url") { req -> EventLoopFuture<[String: String]> in
|
|
|
|
let url = URL(string: req.parameters.get("url")!)
|
|
|
|
return req.client.get("https://isitdown.site/api/v3/\(url!)").flatMapThrowing { res in
|
|
|
|
try res.content.decode(isItUp.self)
|
|
|
|
}.map { json in
|
|
|
|
["down": String(json.isitdown), "code": String(json.response_code)]
|
|
|
|
}
|
|
|
|
}
|
2020-11-12 21:23:25 +00:00
|
|
|
|
|
|
|
app.get("login") { req -> EventLoopFuture<View> in
|
|
|
|
let fileData = try String(contentsOfFile: "\(app.directory.resourcesDirectory)/config.json").data(using: .utf8)
|
|
|
|
let config: Config = try! JSONDecoder().decode(Config.self, from: fileData!)
|
|
|
|
return req.view.render("login", LContext(config: config, loggedIn: false))
|
|
|
|
}
|
|
|
|
|
|
|
|
app.post("login") { req -> Response in
|
|
|
|
let data = try req.content.decode(Login.self)
|
|
|
|
let fileData = try String(contentsOfFile: "\(app.directory.resourcesDirectory)/config.json").data(using: .utf8)
|
|
|
|
let config: Config = try! JSONDecoder().decode(Config.self, from: fileData!)
|
|
|
|
let loginPassData = data.password?.data(using: .utf8)
|
|
|
|
let loginPassHash = SHA256.hash(data: loginPassData ?? "".data(using: .utf8)!)
|
|
|
|
let stringHash = loginPassHash.map { String(format: "%02hhx", $0) }.joined()
|
|
|
|
print("Recv: \(stringHash)")
|
|
|
|
print("Conf: \(config.passwordHash)")
|
|
|
|
if stringHash == config.passwordHash {
|
|
|
|
req.session.data["loggedIn"] = "true"
|
|
|
|
return try req.redirect(to: "/")
|
|
|
|
} else {
|
|
|
|
throw Abort(.unauthorized)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
app.get("logout") { req -> Response in
|
|
|
|
req.session.destroy()
|
|
|
|
return try req.redirect(to: "/")
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-11 22:49:55 +00:00
|
|
|
}
|