mainform.cpp

来自「著名的SecureBlackBox控件完整源码」· C++ 代码 · 共 252 行

CPP
252
字号
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "MainForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

  TElSSHClient * Client;
  TElShellSSHTunnel* Tunnel;
  TElSSHTunnelList* TunnelList;
  TElSSHTunnelConnection* Connection;
  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 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 = HandleClientKeyValidate;

  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;

}
//---------------------------------------------------------------------------

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::HandleClientOpenConnection(System::TObject* Sender)
{
  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::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::HandleTunnelOpen(System::TObject* Sender, TElSSHTunnelConnection* TunnelConnection)
{
  Connection = TunnelConnection;
  Connection->OnData = HandleConnectionData;
  Connection->OnError = HandleConnectionError;
  Connection->OnClose = HandleConnectionClose;

}

void __fastcall TForm1::HandleTunnelClose(System::TObject* Sender, TElSSHTunnelConnection* TunnelConnection)
{
    Connection = NULL;
}

void __fastcall TForm1::HandleTunnelError(System::TObject* Sender, int Error, void* Data)
{
  Memo2->Lines->Add(AnsiString::Format("Tunnel error %d", ARRAYOFCONST((Error))));
}

void __fastcall TForm1::HandleConnectionData(System::TObject* Sender, void *Buffer, int Size)
{
  AnsiString S;
  S.SetLength(Size);
  Move(Buffer, &(S[1]), Size);

  Memo1->Lines->Text = Memo1->Lines->Text + S;
}

void __fastcall TForm1::HandleConnectionError(System::TObject* Sender, int Error)
{
  Memo2->Lines->Add(AnsiString::Format("Connection error %d", ARRAYOFCONST((Error))));
}

void __fastcall TForm1::HandleConnectionClose(System::TObject* Sender, TSSHCloseType CloseType)
{
	Memo2->Lines->Add("Connection closed");
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ClientSocket1Connect(TObject *Sender,
      TCustomWinSocket *Socket)
{
  Memo2->Lines->Add("Client socket connected");
  Client->Open();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ClientSocket1Read(TObject *Sender,
      TCustomWinSocket *Socket)
{
  Client->DataAvailable();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  TElSSHKey* Key;

  if (!Connected)
  {
    Connection = NULL;
    Client->Versions.Clear();
    if (CheckBox1->Checked)
      Client->Versions << sbSSH1;
    if (CheckBox2->Checked)
      Client->Versions << sbSSH2;
    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 (Connection)
      Client->Close(true);
    Button1->Caption = "Connect";
    Connected = false;
  }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
  Connection->SendText(Edit5->Text + "\n");
  Edit5->Text = "";

}
//---------------------------------------------------------------------------

void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
  if (OpenDialog1->Execute())
	Edit6->Text = OpenDialog1->FileName;

}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?