Skip to content

Commit bebafb0

Browse files
committed
Limit the proxied ping threadpool a little to avoid any flooding issues
Shouldn't be a problem, now that we don't create floods all by ourselves, but it's easy to do and better to handle properly than let anybody pinging heavily cause problems.
1 parent 6e99928 commit bebafb0

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

app/src/main/java/tech/httptoolkit/android/vpn/SessionHandler.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import java.nio.ByteBuffer;
2222
import java.nio.channels.SelectionKey;
2323
import java.util.concurrent.ExecutorService;
24-
import java.util.concurrent.Executors;
24+
import java.util.concurrent.SynchronousQueue;
25+
import java.util.concurrent.ThreadPoolExecutor;
26+
import java.util.concurrent.TimeUnit;
2527

2628
import tech.httptoolkit.android.vpn.transport.ip.IPPacketFactory;
2729
import tech.httptoolkit.android.vpn.transport.ip.IPv4Header;
@@ -52,12 +54,21 @@ public class SessionHandler {
5254
private final SocketNIODataService nioService;
5355
private final ClientPacketWriter writer;
5456

55-
private final ExecutorService pingThreadpool = Executors.newCachedThreadPool();
57+
private final ExecutorService pingThreadpool;
5658

5759
public SessionHandler(SessionManager manager, SocketNIODataService nioService, ClientPacketWriter writer) {
5860
this.manager = manager;
5961
this.nioService = nioService;
6062
this.writer = writer;
63+
64+
// Pool of threads to synchronously proxy ICMP ping requests in the background. We need to
65+
// carefully limit these, or a ping flood can cause us big big problems.
66+
this.pingThreadpool = new ThreadPoolExecutor(
67+
1, 20, // 1 - 20 parallel pings max
68+
60L, TimeUnit.SECONDS,
69+
new SynchronousQueue<Runnable>(),
70+
new ThreadPoolExecutor.DiscardPolicy() // Replace running pings if there's too many
71+
);
6172
}
6273

6374
/**

0 commit comments

Comments
 (0)