some fix WSSongListRoutes.kt

- delete websocket key
- else updated.
This commit is contained in:
dalbodeule 2024-08-05 14:38:16 +09:00
parent cc23ac03e7
commit 75da1c1576
No known key found for this signature in database
GPG Key ID: EFA860D069C9FA65
5 changed files with 34 additions and 10 deletions

View File

@ -9,7 +9,6 @@ import space.mori.chzzk_bot.common.events.*
import space.mori.chzzk_bot.common.models.User import space.mori.chzzk_bot.common.models.User
import space.mori.chzzk_bot.common.services.* import space.mori.chzzk_bot.common.services.*
import space.mori.chzzk_bot.common.utils.getFollowDate import space.mori.chzzk_bot.common.utils.getFollowDate
import space.mori.chzzk_bot.common.utils.getRandomString
import space.mori.chzzk_bot.common.utils.getUptime import space.mori.chzzk_bot.common.utils.getUptime
import space.mori.chzzk_bot.common.utils.getYoutubeVideo import space.mori.chzzk_bot.common.utils.getYoutubeVideo
import xyz.r2turntrue.chzzk4j.chat.ChatMessage import xyz.r2turntrue.chzzk4j.chat.ChatMessage
@ -259,14 +258,13 @@ class MessageHandler(
} }
val session = "${UUID.randomUUID()}${UUID.randomUUID()}".replace("-", "") val session = "${UUID.randomUUID()}${UUID.randomUUID()}".replace("-", "")
val password = getRandomString(8)
SongConfigService.updateSession(user, session, password) SongConfigService.updateSession(user, session)
bot.retrieveUserById(user.discord).queue { discordUser -> bot.retrieveUserById(user.discord).queue { discordUser ->
discordUser?.openPrivateChannel()?.queue { channel -> discordUser?.openPrivateChannel()?.queue { channel ->
channel.sendMessage("여기로 접속해주세요! https://nabot,mori.space/songlist/${session}.\n인증번호는 ||$password|| 입니다.") channel.sendMessage("여기로 접속해주세요! ||https://nabot,mori.space/songlist/${session}||.\n주소가 노출될 경우 방송을 다시 켜셔야 합니다!")
.queue() .queue()
} }
} }

View File

@ -9,7 +9,6 @@ import org.jetbrains.exposed.sql.ReferenceOption
object SongConfigs: IntIdTable("song_config") { object SongConfigs: IntIdTable("song_config") {
val user = reference("user", Users, onDelete = ReferenceOption.CASCADE) val user = reference("user", Users, onDelete = ReferenceOption.CASCADE)
val token = varchar("token", 64).nullable() val token = varchar("token", 64).nullable()
val password = varchar("password", 8).nullable()
val streamerOnly = bool("streamer_only").default(false) val streamerOnly = bool("streamer_only").default(false)
val queueLimit = integer("queue_limit").default(50) val queueLimit = integer("queue_limit").default(50)
val personalLimit = integer("personal_limit").default(5) val personalLimit = integer("personal_limit").default(5)
@ -19,7 +18,6 @@ class SongConfig(id: EntityID<Int>) : IntEntity(id) {
var user by User referencedOn SongConfigs.user var user by User referencedOn SongConfigs.user
var token by SongConfigs.token var token by SongConfigs.token
var password by SongConfigs.password
var streamerOnly by SongConfigs.streamerOnly var streamerOnly by SongConfigs.streamerOnly
var queueLimit by SongConfigs.queueLimit var queueLimit by SongConfigs.queueLimit
var personalLimit by SongConfigs.personalLimit var personalLimit by SongConfigs.personalLimit

View File

@ -60,14 +60,13 @@ object SongConfigService {
} }
} }
fun updateSession(user: User, token: String?, password: String?): SongConfig { fun updateSession(user: User, token: String?): SongConfig {
return transaction { return transaction {
var songConfig = SongConfig.find(SongConfigs.user eq user.id).firstOrNull() var songConfig = SongConfig.find(SongConfigs.user eq user.id).firstOrNull()
if (songConfig == null) { if (songConfig == null) {
songConfig = initConfig(user) songConfig = initConfig(user)
} }
songConfig.token = token songConfig.token = token
songConfig.password = password
songConfig songConfig
} }

View File

@ -5,6 +5,7 @@ import io.ktor.server.application.*
import io.ktor.server.response.* import io.ktor.server.response.*
import io.ktor.server.routing.* import io.ktor.server.routing.*
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import space.mori.chzzk_bot.common.services.SongConfigService
import space.mori.chzzk_bot.common.utils.getStreamInfo import space.mori.chzzk_bot.common.utils.getStreamInfo
@Serializable @Serializable
@ -54,4 +55,31 @@ fun Routing.apiRoutes() {
call.respondText("Require UID", status = HttpStatusCode.NotFound) call.respondText("Require UID", status = HttpStatusCode.NotFound)
} }
} }
route("/session/{sid}") {
get {
val sid = call.parameters["sid"]
if(sid == null) {
call.respondText("Require SID", status = HttpStatusCode.NotFound)
return@get
}
val user = SongConfigService.getUserByToken(sid)
if(user == null) {
call.respondText("User not found", status = HttpStatusCode.NotFound)
return@get
} else {
val chzzkUser = getStreamInfo(user.token)
call.respond(HttpStatusCode.OK, GetUserDTO(
chzzkUser.content!!.channel.channelId,
chzzkUser.content!!.channel.channelName,
chzzkUser.content!!.status == "OPEN",
chzzkUser.content!!.channel.channelImageUrl
))
}
}
}
route("/session") {
get {
call.respondText("Require SID", status = HttpStatusCode.NotFound)
}
}
} }

View File

@ -39,14 +39,13 @@ fun Routing.wsSongListRoutes() {
webSocket("/songlist/{sid}") { webSocket("/songlist/{sid}") {
val sid = call.parameters["sid"] val sid = call.parameters["sid"]
val pw = call.request.headers["X-Auth-Token"]
val session = sid?.let { SongConfigService.getConfig(it) } val session = sid?.let { SongConfigService.getConfig(it) }
val user = sid?.let {SongConfigService.getUserByToken(sid) } val user = sid?.let {SongConfigService.getUserByToken(sid) }
if (sid == null) { if (sid == null) {
close(CloseReason(CloseReason.Codes.CANNOT_ACCEPT, "Invalid SID")) close(CloseReason(CloseReason.Codes.CANNOT_ACCEPT, "Invalid SID"))
return@webSocket return@webSocket
} }
if (user == null || session == null || session.password != pw) { if (user == null || session == null) {
close(CloseReason(CloseReason.Codes.CANNOT_ACCEPT, "Invalid SID")) close(CloseReason(CloseReason.Codes.CANNOT_ACCEPT, "Invalid SID"))
return@webSocket return@webSocket
} }
@ -149,6 +148,8 @@ fun Routing.wsSongListRoutes() {
} }
dispatcher.subscribe(TimerEvent::class) { dispatcher.subscribe(TimerEvent::class) {
if(it.type == TimerType.STREAM_OFF) { if(it.type == TimerType.STREAM_OFF) {
val user = UserService.getUser(it.uid)
SongConfigService.updateSession(user!!, null)
CoroutineScope(Dispatchers.Default).launch { CoroutineScope(Dispatchers.Default).launch {
sessions[it.uid]?.forEach { ws -> sessions[it.uid]?.forEach { ws ->
ws.sendSerialized(SongResponse( ws.sendSerialized(SongResponse(