Skip to content

Commit

Permalink
Add native encryptConnection (bad write perf).
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Sep 14, 2022
1 parent ae1a54b commit f5ca01a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import java.nio.ByteOrder
import java.util.UUID
import java.util.concurrent.locks.ReentrantReadWriteLock
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec
import kotlin.concurrent.read
import kotlin.concurrent.thread
import kotlin.concurrent.write
Expand All @@ -26,7 +28,6 @@ class ConnectionModule(reactContext: ReactApplicationContext)
private var connectionId: UUID? = null
private var compressionThreshold = -1
private var compressionEnabled = false
// TODO: Use AES ciphers for reading and writing. Maybe JS can initialise these? Or do we handle Encryption Request?
private var aesDecipher: Cipher? = null
private var aesCipher: Cipher? = null
private var loggedIn = false
Expand Down Expand Up @@ -79,6 +80,33 @@ class ConnectionModule(reactContext: ReactApplicationContext)
}
}

@ReactMethod fun enableEncryption(
connId: String, secret: String, packet: String, promise: Promise
) = runBlocking {
launch(Dispatchers.IO) {
lock.write {
if (connId == connectionId.toString()) {
try {
val packetBytes = Base64.decode(packet, Base64.DEFAULT)
val secretBytes = Base64.decode(secret, Base64.DEFAULT)
val secretKey = SecretKeySpec(secretBytes, "AES")
val iv = IvParameterSpec(secretBytes)
aesDecipher = Cipher.getInstance("AES/CFB8/PKCS5Padding").apply {
init(Cipher.DECRYPT_MODE, secretKey, iv)
}
val result = directlyWritePacket(0x01, packetBytes)
aesCipher = Cipher.getInstance("AES/CFB8/PKCS5Padding").apply {
init(Cipher.ENCRYPT_MODE, secretKey, iv)
}
promise.resolve(result)
} catch (e: Exception) {
promise.reject(e)
}
} else promise.resolve(false)
}
}
}

@ReactMethod fun closeConnection(id: String) = lock.write {
if (id == connectionId.toString()) directlyCloseConnection()
}
Expand Down
23 changes: 11 additions & 12 deletions src/minecraft/connection/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,9 @@ export class NativeServerConnection
this,
is119,
async (secret: Buffer, response: Buffer) => {
// const AES_ALG = 'aes-128-cfb8'
// conn.aesDecipher = createDecipheriv(AES_ALG, secret, secret)
await this.writePacket(0x01, response)
// conn.aesCipher = createCipheriv(AES_ALG, secret, secret)
const eSecret = secret.toString('base64')
const eResp = response.toString('base64')
return ConnectionModule.enableEncryption(this.id, eSecret, eResp)
}
)
}
Expand All @@ -130,12 +129,7 @@ export class NativeServerConnection
})
this.eventEmitter.addListener('ecm:close', (event: NativeEvent) => {
if (event.connectionId !== this.id) return
this.eventEmitter.removeAllListeners('ecm:packet')
this.eventEmitter.removeAllListeners('ecm:error')
this.eventEmitter.removeAllListeners('ecm:close')
this.eventEmitter.removeAllListeners('ecm:log')
this.closed = true
this.emit('close')
this.internalClose(false)
})
}

Expand All @@ -144,14 +138,19 @@ export class NativeServerConnection
return ConnectionModule.writePacket(this.id, packetId, toWrite)
}

close() {
internalClose(closeConnection: boolean) {
if (this.closed) return
this.closed = true
ConnectionModule.closeConnection(this.id)
if (closeConnection) ConnectionModule.closeConnection(this.id)
this.eventEmitter.removeAllListeners('ecm:packet')
this.eventEmitter.removeAllListeners('ecm:error')
this.eventEmitter.removeAllListeners('ecm:close')
this.eventEmitter.removeAllListeners('ecm:log')
this.emit('close')
}

close() {
this.internalClose(true)
}
}

Expand Down

0 comments on commit f5ca01a

Please sign in to comment.