Only 44 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Web proxy server in java  RSS feed

 
Alexander Hakim
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone:

For our previous topic about "Proxy Server in Java" I want to add the following futurities

The web proxy server to filters out and blocks requests for undesirable URLs, using HTTP redirection to display an error page.

I want the user to select the proxy port (i.e. the port number should not be hard coded)

The proxy should be smart in selection of what HTTP content should be searched for the forbidden keywords. For example, you probably agree that it is not wise to search inside compressed or other non-text-based HTTP content such as graphic files, etc.

Also i want to make sure it is compatible with all major browsers (e.g. Internet Explorer, Mozilla Firefox, Google Chrome, etc.)

Here is the code;


package ProxyServer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class MyProxyServer
{

public static final int portNumber = 8888;

public static void main(String[] args)
{
MyProxyServer myProxyServer = new MyProxyServer();
myProxyServer.start();
}
public void start()
{
System.out.println("Starting the MyProxyServer ...");
try
{

ServerSocket serverSocket = new ServerSocket(MyProxyServer.portNumber,1);

/*File file = new File("test.txt");
FileOutputStream fileOutputStream = new FileOutputStream(file);

PrintStream printStream = new PrintStream(fileOutputStream);*/

while(true)
{
System.out.println("Inside while loop ");
Socket clientSocket = serverSocket.accept();
System.out.println("Connection to MyProxyServer is "+clientSocket.isConnected());

InputStreamReader inputStreamReader = new InputStreamReader(clientSocket.getInputStream());

BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String command = bufferedReader.readLine();


//printStream.print(command);

//bufferedReader.close();
System.out.println("Client has asked to ....\n"+command);

if(command.equals("Cancel"))
{
System.out.println("Shutting down the server ...");
break;
}

// iterate over all the lines following the GET/POST request
String meta = null;
while((meta = bufferedReader.readLine()).length() > 0)
{
System.out.println(meta);
}
bufferedReader.close();
/*printStream.close();
fileOutputStream.close();*/
}



} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}



With my best regards;
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any particular reason you're starting from scratch instead of adapting an existing proxy like Muffin?
 
Alexander Hakim
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ulf Dittmer;

Actually I did also put my question in the already started profile by omkar patkar about proxy server. I am sorry not to put the code in the "code" tag,

Looking for the help;

Thanks;
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you saying that you have two accounts?If so, which one do you want to use going forward? We will need to close the other one.

Also, what is the answer to my question?
 
Alexander Hakim
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to this forum, I have only one account which is i am using now. And this is my account which i will use going forward.

I have a task to solve about "Web proxy server using Java", I was searching and reading in the internet and i found this forum interesting and i crate a profile today to keep discussing with professionals.

The reason i start the discussion from scratch is because i though it is a different scope from what other people had been discussing in the previous topic, so that i can clearly understand the solution for my questions. If you

think this is wrong, we can keep discussing in the existing proxy topic like Muffin.


thank you for help;
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there is an existing topic where you think your question fits in, then by all means, let's continue in that topic. If you have code of your own that you want to talk about, that is likely not the case, though.

But I'm still not clear whether for some reason you want/need to write code of your own, or whether basing your effort on an existing solution - Muffin or something else- would be acceptable.
 
Alexander Hakim
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i am discussing about an existing topic. You can look at the previous topic "Proxy Server in Java", Topic Starter "omkar patkar". I also post my question there but i didn't get a reply. I don't have any special reason to start my discussion as a new topic, if it is wrong, am sorry for that. Now let us refer to our previous topic "Proxy Server in Java", and lets discuss about my question there.

Sorry for the wrong thing i did

Thanks;
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know that you did anything wrong. You mentioned a number of things that your code is lacking - if memory serves, Muffin and other existing proxies do those. So the question is: do you want/need your code to do this, or would adapting existing code suffice? If the latter, then it doesn't make sense to go to any length discussing the former. Or maybe I missed where you addressed that?
 
Alexander Hakim
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My dear;

I don't know if my English is not clear, may be because it is my second language. What i want is, there is a web proxy server java code which i posted it above, i want to modify it with the above features i listed. And i am looking for a help so that the above code will fulfill my requirements.

I hope now you understand my point:

Thanks/
 
Alexander Hakim
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, i think i got your point now:

I am not looking for already built in software like what you mentioned "Muffin" or others like "Net Nanny". The thing is i want to write my own solution to restrict access to Web content, and i want to develop web proxy server using Java.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, now I understand what you want (although I don't understand *why* you want to write it yourself - seems like reinventing the wheel).

The port number can be passed in via the command line, that's a very small change.

The list of undesirable words should probably be in a file, one word per line. You should read it into a List<String> when starting the proxy.

You will need to change you code from handling text to handling binary data. Only if you determine that you need to examine the content of a file should you convert it to text, so that you can use String methods. Be aware that determining the encoding of a file is not as easy as it might at first seem.

Considering all this (and there's more I haven't mentioned) you may wish to reconsider using existing software. Otherwise you have a lot of work ahead of you.

As to supporting different browsers, a proxy should be agnostic to that.
 
Alexander Hakim
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help;

The reason why i want to write it my self is because i have a small project in my Networking course and i am required to develop my own web proxy server that blocks undesirable web address.

I would appreciate if i can see the implementation(coding) part of what you tried to explain above.

Thanks again for your help;
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the implementation is for you to write - it's your project, after all. We can try to help with specific questions in case you get stuck.
 
Alexander Hakim
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ulf;

Thanks for your attention toward my problem;

Below is my code, i have text file called test.txt that i used to store the restricted URL texts and i write the code below to read from the text file, now i am expected to compare the request from the client with the text in my text file. how can i compare two BufferedReaders? One buffereReader for the client request and one buffereReader for the text file.

 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said, you should read the banned words into a data structure once at startup, not repeatedly for each file you want to scan.

In the most trivial implementation, you would iterate over that data structure for each line of HTML you're checking.
 
Alexander Hakim
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay;

What if i store the list of undesirable words in variables inside the code? Do you think i need to change my code from handling text to handling binary data? Can you give me a sample code of doing this please, or reading materials?

thanks
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already mentioned the data structure that I think is the natural choice for storing those words.

Binary data is handled by Streams instead of Readers, and read into byte[]. You may want to treat all files as binary that you don't intend to filter, to avoid the overhead of converting back and forth between bytes and text.

You will need an algorithm to determine which files to filter, probably by looking at the requested URL. To do that, you will need to extract HTTP headers from the byte stream. To do that, you will need a good understanding of HTTP.

This is a big project.
 
Alexander Hakim
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf;

Seems it is working now, i am able to compare the two string files (the string from the text file and the client request string that i declared as above 'command') with simple java code .contains, using this am able to refuse if the request contains bad word.

thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!