mainform.cpp

来自「著名的SecureBlackBox控件完整源码」· C++ 代码 · 共 424 行 · 第 1/2 页

CPP
424
字号
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "MainForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "SBSSHClient"
#pragma link "SBSSHCommon"
#pragma resource "*.dfm"
TFormMain *FormMain;
//---------------------------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent* Owner)
        : TForm(Owner)
{
  SetLicenseKey((AnsiString)
  "ADDCD14AD06709806817E0B3D7BFD0A2222D536FE156466C5D5FE65DB5DEAE76" + 
  "FFDEBC07E915A5751C12C01C783958872A38E4A5EDA140E7247E0F2E56442A3C" + 
  "F3E9347AD8FDE52083A0DFC86BC00ECB0FD0CF1B51159A2BCB84F6EA6349EF47" + 
  "5C15A59AFCC55F7C3AAD26C279628B5D91B1DC94BD2385354A70CCA3B76101D9" + 
  "F41C84A639FC3CCE4BA8F0CC4A66DCD150114A3F58C1AD46B7B94643741BC20A" + 
  "8DCA83AB921480951B423CAA19EF1863A47CA2C3422E7E5634BED98939A5AE43" + 
  "DE1E4BAD79E66D8A5C973B3455656C8C9B6FF024FADD6CDA02D0F506D98493C8" + 
  "BD1ED7B237DB75FA31F2C82654490CDDDEE24E19939137B9E1DB05508733B22F");
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::FormCreate(TObject *Sender)
{
    ConnList = new TList;
    SocketList = new TList;
    ConnActive = false;
    BufferList = new TStringList;
    StatusList = new TList;
    ElSSHClient->CloseIfNoActiveTunnels = false;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::FormDestroy(TObject *Sender)
{
    delete ConnList;
    delete SocketList;
    delete BufferList;
    delete StatusList;
}
//---------------------------------------------------------------------------
void TFormMain::Log(const AnsiString S, bool AError)
{
    TListItem* Item = ListView->Items->Add();
    Item->SubItems->Add(DateTimeToStr(TDateTime::CurrentDateTime()));
    Item->SubItems->Add(S);
    if (AError)
        Item->ImageIndex = 0;
    else
        Item->ImageIndex = 1;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ClientSocketConnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
    ElSSHClient->Open();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ClientSocketRead(TObject *Sender,
      TCustomWinSocket *Socket)
{
    ElSSHClient->DataAvailable();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ClientSocketDisconnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
    Log("Socket connection closed");
    if (ElSSHClient->Active)
        ElSSHClient->Close(true);
    ButtonStart->Caption = "Start";
    ConnActive = false;
    ClearConnections();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ElSSHClientAuthenticationFailed(TObject *Sender,
      int AuthenticationType)
{
    Log((AnsiString)"Authentication failed for type " + IntToStr(AuthenticationType), true);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ElSSHClientAuthenticationSuccess(
      TObject *Sender)
{
    Log("Authentication succeeded");
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ElSSHClientCloseConnection(TObject *Sender)
{
    Log("SSH connection closed");
    ButtonStart->Caption = "Start";
    ConnActive = false;
    if (ClientSocket->Active)
        ClientSocket->Close();
    ClearConnections();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::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 TFormMain::ElSSHClientError(TObject *Sender, int ErrorCode)
{
    Log((AnsiString)"Error " + IntToStr(ErrorCode), true);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::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 TFormMain::ElSSHClientOpenConnection(TObject *Sender)
{
    Log("SSH connection established");
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ElSSHClientReceive(TObject *Sender,
      Pointer Buffer, int MaxSize, int Written)
{
    if (!ClientSocket->Active)
    {
        Written = 0;
        return;
    }
    Written = ClientSocket->Socket->ReceiveBuf(Buffer, MaxSize);
    if (Written < 0)
        Written = 0;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ElSSHClientSend(TObject *Sender, Pointer Buffer,
      int Size)
{
    if (ClientSocket->Active)
        ClientSocket->Socket->SendBuf(Buffer, Size);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ElRemotePortForwardSSHTunnelClose(
      TObject *Sender, TElSSHTunnelConnection *TunnelConnection)
{
    RemoveConnectionFromList(TunnelConnection);
    RefreshListView();
}
//---------------------------------------------------------------------------
void TFormMain::RemoveConnectionFromList(TElSSHTunnelConnection* Conn)
{
    int I = ConnList->IndexOf(Conn);
    if (I < 0)
        return;
    ConnList->Remove(Conn);
    TClientSocket* sck = (TClientSocket*)(SocketList->Items[I]);
    SocketList->Delete(I);
    BufferList->Delete(I);
    PForwardStatus P = (PForwardStatus)(StatusList->Items[I]);
    StatusList->Delete(I);
    delete P;
    sck->Close();
}
//---------------------------------------------------------------------------
void TFormMain::RemoveConnectionFromList(TClientSocket* Socket)
{
    int I = SocketList->IndexOf(Socket);
    if (I < 0)
        return;
    SocketList->Remove(Socket);
    TElSSHTunnelConnection* conn = (TElSSHTunnelConnection*)(ConnList->Items[I]);
    ConnList->Delete(I);
    BufferList->Delete(I);
    PForwardStatus P = PForwardStatus(StatusList->Items[I]);
    StatusList->Delete(I);
    delete P;
    if (conn!=NULL)
        conn->Close(true);
}
//---------------------------------------------------------------------------
void TFormMain::SetBufferCount(int Value)
{
    while (BufferList->Count < Value)
          BufferList->Add("");
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ElRemotePortForwardSSHTunnelError(
      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();

⌨️ 快捷键说明

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