mainform.cpp
来自「著名的SecureBlackBox控件完整源码」· C++ 代码 · 共 214 行
CPP
214 行
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "MainForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TElSimpleSSHClient * Client;
bool Connected = false;
TElSSHMemoryKeyStorage *KeyStorage;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
SetLicenseKey((AnsiString)
"ADDCD14AD06709806817E0B3D7BFD0A2222D536FE156466C5D5FE65DB5DEAE76" +
"FFDEBC07E915A5751C12C01C783958872A38E4A5EDA140E7247E0F2E56442A3C" +
"F3E9347AD8FDE52083A0DFC86BC00ECB0FD0CF1B51159A2BCB84F6EA6349EF47" +
"5C15A59AFCC55F7C3AAD26C279628B5D91B1DC94BD2385354A70CCA3B76101D9" +
"F41C84A639FC3CCE4BA8F0CC4A66DCD150114A3F58C1AD46B7B94643741BC20A" +
"8DCA83AB921480951B423CAA19EF1863A47CA2C3422E7E5634BED98939A5AE43" +
"DE1E4BAD79E66D8A5C973B3455656C8C9B6FF024FADD6CDA02D0F506D98493C8" +
"BD1ED7B237DB75FA31F2C82654490CDDDEE24E19939137B9E1DB05508733B22F");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Client = new TElSimpleSSHClient(this);
Client->OnSend = HandleClientSend;
Client->OnReceive = HandleClientReceive;
Client->OnCloseConnection = HandleClientCloseConnection;
Client->OnError = HandleClientError;
Client->OnAuthenticationSuccess = HandleClientAuthenticationSuccess;
Client->OnAuthenticationFailed = HandleClientAuthenticationFailed;
Client->OnKeyValidate = HandleClientKeyValidate;
KeyStorage = new TElSSHMemoryKeyStorage(this);
Client->KeyStorage = KeyStorage;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
delete Client;
delete KeyStorage;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleClientSend(System::TObject* Sender, void * Buffer, int Size)
{
ClientSocket1->Socket->SendBuf(Buffer, Size);
}
void __fastcall TForm1::HandleClientReceive(System::TObject* Sender, void *Buffer, int MaxSize,
int &Written)
{
Written = ClientSocket1->Socket->ReceiveBuf(Buffer, MaxSize);
if (Written < 0)
Written = 0;
}
void __fastcall TForm1::HandleClientCloseConnection(System::TObject* Sender)
{
Memo2->Lines->Add("Connection closed.");
ClientSocket1->Close();
Button1->Caption = "Connect";
Connected = false;
}
void __fastcall TForm1::HandleClientDebugData(System::TObject* Sender, void *Buffer,
int Size)
{
AnsiString S;
S.SetLength(Size);
Move(Buffer, &(S[1]), Size);
Memo2->Lines->Add("Debug: ");
Memo2->Lines->Add(S);
}
void __fastcall TForm1::HandleClientError(System::TObject* Sender, int Error)
{
Memo2->Lines->Add(AnsiString::Format("Error %d", ARRAYOFCONST((Error))));
}
void __fastcall TForm1::HandleClientAuthenticationSuccess(System::TObject* Sender)
{
Memo2->Lines->Add("Authentication succeeded");
}
void __fastcall TForm1::HandleClientAuthenticationFailed(System::TObject* Sender, int AuthType)
{
Memo2->Lines->Add(AnsiString::Format("Authentication failed, AuthType= %d", ARRAYOFCONST((AuthType))));
}
void __fastcall TForm1::HandleClientKeyValidate(System::TObject* Sender, TElSSHKey *ServerKey, bool &Validate)
{
Memo2->Lines->Add("Server key [" + DigestToStr(ServerKey->FingerprintMD5, false) + "] received");
Validate = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientSocket1Connect(TObject *Sender,
TCustomWinSocket *Socket)
{
Memo2->Lines->Add("Client socket connected");
Client->Open();
if (Client->Active)
{
Memo2->Lines->Add("Connection started");
Memo2->Lines->Add("Server: ");
Memo2->Lines->Add(Client->ServerSoftwareName);
switch (Client->Version)
{
case sbSSH1 :
Memo2->Lines->Add("Version: SSHv1");
break;
case sbSSH2 :
Memo2->Lines->Add("Version: SSHv2");
break;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientSocket1Read(TObject *Sender,
TCustomWinSocket *Socket)
{
if (!Client->Active)
return;
char S[1024];
char ES[1024];
int BufSize = 1024;
int StdErrSize = 1024;
Client->ReceiveData(S, BufSize, ES, StdErrSize);
if (BufSize > 0)
Memo1->Lines->Text = Memo1->Lines->Text + AnsiString::LoadStr((void*)S, BufSize);
if (StdErrSize > 0)
Memo2->Lines->Text = Memo2->Lines->Text + AnsiString::LoadStr((void*)ES, StdErrSize);
//Client->DataAvailable();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TElSSHKey* Key;
if (!Connected)
{
Sbsshcommon::TSSHVersions versions;
versions.Clear();
if (CheckBox1->Checked)
versions << sbSSH1;
if (CheckBox2->Checked)
versions << sbSSH2;
Client->Versions = versions;
Client->Username = Edit3->Text;
Client->Password = Edit4->Text;
KeyStorage->Clear();
Key = new TElSSHKey();
if ((Edit6->Text.Length() != 0) && (Key->LoadPrivateKey(Edit6->Text, "") == 0))
{
KeyStorage->Add(Key);
Client->AuthenticationTypes = Client->AuthenticationTypes | SSH_AUTH_TYPE_PUBLICKEY;
}
else
Client->AuthenticationTypes = Client->AuthenticationTypes & (~SSH_AUTH_TYPE_PUBLICKEY);
delete Key;
ClientSocket1->Host = Edit1->Text;
ClientSocket1->Port = atoi((const char *) Edit2->Text.c_str());
ClientSocket1->Open();
Button1->Caption = "Disconnect";
Connected = true;
}
else
{
if (Client->Active)
Client->Close();
Button1->Caption = "Connect";
Connected = false;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
AnsiString S = Edit5->Text + "\n";
Client->SendData((void*) S.c_str(), S.Length());
Edit5->Text = "";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
if (OpenDialog1->Execute())
Edit6->Text = OpenDialog1->FileName;
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?