Filtering URL content in a self written proxy using Java -
Filtering URL content in a self written proxy using Java -
i need code proxy server assignment in university. should able display content of simple web page given our professor.
additionally should filter url , url content, , block web page if 1 of these contain 1 of words in "bad" array. question is, how can perform filtering in java.
code:
import java.io.*; import java.net.serversocket; import java.net.socket; import java.util.scanner; public class proxyserver{ //crate port user wants proxy on static scanner sc = new scanner(system.in); public static final int portnumber = sc.nextint(); public static void main(string[] args) { proxyserver proxyserver = new proxyserver(); proxyserver.start(); } public void start() { system.out.println("starting simpleproxyserver ..."); seek { string bad[]= new string[4]; bad[0]= "spongebob"; bad[1]= "britney spears"; bad[2]= "norrköping"; bad[3]= "paris hilton"; serversocket serversocket = new serversocket(proxyserver.portnumber); system.out.println(serversocket); byte[] buffer= new byte [1000000] ; while (true) { socket clientsocket = serversocket.accept(); inputstream inputstream = clientsocket.getinputstream(); system.out.println(" das passiert vor dem browser request:"); int n = inputstream.read(buffer); string browserrequest = new string(buffer,0,n); system.out.println("das ist der browserrequest: "+browserrequest); system.out.println("das ist der erste abschnitt"); int start = browserrequest.indexof("host: ") + 6; int end = browserrequest.indexof('\n', start); string host = browserrequest.substring(start, end - 1); system.out.println("connecting host " + host); socket hostsocket = new socket(host, 80); //i can alter host on here outputstream hostoutputstream = hostsocket.getoutputstream(); printwriter writer= new printwriter (hostoutputstream); bufferedreader reader = new bufferedreader(new inputstreamreader(inputstream)); string input= null; while (((input= reader.readline())!= null)){ writer.write(input); writer.flush(); system.out.println("empfangen vom client: "+input); } // (int i=0; i<4;i++){ // if (instring.contains(bad[i])){ // system.out.println("bye idiot"); // break; // } // } system.out.println("forwarding request server"); hostoutputstream.write(buffer, 0, n);// buffer fetched client remains same hostoutputstream.flush(); inputstream hostinputstream = hostsocket.getinputstream(); outputstream clientgetoutput = clientsocket.getoutputstream(); system.out.println("forwarding request server"); { n = hostinputstream.read(buffer); string inhalt= hostinputstream.tostring(); system.out.println("das ist der inhalt vom host: "+ inhalt); string vomhost = new string(buffer,0,n); system.out.println("vom host\n\n"+vomhost); // for(int i=0;i<n;i++){ // system.out.print(buffer[i]); // } system.out.println("receiving " + n + " bytes"); if (n > 0) { clientgetoutput.write(buffer, 0, n); } } while (hostinputstream.read(buffer)!= -1);//n>0 clientgetoutput.flush(); hostsocket.close(); clientsocket.close(); system.out.println("end of communication"); } } grab (ioexception e) { e.printstacktrace(); } } }
java url proxy filtering
Comments
Post a Comment