/* WWWWW : The Wholly Wholesome World Wide Web Delivering only wholesome web pages to your house ... */ import java.io.*; import java.net.*; import java.util.*; public class wwwww2 { static final int SERVERPORT = 8011; public static void main(String args[]) throws Exception { ServerSocket sfd; Socket cfd, pfd; DataInputStream din, pin; DataOutputStream dout, pout; String s; String rsname = "localhost"; // name of remote server int rsport = 80; // default port of remote server sfd = new ServerSocket(SERVERPORT); sfd.setReuseAddress(true); // for debugging byte[] inbuf = new byte[1500]; byte[] outbuf = new byte[1500]; while (true) { cfd = sfd.accept(); cfd.setSoTimeout(8000); // 8 second read timeout System.out.print("wwwww: connect from "+cfd.getInetAddress()+", "); pfd = null; // proxy socket try { din = new DataInputStream(cfd.getInputStream()); dout = new DataOutputStream(cfd.getOutputStream()); // parse request to retrieve destination URL: // "request comes in the form GET http://URL " - for this example // we wont' go into the details of parsing. int rb = din.read(inbuf,0,1500); String DURL = myutils.bufToString(inbuf,0,rb); // System.out.println("wwwww: 1st line: "+DURL); //trace StringTokenizer st = new StringTokenizer(DURL," \t\n:/",true); s = "abc"; s = st.nextToken(); // if (!s.equals("GET")) // throw new Exception("format error: "+s); while (!s.equals("GET")) { System.out.print(s+ " "); s = st.nextToken(); } System.out.println(s+ " received"); s = st.nextToken(); s=st.nextToken(); // skip space if (!s.equals("http")) throw new Exception("protocol not supported"); st.nextToken(); st.nextToken(); st.nextToken(); // skip "://" rsname = st.nextToken(); String colon = st.nextToken(); if (colon.equals(":")) rsport = Integer.parseInt(st.nextToken()); System.out.println("hwwwww: remote server: "+rsname+":"+rsport); // open socket to remote server (the proxy socket) pfd = new Socket(rsname,rsport); System.out.printf("pfd opened to %s:%d\n",rsname,rsport); System.out.flush(); pfd.setSoTimeout(8000); // 8 second read timeout //rsname = "localhost"; rsport = 80; // restore defaults pin = new DataInputStream(pfd.getInputStream()); pout = new DataOutputStream(pfd.getOutputStream()); //myutils.stringToBuf(outbuf,DURL,0); pout.write(inbuf,0,rb); pout.flush(); // relay exactly what was received // pw.print("HTTP/1.1 200 OK\n\0"); pw.flush(); // send preamble to client // begin relay threads: Thread AB = new Thread(new rthread(din,pout,cfd,false)); Thread BA = new Thread(new rthread(pin,dout,pfd,true)); AB.start(); BA.start(); BA.join(); AB.join(); System.out.println("connection from "+cfd.getInetAddress()+" terminated"); pfd.close(); cfd.close(); } catch (Exception e) {System.out.println(e); cfd.close(); pfd.close();} } // while } // main } // wwwww class // a thread that relay info in one direction: class rthread implements Runnable { boolean zflag = false; // zero-terminated strings boolean censor = false; // not needed each way. // We don't relay bad words! --- MAKE IT WHOLESOME! String makewholesome(String s) { s = s.replaceAll("damn ","darn "); // no unwholesome content on the wwwww! s = s.replaceAll("hell ","heck "); s = s.replaceAll("Satan","XXXXX"); // blot out Satan and his followers ... s = s.replaceAll("Liang","XXXXX"); return s; }// make the whole wide world wholly wholesome. DataInputStream din; DataOutputStream dout; Socket stoclose; // the socket to close byte[] inbuf = new byte[1500]; byte[] outbuf = new byte[1500]; public rthread(DataInputStream b, DataOutputStream p, Socket sc, boolean c) { din=b; dout=p; stoclose=sc; censor=c;} private void serve() throws Exception { String s = "ab"; int len = 0; int rb = 1; int inclen = 0; try { while (rb>0) // (s!=null) { rb = din.read(inbuf,0,1500); if (rb>0) s = myutils.bufToString(inbuf,0,rb); else s=null; if (s!=null) len = s.length(); else len=0; if (s!=null && censor) s = makewholesome(s); if (s!=null) myutils.StringToBuf(inbuf,s,0); if (rb>0) dout.write(inbuf,0,rb+inclen); //pw.println(s); //if (zflag) { pw.print("\0"); zflag = false;} dout.flush(); } // System.out.println("thread exiting ..."); System.out.flush(); } catch (Exception e) { din.close(); dout.close(); stoclose.close(); System.out.println(e);} } public void run() { try { serve(); } catch (Exception e) {} } } // rthread /* This program was created for educational purposes only. */