Indy SSL to plain socket pump

核能气质少年 提交于 2019-12-11 19:19:32

问题


I have to provide an SSL front for a plain TCP server, so I'm making a "pump" application that will provide an SSL connection to the outside while the original server can stay plain.

I'm trying to use the Indy components to support my SSL needs, but I can't seem to get any data from the SSL port. I assigned the TIdTCPServer.OnExecute to the following event handler:

procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
var
 c:TIdTCPClient;
 cs,ss:TIdIOHandler;
 b:TBytes;
begin
 c:=TIdTCPClient.Create(Self);
 try
   c.Host:='127.0.0.1';
   c.Port:=60675;
   c.ConnectTimeout:=500;
   c.Connect;
   ss:=c.IOHandler;
   cs:=AContext.Connection.IOHandler;
   while (cs.Connected) and (ss.Connected) do
    begin
     if cs.CheckForDataOnSource(1) then
      begin
       try
        cs.ReadBytes(b,1,False);
       except on e:Exception do
        Memo1.Lines.Add(e.Message); //BAD out of Thread context
       end;
       if Length(b)>0 then
        ss.Write(b);
      end;
     if ss.CheckForDataOnSource(1) then
      begin
       ss.ReadBytes(b,1,False);
       if Length(b)>0 then
        cs.Write(b);
      end;
    end;
 finally
   c.Free;
 end;
end;

The TCP server has an SSL handler attached. I did the same on a plain HTTP server and it worked fine, so I'm assuming my SSL setup is not the issue.

cs=Client Side (the server socket) and ss=Server side (the client for the TCP server I'm trying to add SSL to).

Now, I know it needs cleanup and doing 1ms waits isn't pretty, but before I can attack that issue, I'd like to receive some data.

Neither of my ReadBytes get called. When I used cs.Readable(), I get true just once, but I still couldn't read.

What can I do to make a pump? Why am I not getting data?


回答1:


Try using the TIdMappedPortTCP component instead of TIdTCPServer directly. TIdMappedPortTCP handles all the work of passing data back and forth between a client and another server. By default, the outbound connection to the second server is not encrypted, even if the inbound connection to TIdMappedPortTCP is encrypted.



来源:https://stackoverflow.com/questions/7707429/indy-ssl-to-plain-socket-pump

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!