Hello all!
I am trying to develop some kind of proxy which filters the data the
browser
sents to a webserver (www.google.de, www.gmx.net, ...). For this
reason I set a Proxy Connection in my IE 6.0 Settings at port 8000
(e.g) and the server to localhost (the proxy that runs local). Now I
use the TCPListener which listens and recieves data after I typed in
and pressed the send button from the browser. After this I take the
data and send it via Socket Connection to the url which was listed in
the data from the browser. Then I receieve the response content from
the webserver and want to send it back to the browser, but something
in my sourcecode isnt working right. I sitting here for days with no
idea.
Here is the code so far:
private string ConnectToURL(string server){
Socket s = null;
IPEndPoint hostEndPoint;
IPAddress hostAddress = null;
int conPort = 80;
string content="";
IPHostEntry hostInfo = Dns.Resolve(server);
IPAddress[] IPaddresses = hostInfo.AddressList;
for (int i=0; i<IPaddresses.Length; i++){
hostAddress = IPaddresses[i];
hostEndPoint = new IPEndPoint(hostAddress, conPort);
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
s.Connect(hostEndPoint);
if(s.Connected){
byte[] bytes = Encoding.ASCII.GetBytes(this.textBox1.Text);
s.Send(bytes);
while(s.Receive(bytes)>0) {
content += Encoding.ASCII.GetString(bytes);
}
s.Shutdown(SocketShutdown.Both);
s.Close();
}
}
return content;
}
while(true){
try{
/***************************/
/* get header from browser */
/***************************/
this.tcpListener.Start();
this.socketToClient = this.tcpListener.AcceptSocket();
byte[] bytes = new byte[1024];
this.socketToClient.Receive(bytes, 0,
this.socketToClient.Available, SocketFlags.None);
this.tcpListener.Stop();
string header, host;
header = Encoding.ASCII.GetString(bytes);
host = header.Substring(header.IndexOf("Host: ")+6);
host = host.Substring(0, host.IndexOf("\n"));
this.textBox1.Text = header.ToString().Trim();
/*****************************/
/* Request data from browser */
/*****************************/
string content =ConnectToURL(host.Trim());
/*****************************/
/* Send data back to browser */
/*****************************/
byte[] bytes2 = ec.GetBytes(content);
this.socketToClient.Send(bytes2);
if(this.socketToClient.Connected){
this.socketToClient.Shutdown(SocketShutdown.Both);
this.socketToClient.Close();
}
}
catch (Exception e){
MessageBox.Show(e.ToString());
}
}
thank you in advance