com.simiacryptus.skyenet.webui.servlet.SessionShareServlet.kt Maven / Gradle / Ivy
package com.simiacryptus.skyenet.webui.servlet
import com.simiacryptus.util.JsonUtil
import com.simiacryptus.skyenet.core.platform.ApplicationServices
import com.simiacryptus.skyenet.core.platform.ApplicationServices.authenticationManager
import com.simiacryptus.skyenet.core.platform.ApplicationServices.authorizationManager
import com.simiacryptus.skyenet.core.platform.ApplicationServices.cloud
import com.simiacryptus.skyenet.core.platform.ApplicationServicesConfig.dataStorageRoot
import com.simiacryptus.skyenet.core.platform.AuthorizationInterface.OperationType
import com.simiacryptus.skyenet.core.platform.StorageInterface
import com.simiacryptus.skyenet.core.platform.StorageInterface.Companion.long64
import com.simiacryptus.skyenet.core.platform.User
import com.simiacryptus.skyenet.core.util.Selenium
import com.simiacryptus.skyenet.webui.application.ApplicationServer
import com.simiacryptus.skyenet.webui.application.ApplicationServer.Companion.getCookie
import com.simiacryptus.skyenet.util.Selenium2S3
import jakarta.servlet.http.HttpServlet
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import org.apache.http.client.HttpClient
import org.apache.http.impl.client.HttpClients
import java.net.URI
import kotlin.reflect.jvm.javaType
import kotlin.reflect.typeOf
import com.google.zxing.BarcodeFormat
import com.google.zxing.qrcode.QRCodeWriter
import com.google.zxing.client.j2se.MatrixToImageWriter
import java.io.ByteArrayOutputStream
import java.util.Base64
class SessionShareServlet(
private val server: ApplicationServer,
) : HttpServlet() {
private fun generateQRCodeDataURL(url: String): String {
val qrCodeWriter = QRCodeWriter()
val bitMatrix = qrCodeWriter.encode(url, BarcodeFormat.QR_CODE, 200, 200)
val outputStream = ByteArrayOutputStream()
MatrixToImageWriter.writeToStream(bitMatrix, "PNG", outputStream)
val imageBytes = outputStream.toByteArray()
val base64Image = Base64.getEncoder().encodeToString(imageBytes)
return "data:image/png;base64,$base64Image"
}
override fun doGet(req: HttpServletRequest, resp: HttpServletResponse) {
val user = authenticationManager.getUser(req.getCookie())
val cookies = req.cookies
if (!req.parameterMap.containsKey("url")) {
resp.status = HttpServletResponse.SC_BAD_REQUEST
resp.writer.write("Url is required")
return
}
val url = req.getParameter("url")
val host = URI(url).host
val appName = url.split("/").dropLast(1).last()
val sessionID = url.split("#").lastOrNull() ?: throw IllegalArgumentException("No session id in url: $url")
require(acceptHost(user, host)) { "Invalid url: $url" }
val storageInterface = ApplicationServices.dataStorageFactory(dataStorageRoot)
val session = StorageInterface.parseSessionID(sessionID)
val pool = ApplicationServices.clientManager.getPool(session, user)
val infoFile = storageInterface.getDataDir(user, session).resolve("info.json").apply { parentFile.mkdirs() }
val json = if (infoFile.exists()) JsonUtil.fromJson