suka macos ebani

This commit is contained in:
2026-05-25 01:06:40 +03:00
parent aebee266b7
commit 3e52278e53
5 changed files with 49 additions and 30 deletions

View File

@ -31,6 +31,10 @@ tasks.register<JavaExec>("runServer") {
classpath = sourceSets["main"].runtimeClasspath
mainClass.set("su.sonoma.MainKt")
args = listOf("server")
jvmArgs = listOf(
"--add-opens=java.base/java.io=ALL-UNNAMED"
)
dependsOn("buildTunLib")
}
tasks.register<JavaExec>("runClient") {

View File

@ -3,11 +3,10 @@ import kotlinx.coroutines.*
import java.io.DataInputStream
import java.net.Socket
fun startClient() = runBlocking {
fun startClient(host: String, port: Int) = runBlocking {
val tun = TunDevice("10.1.0.2")
val tun = TunDevice()
val socket = Socket("192.168.88.228", 9093)
val socket = Socket(host, port)
val input = socket.getInputStream()
val output = socket.getOutputStream()

View File

@ -9,9 +9,12 @@ import kotlinx.io.readByteArray
import java.io.FileInputStream
fun main(args: Array<String>) {
val host = "192.168.88.242"
val port = 9093
when (args.firstOrNull()) {
"server" -> startServer()
"client" -> startClient()
"server" -> startServer(port)
"client" -> startClient(host, port)
else -> {
println("Usage: server | client")
}

View File

@ -3,35 +3,48 @@ package su.sonoma
import kotlinx.coroutines.*
import java.net.ServerSocket
import java.net.Socket
import java.io.InputStream
import java.io.DataInputStream
fun startServer() = runBlocking {
val server = ServerSocket(9093)
fun startServer(port: Int) = runBlocking {
val tun = TunDevice("10.1.0.1")
val server = ServerSocket(port)
println("Server started")
while (true) {
val client = server.accept()
println("${client.inetAddress} Client pairing")
launch {
launch(Dispatchers.IO) {
println("${client.inetAddress} Client connected")
try {
handle(client)
} catch (e: Exception) {
println("${client.inetAddress} Client disconnected: ${e.message}")
}
}
}
}
suspend fun handle(socket: Socket) {
val input = socket.getInputStream()
val output = socket.getOutputStream()
val input = withContext(Dispatchers.IO) {
socket.getInputStream()
}
val output = withContext(Dispatchers.IO) {
socket.getOutputStream()
}
val buffer = ByteArray(32768)
withContext(Dispatchers.IO) {
while (true) {
val hi = input.read()
val lo = input.read()
val len = (hi shl 8) or lo
DataInputStream(input).readFully(buffer, 0, len)
@ -42,14 +55,14 @@ suspend fun handle(socket: Socket) {
output.write(response.size shr 8)
output.write(response.size)
output.write(response)
}
}
}
fun forwardPacket(packet: ByteArray): ByteArray {
val host = "example.com"
val port = 80
val host = "localhost"
val port = 1080
val socket = Socket(host, port)

View File

@ -6,9 +6,9 @@ import java.io.FileOutputStream
import java.io.RandomAccessFile
class TunDevice(
private val name: String = "tun0",
private val ip: String = "10.1.0.1"
private val ip: String
) {
val name: String = "tun0"
companion object {
init {