Initial commit
This commit is contained in:
28 files changed
+1009
No files matched your search
@@ -0,0 +1,29 @@
|
||||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
||||
|
||||
plugins {
|
||||
kotlin("multiplatform")
|
||||
kotlin("plugin.serialization")
|
||||
}
|
||||
|
||||
kotlin {
|
||||
explicitApi()
|
||||
|
||||
linuxX64()
|
||||
linuxArm64()
|
||||
macosArm64()
|
||||
mingwX64()
|
||||
jvm { compilerOptions.jvmTarget = JvmTarget.JVM_11 }
|
||||
|
||||
sourceSets {
|
||||
commonMain.dependencies {
|
||||
api("org.jetbrains.kotlinx:kotlinx-serialization-json:1.11.0")
|
||||
api("org.kotlincrypto.hash:md:0.8.0")
|
||||
api("org.jetbrains.kotlinx:kotlinx-io-core:0.9.1")
|
||||
}
|
||||
|
||||
commonTest.dependencies {
|
||||
implementation(kotlin("test"))
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.11.0")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/8/29
|
||||
*/
|
||||
|
||||
package cn.rtast.bldm.core
|
||||
|
||||
public const val REAL_ROOM_ID_URL: String = "https://api.live.bilibili.com/room/v1/Room/room_init?id="
|
||||
public const val DM_SERVER_CONF_URL: String = "https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo?id="
|
||||
public const val USER_NAV_URL: String = "https://api.bilibili.com/x/web-interface/nav"
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/8/29
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.bldm.core.data
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.Transient
|
||||
|
||||
@Serializable
|
||||
public data class DMServerConf(
|
||||
val data: ServerConf,
|
||||
) {
|
||||
@Serializable
|
||||
public data class ServerConf(
|
||||
val token: String,
|
||||
@SerialName("host_list")
|
||||
val hostList: List<ServerHost>,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
public data class ServerHost(
|
||||
val host: String,
|
||||
val port: Int,
|
||||
@SerialName("wss_port")
|
||||
val wssPort: Int,
|
||||
@SerialName("ws_port")
|
||||
val wsPort: Int,
|
||||
)
|
||||
|
||||
@Transient
|
||||
public val defaultServerHost: ServerHost = ServerHost("broadcastlv.chat.bilibili.com", 2243, 2245, 2244)
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/8/29
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.bldm.core.data
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
public data class RealRoomId(
|
||||
val data: RoomId
|
||||
) {
|
||||
@Serializable
|
||||
public data class RoomId(
|
||||
@SerialName("room_id")
|
||||
val roomId: Long
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/8/30
|
||||
*/
|
||||
|
||||
|
||||
@file:OptIn(ExperimentalUnsignedTypes::class)
|
||||
|
||||
package cn.rtast.bldm.core.data
|
||||
|
||||
import cn.rtast.bldm.core.util.digest
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlin.time.Clock
|
||||
|
||||
@Serializable
|
||||
public data class UserNavData(
|
||||
@SerialName("wbi_img")
|
||||
val wbiImg: WbiImg,
|
||||
) {
|
||||
@Serializable
|
||||
public data class WbiImg(
|
||||
@SerialName("img_url")
|
||||
val imgUrl: String,
|
||||
@SerialName("sub_url")
|
||||
val subUrl: String,
|
||||
)
|
||||
|
||||
private companion object {
|
||||
val MIXIN_KEY_ENC_TAB = intArrayOf(
|
||||
46, 47, 18, 2, 53, 8, 23, 32, 15, 50, 10, 31, 58, 3, 45, 35, 27, 43, 5, 49,
|
||||
33, 9, 42, 19, 29, 28, 14, 39, 12, 38, 41, 13, 37, 48, 7, 16, 24, 55, 40,
|
||||
61, 26, 17, 0, 1, 60, 51, 30, 4, 22, 25, 54, 21, 56, 59, 6, 63, 57, 62, 11,
|
||||
36, 20, 34, 44, 52
|
||||
)
|
||||
|
||||
fun String.encodeURIComponent(): String {
|
||||
val bytes = this.encodeToByteArray()
|
||||
val sb = StringBuilder()
|
||||
for (b in bytes) {
|
||||
val c = b.toInt().and(0xFF).toChar()
|
||||
if (c in 'A'..'Z' || c in 'a'..'z' || c in '0'..'9' || c == '-' || c == '_' || c == '.' || c == '~') {
|
||||
sb.append(c)
|
||||
} else {
|
||||
val hex = (b.toInt() and 0xFF).toString(16).uppercase()
|
||||
sb.append('%')
|
||||
if (hex.length == 1) sb.append('0')
|
||||
sb.append(hex)
|
||||
}
|
||||
}
|
||||
return sb.toString()
|
||||
}
|
||||
|
||||
fun Map<String, Any?>.toQueryString(): String {
|
||||
return this.mapNotNull { (key, value) ->
|
||||
if (value != null) {
|
||||
"${key.encodeURIComponent()}=${value.toString().encodeURIComponent()}"
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}.joinToString("&")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* get resorted mixin key
|
||||
*/
|
||||
private fun getMixinKey(): String =
|
||||
(wbiImg.imgUrl.substringAfterLast('/').removeSuffix(".png") +
|
||||
wbiImg.subUrl.substringAfterLast('/').removeSuffix(".png")).let { s ->
|
||||
buildString { repeat(32) { append(s[MIXIN_KEY_ENC_TAB[it]]) } }
|
||||
}
|
||||
|
||||
public fun signWbi(room: Long): String {
|
||||
// val payload = _wbi_sign_payload(room, 0, Clock.System.now().epochSeconds) // ref
|
||||
val wts = Clock.System.now().epochSeconds
|
||||
val params = mapOf("id" to room, "type" to 0)
|
||||
.entries.sortedBy { it.key }.associate { it.key to it.value }.toMutableMap()
|
||||
return buildString {
|
||||
append(params.toQueryString())
|
||||
params["wts"] = wts
|
||||
append("&wts=$wts")
|
||||
append("&w_rid=${(params.toQueryString() + getMixinKey()).digest()}")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/8/30
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.bldm.core.data
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
@Suppress("ClassName")
|
||||
internal data class _wbi_sign_payload(
|
||||
/**
|
||||
* room id
|
||||
*/
|
||||
val id: Long,
|
||||
/**
|
||||
* always be 0
|
||||
*/
|
||||
val type: Int,
|
||||
/**
|
||||
* current unix timestamp (seconds)
|
||||
*/
|
||||
val wts: Long
|
||||
)
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/8/30
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.bldm.core.data.protocol
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
internal data class AuthPayload(
|
||||
/**
|
||||
* user id
|
||||
*/
|
||||
val uid: Int,
|
||||
/**
|
||||
* room id
|
||||
*/
|
||||
val roomid: Long,
|
||||
/**
|
||||
* protocol version
|
||||
* always be 2
|
||||
*/
|
||||
val protover: Int,
|
||||
/**
|
||||
* device id
|
||||
*/
|
||||
val buvid: String,
|
||||
/**
|
||||
* platform always be "web"
|
||||
*/
|
||||
val platform: String,
|
||||
/**
|
||||
* always be 2
|
||||
*/
|
||||
val type: Int,
|
||||
/**
|
||||
* token
|
||||
*/
|
||||
val key: String
|
||||
)
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/8/30
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.bldm.core.protocol
|
||||
|
||||
import kotlinx.io.Buffer
|
||||
import kotlinx.io.readByteArray
|
||||
|
||||
/**
|
||||
* packet header 16 bytes length + body (variant length)
|
||||
*/
|
||||
public data class Packet(
|
||||
/**
|
||||
* total packet length
|
||||
* 4 bytes
|
||||
*/
|
||||
val packetLength: Int = -1,
|
||||
/**
|
||||
* always 16
|
||||
* 2 bytes
|
||||
*/
|
||||
val headerLength: Short = 16,
|
||||
/**
|
||||
* protocol = 1
|
||||
* 2 bytes
|
||||
*/
|
||||
val protocolVersion: Short = 1,
|
||||
/**
|
||||
* packet type
|
||||
* 4 bytes
|
||||
*/
|
||||
val packetType: Int,
|
||||
/**
|
||||
* packet sequence
|
||||
* value is usually 1
|
||||
* 4 bytes
|
||||
*/
|
||||
val sequence: Int = 1,
|
||||
/**
|
||||
* packet body
|
||||
* variant length
|
||||
*/
|
||||
val body: ByteArray,
|
||||
) {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (other == null || this::class != other::class) return false
|
||||
|
||||
other as Packet
|
||||
|
||||
if (packetLength != other.packetLength) return false
|
||||
if (headerLength != other.headerLength) return false
|
||||
if (protocolVersion != other.protocolVersion) return false
|
||||
if (packetType != other.packetType) return false
|
||||
if (sequence != other.sequence) return false
|
||||
if (!body.contentEquals(other.body)) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = packetLength
|
||||
result = 31 * result + headerLength
|
||||
result = 31 * result + protocolVersion
|
||||
result = 31 * result + packetType
|
||||
result = 31 * result + sequence
|
||||
result = 31 * result + body.contentHashCode()
|
||||
return result
|
||||
}
|
||||
|
||||
public fun toByteArray(): ByteArray = Buffer().apply {
|
||||
writeInt(16 + body.size)
|
||||
writeShort(headerLength)
|
||||
writeShort(protocolVersion)
|
||||
writeInt(packetType)
|
||||
writeInt(sequence)
|
||||
write(body, body.size)
|
||||
}.readByteArray()
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/8/30
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.bldm.core.protocol
|
||||
|
||||
class PacketDecoder {
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/8/30
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.bldm.core.protocol
|
||||
|
||||
import cn.rtast.bldm.core.data.protocol.AuthPayload
|
||||
import cn.rtast.bldm.core.util.AutoIncrementInt
|
||||
import cn.rtast.bldm.core.util.encodeJson
|
||||
|
||||
public class PacketEncoder {
|
||||
public companion object {
|
||||
private val HEARTBEAT_BODY: ByteArray = "[Object object]".encodeToByteArray()
|
||||
}
|
||||
|
||||
private val _sequence by AutoIncrementInt()
|
||||
|
||||
public fun authPacket(uid: Int, roomId: Long, buvid: String, token: String): Packet {
|
||||
val payload = AuthPayload(uid, roomId, 2, buvid, "web", 2, token)
|
||||
.encodeJson().encodeToByteArray()
|
||||
return Packet(packetType = PacketType.AUTH, sequence = _sequence, body = payload)
|
||||
}
|
||||
|
||||
public fun heartbeatPacket(): Packet = Packet(packetType = 2, sequence = _sequence, body = HEARTBEAT_BODY)
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/8/30
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.bldm.core.protocol
|
||||
|
||||
public object PacketType {
|
||||
public const val AUTH: Int = 7
|
||||
public const val AUTH_REPLY: Int = 8
|
||||
public const val HEARTBEAT: Int = 2
|
||||
public const val HANDSHAKE_REPLY: Int = 1
|
||||
public const val HEARTBEAT_REPLY: Int = 3
|
||||
public const val SEND_MSG_REPLY: Int = 5
|
||||
// TODO
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/8/30
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.bldm.core.util
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
internal class AutoIncrementInt(initialValue: Int = 1) {
|
||||
private var count = initialValue
|
||||
|
||||
operator fun getValue(thisRef: Any?, property: KProperty<*>): Int {
|
||||
return ++count
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/8/30
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.bldm.core.util
|
||||
|
||||
import org.kotlincrypto.hash.md.MD5
|
||||
|
||||
internal val md5Instance = MD5()
|
||||
|
||||
internal fun String.digest(): String = md5Instance.digest(this.encodeToByteArray()).toHexString()
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/8/30
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.bldm.core.util
|
||||
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
internal inline fun <reified T> String.fromJson(): T = Json.decodeFromString(this)
|
||||
|
||||
internal inline fun <reified T> T.encodeJson(): String = Json.encodeToString(this)
|
||||
@@ -0,0 +1,10 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/8/30
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.bldm.core.util
|
||||
|
||||
internal expect fun ByteArray.zlibDecompress(): ByteArray
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/8/30
|
||||
*/
|
||||
|
||||
|
||||
package test
|
||||
|
||||
import org.kotlincrypto.hash.md.MD5
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class TestMd5 {
|
||||
|
||||
@Test
|
||||
fun `test md5`() {
|
||||
val target = "e10adc3949ba59abbe56e057f20f883e" // 123456
|
||||
|
||||
val res = MD5().digest("123456".encodeToByteArray()).toHexString()
|
||||
|
||||
assertEquals(target, res)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/8/30
|
||||
*/
|
||||
|
||||
|
||||
package test
|
||||
|
||||
import cn.rtast.bldm.core.data.UserNavData
|
||||
import kotlin.test.Test
|
||||
|
||||
class TestWbiSign {
|
||||
|
||||
@Test
|
||||
fun `test wbi sign`() {
|
||||
val imgUrl = "https://i0.hdslb.com/bfs/wbi/7cd084941338484aae1ad9425b84077c.png"
|
||||
val subUrl = "https://i0.hdslb.com/bfs/wbi/4932caff0ff746eab6f01bf08b70ac45.png"
|
||||
val signWbi = UserNavData(UserNavData.WbiImg(imgUrl, subUrl)).signWbi(27245513)
|
||||
println(signWbi)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/8/30
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.bldm.core.util
|
||||
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.util.zip.Inflater
|
||||
|
||||
public actual fun ByteArray.zlibDecompress(): ByteArray {
|
||||
val inflater = Inflater(true)
|
||||
val outputStream = ByteArrayOutputStream(this.size)
|
||||
val buffer = ByteArray(1024)
|
||||
inflater.setInput(this)
|
||||
while (!inflater.finished()) {
|
||||
val length = inflater.inflate(buffer)
|
||||
if (length > 0) outputStream.write(buffer, 0, length)
|
||||
}
|
||||
inflater.end()
|
||||
return outputStream.toByteArray()
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/8/30
|
||||
*/
|
||||
|
||||
|
||||
@file:OptIn(ExperimentalForeignApi::class)
|
||||
|
||||
package cn.rtast.bldm.core.util
|
||||
|
||||
import kotlinx.cinterop.*
|
||||
import platform.zlib.*
|
||||
|
||||
private const val MAX_WBITS = 15
|
||||
|
||||
public actual fun ByteArray.zlibDecompress(): ByteArray {
|
||||
if (isEmpty()) return ByteArray(0)
|
||||
return memScoped {
|
||||
val stream = alloc<z_stream>()
|
||||
stream.zalloc = null
|
||||
stream.zfree = null
|
||||
stream.opaque = null
|
||||
check(inflateInit2_(stream.ptr, MAX_WBITS, ZLIB_VERSION, sizeOf<z_stream>().toInt()) == Z_OK) { "inflateInit2_ failed" }
|
||||
|
||||
val inputPinned = this@zlibDecompress.pin()
|
||||
try {
|
||||
stream.next_in = inputPinned.addressOf(0).reinterpret()
|
||||
stream.avail_in = this@zlibDecompress.size.toUInt()
|
||||
|
||||
val bufferSize = 4096
|
||||
val tempBuffer = ByteArray(bufferSize)
|
||||
val tempPinned = tempBuffer.pin()
|
||||
val output = ArrayList<Byte>(this@zlibDecompress.size * 2)
|
||||
|
||||
try {
|
||||
do {
|
||||
stream.next_out = tempPinned.addressOf(0).reinterpret()
|
||||
stream.avail_out = bufferSize.toUInt()
|
||||
|
||||
val result = inflate(stream.ptr, Z_NO_FLUSH)
|
||||
check(result == Z_OK || result == Z_STREAM_END) { "inflate error: $result" }
|
||||
|
||||
val bytesDecompressed = bufferSize - stream.avail_out.toInt()
|
||||
for (i in 0 until bytesDecompressed) {
|
||||
output.add(tempBuffer[i])
|
||||
}
|
||||
|
||||
if (result == Z_STREAM_END) break
|
||||
} while (stream.avail_out == 0u)
|
||||
} finally {
|
||||
tempPinned.unpin()
|
||||
}
|
||||
|
||||
inflateEnd(stream.ptr)
|
||||
return@memScoped output.toByteArray()
|
||||
} finally {
|
||||
inputPinned.unpin()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user