diff --git a/client/README.md b/client/README.md new file mode 100644 index 0000000..e374b65 --- /dev/null +++ b/client/README.md @@ -0,0 +1,21 @@ +# client +Uses a TUN adapter to receive packets from the OS, and sends it to the server. When packets are received back from the +server, they are written back to the TUN adapter. + +To start the tun adapter: +``` +bash client/scripts/tuntap.sh +``` + +Then run the client. + +To cleanup the adapter: +``` +bash client/scripts/cleanup.sh +``` + +## Debugging: +Using wireshark to inspect packets: +``` +wireshark -k -i TCP@127.0.0.1:19000 +``` \ No newline at end of file diff --git a/core/src/main/kotlin/com/jasonernst/kanonproxy/Session.kt b/core/src/main/kotlin/com/jasonernst/kanonproxy/Session.kt index 5130e15..4e7535b 100644 --- a/core/src/main/kotlin/com/jasonernst/kanonproxy/Session.kt +++ b/core/src/main/kotlin/com/jasonernst/kanonproxy/Session.kt @@ -139,11 +139,11 @@ abstract class Session( for (changeRequest in changeRequests) { when (changeRequest.type) { ChangeRequest.REGISTER -> { - logger.debug("Processing REGISTER") + // logger.debug("Processing REGISTER") changeRequest.channel.register(selector, changeRequest.ops) } ChangeRequest.CHANGE_OPS -> { - logger.debug("Processing CHANGE_OPS") + // logger.debug("Processing CHANGE_OPS") val key = changeRequest.channel.keyFor(selector) key.interestOps(changeRequest.ops) } @@ -151,7 +151,7 @@ abstract class Session( } changeRequests.clear() } - logger.warn("Waiting for SELECT") + // logger.warn("Waiting for SELECT") // lock so we don't add or remove from the selector while we're selecting val session = this@Session val numKeys = @@ -165,7 +165,7 @@ abstract class Session( selector.select() } if (numKeys > 0) { - logger.warn("SELECT RETURNED: $numKeys") + // logger.warn("SELECT RETURNED: $numKeys") } else { if (session is AnonymousTcpSession && session.isConnecting.get()) { val currentTime = System.currentTimeMillis() @@ -185,7 +185,7 @@ abstract class Session( try { val selectedKeys = selector.selectedKeys() if (selectedKeys.size > 0) { - logger.warn("SELECT: $selectedKeys") + // logger.warn("SELECT: $selectedKeys") } val keyStream = selectedKeys.parallelStream() keyStream @@ -208,7 +208,7 @@ abstract class Session( } if (it.isConnectable) { val socketChannel = it.channel() as SocketChannel - logger.debug("Tcp connectable, trying to finish connection to ${socketChannel.remoteAddress}") + // logger.debug("Tcp connectable, trying to finish connection to ${socketChannel.remoteAddress}") if (socketChannel.isConnectionPending) { try { val result = socketChannel.finishConnect() diff --git a/server/src/main/kotlin/com/jasonernst/kanonproxy/ProxySession.kt b/server/src/main/kotlin/com/jasonernst/kanonproxy/ProxySession.kt index 8d0b607..e8568be 100644 --- a/server/src/main/kotlin/com/jasonernst/kanonproxy/ProxySession.kt +++ b/server/src/main/kotlin/com/jasonernst/kanonproxy/ProxySession.kt @@ -17,6 +17,7 @@ class ProxySession( private val clientAddress: InetSocketAddress, private val kAnonProxy: KAnonProxy, private val socket: DatagramSocket, + private val sessionManager: ProxySessionManager, ) { private val logger = LoggerFactory.getLogger(javaClass) private val readFromProxyJob = SupervisorJob() @@ -47,6 +48,7 @@ class ProxySession( val datagramPacket = DatagramPacket(buffer, buffer.size, clientAddress) socket.send(datagramPacket) } + sessionManager.removeSession(clientAddress) } fun stop() { diff --git a/server/src/main/kotlin/com/jasonernst/kanonproxy/ProxySessionManager.kt b/server/src/main/kotlin/com/jasonernst/kanonproxy/ProxySessionManager.kt new file mode 100644 index 0000000..44b0429 --- /dev/null +++ b/server/src/main/kotlin/com/jasonernst/kanonproxy/ProxySessionManager.kt @@ -0,0 +1,7 @@ +package com.jasonernst.kanonproxy + +import java.net.InetSocketAddress + +interface ProxySessionManager { + fun removeSession(clientAddress: InetSocketAddress) +} diff --git a/server/src/main/kotlin/com/jasonernst/kanonproxy/Server.kt b/server/src/main/kotlin/com/jasonernst/kanonproxy/Server.kt index 3a29144..c7b72e1 100644 --- a/server/src/main/kotlin/com/jasonernst/kanonproxy/Server.kt +++ b/server/src/main/kotlin/com/jasonernst/kanonproxy/Server.kt @@ -16,7 +16,7 @@ import java.util.concurrent.atomic.AtomicBoolean class Server( private val port: Int = 8080, -) { +) : ProxySessionManager { private lateinit var socket: DatagramSocket private val isRunning = AtomicBoolean(false) private val kAnonProxy = KAnonProxy(IcmpLinux) @@ -78,13 +78,17 @@ class Server( val clientAddress = InetSocketAddress(packet.address, packet.port) kAnonProxy.handlePackets(packets, clientAddress) sessions.getOrPut(clientAddress) { - val session = ProxySession(clientAddress, kAnonProxy, socket) + val session = ProxySession(clientAddress, kAnonProxy, socket, this) session.start() session } } } + override fun removeSession(clientAddress: InetSocketAddress) { + sessions.remove(clientAddress) + } + fun stop() { isRunning.set(false) socket.close()