title: delphi 7, indy tcp proxy without remote server
body: <p>Is it possible to implement something like <strong>IdMappedPortTCP without connecting to a remote proxy server</strong>?</p>
  
  <p>What I need is 
  a) a way to edit every HTTP header (for example change the User-Agent for each request) the  for every request sent from my computer without having to connect to a remote server. And 
  b) If possible, I would also like to capture all the http traffic in delphi without the need of a third party application like proxifier.</p>
  
  <p>What i have tried so far is:</p>
  
  <p>a) IdMappedPortTCP and then binding to a remote proxy server, I then modify the AThread.NetData in each request in the IdMappedPortTCPExecute method. </p>
  
  <p>b) using proxifier to capture all http traffic in the computer.</p>
  
  <p>What I have tried so far is using Mapping with <code>IdMappedPortTCP</code> to a local proxy server (e.g. <code>squid</code>, <code>delegate</code>, <code>fiddler</code>, <code>ccproxy</code>), create my own proxy server (using <code>indy 10</code>) - All these worked great for HTTP connections but require installation of root certificate to modify HTTPS requests which is undesired. If its possible to implement any local proxy without having to install root certificates it would be awesome!</p>
  
  <p>I have also tried to modify the <a href="http://users.atw.hu/delphicikk/listaz.php?id=2325&amp;oldal=55" rel="nofollow">TCP REDIRECTOR CODE</a> but being fresh n all in programming, i haven't been successful. I figured i could change the </p>
  
  <pre><code>procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
  var
    Cli: TIdTCPClient;
    Len: Cardinal;
    Data: string;
  begin
    try
      Cli := nil;
      try
       { Create &amp; Connect to Server }
       Cli := TIdTCPClient.Create(nil);
       Cli.Host := 'www.borland.com';
       Cli.Port := 80;
       { Connect to the remote server }
       Cli.Connect;
       ..............
  </code></pre>
  
  <p>Such that I would extract the host and port from request and then assign <code>cli.host</code> to that host and port dynamically for each request. I don't know how viable that is. Like, would it cause computer to hang because of connecting to too many remote host?</p>
  
  <p>Update: with <code>TIdMappedPortTCP</code>, I used <code>AThread.Connection.Capture(myheaders,'');</code> so now I can assign my host to <code>myheaders.Values['host']</code> and if <code>AThread.Connection.ReadLn = 'CONNECT'</code> I set port to 443 otherwise I set it as 80. Am I on the right track?</p>
  
  <pre><code>procedure TForm1.IdMappedPortTCP1Connect(AThread: TIdMappedPortThread);
  var
    myheaders: TIdHeaderList;
    method : string;
  begin
    myheaders:=TIdHeaderList.Create;
    try
      method:= AThread.Connection.ReadLn;
      Athread.Connection.Capture(myheaders);        
      if myheaders.Count&lt;&gt;0 then begin
        if Pos('CONNECT',method)&lt;&gt;0 then begin
         with TIdTCPClient(AThread.OutboundClient) do begin
           Host:=myheaders.Values['host'];
           Port:=443;
         end;
       end else begin
         with TIdTCPClient(AThread.OutboundClient) do begin
           Host:=myheaders.Values['host'];
           Port:=80;
         end;
       end;
       TIdMappedPortThread(AThread).NetData:= method + #13#10 + myheaders.Text + #13#10 + #13#10;
      end else begin
        TIdMappedPortThread(AThread).NetData:= method + #13#10 + #13#10;
        outs.Lines.Add(TIdMappedPortThread(AThread).NetData);
      end;
    finally
      myheaders.Free;
    end;
  end;
  </code></pre>
  
  <p>I have put that code in the OnConnect event but it does not seem to be working. What have I done wrong? </p>