json configs
This commit is contained in:
@ -2,6 +2,7 @@ import org.gradle.internal.jvm.Jvm
|
||||
|
||||
plugins {
|
||||
kotlin("jvm") version "2.3.0"
|
||||
kotlin("plugin.serialization") version "2.0.0"
|
||||
}
|
||||
|
||||
group = "su.sonoma"
|
||||
@ -16,6 +17,7 @@ dependencies {
|
||||
implementation("io.ktor:ktor-network:3.1.3")
|
||||
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.9.22")
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2")
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.0")
|
||||
}
|
||||
|
||||
kotlin {
|
||||
@ -46,16 +48,62 @@ tasks.test {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
|
||||
tasks.register("generateTestServerConfig") {
|
||||
|
||||
val outputFile = layout.buildDirectory.file("tmp/server.json")
|
||||
|
||||
outputs.file(outputFile)
|
||||
|
||||
doLast {
|
||||
|
||||
val file = outputFile.get().asFile
|
||||
|
||||
file.parentFile.mkdirs()
|
||||
|
||||
file.writeText("""
|
||||
{
|
||||
"type": "server",
|
||||
"address": "10.1.0.1",
|
||||
"port": 9093
|
||||
}
|
||||
""".trimIndent())
|
||||
}
|
||||
}
|
||||
|
||||
tasks.register("generateTestClientConfig") {
|
||||
|
||||
val outputFile = layout.buildDirectory.file("tmp/client.json")
|
||||
|
||||
outputs.file(outputFile)
|
||||
|
||||
doLast {
|
||||
|
||||
val file = outputFile.get().asFile
|
||||
|
||||
file.parentFile.mkdirs()
|
||||
|
||||
file.writeText("""
|
||||
{
|
||||
"type": "client",
|
||||
"host": "192.168.88.247",
|
||||
"address": "10.1.0.2",
|
||||
"port": 9093
|
||||
}
|
||||
""".trimIndent())
|
||||
}
|
||||
}
|
||||
|
||||
tasks.register<JavaExec>("runServer") {
|
||||
group = "app"
|
||||
description = "Run server"
|
||||
classpath = sourceSets["main"].runtimeClasspath
|
||||
mainClass.set("su.sonoma.MainKt")
|
||||
args = listOf("server")
|
||||
args = listOf("${projectDir}/build/tmp/server.json")
|
||||
jvmArgs = listOf(
|
||||
"--add-opens=java.base/java.io=ALL-UNNAMED"
|
||||
)
|
||||
dependsOn("buildTunLib")
|
||||
dependsOn("generateTestServerConfig")
|
||||
|
||||
systemProperty(
|
||||
"java.library.path",
|
||||
@ -68,11 +116,12 @@ tasks.register<JavaExec>("runClient") {
|
||||
description = "Run client"
|
||||
classpath = sourceSets["main"].runtimeClasspath
|
||||
mainClass.set("su.sonoma.MainKt")
|
||||
args = listOf("client")
|
||||
args = listOf("${projectDir}/build/tmp/client.json")
|
||||
jvmArgs = listOf(
|
||||
"--add-opens=java.base/java.io=ALL-UNNAMED"
|
||||
)
|
||||
dependsOn("buildTunLib")
|
||||
dependsOn("generateTestClientConfig")
|
||||
|
||||
systemProperty(
|
||||
"java.library.path",
|
||||
|
||||
@ -4,21 +4,23 @@ import kotlinx.coroutines.*
|
||||
import java.io.DataInputStream
|
||||
import java.net.Socket
|
||||
|
||||
fun startClient(host: String, port: Int) = runBlocking {
|
||||
val tun = TunDevice("10.1.0.2")
|
||||
fun startClient(host: String, address: String, port: Int) = runBlocking {
|
||||
println("Client started")
|
||||
val tun = TunDevice(address)
|
||||
|
||||
println("Client trying to connect - $host:$port")
|
||||
val socket = Socket(host, port)
|
||||
|
||||
val input = socket.getInputStream()
|
||||
val output = socket.getOutputStream()
|
||||
|
||||
println("Client started")
|
||||
|
||||
launch {
|
||||
val buffer = ByteArray(32768)
|
||||
println("Client trying to pair")
|
||||
|
||||
while (true) {
|
||||
val len = tun.input.read(buffer)
|
||||
println("Client connected - $host:$port")
|
||||
|
||||
if (len > 0) {
|
||||
output.write(len shr 8)
|
||||
|
||||
20
src/main/kotlin/Config.kt
Normal file
20
src/main/kotlin/Config.kt
Normal file
@ -0,0 +1,20 @@
|
||||
package su.sonoma
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.Json
|
||||
import java.io.File
|
||||
|
||||
@Serializable
|
||||
data class Config(
|
||||
val type: String,
|
||||
val host: String? = null,
|
||||
val address: String,
|
||||
val port: Int,
|
||||
)
|
||||
|
||||
fun loadConfig(file: File): Config {
|
||||
|
||||
val text = file.readText()
|
||||
|
||||
return Json.decodeFromString<Config>(text)
|
||||
}
|
||||
@ -1,14 +1,16 @@
|
||||
package su.sonoma
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val host = "192.168.88.227"
|
||||
val port = 9093
|
||||
import java.io.File
|
||||
|
||||
when (args.firstOrNull()) {
|
||||
"server" -> startServer(port)
|
||||
"client" -> startClient(host, port)
|
||||
fun main(args: Array<String>) {
|
||||
val configArg = args.firstOrNull() ?: error("Usage: awake <CONFIG.JSON>")
|
||||
val config = loadConfig(File(configArg))
|
||||
|
||||
when (config.type) {
|
||||
"server" -> startServer(config.address, config.port)
|
||||
"client" -> startClient(config.host ?: error("Invalid config"), config.address, config.port)
|
||||
else -> {
|
||||
println("Usage: server | client")
|
||||
error("Invalid type")
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -5,11 +5,11 @@ import java.net.ServerSocket
|
||||
import java.net.Socket
|
||||
import java.io.DataInputStream
|
||||
|
||||
fun startServer(port: Int) = runBlocking {
|
||||
val tun = TunDevice("10.1.0.1")
|
||||
fun startServer(address: String, port: Int) = runBlocking {
|
||||
val tun = TunDevice(address)
|
||||
|
||||
val server = ServerSocket(port)
|
||||
println("Server started")
|
||||
println("Server started on $port")
|
||||
|
||||
while (true) {
|
||||
val client = server.accept()
|
||||
|
||||
Reference in New Issue
Block a user