//---------------------------------------------------------------------------
#ifndef TunnelSocketsH
#define TunnelSocketsH
//---------------------------------------------------------------------------
#include
//---------------------------------------------------------------------------
#define TNLSCK_WAIT_TIME 5000
#define TNLSCK_RETRIES 6
//---------------------------------------------------------------------------
namespace NsHttpTunneling
{
enum TSocketType { stProxy, stSource, stTarget };
typedef void __fastcall (__closure *TNotifyLogEntry)(AnsiString);
//-----------------------------------------------------------------------
class TSocketLog : public TComponent
{
private:
static AnsiString m_strLogFile;
static int m_iInstances;
static TStringList* m_slLog;
static bool m_bLog;
int m_iID;
TNotifyLogEntry m_OnAddLogEntry;
void __fastcall SetLog(bool bLog);
protected:
public:
__fastcall TSocketLog(TComponent* Owner);
__fastcall ~TSocketLog();
void __fastcall AddEntry(AnsiString strText);
void __fastcall SaveLog();
__property AnsiString __fastcall LogFile = { read = m_strLogFile, write = m_strLogFile };
__property bool __fastcall Logging = { read = m_bLog, write = SetLog };
__property int __fastcall Instances = { read = m_iInstances };
__property int __fastcall ID = { read = m_iID };
__property TNotifyLogEntry OnAddLogEntry = { read = m_OnAddLogEntry, write = m_OnAddLogEntry };
};
//-----------------------------------------------------------------------
AnsiString TSocketLog::m_strLogFile = "";
int TSocketLog::m_iInstances = 0;
TStringList* TSocketLog::m_slLog = NULL;
bool TSocketLog::m_bLog = true;
//-----------------------------------------------------------------------
class TSocketPair : public TSocketLog
{
private:
AnsiString m_strTargetHost;
int m_iTargetPort;
TClientSocket* m_clTargetSocket;
TCustomWinSocket* m_cwSourceSocket;
bool m_bSrcConnected;
bool m_bTgtConnected;
bool m_bBusy;
bool m_bError;
TSocketNotifyEvent m_OnTargetConnected;
TSocketNotifyEvent m_OnTargetRead;
TSocketNotifyEvent m_OnSourceRead;
TCustomWinSocket* __fastcall GetTargetSocket();
void __fastcall SetTargetHost(AnsiString strHost);
void __fastcall SetTargetPort(int iPort);
void __fastcall TargetConnected(TObject* Sender, TCustomWinSocket* Socket);
void __fastcall OnReadData(TObject* Sender, TCustomWinSocket* Socket);
void __fastcall OnDisconnect(TObject* Sender, TCustomWinSocket* Socket);
void __fastcall SendToSource(TObject* Sender, TCustomWinSocket* Socket);
void __fastcall SendToTarget(TObject* Sender, TCustomWinSocket* Socket);
void __fastcall SocketEventHandler(TObject* Sender, TCustomWinSocket* Socket, TSocketEvent SocketEvent);
void __fastcall SocketErrorHandler(TObject* Sender, TCustomWinSocket* Socket, TErrorEvent ErrorEvent, int &ErrorCode);
protected:
__property TCustomWinSocket* __fastcall TargetSocket = { read = GetTargetSocket };
__property TCustomWinSocket* __fastcall SourceSocket = { read = m_cwSourceSocket };
AnsiString __fastcall ParseHttpGetQuery(AnsiString strText, int& iPort);
char* __fastcall ReadDataFromSocket(TCustomWinSocket* cwSocket, int& iLen);
void __fastcall SendData(TCustomWinSocket* cwSocket, char* cBuffer, int iLen);
public:
__fastcall TSocketPair(TComponent* Owner, TCustomWinSocket* cwSourceSocket);
__fastcall ~TSocketPair();
void __fastcall Open();
void __fastcall Close();
__property AnsiString __fastcall TargetHost = { read = m_strTargetHost, write = SetTargetHost };
__property int __fastcall TargetPort = { read = m_iTargetPort, write = SetTargetPort };
__property bool __fastcall Busy = { read = m_bBusy };
__property TSocketNotifyEvent OnTargetConnected = { read = m_OnTargetConnected, write = m_OnTargetConnected };
__property TSocketNotifyEvent OnTargetRead = { read = m_OnTargetRead, write = m_OnTargetRead };
__property TSocketNotifyEvent OnSourceRead = { read = m_OnSourceRead, write = m_OnSourceRead };
};
//-----------------------------------------------------------------------
class TSslBridge : public TSocketPair
{
private:
char* m_cBuffer;
int m_iLen;
void __fastcall SslTargetConnected(TObject* Sender, TCustomWinSocket* Socket);
void __fastcall SslSourceRead(TObject* Sender, TCustomWinSocket* Socket);
protected:
public:
__fastcall TSslBridge(TComponent* Owner, TCustomWinSocket* cwSourceSocket);
__fastcall ~TSslBridge();
};
//-----------------------------------------------------------------------
class THttpTunnel : public TSocketPair
{
private:
char* m_cBuffer;
int m_iLen;
AnsiString m_strBridgeHost;
AnsiString m_strProxyHost;
AnsiString m_strUser;
AnsiString m_strPassword;
int m_iProxyPort;
void __fastcall SetProxyHost(AnsiString strHost);
void __fastcall SetProxyPort(int iPort);
AnsiString __fastcall IntToBase64(unsigned int uiNmb);
AnsiString __fastcall Base64Encode(AnsiString strText);
void __fastcall TunnelTargetConnected(TObject* Sender, TCustomWinSocket* Socket);
void __fastcall TunnelTargetRead(TObject* Sender, TCustomWinSocket* Socket);
void __fastcall TunnelSourceRead(TObject* Sender, TCustomWinSocket* Socket);
protected:
public:
__fastcall THttpTunnel(TComponent* Owner, TCustomWinSocket* cwSourceSocket);
__fastcall ~THttpTunnel();
__property AnsiString __fastcall BridgeHost = { read = m_strBridgeHost, write = m_strBridgeHost };
__property AnsiString __fastcall ProxyHost = { read = m_strProxyHost, write = SetProxyHost };
__property int __fastcall ProxyPort = { read = m_iProxyPort, write = SetProxyPort };
__property AnsiString __fastcall ProxyUser = { read = m_strUser, write = m_strUser };
__property AnsiString __fastcall ProxyPswd = { read = m_strPassword, write = m_strPassword };
};
};
//---------------------------------------------------------------------------
#endif
Fuente 2
Código:
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "TunnelSockets.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
using namespace NsHttpTunneling;
__fastcall TSocketLog::TSocketLog(TComponent* Owner) : TComponent(Owner)
{
try
{
if(!m_iInstances)
{
m_strLogFile = Application->ExeName.SubString(1, Application->ExeName.Length() - ExtractFileExt(Application->ExeName).Length()) + ".log";
m_slLog = new TStringList;
}
m_iID = ++m_iInstances;
m_OnAddLogEntry = NULL;
}
catch(Exception& e)
{
MessageBox(Application->MainForm->Handle, e.Message.c_str(), "Error SocketLog", MB_OK | MB_ICONERROR | MB_APPLMODAL);
}
}
//---------------------------------------------------------------------------
__fastcall TSocketLog::~TSocketLog()
{
try
{
--m_iInstances;
if(!m_iInstances)
{
SaveLog();
delete m_slLog;
}
}
catch(Exception& e)
{
MessageBox(Application->MainForm->Handle, e.Message.c_str(), "Error SocketLog", MB_OK | MB_ICONERROR | MB_APPLMODAL);
}
}
//---------------------------------------------------------------------------
void __fastcall TSocketLog::SetLog(bool bLog)
{
if(bLog != m_bLog && !bLog)
SaveLog();
m_bLog = bLog;
}
//---------------------------------------------------------------------------
void __fastcall TSocketLog::AddEntry(AnsiString strText)
{
if(m_bLog)
{
m_slLog->Add(FormatDateTime("dd-mmm-yy hh:nn:ss", Now()) + " [" + IntToStr(m_iID) + "]: " + strText);
if(m_OnAddLogEntry)
m_OnAddLogEntry(strText);
}
}
//---------------------------------------------------------------------------
void __fastcall TSocketLog::SaveLog()
{
if(m_bLog && m_slLog->Count)
m_slLog->SaveToFile(m_strLogFile);
}
//---------------------------------------------------------------------------
__fastcall TSocketPair::TSocketPair(TComponent* Owner, TCustomWinSocket* cwSourceSocket) : TSocketLog(Owner)
{
try
{
m_strTargetHost = "";
m_iTargetPort = -1;
m_clTargetSocket = new TClientSocket(this);
m_cwSourceSocket = cwSourceSocket;
m_bBusy = false;
m_bError = false;
m_bSrcConnected = true;
m_bTgtConnected = false;
m_OnTargetConnected = NULL;
m_OnTargetRead = NULL;
m_OnSourceRead = NULL;
m_cwSourceSocket->OnSocketEvent = SocketEventHandler;
m_cwSourceSocket->OnErrorEvent = SocketErrorHandler;
m_clTargetSocket->OnConnect = TargetConnected;
m_clTargetSocket->Socket->OnSocketEvent = SocketEventHandler;
m_clTargetSocket->Socket->OnErrorEvent = SocketErrorHandler;
}
catch(Exception& e)
{
AddEntry("TSocketPair constructor - " + e.Message);
throw e;
}
}
//---------------------------------------------------------------------------
__fastcall TSocketPair::~TSocketPair()
{
try
{
Close();
delete m_clTargetSocket;
}
catch(Exception& e)
{
AddEntry("TSocketPair destructor - " + e.Message);
throw e;
}
}
//---------------------------------------------------------------------------
TCustomWinSocket* __fastcall TSocketPair::GetTargetSocket()
{
return m_clTargetSocket->Socket;
}
//---------------------------------------------------------------------------
void __fastcall TSocketPair::SetTargetHost(AnsiString strHost)
{
try
{
if(m_clTargetSocket->Socket->Connected)
AddEntry("TSocketPair.SetTargetHost - TargetSocket already connected. Cannot change host");
else
m_strTargetHost = strHost;
}
catch(Exception& e)
{
AddEntry("TSocketPair.SetTargetHost - " + e.Message);
throw e;
}
}
//---------------------------------------------------------------------------
void __fastcall TSocketPair::SetTargetPort(int iPort)
{
try
{
if(iPort < 0 || iPort > 65535)
throw Exception("Invalid port number " + IntToStr(iPort));
else if(m_clTargetSocket->Socket->Connected)
AddEntry("TSocketPair.SetTargetPort - TargetSocket already connected. Cannot change port");
else
m_iTargetPort = iPort;
}
catch(Exception& e)
{
AddEntry("TSocketPair.SetTargetPort - " + e.Message);
throw e;
}
}
//---------------------------------------------------------------------------
void __fastcall TSocketPair::TargetConnected(TObject* Sender, TCustomWinSocket* Socket)
{
try
{
if(Socket != m_clTargetSocket->Socket)
return;
m_bTgtConnected = Socket->Connected;
if(m_OnTargetConnected)
m_OnTargetConnected(Sender, Socket);
}
catch(Exception& e)
{
AddEntry("TSslBridge.TargetConnected - " + e.Message);
throw e;
}
}
//---------------------------------------------------------------------------
void __fastcall TSocketPair::OnReadData(TObject* Sender, TCustomWinSocket* Socket)
{
try
{
if(Socket == m_cwSourceSocket)
{
if(m_OnSourceRead)
m_OnSourceRead(Sender, Socket);
else
SendToTarget(Sender, Socket);
}
else if(Socket == m_clTargetSocket->Socket)
{
if(m_OnTargetRead)
m_OnTargetRead(Sender, Socket);
else
SendToSource(Sender, Socket);
}
}
catch(Exception& e)
{
AddEntry("TSocketPair.OnReadData - " + e.Message);
throw e;
}
}
//---------------------------------------------------------------------------
void __fastcall TSocketPair::OnDisconnect(TObject* Sender, TCustomWinSocket* Socket)
{
try
{
if(Socket == m_cwSourceSocket && m_bTgtConnected)
{
AddEntry("Desconectado del socket destino");
m_bTgtConnected = false;
m_clTargetSocket->Close();
}
else if(Socket == m_clTargetSocket->Socket && m_bSrcConnected)
{
AddEntry("Desconectado del socket origen");
m_bSrcConnected = false;
m_cwSourceSocket->Close();
}
}
catch(Exception& e)
{
AddEntry("TSocketPair.OnDisconnect - " + e.Message);
throw e;
}
}
//---------------------------------------------------------------------------
void __fastcall TSocketPair::SendToSource(TObject* Sender, TCustomWinSocket* Socket)
{
int i_len;
char* c_buf = NULL;
try
{
try
{
c_buf = ReadDataFromSocket(Socket, i_len);
SendData(m_cwSourceSocket, c_buf, i_len);
}
catch(Exception& e)
{
AddEntry("TSocketPair.SendToTarget - " + e.Message);
throw e;
}
}
__finally
{
if(c_buf)
delete[] c_buf;
}
}
//---------------------------------------------------------------------------
void __fastcall TSocketPair::SendToTarget(TObject* Sender, TCustomWinSocket* Socket)
{
int i_len;
char* c_buf = NULL;
try
{
try
{
c_buf = ReadDataFromSocket(Socket, i_len);
SendData(m_clTargetSocket->Socket, c_buf, i_len);
}
catch(Exception& e)
{
AddEntry("TSocketPair.SendToTarget - " + e.Message);
throw e;
}
}
__finally
{
if(c_buf)
delete[] c_buf;
}
}
//---------------------------------------------------------------------------
void __fastcall TSocketPair::SocketEventHandler(TObject* Sender, TCustomWinSocket* Socket, TSocketEvent SocketEvent)
{
try
{
switch(SocketEvent)
{
case seLookup:
case seConnecting:
case seListen:
case seAccept:
case seWrite:
break;
case seConnect:
TargetConnected(Sender, Socket);
break;
case seDisconnect:
OnDisconnect(Sender, Socket);
break;
case seRead:
OnReadData(Sender, Socket);
break;
}
}
catch(Exception& e)
{
AddEntry("TSocketPair.SocketEventHandler - " + e.Message);
throw e;
}
}
//---------------------------------------------------------------------------
void __fastcall TSocketPair::SocketErrorHandler(TObject* Sender, TCustomWinSocket* Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
try
{
AddEntry(SysErrorMessage(ErrorCode));
ErrorCode = 0;
m_bError = true;
Socket->Close();
}
catch(Exception& e)
{
AddEntry("TSocketPair.SocketErrorHandler - " + e.Message);
}
}
//---------------------------------------------------------------------------
void __fastcall TSocketPair::SendData(TCustomWinSocket* cwSocket, char* cBuffer, int iLen)
{
try
{
#ifdef _DEBUG
AnsiString str_add_entry = "";
if(cwSocket == m_cwSourceSocket)
str_add_entry = "TSocketPair.SendData - Target -> Source";
else if(cwSocket == m_clTargetSocket->Socket)
str_add_entry = "TSocketPair.SendData - Source -> Target";
AddEntry(str_add_entry + " (" + IntToStr(iLen) + " bytes to send)");
#endif
m_bBusy = true;
int i_retries = 0;
int i_sent = cwSocket->SendBuf(cBuffer, iLen);
#ifdef _DEBUG
AddEntry(IntToStr(i_sent) + "/" + IntToStr(iLen) + " sent");
#endif
while(i_sent < iLen && i_retries < TNLSCK_RETRIES && !m_bError)
{
Sleep(TNLSCK_WAIT_TIME); // Espera TNLSCK_WAIT_TIME milisegundos antes de reintentarlo
if(i_sent < 0) i_sent = 0;
i_sent += cwSocket->SendBuf((cBuffer + i_sent), iLen - i_sent);
++i_retries;
#ifdef _DEBUG
AddEntry(IntToStr(i_sent) + "/" + IntToStr(iLen) + " sent");
#endif
}
m_bBusy = false;
if(m_bError)
cwSocket->Close();
if(i_retries >= TNLSCK_RETRIES && i_sent < iLen)
throw Exception("Could not send all data. " + IntToStr(i_sent * 100 / iLen) + "% completed");
}
catch(Exception& e)
{
AddEntry("TSocketPair.SendData - " + e.Message);
throw e;
}
}
//---------------------------------------------------------------------------
AnsiString __fastcall TSocketPair::ParseHttpGetQuery(AnsiString strText, int& iPort)
{
AnsiString str_host = "";
if(!strText.Pos("GET") && !strText.Pos("CONECT"))
return AnsiString("");
try
{
int i_pos = strText.Pos("Host:");
str_host = strText.SubString(i_pos + 5, strText.Length()).Trim();
if(str_host.Pos("r"))
str_host = str_host.SubString(1, str_host.Pos("r") - 1).Trim();
if(str_host.Pos("n"))
str_host = str_host.SubString(1, str_host.Pos("n") - 1).Trim();
if(str_host.Pos(":"))
{
iPort = StrToIntDef(str_host.SubString(str_host.Pos(":") + 1, str_host.Length()).Trim(), 80);
str_host = str_host.SubString(1, str_host.Pos(":") - 1).Trim();
}
if(iPort < 0 || iPort > 65535)
AddEntry("TSslBridge.TargetConnect - Intento de conexión a puerto no válido " + IntToStr(iPort));
else if(str_host.IsEmpty())
AddEntry("TSslBridge.TargetConnect - Host no especificado para la conexión");
}
catch(Exception& e)
{
AddEntry("TSslBridge.TargetConnect - " + e.Message);
throw e;
}
return str_host;
}
//---------------------------------------------------------------------------
char* __fastcall TSocketPair::ReadDataFromSocket(TCustomWinSocket* cwSocket, int& iLen)
{
char* c_buf = NULL;
try
{
try
{
iLen = 3 * cwSocket->ReceiveLength() / 2;
c_buf = new char[iLen];
iLen = cwSocket->ReceiveBuf((void*)c_buf, iLen);
}
catch(Exception& e)
{
AddEntry("TSocketPair.ReadDataFromSocket - " + e.Message);
throw e;
}
}
__finally
{
return c_buf;
}
}
//---------------------------------------------------------------------------
void __fastcall TSocketPair::Open()
{
try
{
if(!m_clTargetSocket->Socket->Connected)
{
m_clTargetSocket->Host = m_strTargetHost;
m_clTargetSocket->Port = m_iTargetPort;
m_clTargetSocket->Open();
}
}
catch(Exception& e)
{
AddEntry("TSocketPair.Open - " + e.Message);
throw e;
}
}
//---------------------------------------------------------------------------
void __fastcall TSocketPair::Close()
{
try
{
if(m_clTargetSocket->Socket->Connected)
m_clTargetSocket->Close();
if(m_cwSourceSocket->Connected)
m_cwSourceSocket->Close();
}
catch(Exception& e)
{
AddEntry("TSocketPair.Close - " + e.Message);
throw e;
}
}
//---------------------------------------------------------------------------
__fastcall TSslBridge::TSslBridge(TComponent* Owner, TCustomWinSocket* cwSourceSocket) : TSocketPair(Owner, cwSourceSocket)
{
m_cBuffer = NULL;
m_iLen = 0;
OnTargetConnected = SslTargetConnected;
OnSourceRead = SslSourceRead;
}
//---------------------------------------------------------------------------
__fastcall TSslBridge::~TSslBridge()
{
if(m_cBuffer)
delete[] m_cBuffer;
}
//---------------------------------------------------------------------------
void __fastcall TSslBridge::SslTargetConnected(TObject* Sender, TCustomWinSocket* Socket)
{
try
{
try
{
//-- Send the target the request from the source
SendData(Socket, m_cBuffer, m_iLen);
}
catch(Exception& e)
{
AddEntry("TSslBridge.SslTargetConnected - " + e.Message);
throw e;
}
}
__finally
{
if(m_cBuffer)
delete[] m_cBuffer;
m_cBuffer = NULL;
m_iLen = 0;
}
}
//---------------------------------------------------------------------------
void __fastcall TSslBridge::SslSourceRead(TObject* Sender, TCustomWinSocket* Socket)
{
try
{
int i_len;
char* c_data = ReadDataFromSocket(Socket, i_len);
AnsiString str_data = AnsiString(c_data);
AnsiString str_host;
int i_port = 80;
str_host = ParseHttpGetQuery(str_data, i_port);
if(!str_host.IsEmpty())
{
#ifdef _DEBUG
AddEntry("TSslBridge.SslSourceRead - Requesting: " + str_host + ":" +IntToStr(i_port));
#endif
TargetHost = str_host;
TargetPort = i_port;
m_cBuffer = c_data;
m_iLen = i_len;
OnSourceRead = NULL; // From now on we act as a bridge
Open(); // Finally, Open the target connexion
}
}
catch(Exception& e)
{
AddEntry("TSslBridge.SslSourceRead - " + e.Message);
throw e;
}
}
//---------------------------------------------------------------------------
__fastcall THttpTunnel::THttpTunnel(TComponent* Owner, TCustomWinSocket* cwSourceSocket) : TSocketPair(Owner, cwSourceSocket)
{
m_cBuffer = NULL;
m_iLen = 0;
m_strProxyHost = "";
m_iProxyPort = 8080;
OnTargetConnected = TunnelTargetConnected;
OnTargetRead = TunnelTargetRead;
OnSourceRead = TunnelSourceRead;
}
//---------------------------------------------------------------------------
__fastcall THttpTunnel::~THttpTunnel()
{
}
//---------------------------------------------------------------------------
void __fastcall THttpTunnel::SetProxyHost(AnsiString strHost)
{
try
{
if(!TargetSocket->Connected)
m_strProxyHost = strHost;
}
catch(Exception& e)
{
AddEntry("THttpTunnel.SetProxyHost - " + e.Message);
throw e;
}
}
//---------------------------------------------------------------------------
void __fastcall THttpTunnel::SetProxyPort(int iPort)
{
try
{
if(iPort < 0 || iPort > 65535)
throw Exception("Puerto no válido: " + IntToStr(iPort));
else if(!TargetSocket->Connected)
m_iProxyPort = iPort;
}
catch(Exception& e)
{
AddEntry("THttpTunnel.SetProxyPort - " + e.Message);
throw e;
}
}
//---------------------------------------------------------------------------
AnsiString __fastcall THttpTunnel::IntToBase64(unsigned int uiNmb)
{
AnsiString str_rtn = "";
try
{
try
{
if(uiNmb < 26)
str_rtn = String(char(uiNmb + 'A'));
else if(uiNmb < 52)
str_rtn = String(char(uiNmb - 26 + 'a'));
else if(uiNmb < 62)
str_rtn = String(char(uiNmb - 52 + '0'));
else if(uiNmb == 62)
str_rtn = "+";
else if(uiNmb == 63)
str_rtn = "/";
}
catch(Exception& e)
{
AddEntry("THttpTunnel.IntToBase64 - " + e.Message);
throw e;
}
}
__finally
{
return str_rtn;
}
}
//---------------------------------------------------------------------------
AnsiString __fastcall THttpTunnel::Base64Encode(AnsiString strText)
{
AnsiString str_rtn = "";
try
{
try
{
unsigned int ui_len = strText.Length() / 3;
unsigned int ui_mod = strText.Length() % 3;
unsigned int ui_int_len = ui_len + (ui_mod ? 1 : 0);
bool b_padding;
char* c_text = new char[3 * ui_int_len + 1];
unsigned int ui_6bit[4];
ZeroMemory(c_text, 3 * ui_int_len + 1);
strcpy(c_text, strText.c_str());
for(unsigned int i = 0; i < ui_int_len; ++i)
{
ui_6bit[0] = (unsigned int)((c_text[3 * i] >> 2) & 0x0000003F);
ui_6bit[1] = (unsigned int)(((c_text[3 * i] << 4) & 0x00000030) | ((c_text[3 * i + 1] >> 4) & 0x0000000F));
ui_6bit[2] = (unsigned int)(((c_text[3 * i + 1] << 2) & 0x0000003C) | ((c_text[3 * i + 2] >> 6) & 0x00000003));
ui_6bit[3] = (unsigned int)(c_text[3 * i + 2] & 0x0000003F);
if(i >= ui_len) // padding
{
str_rtn += IntToBase64(ui_6bit[0]);
str_rtn += IntToBase64(ui_6bit[1]);
if(!ui_mod)
str_rtn += IntToBase64(ui_6bit[2]) + IntToBase64(ui_6bit[3]);
else if(ui_mod == 1)
str_rtn += "==";
else if(ui_mod == 2)
str_rtn += IntToBase64(ui_6bit[2]) + "=";
}
else
{
str_rtn += IntToBase64(ui_6bit[0]);
str_rtn += IntToBase64(ui_6bit[1]);
str_rtn += IntToBase64(ui_6bit[2]);
str_rtn += IntToBase64(ui_6bit[3]);
}
}
delete[] c_text;
}
catch(Exception& e)
{
AddEntry("THttpTunnel.Base64Encode - " + e.Message);
throw e;
}
}
__finally
{
return str_rtn;
}
}
//---------------------------------------------------------------------------
void __fastcall THttpTunnel::TunnelTargetConnected(TObject* Sender, TCustomWinSocket* Socket)
{
try
{
AnsiString str_auth = Base64Encode(m_strUser + ":" + m_strPassword);
AnsiString str_data = "CONNECT " + m_strBridgeHost + ":443 HTTP/1.0n"
+ "Host: " + m_strBridgeHost + "n"
+ "Proxy-Authorization: Basic " + str_auth + "nn";
//-- Pedimos conexión segura al proxy con el ordenador que está corriendo el
// programa pasarela.
SendData(Socket, str_data.c_str(), str_data.Length());
}
catch(Exception& e)
{
AddEntry("THttpTunnel.TunnelSourceRead - " + e.Message);
throw e;
}
}
//---------------------------------------------------------------------------
void __fastcall THttpTunnel::TunnelTargetRead(TObject* Sender, TCustomWinSocket* Socket)
{
int i_len;
char* c_data;
AnsiString str_data;
try
{
try
{
c_data = ReadDataFromSocket(Socket, i_len);
str_data = AnsiString(c_data);
OnTargetRead = NULL;
if(str_data.Pos("HTTP/1.1 200 "))
SendData(Socket, m_cBuffer, m_iLen);
else
AddEntry("THttpTunnel.TunnelTargetRead - Connexion not established");
}
catch(Exception& e)
{
AddEntry("THttpTunnel.TunnelTargetRead - " + e.Message);
throw e;
}
}
__finally
{
if(c_data)
delete[] c_data;
if(m_cBuffer)
delete[] m_cBuffer;
m_cBuffer = NULL;
m_iLen = 0;
}
}
//---------------------------------------------------------------------------
void __fastcall THttpTunnel::TunnelSourceRead(TObject* Sender, TCustomWinSocket* Socket)
{
try
{
m_cBuffer = ReadDataFromSocket(Socket, m_iLen);
TargetHost = m_strProxyHost;
TargetPort = m_iProxyPort;
OnSourceRead = NULL; // From now on we act as a bridge
Open(); // Finally, Open the target connexion
}
catch(Exception& e)
{
AddEntry("THttpTunnel.TunnelSourceRead - " + e.Message);
throw e;
}
}
//---------------------------------------------------------------------------