mainform.cpp

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

CPP
733
字号
//---------------------------------------------------------------------------

#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)
{
  SSHClient = new TElSSHClient(this);
  SSHClient->Versions = Sbsshcommon::TSSHVersions()<<sbSSH2;
  SSHClient->OnSend = HandleSSHClientSend;
  SSHClient->OnReceive = HandleSSHClientReceive;
  SSHClient->OnOpenConnection = HandleSSHClientOpenConnection;
  SSHClient->OnCloseConnection = HandleSSHClientCloseConnection;
  SSHClient->OnAuthenticationSuccess = HandleSSHClientAuthSuccess;
  SSHClient->OnAuthenticationFailed = HandleSSHClientAuthFailed;
  SSHClient->OnKeyValidate = HandleSSHClientKeyValidate;
  TunnelList = new TElSSHTunnelList(this);
  SftpTunnel = new TElSubsystemSSHTunnel(this);
  SftpClient = new TElSftpClient(this);
  SftpTunnel->TunnelList = TunnelList;
  SSHClient->TunnelList = TunnelList;
  SftpClient->Tunnel = SftpTunnel;
  SftpClient->OnOpenConnection = HandleSftpOpenConnection;
  SftpClient->OnCloseConnection = HandleSftpCloseConnection;
  SftpClient->OnOpenFile = HandleSftpOpenFile;
  SftpClient->OnError = HandleSftpError;
  SftpClient->OnSuccess = HandleSftpSuccess;
  SftpClient->OnDirectoryListing = HandleSftpDirectoryListing;
  SftpClient->OnData = HandleSftpData;
  SftpClient->OnAbsolutePath = HandleSftpAbsolutePath;
  SftpClient->OnFileAttributes = HandleSftpFileAttributes;

  KeyStorage = new TElSSHMemoryKeyStorage(this);
  SSHClient->KeyStorage = KeyStorage;

  CurrentFileList = new TList;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
  delete KeyStorage;
  delete SSHClient;
  delete TunnelList;
  delete SftpTunnel;
  delete SftpClient;
  ClearFileList();
  delete CurrentFileList;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::scktClientConnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
    Log("TCP connection opened");
    SSHClient->EncryptionAlgorithms[SSH_EA_3DES] = false;
    SSHClient->EncryptionAlgorithms[SSH_EA_DES] = false;
    SSHClient->EncryptionAlgorithms[SSH_EA_BLOWFISH] = false;

    SSHClient->UserName = editUserName->Text;
    SSHClient->Password = editPassword->Text;

    KeyStorage->Clear();
    TElSSHKey* Key = new TElSSHKey;
    if ((!edPrivateKey->Text.IsEmpty()) && FileExists(edPrivateKey->Text) && (Key->LoadPrivateKey(edPrivateKey->Text, "") == 0))
    {
        KeyStorage->Add(Key);
        SSHClient->AuthenticationTypes = SSHClient->AuthenticationTypes | SSH_AUTH_TYPE_PUBLICKEY;
    }
    else
        SSHClient->AuthenticationTypes = SSHClient->AuthenticationTypes & (~SSH_AUTH_TYPE_PUBLICKEY);

    delete Key;

    SSHClient->Open();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::scktClientDisconnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
    Log("TCP Connection closed");
    btnConnect->Caption = "Disconnect";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::scktClientRead(TObject *Sender,
      TCustomWinSocket *Socket)
{
    SSHClient->DataAvailable();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::sbPrivateKeyClick(TObject *Sender)
{
    if (OpenDialog->Execute())
        edPrivateKey->Text = OpenDialog->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnConnectClick(TObject *Sender)
{
    if (scktClient->Active)
    {
        scktClient->Close();
        btnConnect->Caption = "Connect";
    }
    else
    {
        SftpClient->Versions = Sbsftpcommon::TSBSftpVersions();
        if (CheckBoxV2->Checked)
            SftpClient->Versions = SftpClient->Versions << sbSFTP2;
        if (CheckBoxV3->Checked)
            SftpClient->Versions = SftpClient->Versions << sbSFTP3;
        if (CheckBoxV4->Checked)
            SftpClient->Versions = SftpClient->Versions << sbSFTP4;
        btnConnect->Caption = "Disconnect";

        if (editHost->Text.Pos(":") > 0)
        {
            scktClient->Host = editHost->Text.SubString(1, editHost->Text.Pos(":") - 1);
            scktClient->Port = StrToIntDef(editHost->Text.SubString(editHost->Text.Pos(":") + 1, editHost->Text.Length()), 22);
        }
        else
        {
            scktClient->Host = editHost->Text;
            scktClient->Port = 22;
        }

        scktClient->Open();
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::StringGrid1DblClick(TObject *Sender)
{
    if (StringGrid1->Row == 0)
        return;
    if (!scktClient->Active)
    {
        Log("Error: not connected");
        return;
    }
    TElSftpFileInfo* Info = ((TElSftpFileInfo*)(CurrentFileList->Items[StringGrid1->Row - 1]));
    if (Info->Attributes->Directory)
        ChangeDir(Info->Name);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::StringGrid1MouseMove(TObject *Sender,
      TShiftState Shift, int X, int Y)
{
    int C, R;

    StringGrid1->MouseToCell(X, Y, C, R);
    if (R > 0)
    {
        if ((CurrentFileList->Count > R) && (CurrentFileList->Items[R - 1] != NULL))
            Edit4->Text = ((TElSftpFileInfo*)(CurrentFileList->Items[R - 1]))->LongName;
    }
    else
      Edit4->Text = "";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    if (!scktClient->Active)
    {
        Log("Error: not connected");
        return;
    }
    AnsiString S = InputBox("New directory...", "Enter directory name:", "");
    if (S.IsEmpty())
    {
        Log("Invalid directory name, creation cancelled.");
        return;
    }
    MakeDir(S);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
    if (StringGrid1->Row == 0)
        return;
    TElSftpFileInfo* Info = ((TElSftpFileInfo*)(CurrentFileList->Items[StringGrid1->Row - 1]));
    AnsiString S = InputBox("Rename...", "Enter NEW name:", "");
    if (S.IsEmpty())
    {
        Log("Invalid filename, renaming cancelled.");
        return;
    }
    RenameFile(Info->Name, S);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
    if (StringGrid1->Row == 0)
        return;
    if (MessageDlg("Do you really want to remove this item?", mtConfirmation, TMsgDlgButtons()<<mbYes<<mbNo, 0) == mrYes)
    {
        TElSftpFileInfo* Info = ((TElSftpFileInfo*)(CurrentFileList->Items[StringGrid1->Row - 1]));
        if (Info->Attributes->Directory)
            DeleteDir(Info->Name);
        else
            DeleteFile(Info->Name);
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
    if (StringGrid1->Row == 0)
        return;
    TElSftpFileInfo* Info = ((TElSftpFileInfo*)(CurrentFileList->Items[StringGrid1->Row - 1]));

    if (Info->Attributes->Directory)
    {
        Log("Can not download directory. Please choose ordinary file.");
        return;
    }
    if (SaveDialog1->Execute())
        DownloadFile(Info, SaveDialog1->FileName);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
  if (OpenDialog1->Execute())
      UploadFile(OpenDialog1->FileName);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnUpdateFileInfoClick(TObject *Sender)
{
    TElSftpFileInfo* Info = ((TElSftpFileInfo*)(CurrentFileList->Items[StringGrid1->Row - 1]));
    Log((AnsiString)"Requesting  download, \"" + Info->Name + "\"");
    SftpClient->RequestAttributes(CurrentDir + "/" + Info->Name, false);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleSSHClientSend(TObject* Sender, void* Buffer, int Size)
{
    while (scktClient->Socket->SendBuf(Buffer, Size) == -1)
        Sleep(50);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleSSHClientReceive(TObject* Sender, void* Buffer, int MaxSize, int &Written)
{
    Written = scktClient->Socket->ReceiveBuf(Buffer, MaxSize);
    if (Written < 0)
        Written = 0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleSSHClientOpenConnection(TObject* Sender)
{
    Log((AnsiString)"SSH Connection started with " + SSHClient->ServerSoftwareName);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleSSHClientCloseConnection(TObject* Sender)
{
    Log("SSH Connection closed");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleSSHClientAuthSuccess(TObject* Sender)
{
    Log("Authentication succeeded");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleSSHClientAuthFailed(TObject* Sender, int AuthType)
{
    Log("Authentication failed. Unknown user or Invalid password.");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleSftpOpenConnection(TObject* Sender)
{
    AnsiString S;
    Log("Sftp connection started");
    if (SftpClient->Version == sbSFTP1)
        S = "1";
    else if (SftpClient->Version == sbSFTP2)
        S = "2";
    else if (SftpClient->Version == sbSFTP3)
        S = "3";
    else if (SftpClient->Version == sbSFTP4)
        S = "4";
    else
        S = "unknown";
    Log((AnsiString)"Sftp version is " + S);
    CurrentDir = ".";
    BuildFileList(".");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleSftpCloseConnection(TObject* Sender)
{
    Log("Sftp connection closed");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleSftpOpenFile(TObject* Sender, TSBSftpFileHandle Handle)
{
    if (State == STATE_OPEN_DIRECTORY_SENT)
    {
        Log("Directory opened");
        CurrentHandle = Handle;
        SftpClient->ReadDirectory(CurrentHandle);
        State = STATE_READ_DIRECTORY_SENT;
    }
    else if (State == STATE_CHANGE_DIR)
        SftpClient->CloseHandle(Handle);
    else if (State == STATE_DOWNLOAD_OPEN)
    {
        CurrentHandle = Handle;
        Form2->Gauge1->Progress = 0;
        Form2->Show();
        SftpClient->Read(Handle, CurrentFileOffset, FILE_BLOCK_SIZE);
        State = STATE_DOWNLOAD_RECEIVE;
    }
    else if (State == STATE_UPLOAD_OPEN)
    {
        CurrentHandle = Handle;
        Form2->Gauge1->Progress = 0;
        Form2->Show();
        WriteNextBlockToFile();
        State = STATE_UPLOAD_SEND;
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleSftpError(TObject* Sender, int ErrorCode, const AnsiString Comment)
{
    if ((State == STATE_READ_DIRECTORY_SENT) && (ErrorCode == SSH_ERROR_EOF))
    {
        Log("File list received");
        CloseCurrentHandle();
        OutputFileList();
    }
    else if ((State == STATE_DOWNLOAD_RECEIVE) && (ErrorCode == SSH_ERROR_EOF))
    {
        Log("File received");
//        CloseFile(CurrentFile);
        delete CurrentFile;
        CurrentFile = NULL;
        CloseCurrentHandle();
    }
    else
        Log((AnsiString)"Error #" + IntToStr(ErrorCode) + " with comment \"" + Comment + "\"");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HandleSftpSuccess(TObject* Sender, const AnsiString Comment)
{
    if (State == STATE_CHANGE_DIR)
    {
        Log((AnsiString)"Operation succeeded with comment \"" + Comment + "\"");
        RequestAbsolutePath(CurrentDir + "/" + RelDir + "/");
    }
    else if ((State == STATE_MAKE_DIR) || (State == STATE_RENAME) || (State == STATE_REMOVE))
    {
        Log("Operation succeeded with comment \"" + Comment + "\"");
        BuildFileList(CurrentDir);
    }
    else if (State == STATE_UPLOAD_SEND)

⌨️ 快捷键说明

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