mainform.cpp
来自「著名的SecureBlackBox控件完整源码」· C++ 代码 · 共 424 行 · 第 1/2 页
CPP
424 行
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ElRemotePortForwardSSHTunnelOpen(
TObject *Sender, TElSSHTunnelConnection *TunnelConnection)
{
Log((AnsiString)"New logical tunnel connection [" + IntToHex(int(TunnelConnection), 8) + "] is established");
Log("Opening socket connection to destination host");
TunnelConnection->OnData = TunnelConnectionData;
TunnelConnection->OnClose = TunnelConnectionClose;
TunnelConnection->OnError = TunnelConnectionError;
ConnList->Add(TunnelConnection);
BufferList->Add("");
PForwardStatus P = new TForwardStatus;
P->Sent = 0;
P->Received = 0;
P->SocketState = STATE_ESTABLISHING_FORWARDING;
StatusList->Add(P);
TClientSocket* sck = new TClientSocket(NULL);
SocketList->Add(sck);
sck->OnRead = SocketRead;
sck->OnConnect = SocketConnect;
sck->OnDisconnect = SocketDisconnect;
sck->OnError = SocketError;
sck->Host = EditDestHost->Text;
sck->Port = StrToInt(EditDestPort->Text);
sck->Open();
RefreshListView();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::TunnelConnectionData(TObject* Sender, void* Buffer, int Size)
{
Log(IntToStr(Size) + " bytes received from logical connection [" + IntToHex(int(Sender), 8) + "]");
int I = ConnList->IndexOf(Sender);
if (I < 0)
return;
if (((TClientSocket*)(SocketList->Items[I]))->Active)
{
((TClientSocket*)(SocketList->Items[I]))->Socket->SendBuf(Buffer, Size);
PForwardStatus(StatusList->Items[I])->Sent += Size;
}
else
{
char* S = new char[Size+1];
Move(Buffer, S, Size);
S[Size]=0;
BufferList->Strings[I] = BufferList->Strings[I] + S;
delete[] S;
}
RefreshListView();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::TunnelConnectionError(TObject* Sender, int ErrorCode)
{
// connection error should be handled here
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::TunnelConnectionClose(TObject* Sender, TSSHCloseType CloseType)
{
Log((AnsiString)"Tunnel connection [" + IntToHex(int(Sender), 8) + "] was closed");
int I = ConnList->IndexOf(Sender);
if (I >= 0)
PForwardStatus(StatusList->Items[I])->SocketState = STATE_DISCONNECTED;
RemoveConnectionFromList((TElSSHTunnelConnection*)(Sender));
RefreshListView();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::SocketConnect(TObject* Sender, TCustomWinSocket* Socket)
{
int I = SocketList->IndexOf(Sender);
if (I < 0)
{
Log("Unexpected socket object", true);
return;
}
PForwardStatus P = PForwardStatus(StatusList->Items[I]);
int ToSend = BufferList->Strings[I].Length();
P->SocketState = STATE_ACTIVE;
if (ToSend > 0)
{
Socket->SendBuf(BufferList->Strings[I].c_str(), ToSend);
P->Sent += ToSend;
BufferList->Strings[I] = "";
}
RefreshListView();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::SocketRead(TObject* Sender, TCustomWinSocket* Socket)
{
int I = SocketList->IndexOf(Sender);
if (I < 0)
{
Log("Warning: Received data from unexpected socket object", true);
return;
}
char* Buf = new char[16384];
int Size = Socket->ReceiveBuf(Buf, 16384);
if (Size > 0)
{
((TElSSHTunnelConnection*)(ConnList->Items[I]))->SendData(Buf, Size);
PForwardStatus(StatusList->Items[I])->Received += Size;
}
delete[] Buf;
RefreshListView();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::SocketDisconnect(TObject* Sender, TCustomWinSocket* Socket)
{
int I = SocketList->IndexOf(Sender);
if (I < 0)
{
Log("Warning: Received disconnect from unexpected socket object", true);
return;
}
char* Buf = new char[16384];
int Size = Socket->ReceiveBuf(Buf, 16384);
if (Size > 0)
{
((TElSSHTunnelConnection*)(ConnList->Items[I]))->SendData(Buf, Size);
PForwardStatus(StatusList->Items[I])->Received += Size;
}
delete[] Buf;
PForwardStatus(StatusList->Items[I])->SocketState = STATE_DISCONNECTED;
RemoveConnectionFromList((TClientSocket*)(Sender));
RefreshListView();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::SocketError(TObject* Sender, TCustomWinSocket* Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
//
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ButtonStartClick(TObject *Sender)
{
if (!ConnActive)
{
Log("Opening listening socket");
if (ClientSocket->Active)
ClientSocket->Close();
ClientSocket->Host = EditHost->Text;
ClientSocket->Port = StrToInt(EditPort->Text);
ElSSHClient->UserName = EditUsername->Text;
ElSSHClient->Password = EditPassword->Text;
ElRemotePortForwardSSHTunnel->Port = StrToInt(EditListeningPort->Text);
ElRemotePortForwardSSHTunnel->ToHost = EditDestHost->Text;
ElRemotePortForwardSSHTunnel->ToPort = StrToInt(EditDestPort->Text);
ListView->Items->Clear();
Log((AnsiString)"Connecting to host " + EditHost->Text);
ClientSocket->Open();
ButtonStart->Caption = "Stop";
ConnActive = true;
}
else
{
if (ElSSHClient->Active)
ElSSHClient->Close(true);
ButtonStart->Caption = "Start";
ConnActive = false;
}
}
//---------------------------------------------------------------------------
void TFormMain::RefreshListView(void)
{
ListViewConnections->Items->BeginUpdate();
ListViewConnections->Items->Clear();
for (int I = 0; I<StatusList->Count; I++)
{
PForwardStatus P = PForwardStatus(StatusList->Items[I]);
TListItem* Item = ListViewConnections->Items->Add();
Item->Caption = "";
Item->SubItems->Add(((TClientSocket*)(SocketList->Items[I]))->Socket->LocalHost + ":" +
IntToStr(((TClientSocket*)(SocketList->Items[I]))->Socket->LocalPort));
Item->SubItems->Add(IntToStr(P->Sent));
Item->SubItems->Add(IntToStr(P->Received));
AnsiString S;
switch (P->SocketState)
{
case STATE_NOT_CONNECTED:
Item->ImageIndex = 2;
S = "Not connected";
break;
case STATE_ESTABLISHING_FORWARDING:
Item->ImageIndex = 1;
S = "Establishing";
break;
case STATE_ACTIVE:
Item->ImageIndex = 0;
S = "Active";
break;
case STATE_DISCONNECTED:
Item->ImageIndex = 2;
S = "Disconnected";
break;
}
Item->SubItems->Add(S);
}
ListViewConnections->Items->EndUpdate();
}
//---------------------------------------------------------------------------
void TFormMain::ClearConnections(void)
{
while (SocketList->Count > 0)
RemoveConnectionFromList((TClientSocket*)(SocketList->Items[0]));
ConnList->Clear();
SocketList->Clear();
BufferList->Clear();
for (int I = 0; I<StatusList->Count; I++)
delete PForwardStatus(StatusList->Items[I]);
StatusList->Clear();
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?