Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server session fix #48

Merged
merged 5 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions client/README.md
Original file line number Diff line number Diff line change
@@ -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 <user>
```

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
```
12 changes: 6 additions & 6 deletions core/src/main/kotlin/com/jasonernst/kanonproxy/Session.kt
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,19 @@ 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)
}
}
}
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 =
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -47,6 +48,7 @@ class ProxySession(
val datagramPacket = DatagramPacket(buffer, buffer.size, clientAddress)
socket.send(datagramPacket)
}
sessionManager.removeSession(clientAddress)
}

fun stop() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.jasonernst.kanonproxy

import java.net.InetSocketAddress

interface ProxySessionManager {
fun removeSession(clientAddress: InetSocketAddress)
}
8 changes: 6 additions & 2 deletions server/src/main/kotlin/com/jasonernst/kanonproxy/Server.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
Loading