Article View: borland.public.cppbuilder.internet.socket
Article #5142Re: Indy TCPServer and proxies
From: Lapo Guidi
Date: Sat, 09 Oct 2004 16:52
Date: Sat, 09 Oct 2004 16:52
132 lines
5015 bytes
5015 bytes
Hi Remy,
> Not only that, but IE's proxy configuration is global, meaning that
setting
> up IE to use a proxy server is going to effect all browser windows through
> your whole machine, not just your particular program.
It's good for me because all the traffic should be logged.
> What do you mean by "manage" exactly?
I would like to lock some sites or redirect some others.
I suppose i can do that replacing some strings from downloaded files before
to sent to the requester.
If you have the time, I write a snip of code below.
I have a TIdTCPServer that listen on a port.
on it's OnExecute it creates a TThread descendant with a TIdTCPClient inside
that will ask to the remote server, elaborate the response and resend back
to the requester.
But in some cases it doesn't work properly.
Thanks for any tip.
Lapo
------------------------
void __fastcall TForm1::ServerExecute(TIdPeerThread *AThread)
{
TProxyConnection *Proxy=new TProxyConnection(AThread->Connection, Log);
Proxy->WaitFor();
delete Proxy;
}
//--------------------------------------------------------------------------
-
__fastcall TProxyConnection::TProxyConnection(TIdTCPServerConnection
*AReqConnection, TMemo *ALogMemo)
: TThread(true)
{
FreeOnTerminate=false;
ReqConnection=AReqConnection;
LogMemo=ALogMemo;
RemData="";
Resume();
}
//--------------------------------------------------------------------------
-
void __fastcall TProxyConnection::Execute()
{
AnsiString RawData="";
try {
try {
// Get the requested string
RawData=ReqConnection->CurrentReadBuffer();
if(RawData.Length()>0) {
RemData="From Local\r\n"+RawData;
Synchronize(Log);
RemData="";
// Get RemoteHost and Port
AnsiString Host;
int Port;
int pos=0;
pos=RawData.Pos("Host: ");
if(pos) {
Host=RawData.SubString(pos+6, 4096);
int pos=1;
while(!Host.IsDelimiter("\r\n\0", pos))
pos++;
Host.SetLength(pos-1);
pos=Host.Pos(":");
if(pos) {
AnsiString PortStr=Host.SubString(pos+1, 5);
Port=StrToInt(PortStr);
Host.SetLength(pos-1);
}
else {
Port=80;
}
if(Host.Length()>0 && Port) {
// Create the Client socket
Client=new TIdTCPClient(NULL);
// Connect to the remote host
Client->Host=Host;
Client->Port=Port;
Client->Connect(5000);
// Send the requested string
Client->Write(RawData);
// Get the response string
RemData="";
while(Client->Connected()) {
RemData=RemData+Client->CurrentReadBuffer();
}
ResData=RemData;
// Log the response string
RemData="From Remote\r\n"+RemData;
Synchronize(Log);
RemData="";
// Send back the response string
ReqConnection->Write(ResData);
}
}
}
}
catch(...) {
RemData="ERROR \r\nRawData:"+RawData+"\r\nRemData:"+RemData;
ReqConnection->Disconnect();
Synchronize(Log);
}
}
__finally {
delete Client;
}
}
//--------------------------------------------------------------------------
-
void __fastcall TProxyConnection::Log(void)
{
LogMemo->Lines->Add(RemData);
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::NavigationClick(TObject *Sender)
{
AnsiString UrlStr="http://www.yahoo.com";
Web->Navigate(WideString(UrlStr), TVariant(0), TVariant(""), TVariant(""),
TVariant("\r\n"));
Web->SetFocus();
}
//--------------------------------------------------------------------------
-
Technical Details
Message-ID:
<4168173c$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!news-FFM2.ecrc.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<4166efc5$1@newsgroups.borland.com> <4166fc3a$1@newsgroups.borland.com> <41671c26@newsgroups.borland.com> <4167289e$1@newsgroups.borland.com> <416732b1$1@newsgroups.borland.com> <41673f29$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,3acb790ca748eb20 X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Lapo Guidi" <lapoguidi@inwind.it> Newsgroups: borland.public.cppbuilder.internet.socket References: <4166efc5$1@newsgroups.borland.com> <4166fc3a$1@newsgroups.borland.com> <41671c26@newsgroups.borland.com> <4167289e$1@newsgroups.borland.com> <416732b1$1@newsgroups.borland.com> <41673f29$1@newsgroups.borland.com> Subject: Re: Indy TCPServer and proxies Date: Sat, 9 Oct 2004 18:52:48 +0200 Lines: 134 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1437 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441 NNTP-Posting-Host: 82.49.68.221 Message-ID: <4168173c$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1097340732 82.49.68.221 (9 Oct 2004 09:52:12 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!news-FFM2.ecrc.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail