Skip to content

Commit

Permalink
yes
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCGuyGitHub committed Sep 24, 2024
1 parent 2112b7e commit 9850636
Show file tree
Hide file tree
Showing 8 changed files with 348 additions and 43 deletions.
3 changes: 2 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ dependencies {
compileOnly("eu.cloudnetservice.cloudnet:node:4.0.0-RC10")
compileOnly("eu.cloudnetservice.cloudnet:bridge:4.0.0-RC10")

compileOnly("com.github.cloudnetservice.cloud-command-framework:cloud-core:1.9.0-cn1")
compileOnly("com.github.cloudnetservice.cloud-command-framework:cloud-annotations:1.9.0-cn1")
compileOnly("com.github.cloudnetservice.cloud-command-framework:cloud-core:2.0.0-cn1")
compileOnly("com.github.cloudnetservice.cloud-command-framework:cloud-annotations:2.0.0-cn1")

implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version")
implementation("io.ktor:ktor-server-core:$ktor_version")
Expand All @@ -36,6 +36,8 @@ dependencies {
implementation("io.ktor:ktor-server-content-negotiation:$ktor_version")
implementation("io.ktor:ktor-server-auth:$ktor_version")
implementation("io.ktor:ktor-server-auth-jwt:$ktor_version")
implementation("io.ktor:ktor-server-websockets:$ktor_version")
implementation("io.ktor:ktor-server-cors:$ktor_version")

implementation("org.mariadb.jdbc:mariadb-java-client:3.4.0")

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package io.github.thecguy.cloudnet_rest_module.commands

import cloud.commandframework.annotations.Argument
import cloud.commandframework.annotations.CommandMethod
import cloud.commandframework.annotations.CommandPermission
import cloud.commandframework.annotations.parsers.Parser
import cloud.commandframework.annotations.suggestions.Suggestions
import cloud.commandframework.context.CommandContext
import org.incendo.cloud.annotations.Argument
import org.incendo.cloud.annotations.parser.Parser
import org.incendo.cloud.annotations.suggestion.Suggestions
import org.incendo.cloud.context.CommandContext
import org.incendo.cloud.annotations.Permission

import eu.cloudnetservice.common.language.I18n
import eu.cloudnetservice.driver.inject.InjectionLayer
Expand All @@ -17,14 +16,15 @@ import eu.cloudnetservice.node.command.source.CommandSource
import io.github.thecguy.cloudnet_rest_module.utli.DBManager

import jakarta.inject.Singleton
import org.incendo.cloud.annotations.Command

import org.jetbrains.annotations.NotNull

import java.util.*


@Singleton
@CommandPermission("thecguy.test")
@Permission("thecguy.rest")
@Description("test")
class rest {
private val dbManager = DBManager()
Expand All @@ -41,14 +41,14 @@ class rest {
return taskProvider.serviceTasks().toList()
}

@CommandMethod("rest users")
@Command("rest users")
fun users(
source: CommandSource,
) {
source.sendMessage(I18n.trans("module-rest-command-users"))
source.sendMessage(dbManager.cmd_rest_users())
}
@CommandMethod("rest user create <username> <password>")
@Command("rest user create <username> <password>")
fun createUser(
source: CommandSource,
@NotNull @Argument("username") username: String,
Expand All @@ -63,7 +63,7 @@ class rest {
source.sendMessage(I18n.trans("module-rest-command-createduser"))
}
}
@CommandMethod("rest user delete <username>")
@Command("rest user delete <username>")
fun deleteUser(
source: CommandSource,
@NotNull @Argument("username") username: String
Expand All @@ -76,7 +76,7 @@ class rest {
source.sendMessage(I18n.trans("module-rest-command-usernotexist"))
}
}
@CommandMethod("rest user user <username> add permission <permission>")
@Command("rest user user <username> add permission <permission>")
fun addPermsToUser(
source: CommandSource,
@NotNull @Argument("username") username: String,
Expand All @@ -95,7 +95,7 @@ class rest {
source.sendMessage(I18n.trans("module-rest-command-usernotexist"))
}
}
@CommandMethod("rest user user <username> remove permission <permission>")
@Command("rest user user <username> remove permission <permission>")
fun remPerms(
source: CommandSource,
@NotNull @Argument("username") username: String,
Expand All @@ -114,4 +114,5 @@ class rest {
source.sendMessage(I18n.trans("module-rest-command-usernotexist"))
}
}

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package io.github.thecguy.cloudnet_rest_module.utli


import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.security.SecureRandom


class AuthUtil internal constructor() {
val dbManager = DBManager()
private val dbManager = DBManager()
private val Logger: Logger = LoggerFactory.getLogger(AuthUtil::class.java)

init {

init {
println("init success!")
}


fun generateToken(length: Int): String {
val characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/*-+-_!"
val characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/*-_!"
val random = SecureRandom()
val token = StringBuilder(length)

Expand All @@ -29,6 +34,7 @@ class AuthUtil internal constructor() {
}

fun authToken(token: String, permission: String): Boolean {
Logger.info(token)
return if (validToken(token)) {
val user = dbManager.tokenToUser(token)
val perms = dbManager.cmd_rest_perms(user)
Expand All @@ -42,8 +48,9 @@ class AuthUtil internal constructor() {
}
}

private fun validToken(token: String): Boolean {
fun validToken(token: String): Boolean {
val tokens = dbManager.tokens()
Logger.info(tokens.toString())
return tokens.contains(token)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ class DBManager internal constructor() {


init {
val hikariLogger = LoggerFactory.getLogger("com.zaxxer.hikari") as ch.qos.logback.classic.Logger
hikariLogger.level = Level.DEBUG

val config = HikariConfig()

val host = "127.0.0.1"

val host = "192.168.0.204"
val port = 3306
val database = "cloudnet_rest"
val username = "cloudnet"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.github.thecguy.cloudnet_rest_module.utli

import eu.cloudnetservice.driver.cluster.NodeInfoSnapshot
import eu.cloudnetservice.driver.provider.ServiceTaskProvider
import eu.cloudnetservice.node.service.CloudServiceManager
import io.github.thecguy.cloudnet_rest_module.config.Configuration
import kong.unirest.core.json.JSONArray
import kong.unirest.core.json.JSONObject
import org.jetbrains.annotations.NotNull
Expand Down Expand Up @@ -150,8 +150,38 @@ class JsonUtils internal constructor() {
return jTask
}



fun nodeInfo(nodeInfoSnapshot: NodeInfoSnapshot): JSONObject {
val nodeInfo = JSONObject()

val nNode = JSONObject()
nNode.put("listeners", nodeInfoSnapshot.node().listeners())
nNode.put("uniqueId", nodeInfoSnapshot.node().uniqueId())
nodeInfo.put("node", nNode)

nodeInfo.put("modules", nodeInfoSnapshot.modules())
nodeInfo.put("creationTime", nodeInfoSnapshot.creationTime())
nodeInfo.put("version", nodeInfoSnapshot.version())
nodeInfo.put("currentServicesCount", nodeInfoSnapshot.currentServicesCount())
nodeInfo.put("draining", nodeInfoSnapshot.draining())
nodeInfo.put("maxMemory", nodeInfoSnapshot.maxMemory())
nodeInfo.put("maxProcessorUsageToStartServices", nodeInfoSnapshot.maxProcessorUsageToStartServices())
nodeInfo.put("reservedMemory", nodeInfoSnapshot.reservedMemory())
nodeInfo.put("startupMillis", nodeInfoSnapshot.startupMillis())
nodeInfo.put("usedMemory", nodeInfoSnapshot.usedMemory())
val processSnapshot = JSONObject()
processSnapshot.put("pid", nodeInfoSnapshot.processSnapshot().pid)
processSnapshot.put("threads", nodeInfoSnapshot.processSnapshot().threads)
processSnapshot.put("cpuUsage", nodeInfoSnapshot.processSnapshot().cpuUsage)
processSnapshot.put("maxHeapMemory", nodeInfoSnapshot.processSnapshot().maxHeapMemory)
processSnapshot.put("systemCpuUsage", nodeInfoSnapshot.processSnapshot().systemCpuUsage)
processSnapshot.put("heapUsageMemory", nodeInfoSnapshot.processSnapshot().heapUsageMemory)
processSnapshot.put("currentLoadedClassCount", nodeInfoSnapshot.processSnapshot().currentLoadedClassCount)
processSnapshot.put("totalLoadedClassCount", nodeInfoSnapshot.processSnapshot().totalLoadedClassCount)
processSnapshot.put("unloadedClassCount", nodeInfoSnapshot.processSnapshot().unloadedClassCount)
nodeInfo.put("processSnapshot", processSnapshot)

return nodeInfo
}


fun token(user: String):JSONObject {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.github.thecguy.cloudnet_rest_module.utli

import io.ktor.websocket.*
import java.util.concurrent.ConcurrentHashMap

class WebsocketManager {

object WebSocketManager {
// Store sessions by service name
private val serviceSessions: MutableMap<String, MutableSet<DefaultWebSocketSession>> = ConcurrentHashMap()

// Add a WebSocket session for a specific service
fun addSession(service: String, session: DefaultWebSocketSession) {
serviceSessions.computeIfAbsent(service) { ConcurrentHashMap.newKeySet() }.add(session)
}

// Remove a WebSocket session for a specific service
fun removeSession(service: String, session: DefaultWebSocketSession) {
serviceSessions[service]?.remove(session)
if (serviceSessions[service]?.isEmpty() == true) {
serviceSessions.remove(service) // Clean up if no sessions are left
}
}

// Send message to all open WebSocket sessions for a specific service
suspend fun broadcast(service: String, message: String) {
serviceSessions[service]?.forEach { session ->
try {
session.send(message)
} catch (e: Exception) {
e.printStackTrace() // Handle errors during message sending
}
}
}
}
}

0 comments on commit 9850636

Please sign in to comment.