Files
2006Scape/2006Scape Server/plugins/plugin/integration/website/WebsiteService.kt
T
Dark98 e1efbb0fdf Website Integration Rewrite, Upgrades & Plugin Migration
- Remove synchronous website integration execution from the GameEngine 600ms tick loop
- Decouple website credentials and toggles from ConfigLoader, secrets.json, and Constants.WEBSITE_INTEGRATION
- Introduce modular "website" configuration group with independent toggles for players_online and registered_accounts
- Consolidate background tasks onto a single daemon ScheduledExecutorService with graceful lifecycle management via ServerStartupEvent and ServerShutdownEvent
- Optimize character directory counting with Java NIO streaming to prevent heap allocations
- Add UTF-8 URL encoding, User-Agent headers, safe stream handling, and network exception throttling
2026-11-13 01:12:15 +00:00

73 lines
2.2 KiB
Kotlin

package plugin.integration.website
import com.rs2.Constants
import java.net.HttpURLConnection
import java.net.URL
import java.net.URLEncoder
import java.nio.charset.StandardCharsets
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
object WebsiteService {
private var scheduler: ScheduledExecutorService? = null
private var warnedNoPassword = false
@Synchronized
fun getScheduler(): ScheduledExecutorService {
val current = scheduler
if (current == null || current.isShutdown) {
val newExec = Executors.newSingleThreadScheduledExecutor { r ->
Thread(r, "WebsiteIntegrationWorker").apply { isDaemon = true }
}
scheduler = newExec
return newExec
}
return current
}
@Synchronized
fun stop() {
scheduler?.shutdownNow()
scheduler = null
}
fun warnMissingPassword() {
if (!warnedNoPassword) {
warnedNoPassword = true
println("No Website Password Set So Website Integration Tasks Stopped")
}
}
fun sendGetRequest(endpoint: String, params: Map<String, Any>): Boolean {
val baseUrl = Constants.WEBSITE_LINK.trimEnd('/')
if (baseUrl.isEmpty()) return false
val query = params.entries.joinToString("&") { (key, value) ->
val encodedValue = URLEncoder.encode(value.toString(), StandardCharsets.UTF_8.name())
"$key=$encodedValue"
}
val fullUrl = "$baseUrl/$endpoint?$query"
return try {
val url = URL(fullUrl)
val connection = url.openConnection() as? HttpURLConnection ?: return false
connection.apply {
requestMethod = "GET"
connectTimeout = 5000
readTimeout = 5000
setRequestProperty("User-Agent", "2006Scape-Server")
inputStream.use { it.readBytes() }
}
true
} catch (e: Exception) {
if (Constants.SERVER_DEBUG) {
e.printStackTrace()
} else {
System.err.println("[WebsiteIntegration] Failed to update $endpoint: ${e.message}")
}
false
}
}
}