import java.net.*; import java.io.*; class IOStreamTunnel extends Thread { private BufferedInputStream in; private DataOutputStream out; private String inname, outname, prefix; IOStreamTunnel(String pr, String iname, BufferedInputStream i, String oname, DataOutputStream o) { in = i; out = o; if(iname == null) inname = null; else inname = new String(iname); if(oname == null) outname = null; else outname = new String(oname); if(pr == null) prefix = ""; else prefix = new String(pr); } private char toChar(int c) { if( c < 10 || c > 126) return '#'; else return (char)c; } public void run() { if( in == null || out == null || inname == null || outname == null ) return; int ch; String outstr = ""; String logstr = ""; try { while(-1 != (ch = in.read())) { outstr = outstr + (char) ch; logstr = logstr + toChar(ch); if (ch==10) { out.writeBytes(outstr); out.flush(); System.out.print(prefix + logstr); outstr = ""; logstr = ""; } } } catch (IOException e) { System.out.println("IO Error in tunnel [" + inname +"->"+ outname +"]: " + e.getMessage()); inname = null; outname = null; return; } } } class XSniffer extends Thread { private Socket svSocket, clSocket; private BufferedInputStream clbis, svbis; private DataOutputStream clbos, svbos; public XSniffer() { svSocket = null; clSocket = null; svbis = null; svbos = null; clbis = null; clbos = null; } public XSniffer(String server, int svPort, int listenPort) { svSocket = null; clSocket = null; svbis = null; svbos = null; clbis = null; clbos = null; initConnections(server, svPort, listenPort); } public boolean bad() { return svSocket == null || clSocket == null || svbis == null || svbos == null || clbis == null || clbos == null; } public static void main(String argv[]) { if(argv.length < 3) { System.out.println("usage: Sniffer "); return; } XSniffer me = new XSniffer(argv[0], Integer.parseInt(argv[1]), Integer.parseInt(argv[2])); if(me.bad()) return; else me.start(); return; } public boolean initConnections(String server, int svPort, int listenPort) { try { ServerSocket tmpSocket = new ServerSocket(listenPort); System.out.println("Waiting for client connection on " + listenPort + "..."); clSocket = tmpSocket.accept(); tmpSocket.close(); tmpSocket = null; System.out.println("Client connection accepted."); clbis = new BufferedInputStream(clSocket.getInputStream()); clbos = new DataOutputStream(clSocket.getOutputStream()); } catch (IOException e) { System.out.println("Could not accept client connection: " + e.getMessage()); return false; } System.out.println("Connecting to server " + server + ":" + svPort + "..."); try { svSocket = new Socket(server, svPort); svbis = new BufferedInputStream(svSocket.getInputStream()); svbos = new DataOutputStream(svSocket.getOutputStream()); } catch (IOException e) { svSocket = null; clSocket = null; clbis = null; clbos = null; System.out.println("Could not make server connection:" + e.getMessage()); return false; } return true; } public void run() { try { IOStreamTunnel clientToServer = new IOStreamTunnel("C: ", "client", clbis, "server", svbos); IOStreamTunnel serverToClient = new IOStreamTunnel("S: ", "server", svbis, "client", clbos); serverToClient.start(); clientToServer.start(); while (serverToClient.isAlive() && clientToServer.isAlive()) try { sleep(1000); } catch (InterruptedException e) {} clSocket.close(); svSocket.close(); } catch (IOException e) { System.out.println("Error with Sniffer."); } } }