Content of ProxyConnection.java extracted from proxy.jar
package proxy; import java.net.*; import java.io.*; import java.util.*; import java.text.DateFormat; class ProxyConnection extends Thread { final boolean textimageProxy = false; OutputStream f, content = null; Socket fromClient; ProxyConnection(Socket s) { fromClient=s; start(); } public static synchronized void println(String s) { System.out.println(s); if (Proxy.out != null) Proxy.out.println(s); } static void filter(buffer in) { String test = in.toString(); String ustring = test.toUpperCase(); int i=0, j; search: while (ustring != null) { if ((i = ustring.indexOf("<A", i))==-1) break search; do { if ((i = ustring.indexOf(">", i))==-1) break search; i++; if ((j = ustring.indexOf("<", i))==-1) j = ustring.length(); // not correct 100%! } while (ustring.substring(i, j).trim().length()==0); if (Proxy.wordSet.contains(ustring.substring(i, j).trim())) { in.buf[j-1]='#'; } } } public void run() { Proxy.clientCount++; buffer abuf = new buffer(); DataInputStream clientIn = null; DataOutputStream clientOut = null; DataInputStream serverIn = null; DataOutputStream serverOut = null; Socket toServer = null; String filename =""; File dir = null; int responseCode = 500; long lastModified = 0; int hashcode = 0; long time0 = new Date().getTime(); try { fromClient.setTcpNoDelay(true); clientIn = new DataInputStream(fromClient.getInputStream()); clientOut = new DataOutputStream(fromClient.getOutputStream()); String line, length =""; boolean first = true; abuf.clear(); while (((line = clientIn.readLine()) != null) && (line.trim().length() != 0)) { if (first) { first = false; String [] request = line.split(" "); if (!request[2].startsWith("HTTP")) { abuf.clear(); abuf.insert("HTTP/1.0 501 Not Implemented\r\n"); abuf.writeStream(clientOut); clientOut.flush(); throw new Exception("bad request"); } println(getName()+" fetching "+request[1]); hashcode = request[1].toUpperCase().hashCode(); URI server = new URI(request[1]); try { if (!Proxy.tunnel) { int port=server.getPort(); if (port == -1) { port = 80; } if ((server.getHost() == null)&&(fromClient.getLocalPort() == port)) { abuf.clear(); abuf.insert("HTTP/1.0 404 Not Found\r\n"); abuf.writeStream(clientOut); clientOut.flush(); throw new Exception("missing host in proxy mode"); } toServer = new Socket(server.getHost(),port); } else toServer = new Socket(Proxy.tunnel_host,Proxy.tunnel_port); } catch (UnknownHostException e) { abuf.clear(); abuf.insert("HTTP/1.0 404 Not Found\r\n"); abuf.writeStream(clientOut); clientOut.flush(); throw e; } println(getName()+" connecting to "+toServer); serverIn = new DataInputStream(toServer.getInputStream()); serverOut = new DataOutputStream(toServer.getOutputStream()); String path = server.getPath()+(server.getRawQuery()==null ? "" :"?"+server.getRawQuery())+(server.getRawFragment()==null ? "" :"#"+server.getRawFragment()); filename = server.getPath(); if (path.length() == 0) path ="/"; dir = new File(Proxy.cache+"/"+server.getHost()); if (!dir.exists()) dir.mkdir(); if (filename.endsWith("/")) { filename += "index.htm"; } filename = dir.toString()+filename.charAt(0)+filename.substring(1).replace('/','.')+(server.getRawQuery()==null ? "" :"^"+server.getRawQuery().hashCode()); if (!Proxy.tunnel) line = request[0]+" "+path+" "+request[2]; } abuf.insert(line+"\r\n"); if (line.startsWith("Content-Length:")) { length = line.substring(15).trim(); // 15 è la lunghezza di "Content-Length:" } } // end of while abuf.insert("\r\n"); f = new FileOutputStream(dir.toString()+"/"+getName()+".log"); abuf.writeStream(f); int fc = abuf.size; abuf.writeStream(serverOut); int r0, nc=0; if (length.length() > 0) while ((nc != Integer.parseInt(length))&&((r0 = abuf.readStream(clientIn)) > 0)) { nc+=r0; abuf.writeStream(f); abuf.writeStream(serverOut); } println(getName()+" "+(fc+nc)+" bytes from client"); serverOut.flush(); // tutti i bytes sono stati inviati al server long contentLength = -1; String type =""; first =true; abuf.clear(); while (((line = serverIn.readLine()) != null) && (line.trim().length() != 0)) { abuf.insert(line+"\r\n"); if (first) { first = false; String [] response = line.split(" "); if ((response.length < 3)||!response[0].startsWith("HTTP")) break; // il server ha risposto in modo non HTTP... responseCode = Integer.parseInt(response[1]); println(getName()+" response code: "+responseCode); } if (line.startsWith("Last-Modified:")) { // lastModified=new Date(line.substring(14).trim()).getTime(); // 14 è la lunghezza di "Last-Modified:" try { lastModified = DateFormat.getDateInstance().parse(line.substring(14).trim()).getTime(); // 14 è la lunghezza di "Last-Modified:" } catch (java.text.ParseException ex) { } } if (line.startsWith("Content-Type:")) { type = line.substring(13).trim().split("/")[0]; // 13 è la lunghezza di "Content-Type:" println(getName()+" type: "+type); } } abuf.insert("\r\n"); abuf.writeStream(f); // esempio di intervento del proxy: solo testi e immagini vengono inviati verso il client: // BEGIN if (textimageProxy) { if (type.length() != 0) { if (!(type.startsWith("text")||type.startsWith("image"))) { abuf.clear(); abuf.insert("HTTP/1.0 404 Not Found\r\n"); abuf.writeStream(clientOut); println(getName()+" cancelling request"); clientOut.flush(); throw new RuntimeException("Proxy filter cancelled request"); } } } // END abuf.writeStream(clientOut); if (responseCode == 200) { content = new FileOutputStream(filename); } int r1, ns=abuf.size; while ((r1 = abuf.readStream(serverIn)) > 0) { ns+=r1; if (content != null) abuf.writeStream(content); // esempio di intervento del proxy: cerca gli iperlink che contengono le parole listate nel file list.txt // in caso positivo i testi dei link sono alterati con il simbolo # // BEGIN if (Proxy.wordSet != null) { if (type.startsWith("text")) filter(abuf); } // END abuf.writeStream(clientOut); } println(getName()+" "+ns+" bytes from server"); if (content != null) { content.close(); if (lastModified != 0) { new File(filename).setLastModified(lastModified); } } clientOut.flush(); } catch(SocketException e) { println(getName()+" "+e.getMessage()); // for some reason the client has closed the connection... } catch(UnknownHostException e) { println(getName()+" Unknown host: "+e.getMessage()); } catch(Exception e) { println(getName());e.printStackTrace(System.err); } finally { try { if (f != null) f.close(); if (clientIn != null) clientIn.close(); if (clientOut != null) clientOut.close(); if (serverIn != null) serverIn.close(); if (serverOut != null) serverOut.close(); if (fromClient != null) fromClient.close(); if (toServer != null) toServer.close(); Proxy.clientCount--; println(getName()+" quitting after "+((new Date().getTime())-time0)+ "ms"); if (Proxy.out != null) Proxy.out.flush(); } catch(Exception e) { println(getName()+"exception during quitting...");e.printStackTrace(System.err); } } } } // ProxyConnection