suka macos ebani
This commit is contained in:
@ -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") {
|
||||
|
||||
@ -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()
|
||||
|
||||
@ -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")
|
||||
}
|
||||
|
||||
@ -3,53 +3,66 @@ 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 {
|
||||
handle(client)
|
||||
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)
|
||||
|
||||
while (true) {
|
||||
withContext(Dispatchers.IO) {
|
||||
while (true) {
|
||||
|
||||
val hi = input.read()
|
||||
val lo = input.read()
|
||||
val len = (hi shl 8) or lo
|
||||
val hi = input.read()
|
||||
|
||||
DataInputStream(input).readFully(buffer, 0, len)
|
||||
val lo = input.read()
|
||||
|
||||
val packet = buffer.copyOf(len)
|
||||
val len = (hi shl 8) or lo
|
||||
|
||||
val response = forwardPacket(packet)
|
||||
DataInputStream(input).readFully(buffer, 0, len)
|
||||
|
||||
output.write(response.size shr 8)
|
||||
output.write(response.size)
|
||||
val packet = buffer.copyOf(len)
|
||||
|
||||
output.write(response)
|
||||
val response = forwardPacket(packet)
|
||||
|
||||
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)
|
||||
|
||||
|
||||
@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user