I try to create a firewall that have packet redirect for unwanted web request. The problem that I have next is how to make our HTML file as the web response for the client request. The scene like this :
1. Client request for something on web. [DONE]
2. My firewall scan for the web request from client, check if the request is not allowed [DONE]
3. Create an HTML file that will show when the request from client is not allowed. [DONE]
4. Transfer the HTML file as the web request. [NOT DONE]
5. If the request is blocked then the client web browser will display our HTML. [NOT DONE]
Anyone know to resolve this. I have analize the packet for web response, it seem encrypted or something else.
Thanks. Happy coding.

Create web response from HTML file ?
James F
I didn't try to give you a complete solution. I gave you some sample code to get you started. You are not communicating what exactly you are wanting, so this is the best we can do to help you until you state your problem more clearly.
Good luck.
Thomas_K
Tamir Gal
Kiran_deep
GoodNews
I want to create the same as SQUID but for windows. I have an application like SQUID for windows but very difficult to understand. I want to create like that but the methods are simple so that even a colleger first degree understand. I live in Indonesia, I can create a difficult way but other of my friend will not understand. I want they to understand so that I can make a better application.
Ok that the past. The point that for blocking of request I've been made, but then I want to tell the client that the request have been blocked by my application (in this case I can call it Firewall, for you maybe else). If client request by browser, I want to change the error "Request timeout" by HTML file that I create.
Client Request -------------------->|---|
| P |----------------->|----------|
| y | | Internet |
| B | | |
1.My HTML Error Page | | | |
Client Browser <--------------------| | | |
2.Web Response | | | |
Client Response <-------------------|---|<-----------------| |
| | | |
|---| |----------|
Now my sistem like above, if client request need blocked then create packet web response with My HTML Error Page (Process 1), If not blocked then request will passed, if INTERNET response, then the response will passed to client (Process 2).
PS: Difficult to put image in here.
I hope anyone understand, if not you can ask me what I doing (sound silly huh).
Thanks.
derekslager
Anyway thanks for reply.
John Talbott
I completely understood what you are doing.
This is what you need to do.
1. Accept connection
2. Parse the incoming HTTP request
3. If it is good then connect to the real server and send the request to the real server
4. Wait for teh response
5. Send the response back to the client
6. You might have to modify the headers to indicate that the request is coming via a proxy
In case the request is not valid you are generating an HTML response your self
This should be in the form of
HTTP/1.1 403 Forbidden
Content-Type: text/html
Content-Length: <nnnnn>
The content you want to get is blocked
Thats all you need to do.
What you must also do is to maintain connections on your proxy.
Note that multiple browsers can connect to your server simultaneously.
Harinadh
Wow, that was a very complex answer to a simple question. Sounds like you are using sockets directly to read the request from the client. Looks to me like Durgaprasad already answered your question.
His example is correct, all you need to do is send back the response headers followed by an empty line and then the body of the response. In your case, the response body will be the contents of an html file...
Here is some sample code...note that I am sending back the "Connection: Close" header, which you may or may not want. You may want to read up on the HTTP RFC to give you a little more understanding as to what is happening.
void SendHtmlResponse(Socket client, int statusCode, string statusDescription, string htmlFilePath)
{
FileStream htmlStream = File.OpenRead(htmlFilePath);
try
{
string respHeaders = "HTTP/1.1 {0} {1}\r\nContent-Type: text/html\r\nContent-Length: {2}\r\nConnection: Close\r\n\r\n";
respHeaders = String.Format(respHeaders,statusCode,statusDescription,htmlStream.Length);
client.Send(Encoding.ASCII.GetBytes(respHeaders));
byte [] buffer = new byte[1024];
int count = htmlStream.Read(buffer,0,buffer.Length);
while (count > 0) {
client.Send(buffer,0,count,SocketFlags.None);
count = htmlStream.Read(buffer,0,buffer.Length);
}
}
finally
{
htmlStream.Close();
}
}
voidlogic
GATEWAY
+------+ +----------------------------------------+
|Client|<---------------->|-| +---------------+|
+------+ | | +----------------+ | Block using ||
| ->| Capture Packet |->| Packet Filter ||
+------+ | | +----------------+ | API || +----------+
|Client|<---------------->|-| || +---------------+|<---->| INTERNET |
+------+ | | \/ | +----------+
| | +-------------------+ |
+------+ | | | Modify source | |
|Client|<---------------->|-| | address of packet |<-------------->|
+------+ | | as Gateway IP | |
| +-------------------+ |
+----------------------------------------+
(Better view copy this into notepad).
Scene:
1. Client request something into Internet.
2. Capture the packet using Sniff method.
3. We know even using Sniff method Network Packet still passed into internet, thats why we put Packet Filter API to block client packet passed into INTERNET.
4. After capture, modify the source address of packet into GATEWAY IP, because for Packet Filter only Packet with GATEWAY IP can passed into INTERNET. PS: Don't forget save the packet into Hashtable so we can know who request this if we receive web response from INTERNET.
5. Pass this packet into internet. (Use system.net.socket with mode Raw Socket, this way we can send custom IP header).
I think it very simple, because I don't like make sistem complicated.
"SIMPLE BUT SECURE"
PS: Still difficult put image on this forum.
I hope you understand.
Frenske
Thanks for tell me of System.IO.Compression Namespace, I really new on .NET Programming.
Thank you.
sydneyausguy
Well basically your firewall needs to send HTML back to the browser
You need to send the HTTP header for response status 200 or whatever status code you want to use you may want to use 403 forbidden or so. Then set the content length header and send the content along with it.
Shola
Keith Dorken
Can you please clearly explain what exactly is doing on
Did you check out the System.IO.Compression namespace