DevUpdateServer: Simple code cleanup change.

This commit is contained in:
Kenneth Endfinger
2021-12-23 02:45:53 -05:00
parent ad8c82725b
commit 4284791804

View File

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