mainform.cpp
来自「著名的SecureBlackBox控件完整源码」· C++ 代码 · 共 298 行
CPP
298 行
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#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 TElSSHClient(this);
Client->OnSend = HandleClientSend;
Client->OnReceive = HandleClientReceive;
Client->OnOpenConnection = HandleClientOpenConnection;
Client->OnCloseConnection = HandleClientCloseConnection;
Client->OnDebugData = HandleClientDebugData;
Client->OnError = HandleClientError;
Client->OnAuthenticationSuccess = HandleClientAuthenticationSuccess;
Client->OnAuthenticationFailed = HandleClientAuthenticationFailed;
Client->OnKeyValidate = HandleKeyValidate;
Tunnel = new TElShellSSHTunnel(this);
Tunnel->OnOpen = HandleTunnelOpen;
Tunnel->OnClose = HandleTunnelClose;
Tunnel->OnError = HandleTunnelError;
TunnelList = new TElSSHTunnelList(this);
Tunnel->TunnelList = TunnelList;
Client->TunnelList = TunnelList;
KeyStorage = new TElSSHMemoryKeyStorage(this);
Client->KeyStorage = KeyStorage;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
delete Client;
delete TunnelList;
delete Tunnel;
delete KeyStorage;
}
//---------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
// Handlers
void __fastcall TForm1::HandleClientSend(TObject* Sender, void* Buffer, int Size)
{
ClientSocket1->Socket->SendBuf(Buffer, Size);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleClientReceive(TObject* Sender, void* Buffer, int MaxSize, int &Written)
{
Written = ClientSocket1->Socket->ReceiveBuf(Buffer, MaxSize);
if (Written < 0)
Written = 0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleClientOpenConnection(TObject* Sender)
{
ClientState = CLIENT_STATE_CONNECTED;
Memo2->Lines->Add("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));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleClientCloseConnection(TObject* Sender)
{
ClientState = CLIENT_STATE_DISCONNECTED;
Memo2->Lines->Add((AnsiString)"Connection closed. " + Client->ServerCloseReason);
ClientSocket1->Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleClientDebugData(TObject* Sender, void* Buffer, int Size)
{
char* S = new char[Size+1];
memset(S,0,Size+1);
memcpy(S,Buffer,Size);
Memo2->Lines->Add((AnsiString)"Debug: " + S);
delete[] S;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleClientError(TObject* Sender, int Error)
{
ClientState = CLIENT_STATE_ERROR;
Memo2->Lines->Add((AnsiString)"Error: " + IntToStr(Error));
}
//---------------------------------------------------------------------------
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::HandleTunnelOpen(TObject* Sender, TElSSHTunnelConnection* TunnelConnection)
{
Connection = TunnelConnection;
Connection->OnData = HandleConnectionData;
Connection->OnError = HandleConnectionError;
Connection->OnClose = HandleConnectionClose;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleTunnelClose(TObject* Sender, TElSSHTunnelConnection* TunnelConnection)
{
//
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleTunnelError(TObject* Sender, int Error, void* Data)
{
Memo2->Lines->Add((AnsiString)"Tunnel error: " + IntToStr(Error));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleConnectionData(TObject* Sender, void* Buffer, int Size)
{
char* S = new char[Size+1];
memset(S,0,Size+1);
memcpy(S,Buffer, Size);
Memo1->Lines->Text = Memo1->Lines->Text + S;
delete[] S;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleConnectionError(TObject* Sender, int Error)
{
Memo2->Lines->Add((AnsiString)"Connection error: " + IntToStr(Error));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleConnectionClose(TObject* Sender, TSSHCloseType CloseType)
{
Memo2->Lines->Add("Shell connection closed");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (!Connected)
{
Connection = NULL;
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();
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;
ClientSocket1->Host = Edit1->Text;
ClientSocket1->Port = StrToInt(Edit2->Text);
ClientSocket1->Open();
ClientState = CLIENT_STATE_CONNECTING;
Client->Open();
while (ClientState == CLIENT_STATE_CONNECTING)
Client->DataAvailable();
if (ClientState != CLIENT_STATE_CONNECTED)
{
ShowMessage("Failed to establish SSH connection");
return;
}
Button1->Caption = "Disconnect";
Connected = true;
}
else
{
ClientState = CLIENT_STATE_NOT_CONNECTED;
if (Connection!=NULL)
Client->Close(true);
Button1->Caption = "Connect";
Connected = false;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientSocket1Connect(TObject *Sender,
TCustomWinSocket *Socket)
{
Memo2->Lines->Add("Client socket connected");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientSocket1Read(TObject *Sender,
TCustomWinSocket *Socket)
{
//Client.DataAvailable;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Connection->SendText(Edit5->Text + "\n");
Edit5->Text = "";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BrowseBtnClick(TObject *Sender)
{
if (OpenDialog1->Execute())
Edit6->Text = OpenDialog1->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SetConnected(bool Value)
{
FConnected = Value;
Timer1->Enabled = Value;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
TFDSet FDSet;
TTimeVal TimeVal;
PTimeVal PTV;
if (ClientState == CLIENT_STATE_CONNECTED)
{
FD_ZERO(&FDSet);
FD_SET(ClientSocket1->Socket->SocketHandle, &FDSet);
TimeVal.tv_sec = 0;
TimeVal.tv_usec = 0;
PTV = &TimeVal;
switch(select(ClientSocket1->Socket->SocketHandle + 1, &FDSet, NULL, NULL, PTV))
{
case -1:
ClientState = CLIENT_STATE_NOT_CONNECTED;
ClientSocket1->Close();
Connected = false;
break;
case 0:
break;
case 1:
Client->DataAvailable();
break;
}
}
}
//---------------------------------------------------------------------------
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 + -
显示快捷键?