chore: improve x11

This commit is contained in:
hstyi
2025-04-01 17:51:15 +08:00
committed by hstyi
parent 1ab0d26bab
commit 46412054c4
2 changed files with 16 additions and 22 deletions

View File

@@ -3,8 +3,10 @@ package app.termora.x11
import org.apache.sshd.client.channel.ChannelShell import org.apache.sshd.client.channel.ChannelShell
import org.apache.sshd.common.SshConstants import org.apache.sshd.common.SshConstants
import org.apache.sshd.common.channel.PtyChannelConfigurationHolder import org.apache.sshd.common.channel.PtyChannelConfigurationHolder
import java.math.BigInteger
import kotlin.random.Random import kotlin.random.Random
class ChannelShell( class ChannelShell(
configHolder: PtyChannelConfigurationHolder?, configHolder: PtyChannelConfigurationHolder?,
env: MutableMap<String, *>? env: MutableMap<String, *>?
@@ -22,7 +24,7 @@ class ChannelShell(
buffer.putBoolean(false) // want-reply buffer.putBoolean(false) // want-reply
buffer.putBoolean(false) buffer.putBoolean(false)
buffer.putString("MIT-MAGIC-COOKIE-1") buffer.putString("MIT-MAGIC-COOKIE-1")
buffer.putBytes(getFakedCookie()) buffer.putString(getFakedCookie())
buffer.putInt(0) buffer.putInt(0)
writePacket(buffer) writePacket(buffer)
} }
@@ -30,30 +32,26 @@ class ChannelShell(
super.doOpenPty() super.doOpenPty()
} }
private fun getFakedCookie(): ByteArray { private fun getFakedCookie(): String {
val session = super.getSession() val session = super.getSession()
var cookie = ChannelX11.X11_COOKIE_HEX.getOrNull(session) var cookie = session.getAttribute(ChannelX11.X11_COOKIE_HEX)
if (cookie != null) { if (cookie != null) {
return cookie as ByteArray return cookie
} }
synchronized(session) { synchronized(session) {
cookie = ChannelX11.X11_COOKIE_HEX.getOrNull(session) cookie = session.getAttribute(ChannelX11.X11_COOKIE_HEX)
if (cookie != null) { if (cookie != null) {
return cookie as ByteArray return cookie
} }
val foo = Random.nextBytes(16) val foo = Random.nextBytes(16)
ChannelX11.X11_COOKIE.set(session, foo) session.setAttribute(ChannelX11.X11_COOKIE, foo)
val bar = foo.copyOf(32) cookie = String.format("%032x", BigInteger(1, foo))
for (i in 0..15) { session.setAttribute(ChannelX11.X11_COOKIE_HEX, cookie)
bar[2 * i] = ChannelX11.COOKIE_TABLE[(foo[i].toInt() ushr 4) and 0xf]
bar[2 * i + 1] = ChannelX11.COOKIE_TABLE[foo[i].toInt() and 0xf]
}
ChannelX11.X11_COOKIE_HEX.set(session, bar)
return bar return cookie
} }
} }
} }

View File

@@ -4,7 +4,7 @@ import org.apache.sshd.client.channel.AbstractClientChannel
import org.apache.sshd.client.future.DefaultOpenFuture import org.apache.sshd.client.future.DefaultOpenFuture
import org.apache.sshd.client.future.OpenFuture import org.apache.sshd.client.future.OpenFuture
import org.apache.sshd.client.session.ClientConnectionService import org.apache.sshd.client.session.ClientConnectionService
import org.apache.sshd.common.Property import org.apache.sshd.common.AttributeRepository
import org.apache.sshd.common.SshConstants import org.apache.sshd.common.SshConstants
import org.apache.sshd.common.channel.ChannelOutputStream import org.apache.sshd.common.channel.ChannelOutputStream
import org.apache.sshd.common.io.IoConnectFuture import org.apache.sshd.common.io.IoConnectFuture
@@ -21,12 +21,8 @@ class ChannelX11(
) : AbstractClientChannel("x11") { ) : AbstractClientChannel("x11") {
companion object { companion object {
val X11_COOKIE: Property<Any> = Property.`object`("x11-cookie") val X11_COOKIE = AttributeRepository.AttributeKey<ByteArray>()
val X11_COOKIE_HEX: Property<Any> = Property.`object`("x11-cookie-hex") val X11_COOKIE_HEX = AttributeRepository.AttributeKey<String>()
val COOKIE_TABLE = byteArrayOf(
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61,
0x62, 0x63, 0x64, 0x65, 0x66
)
} }
private lateinit var x11: IoSession private lateinit var x11: IoSession
@@ -69,7 +65,7 @@ class ChannelX11(
override fun doWriteData(data: ByteArray, off: Int, len: Long) { override fun doWriteData(data: ByteArray, off: Int, len: Long) {
if (isInitialized.compareAndSet(false, true)) { if (isInitialized.compareAndSet(false, true)) {
val cookie = X11_COOKIE.getOrNull(session) ?: return val cookie = session.getAttribute(X11_COOKIE) ?: return
val foo = data.copyOfRange(off, off + len.toInt()) val foo = data.copyOfRange(off, off + len.toInt())
val s = 0 val s = 0
val l = foo.size val l = foo.size