/* 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; // Proxy port public static void main(String args[]) throws Exception { ServerSocket sfd; Socket cfd, pfd; BufferedReader br, brp; PrintWriter pw, pwp; String s; String rsname = "localhost"; // name of remote server int rsport = 80; // port of remote server sfd = new ServerSocket(SERVERPORT); sfd.setReuseAddress(true); while (true) { cfd = sfd.accept(); cfd.setSoTimeout(8000); // 8 second read timeout System.out.print("wwwww: connect from "+cfd.getInetAddress()+", "); pfd = cfd; // just to initialize pfd, ignore this; java idiosyncrasy // connect to remote server: // pfd = new Socket(args[0],Integer.parseInt(args[1])); try { br = new BufferedReader(new InputStreamReader(cfd.getInputStream())); pw = new PrintWriter(new OutputStreamWriter(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. String DURL = br.readLine(); // System.out.println("wwwww: 1st line: "+DURL); StringTokenizer st = new StringTokenizer(DURL," \t\n:/",true); 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("wwwww: remote server: "+rsname+":"+rsport); // open socket to remote server (the proxy socket) pfd = new Socket(rsname,rsport); pfd.setSoTimeout(8000); // 8 second read timeout rsname = "localhost"; rsport = 80; // restore defaults brp = new BufferedReader (new InputStreamReader(pfd.getInputStream())); pwp = new PrintWriter(new OutputStreamWriter(pfd.getOutputStream())); pwp.println(DURL); pwp.flush(); // retransmit first line // begin relay threads: Thread AB = new Thread(new rthread(br,pwp,cfd)); Thread BA = new Thread(new rthread(brp,pw,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 threads 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"); return s; } BufferedReader br; PrintWriter pw; Socket stoclose; // the socket to close public rthread(BufferedReader b, PrintWriter p, Socket sc) { br=b; pw=p; stoclose=sc; } private void serve() throws Exception { String s = "ab"; try { while (s!=null) { s = br.readLine(); //System.out.println("RT: "+s); s = makewholesome(s); if (s!=null) {pw.println(s); pw.flush();} else { pw.close(); br.close(); stoclose.close(); } } // System.out.println("thread exiting ..."); System.out.flush(); } catch (Exception e) {br.close(); pw.close(); stoclose.close(); } } public void run() { try { serve(); } catch (Exception e) {} } } // rthread /* This program was created for educational purposes only. */