/* 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 wwwww { static final int SERVERPORT = 8010; 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; try { din = new DataInputStream(cfd.getInputStream()); // using binary io dout = new DataOutputStream(cfd.getOutputStream()); // to be sure // 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); // parsing s = st.nextToken(); if (!s.equals("GET")) throw new Exception("format error"); 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); // trace // 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 // begin relay threads: Thread AB = new Thread(new rthread(din,pout,cfd)); Thread BA = new Thread(new rthread(pin,dout,pfd)); 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 { // We don't relay bad words! String makewholesome(String s) { s = s.replaceAll("damn ","darn "); s = s.replaceAll("hell ","heck "); s = s.replaceAll("Liang is evil","It's not Liang's fault"); return s; } // make the 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) { din=b; dout=p; stoclose=sc; } private void serve() throws Exception { String s = "ab"; try { while (s!=null) { int rb = din.read(inbuf,0,1500); s = myutils.bufToString(inbuf,0,rb); //s = br.readLine(); //System.out.println("RT: "+s); //trace if (s!=null) s = makewholesome(s); if (s!=null) myutils.StringToBuf(inbuf,s,0); dout.write(inbuf,0,rb); dout.flush(); } } catch (Exception e) { din.close(); dout.close(); stoclose.close(); } } public void run() { try { serve(); } catch (Exception e) {} } } // rthread /* This program was created for educational purposes only. */