Created
January 19, 2024 15:45
-
-
Save ehsavoie/f390b5aafdd499a399522c9735b17202 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//DEPS info.picocli:picocli:4.7.5 | |
//JAVA 21 | |
import java.io.IOException; | |
import java.net.InetSocketAddress; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.util.concurrent.Callable; | |
import picocli.CommandLine; | |
import picocli.CommandLine.Command; | |
public class SimpleProxy implements Callable<Integer> { | |
@CommandLine.Option(names = "--port", description = "Set the port of the listening server", defaultValue = "43812") | |
private int port; | |
private ServerSocket serverSocket = null; | |
@Override | |
public Integer call() throws Exception { | |
serverSocket = new ServerSocket(port); | |
System.out.println("Proxy server listening on " + serverSocket.getInetAddress() + ":" + serverSocket.getLocalPort()); | |
return 0; | |
} | |
public void listen() throws IOException { | |
System.out.println("Starting the proxy server"); | |
while (true) { | |
Socket clientSocket = serverSocket.accept(); | |
System.out.println("Accepted connection from client: " + clientSocket.getInetAddress()); | |
Thread proxyThread = new Thread(() -> handleClient(clientSocket)); | |
proxyThread.start(); | |
} | |
} | |
private void handleClient(Socket clientSocket) { | |
try { | |
// Simulate connection reset by closing the client socket immediately | |
//clientSocket.close(); | |
serverSocket.close(); | |
System.out.println("Connection closed for client: " + clientSocket.getInetAddress()); | |
return; // Exit the method after simulating the reset | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void main(String[] args) throws IOException { | |
SimpleProxy proxy = new SimpleProxy(); | |
new CommandLine(proxy).execute(args); | |
proxy.listen(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment