mainform.cpp
来自「著名的SecureBlackBox控件完整源码」· C++ 代码 · 共 499 行 · 第 1/2 页
CPP
499 行
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "MainForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "SBSSHClient"
#pragma link "SBSSHCommon"
#pragma resource "*.dfm"
TfrmMain *frmMain;
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
: TForm(Owner)
{
SetLicenseKey((AnsiString)
"ADDCD14AD06709806817E0B3D7BFD0A2222D536FE156466C5D5FE65DB5DEAE76" +
"FFDEBC07E915A5751C12C01C783958872A38E4A5EDA140E7247E0F2E56442A3C" +
"F3E9347AD8FDE52083A0DFC86BC00ECB0FD0CF1B51159A2BCB84F6EA6349EF47" +
"5C15A59AFCC55F7C3AAD26C279628B5D91B1DC94BD2385354A70CCA3B76101D9" +
"F41C84A639FC3CCE4BA8F0CC4A66DCD150114A3F58C1AD46B7B94643741BC20A" +
"8DCA83AB921480951B423CAA19EF1863A47CA2C3422E7E5634BED98939A5AE43" +
"DE1E4BAD79E66D8A5C973B3455656C8C9B6FF024FADD6CDA02D0F506D98493C8" +
"BD1ED7B237DB75FA31F2C82654490CDDDEE24E19939137B9E1DB05508733B22F");
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormCreate(TObject *Sender)
{
TunnelInfo = new TTunnelInfoList;
TunnelInfo->Sorted = false;
ConnList = new TList;
SocketList = new TList;
ConnActive = false;
BufferList = new TStringList;
StatusList = new TList;
ElSSHClient->CloseIfNoActiveTunnels = false;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormDestroy(TObject *Sender)
{
delete ConnList;
delete SocketList;
delete BufferList;
delete StatusList;
}
//---------------------------------------------------------------------------
void TfrmMain::Log(const AnsiString S, bool AError)
{
TListItem* Item = lvEvents->Items->Add();
Item->SubItems->Add(DateTimeToStr(TDateTime::CurrentDateTime()));
Item->SubItems->Add(S);
if (AError)
Item->ImageIndex = 0;
else
Item->ImageIndex = 1;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::SSHClientSocketConnect(TObject *Sender,
TCustomWinSocket *Socket)
{
ElSSHClient->Open();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::SSHClientSocketRead(TObject *Sender,
TCustomWinSocket *Socket)
{
ElSSHClient->DataAvailable();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::SSHClientSocketDisconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
Log("Socket connection closed");
if (ElSSHClient->Active)
ElSSHClient->Close(true);
btnStart->Caption = "Start";
ConnActive = false;
ClearConnections();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ElSSHClientAuthenticationFailed(TObject *Sender,
int AuthenticationType)
{
Log((AnsiString)"Authentication failed for type " + IntToStr(AuthenticationType), true);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ElSSHClientAuthenticationSuccess(TObject *Sender)
{
Log("Authentication succeeded");
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ElSSHClientCloseConnection(TObject *Sender)
{
Log("SSH connection closed");
btnStart->Caption = "Start";
ConnActive = false;
ClearConnections();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ElSSHClientDebugData(TObject *Sender,
Pointer Buffer, int Size)
{
char* S = new char[Size+1];
Move(Buffer, S, Size);
S[Size] = 0;
Log((AnsiString)"[Debug data] " + S);
delete[] S;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ElSSHClientError(TObject *Sender, int ErrorCode)
{
Log((AnsiString)"Error " + IntToStr(ErrorCode), true);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ElSSHClientKeyValidate(TObject *Sender,
TElSSHKey *ServerKey, bool &Validate)
{
AnsiString AlgLine;
if (ServerKey->Algorithm == ALGORITHM_RSA)
AlgLine = "RSA";
else if (ServerKey->Algorithm == ALGORITHM_DSS)
AlgLine = "DSS";
else
AlgLine = "unknown";
TMessageDigest128 M128 = ServerKey->FingerprintMD5;
AnsiString S = DigestToStr(M128, false);
Log((AnsiString)"Server key received (" + AlgLine + "). Fingerprint is " + BeautifyBinaryString(S, ':'));
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ElSSHClientOpenConnection(TObject *Sender)
{
Log("SSH connection established");
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ElSSHClientReceive(TObject *Sender,
Pointer Buffer, int MaxSize, int Written)
{
Written = SSHClientSocket->Socket->ReceiveBuf(Buffer, MaxSize);
if (Written < 0)
Written = 0;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ElSSHClientSend(TObject *Sender, Pointer Buffer,
int Size)
{
char* Ptr = (char*)Buffer;
while (Size > 0)
{
int Sent = SSHClientSocket->Socket->SendBuf(Ptr, Size);
if (Sent > 0)
{
Ptr += Sent;
Size -= Sent;
}
else
Sleep(0);
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ElLocalPortForwardSSHTunnelClose(TObject *Sender,
TElSSHTunnelConnection *TunnelConnection)
{
RemoveConnectionFromList(TunnelConnection);
}
//---------------------------------------------------------------------------
void TfrmMain::RemoveConnectionFromList(TElSSHTunnelConnection* Conn)
//procedure TfrmMain.RemoveConnectionFromList(Conn : TElSSHTunnelConnection);
{
int I = ConnList->IndexOf(Conn);
if (I < 0)
return;
ConnList->Remove(Conn);
TServerClientWinSocket* sck = (TServerClientWinSocket*)(SocketList->Items[I]);
SocketList->Delete(I);
BufferList->Delete(I);
PForwardStatus P = (PForwardStatus)(StatusList->Items[I]);
StatusList->Delete(I);
delete[] P; // Dispose(P);
sck->Close();
RefreshListView();
}
//---------------------------------------------------------------------------
void TfrmMain::SetBufferCount(int Value)
{
while (BufferList->Count < Value)
BufferList->Add("");
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ElLocalPortForwardSSHTunnelError(TObject *Sender,
int Error, Pointer Data)
{
Log((AnsiString)"Tunnel error " + IntToStr(Error));
int I = ConnList->Add(NULL);
if (I >= BufferList->Count)
{
Log("Unexpected error: not enough buffers. Adding missing buffers.");
SetBufferCount(I + 1);
}
PForwardStatus(StatusList->Items[I])->SocketState = STATE_DISCONNECTED;
RefreshListView();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ElLocalPortForwardSSHTunnelOpen(TObject *Sender,
TElSSHTunnelConnection *TunnelConnection)
{
Log((AnsiString)"New logical tunnel connection [" + IntToHex(int(TunnelConnection), 8) + "] is established");
TunnelConnection->OnData = TunnelConnectionData;
TunnelConnection->OnClose = TunnelConnectionClose;
TunnelConnection->OnError = TunnelConnectionError;
int I = ConnList->Add(TunnelConnection);
if (I >= BufferList->Count)
{
Log("Unexpected error: not enough buffers. Adding missing buffers.");
SetBufferCount(I + 1);
}
PForwardStatus(StatusList->Items[I])->SocketState = STATE_ACTIVE;
if (!(BufferList->Strings[I].IsEmpty()))
{
TunnelConnection->SendData(BufferList->Strings[I].c_str(), BufferList->Strings[I].Length()+1);
PForwardStatus(StatusList->Items[I])->Sent += BufferList->Strings[I].Length();
BufferList->Strings[I] = "";
}
RefreshListView();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ServerSocketClientConnect(TObject *Sender,
TCustomWinSocket *Socket)
{
PForwardStatus P;
Log((AnsiString)"Client connection accepted, adding socket [" + IntToHex(int(Socket), 8) + "] to queue");
SocketList->Add(Socket);
BufferList->Add("");
P = new TForwardStatus;
P->Sent = 0;
P->Received = 0;
P->SocketState = STATE_ESTABLISHING_FORWARDING;
StatusList->Add(P);
// We pass socket object to Tunnel data to easy determine which socket are working
ElLocalPortForwardSSHTunnel->Open(Socket);
RefreshListView();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ServerSocketClientDisconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
for (int i=0; i<ElLocalPortForwardSSHTunnel->ConnectionCount; i++)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?