SSL tunnel using Synapse? lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- BigChimp · 2011-06-10 10:32 Hi, For my Checkride UltraVNC over SSL reverse connect remote support tool https://bitbucket.org/reiniero/checkride I'm using the stunnel executable to listen on a local port for VNC traffic and forward it through an SSL/TLS tunnel to the viewer on the other end (same setup: stunnel provides the SSL connection). This will be my first multithreading/networking project, so I'd like to find out if I'm thinking in the right direction. Would it be feasible to replace stunnel with Pascal code using e.g. synapse? I'd imagine I'd have to create 2 additional threads, one to listen/send on the local socket, one to set up the SSL connnection and listen/send on that. Then I'd have to have some kind of queue and forward the packets in both threads using something like critical sections. Any similar code that I could review for inspiration? Thanks, BigChimp ---------------------------------------------------------------------- BigChimp · 2013-05-18 08:30 Bump: Quote from: BigChimp on June 10, 2011, 12:32:37 pmWould it be feasible to replace stunnel with Pascal code using e.g. synapse? Any similar code that I could review for inspiration? ... perhaps there are some new people with new insights ---------------------------------------------------------------------- snorkel · 2013-06-17 22:07 You can do this with Synapse, there is a port forwarding example somewhere. Let me see if I can find the code. You only need one thread I believe as one socket can read and write, you just have to use select to monitor the socket for new data and when new data is available you just write it to the remote host. I couldn't find the full project but these are the two methods it used. Code: [Select]PROCEDURE DoTransfer(FromSocket : TTCPBlockSocket ; ToSocket : TTCPBlockSocket ); VAR Buff : ARRAY[0..cMaxBuffer-1] OF CHAR ; BuffLen : INTEGER ; P : Pchar ; Total : INTEGER ; Delta : INTEGER ; Count : INTEGER ; BEGIN BuffLen := SIZEOF(Buff); BuffLen := FromSocket.recvBuffer(@Buff, BuffLen); IF FromSocket.LastError = 0 THEN BEGIN Total := 0 ; Delta := 0 ; P := Buff ; Count := BuffLen; WHILE Total < Count DO BEGIN TRY Delta := Count - Total ; IF Delta > cMaxBuffer THEN Delta := cMaxBuffer ; Delta := ToSocket.SendBuffer(P,Delta); EXCEPT BREAK ; END ; INC(P,Delta); INC(Total,Delta); END ; END ELSE BEGIN BREAK; END; END; function RelayTCP(const fsock, dsock: TTCPBlockSocket;timeout:integer): boolean; var FDSet: TFDSet; FDSetSave: TFDSet; TimeVal: PTimeVal; TimeV: TTimeVal; begin result := false; //buffer maybe contains some pre-readed datas... if fsock.LineBuffer <> '' then begin buf := fsock.RecvPacket(timeout); if fsock.LastError <> 0 then Exit; dsock.SendString(buf); end; //begin relaying of TCP try TimeV.tv_usec := (Timeout mod 1000) * 1000; TimeV.tv_sec := Timeout div 1000; TimeVal := @TimeV; if Timeout = -1 then TimeVal := nil; FD_ZERO(FDSetSave); FD_SET(fsock.Socket, FDSetSave); FD_SET(dsock.Socket, FDSetSave); FDSet := FDSetSave; while synsock.Select(65535, @FDSet, nil, nil, TimeVal) > 0 do begin if FD_ISSET(dsock.Socket, FDSet) then DoTransfer(dsock,fsock); if FD_ISSET(fsock.Socket, FDSet) then DoTransfer(fsock,dsock); FDSet := FDSetSave; end; finally end; result := true; end; ---------------------------------------------------------------------- BigChimp · 2013-06-18 05:33 Thanks snorkel - I've got some aborted attempt sitting on my harddrive - I'll see if it matches your code PS: Sorry, a bit like looking a gift horse in the mouth but the code in your post would look much better surrounded by code tags