mainform.cpp
来自「著名的SecureBlackBox控件完整源码」· C++ 代码 · 共 202 行
CPP
202 行
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#undef SetPort
#include "MainForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__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->OnCloseConnection = HandleClientCloseConnection;
Client->OnError = HandleClientError;
Client->OnAuthenticationSuccess = HandleClientAuthenticationSuccess;
Client->OnAuthenticationFailed = HandleClientAuthenticationFailed;
Client->OnKeyValidate = HandleKeyValidate;
Client->OnAuthenticationKeyboard = HandleClientAuthenticationKeyboard;
KeyStorage = new TElSSHMemoryKeyStorage(this);
Client->KeyStorage = KeyStorage;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
delete Client;
delete KeyStorage;
}
//---------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
// Handlers
void __fastcall TForm1::HandleClientCloseConnection(TObject* Sender)
{
Memo2->Lines->Add((AnsiString)"Connection closed. " + Client->ServerCloseReason);
Timer1->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleClientError(TObject* Sender, int Error)
{
Memo2->Lines->Add((AnsiString)"Error: " + IntToStr(Error));
Timer1->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleClientAuthenticationSuccess(TObject* Sender)
{
Memo2->Lines->Add("Authentication succeeded");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleClientAuthenticationFailed(TObject* Sender, int AuthType)
{
Memo2->Lines->Add((AnsiString)"Authentication attempt failed, AuthType=" + IntToStr(AuthType));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (!Connected)
{
Client->Versions = Sbsshcommon::TSSHVersions();
if (CheckBox1->Checked)
Client->Versions = Client->Versions << sbSSH1;
if (CheckBox2->Checked)
Client->Versions = Client->Versions << sbSSH2;
Client->Username = Edit3->Text;
Client->Password = Edit4->Text;
KeyStorage->Clear();
Client->AuthenticationTypes = SSH_AUTH_TYPE_PASSWORD | SSH_AUTH_TYPE_KEYBOARD;
TElSSHKey* Key = new TElSSHKey;
if ((!Edit6->Text.IsEmpty()) && (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;
Client->Address = Edit1->Text;
Client->Port = StrToInt(Edit2->Text);
Client->Open();
if (Client->Active)
{
Memo2->Lines->Add((AnsiString)"Connection started");
Memo2->Lines->Add((AnsiString)"Server: " + Client->ServerSoftwareName);
switch(Client->Version)
{
case sbSSH1:
Memo2->Lines->Add("Version: SSHv1");
break;
case sbSSH2:
Memo2->Lines->Add("Version: SSHv2");
break;
}
Memo2->Lines->Add((AnsiString)"PublicKey algorithm: " + IntToStr(Client->PublicKeyAlgorithm));
Memo2->Lines->Add((AnsiString)"Kex algorithm: " + IntToStr(Client->KexAlgorithm));
Memo2->Lines->Add((AnsiString)"Block algorithm: " + IntToStr(Client->EncryptionAlgorithmServerToClient));
Memo2->Lines->Add((AnsiString)"Compression algorithm: " + IntToStr(Client->CompressionAlgorithmServerToClient));
Memo2->Lines->Add((AnsiString)"MAC algorithm: " + IntToStr(Client->MacAlgorithmServerToClient));
}
Button1->Caption = "Disconnect";
Timer1->Enabled = true;
}
else
{
Client->Close();
Button1->Caption = "Connect";
}
Connected = !Connected;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
AnsiString s = Edit5->Text + "\n";
Client->SendData(s.c_str(), s.Length());
Edit5->Text = "";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleClientAuthenticationKeyboard(TObject* Sender, TStringList* Prompts, TBits* Echo, TStringList* Responses)
{
Responses->Clear();
for (int i = 0; i<Prompts->Count; i++)
{
AnsiString S;
if (Prompt(Prompts->Strings[i], Echo->Bits[i], S))
Responses->Add(S);
else
Responses->Add("");
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
if (OpenDialog1->Execute())
Edit6->Text = OpenDialog1->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
if (Client->Active)
{
int BufSize = 1024;
int StdErrSize = 1024;
char* S = new char[1024];
char* ES = new char[1024];
memset(S,0,1024);
memset(ES,0,1024);
Client->ReceiveData(S, BufSize, ES, StdErrSize);
if (BufSize > 0)
Memo1->Lines->Text = Memo1->Lines->Text + S;
if (StdErrSize > 0)
Memo2->Text = Memo2->Text + ES;
delete[] S;
delete[] ES;
}
else
Timer1->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleKeyValidate(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";
AnsiString s;
s.sprintf("Server key received (%s). Fingerprint is %s",AlgLine, BeautifyBinaryString(DigestToStr(ServerKey->FingerprintMD5, false), ':'));
Memo2->Lines->Add(s);
Validate = true;
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?