refactoring signing keys
This commit is contained in:
1 parent
e619b45485
commit
7042e41d70
23 files changed
+221
-76
No files matched your search
@@ -0,0 +1,64 @@
|
||||
package net.sergeych.kiloparsec.adapter
|
||||
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.routing.*
|
||||
import io.ktor.server.websocket.*
|
||||
import io.ktor.websocket.*
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.launch
|
||||
import net.sergeych.crypto2.SigningKey
|
||||
import net.sergeych.kiloparsec.KiloInterface
|
||||
import net.sergeych.kiloparsec.KiloServerConnection
|
||||
import net.sergeych.mp_logger.LogTag
|
||||
import net.sergeych.mp_logger.debug
|
||||
import net.sergeych.mp_logger.warning
|
||||
import net.sergeych.tools.AtomicCounter
|
||||
import java.time.Duration
|
||||
|
||||
fun <S> Application.setupWebsocketServer(
|
||||
localInterface: KiloInterface<S>,
|
||||
path: String = "/kp",
|
||||
serverKey: SigningKey.Secret? = null,
|
||||
createSession: () -> S,
|
||||
) {
|
||||
|
||||
install(Routing)
|
||||
install(WebSockets) {
|
||||
pingPeriod = Duration.ofSeconds(15)
|
||||
timeout = Duration.ofSeconds(15)
|
||||
maxFrameSize = Long.MAX_VALUE
|
||||
masking = false
|
||||
}
|
||||
val counter = AtomicCounter()
|
||||
routing {
|
||||
webSocket(path) {
|
||||
val log = LogTag("KWS:${counter.incrementAndGet()}")
|
||||
log.debug { "opening the connection" }
|
||||
val input = Channel<UByteArray>(256)
|
||||
val output = Channel<UByteArray>(256)
|
||||
launch {
|
||||
while (isActive) {
|
||||
send(output.receive().toByteArray())
|
||||
}
|
||||
}
|
||||
val server = KiloServerConnection(
|
||||
localInterface,
|
||||
ProxyDevice(input, output) { input.close() },
|
||||
createSession(),
|
||||
serverKey
|
||||
)
|
||||
launch { server.run() }
|
||||
for( f in incoming) {
|
||||
if (f is Frame.Binary)
|
||||
input.send(f.readBytes().toUByteArray())
|
||||
else
|
||||
log.warning { "unknown frame type ${f.frameType}, ignoring" }
|
||||
|
||||
}
|
||||
log.debug { "closing the server" }
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,8 @@ import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.channels.ClosedReceiveChannelException
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import net.sergeych.crypto.encodeVarUnsigned
|
||||
import net.sergeych.crypto.readVarUnsigned
|
||||
import net.sergeych.crypto2.encodeVarUnsigned
|
||||
import net.sergeych.crypto2.readVarUnsigned
|
||||
import net.sergeych.kiloparsec.Transport
|
||||
import net.sergeych.mp_logger.LogTag
|
||||
import net.sergeych.mp_logger.warning
|
||||
|
||||
Reference in new issue
Block a user