DevUpdateServer: Simple code cleanup change.

This commit is contained in:
Kenneth Endfinger 2021-12-23 02:45:53 -05:00
parent ad8c82725b
commit 4284791804
No known key found for this signature in database
GPG Key ID: C4E68E5647420E10

View File

@ -45,27 +45,36 @@ class DevUpdateServer(val plugin: FoundationCorePlugin) {
val server = HttpServer.create()
server.createContext("/").setHandler { exchange ->
handle(exchange)
}
server.bind(InetSocketAddress("0.0.0.0", config.port), 0)
server.start()
this.server = server
plugin.slF4JLogger.info("DevUpdate Server listening on port ${config.port}")
}
private fun handle(exchange: HttpExchange) {
val ip = exchange.remoteAddress.address.hostAddress
if (!config.ipAllowList.contains("*") && !config.ipAllowList.contains(ip)) {
plugin.slF4JLogger.warn("DevUpdate Server received request from IP $ip which is not allowed.")
exchange.close()
return@setHandler
return
}
plugin.slF4JLogger.info("DevUpdate Server Request $ip ${exchange.requestMethod} ${exchange.requestURI.path}")
if (exchange.requestMethod != "POST") {
exchange.respond(405, "Method not allowed.")
return@setHandler
return
}
if (exchange.requestURI.path != "/webhook/update") {
exchange.respond(404, "Not Found.")
return@setHandler
return
}
if (exchange.requestURI.query != config.token) {
exchange.respond(401, "Unauthorized.")
return@setHandler
return
}
val payload: DevUpdatePayload
@ -74,7 +83,7 @@ class DevUpdateServer(val plugin: FoundationCorePlugin) {
} catch (e: Exception) {
plugin.slF4JLogger.error("Failed to decode request body.", e)
exchange.respond(400, "Bad Request")
return@setHandler
return
}
if (payload.objectKind != "pipeline" ||
@ -82,7 +91,7 @@ class DevUpdateServer(val plugin: FoundationCorePlugin) {
payload.objectAttributes["status"]?.jsonPrimitive?.content != "success"
) {
exchange.respond(200, "Event was not relevant for update.")
return@setHandler
return
}
exchange.respond(200, "Success.")
@ -96,11 +105,6 @@ class DevUpdateServer(val plugin: FoundationCorePlugin) {
}
}
}
server.bind(InetSocketAddress("0.0.0.0", config.port), 0)
server.start()
this.server = server
plugin.slF4JLogger.info("DevUpdate Server listening on port ${config.port}")
}
fun disable() {
server?.stop(0)