-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
201 additions
and
4 deletions.
There are no files selected for viewing
3 changes: 0 additions & 3 deletions
3
pcbridge-http/src/main/kotlin/com/projectcitybuild/pcbridge/http/RetrofitExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,6 @@ | ||
package com.projectcitybuild.pcbridge.http | ||
|
||
import com.projectcitybuild.pcbridge.http.requests.MojangRequest | ||
import com.projectcitybuild.pcbridge.http.requests.PCBRequest | ||
import retrofit2.Retrofit | ||
|
||
internal fun Retrofit.pcb() = create(PCBRequest::class.java) | ||
|
||
internal fun Retrofit.mojang() = create(MojangRequest::class.java) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
pcbridge-spigot/src/main/kotlin/com/projectcitybuild/pcbridge/WebServerDelegate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.projectcitybuild.pcbridge | ||
|
||
import com.projectcitybuild.pcbridge.webserver.HttpServerDelegate | ||
import java.util.UUID | ||
|
||
class WebServerDelegate() : HttpServerDelegate { | ||
|
||
override fun syncPlayer(uuid: UUID) { | ||
// val player = minecraftServer.onlinePlayers.firstOrNull { | ||
// it.uniqueId.toString().unformatted() == uuid.unformatted() | ||
// } | ||
// if (player == null) { | ||
// logger.info("No matching player found for sync request UUID: $uuid") | ||
// return | ||
// } | ||
// | ||
// log.info { "Syncing player: $uuid" } | ||
// | ||
// scheduler.async<Result<Unit, UpdatePlayerGroups.FailureReason>> { resolver -> | ||
// CoroutineScope(Dispatchers.IO).launch { | ||
// val result = updatePlayerGroups.execute(playerUUID = player.uniqueId) | ||
// resolver(result) | ||
// } | ||
// }.startAndSubscribe { result -> | ||
// when (result) { | ||
// is Failure -> when (result.reason) { | ||
// UpdatePlayerGroups.FailureReason.ACCOUNT_NOT_LINKED | ||
// -> player.send().error("Your rank failed to be updated. Please contact a staff member") | ||
// } | ||
// | ||
// is Success -> { | ||
// player.send().success("Your rank has been updated") | ||
// } | ||
// } | ||
// } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
plugins { | ||
application | ||
} | ||
|
||
application { | ||
mainClass.set("com.projectcitybuild.pcbridge.ApplicationKt") | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation(project(":pcbridge-http")) | ||
|
||
implementation("io.ktor:ktor-server-core:2.2.4") | ||
implementation("io.ktor:ktor-server-netty:2.2.4") | ||
implementation("io.ktor:ktor-server-call-logging:2.2.4") | ||
implementation("io.ktor:ktor-server-auth:2.2.4") | ||
} |
108 changes: 108 additions & 0 deletions
108
pcbridge-web-server/src/main/kotlin/com/projectcitybuild/pcbridge/webserver/HttpServer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package com.projectcitybuild.pcbridge.webserver | ||
|
||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.server.application.call | ||
import io.ktor.server.application.install | ||
import io.ktor.server.auth.Authentication | ||
import io.ktor.server.auth.UserIdPrincipal | ||
import io.ktor.server.auth.authenticate | ||
import io.ktor.server.auth.bearer | ||
import io.ktor.server.engine.embeddedServer | ||
import io.ktor.server.netty.Netty | ||
import io.ktor.server.netty.NettyApplicationEngine | ||
import io.ktor.server.plugins.callloging.CallLogging | ||
import io.ktor.server.request.receiveParameters | ||
import io.ktor.server.response.respond | ||
import io.ktor.server.routing.post | ||
import io.ktor.server.routing.routing | ||
import org.slf4j.LoggerFactory | ||
import org.slf4j.event.Level | ||
import java.math.BigInteger | ||
import java.util.UUID | ||
|
||
interface HttpServerDelegate { | ||
fun syncPlayer(uuid: UUID) | ||
} | ||
|
||
class HttpServer( | ||
private val config: HttpServerConfig, | ||
private val delegate: HttpServerDelegate, | ||
) { | ||
private var server: NettyApplicationEngine? = null | ||
|
||
private val log | ||
get() = LoggerFactory.getLogger("PCBridge.WebServer") | ||
|
||
fun start() { | ||
if (config.authToken.isEmpty()) { | ||
log.warn("Internal web server token not set in config. Web server will not be started") | ||
return | ||
} | ||
|
||
log.info("Starting web server on port ${config.port}") | ||
|
||
server?.stop(gracePeriodMillis = 0, timeoutMillis = 0) | ||
server = embeddedServer(Netty, port = config.port) { | ||
install(CallLogging) { | ||
level = Level.INFO | ||
} | ||
install(Authentication) { | ||
bearer("token") { | ||
authenticate { token -> | ||
if (token.token == config.authToken) { | ||
UserIdPrincipal("pcb-web") | ||
} else { | ||
null | ||
} | ||
} | ||
} | ||
} | ||
routing { | ||
authenticate("token") { | ||
post("events/player/sync") { | ||
val body = call.receiveParameters() | ||
|
||
val rawUUID = body["uuid"] | ||
if (rawUUID.isNullOrEmpty()) { | ||
call.application.environment.log.info("Bad Request: No UUID provided") | ||
call.respond(HttpStatusCode.BadRequest, "UUID cannot be empty") | ||
return@post | ||
} | ||
|
||
val uuid = try { | ||
uuidFromAnyString(rawUUID) | ||
} catch (e: Exception) { | ||
call.application.environment.log.info("Bad Request: Invalid UUID") | ||
call.respond(HttpStatusCode.BadRequest, "Invalid UUID") | ||
return@post | ||
} | ||
|
||
call.application.environment.log.info("Syncing player: $uuid") | ||
delegate.syncPlayer(uuid) | ||
call.respond(HttpStatusCode.OK) | ||
} | ||
} | ||
} | ||
}.start(wait = false) | ||
} | ||
|
||
fun stop() { | ||
log.info("Stopping web server") | ||
|
||
server?.stop(gracePeriodMillis = 0, timeoutMillis = 0) | ||
server = null | ||
} | ||
} | ||
|
||
/** | ||
* Same as UUID.fromString(), except that it allows a non-hyphen string | ||
*/ | ||
private fun uuidFromAnyString(string: String) = string.run { | ||
if (string.contains("-")) { | ||
UUID.fromString(string) | ||
} else { | ||
val bi1 = BigInteger(substring(0, 16), 16) | ||
val bi2 = BigInteger(substring(16, 32), 16) | ||
UUID(bi1.toLong(), bi2.toLong()) | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...ge-web-server/src/main/kotlin/com/projectcitybuild/pcbridge/webserver/HttpServerConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.projectcitybuild.pcbridge.webserver | ||
|
||
data class HttpServerConfig( | ||
val authToken: String, | ||
val port: Int, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ rootProject.name = "pcbridge" | |
|
||
include("pcbridge-http") | ||
include("pcbridge-spigot") | ||
include("pcbridge-web-server") |