json configs
This commit is contained in:
@ -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