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