ftpunit.cpp

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

CPP
550
字号
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "FTPUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "SBCustomCertStorage"
#pragma link "SBWinCertStorage"
#pragma link "SBX509"
#pragma resource "*.dfm"
TFTPForm *FTPForm;
#undef SetPort
//---------------------------------------------------------------------------
__fastcall TFTPForm::TFTPForm(TComponent* Owner)
        : TForm(Owner)
{
/*
     SetLicenseKey((AnsiString)"1B7E829FC081C1788415E9079933176672F293A4BCCE78C035672F35F0D91AB3" +
                               "D2D1EBC93824EDBFEA2CB623971EA31C90210B279D21A11C1FA5088A3D162A94" +
                               "6738D97F9B70B3372CA1691226F4F1479E17FCDF22176B1DA3D19487B9C61E32" +
                               "B61B7577F11F869D031EAD1702F852A26F91155CB956E4CB3E0814B0680A3517" +
                               "6CD51FE61CF7C642E26F3D2E2AFF14AEC197B253611DEE6B6EBD429065112DD2" +
                               "B7E37562C2683CAC49FE76563FC8E56E1FDC340709FB0DF16F94F85CEA044622" +
                               "ADFBCE6DBBB0C7E1F5C9B0348ECB0E5995382C5A5DCE8916DAC3AFEC77F6865A" +
                               "E313C65C9A9B0829DD5647E0D729F27B272D200F20D4BD8ED4A917A67295D446");
*/
}
//---------------------------------------------------------------------------
void __fastcall TFTPForm::btnConnectClick(TObject *Sender)
{
    Connect();
}
//---------------------------------------------------------------------------
void __fastcall TFTPForm::mnuExitClick(TObject *Sender)
{
    ExitApp();
}
//---------------------------------------------------------------------------
void __fastcall TFTPForm::btnDisconnectClick(TObject *Sender)
{
    Disconnect();
}
//---------------------------------------------------------------------------
void __fastcall TFTPForm::ClientCertificateNeededEx(TObject* Sender, TElX509Certificate* &Certificate)
{
    if ((FUseCert) && (FNeededIndex == 0))
    {
        Certificate = FCert;
        FNeededIndex++;
    }
    else
        Certificate = NULL;
}
//---------------------------------------------------------------------------
void __fastcall TFTPForm::ClientCertificateValidate(TObject* Sender, TElX509Certificate* Cert, bool &Validate)
{
    AnsiString S;
    TSBCertificateValidity Validity;
    TSBCertificateValidityReason Reason;

    Log("Certificate received", false);
    S = (AnsiString)"Issuer: " + "CN=" + Cert->IssuerName.CommonName + ", C=" + Cert->IssuerName.Country +
      ", O=" + Cert->IssuerName.Organization + ", L=" + Cert->IssuerName.Locality;
    Log(S, false);
    S = (AnsiString)"Subject: " + "CN=" + Cert->SubjectName.CommonName + ", C=" + Cert->SubjectName.Country +
      ", O=" + Cert->SubjectName.Organization + ", L=" + Cert->SubjectName.Locality;
    Log(S, false);
    FClient->InternalValidate(Validity, Reason);
    if (!(Validity == cvOk || Validity == cvSelfSigned))
    {
        Validity = ElMemoryCertStorage->Validate(Cert, Reason, 0);
        if (!(Validity == cvOk || Validity == cvSelfSigned))
            Log("Warning: certificate is not valid!", true);
        else
            Log("Certificate is OK", false);
    }
    else
        Log("Certificate is OK", false);
    // adding certificate to temporary store
    ElMemoryCertStorage->Add(Cert, true);
    Validate = true;
}
//---------------------------------------------------------------------------
void __fastcall TFTPForm::Connect(void)
{
//    TFileStream* F;
//    int R;
    AnsiString S;

    if (FClient->Active)
    {
        MessageDlg("Already connected, please disconnect first", mtWarning, TMsgDlgButtons() <<mbOK, 0);
        return;
    }

    if (frmConnProps->ShowModal() == mrOk)
    {
        memoOutput->Clear();
        lvLog->Items->Clear();

        FClient->Address = frmConnProps->editHost->Text;
//		FClient->Port = StrToIntDef(frmConnProps->editPort->Text, 21);
        FClient->Username = frmConnProps->editUsername->Text;
        FClient->Password = frmConnProps->editPassword->Text;
        FClient->Versions = Sbconstants::TSBVersions();
        if (frmConnProps->cbSSL2->Checked)
            FClient->Versions = FClient->Versions << sbSSL2;
        if (frmConnProps->cbSSL3->Checked)
            FClient->Versions = FClient->Versions << sbSSL3;
        if (frmConnProps->cbTLS1->Checked)
            FClient->Versions = FClient->Versions << sbTLS1;
        if (frmConnProps->cbTLS11->Checked)
            FClient->Versions = FClient->Versions << sbTLS11;
        FUseCert = false;
        if ((frmConnProps->editCert->Text != "") && (FileExists(frmConnProps->editCert->Text)))
        {
            try
            {
              TFileStream* F = new TFileStream(frmConnProps->editCert->Text, fmOpenRead);
              try
              {
                  int R = FCert->LoadFromStreamPFX(F, frmConnProps->editCertPassword->Text, 0);
                  if (R == 0)
                  {
                      FUseCert = true;
                      Log("Certificate loaded OK", false);
                  }
                  else
                      Log("Failed to load certificate, PFX error " + IntToHex(R, 4), true);
              }
              __finally
              {
                  delete F;
              }
            }
            catch (Exception &e)
            {
                Log(e.Message, true);
            }
        }
        Log((AnsiString)"Connecting to " + FClient->Address + ":" + IntToStr(FClient->Port), false);
        FClient->UseSSL = frmConnProps->cbUseSSL->Checked;
        FClient->EncryptDataChannel = !frmConnProps->cbClear->Checked;
        if (frmConnProps->comboAuthCmd->ItemIndex == -1)
            FClient->AuthCmd = acAuto;
        else
            FClient->AuthCmd = TSBFTPAuthCmd(frmConnProps->comboAuthCmd->ItemIndex);
        FClient->PassiveMode = frmConnProps->cbPassive->Checked;
        if (frmConnProps->cbImplicit->Checked)
            FClient->SSLMode = smImplicit;
        else
            FClient->SSLMode = smExplicit;

        FNeededIndex = 0;
        ElMemoryCertStorage->Clear();

        try
        {
            FClient->Open();
            Log("Connected", false);

            FClient->Login();
            Log("Loggged in", false);

            if (FClient->UseSSL)
            {
                switch (FClient->Version)
                {
                    case sbSSL2:
                        S = "SSL2";
                        break;
                    case sbSSL3:
                        S = "SSL3";
                        break;
                    case sbTLS1:
                        S = "TLS1";
                        break;
                    case sbTLS11:
                        S = "TLS1.1";
                        break;
                    default:
                        S = "Unknown";
                }
                Log((AnsiString)"SSL version is " + S, false);
            }
        }
        catch(Exception &e)
        {
            Log(e.Message, true);
        }
    }
}
//---------------------------------------------------------------------------
void __fastcall TFTPForm::Disconnect(void)
{
    if (FClient->Active)
    {
        Log("Disconnecting", false);
        try
        {
            FClient->Close(true);
            Log("Disconnected", false);
        }
        catch(Exception &e)
        {
            Log(e.Message, true);
        }
    }
}
//---------------------------------------------------------------------------
void __fastcall TFTPForm::ExitApp(void)
{
    this->Close();
}
//---------------------------------------------------------------------------
void __fastcall TFTPForm::FinalizeApp(void)
{
    delete FCert;
    delete FClient;
}
//---------------------------------------------------------------------------
void __fastcall TFTPForm::InitializeApp(void)
{
    FClient = new TElSimpleFTPSClient(NULL);
    FCert = new TElX509Certificate(NULL);
    SetupEvents();
    FClient->CertStorage = ElWinCertStorage;
}
//---------------------------------------------------------------------------
void __fastcall TFTPForm::Log(const AnsiString S, bool Error)
{
    TListItem* Item = lvLog->Items->Add();
    Item->Caption = TimeToStr(TDateTime::CurrentDateTime());
    Item->SubItems->Add(S);
    if (Error)
        Item->ImageIndex = 1;
    else
        Item->ImageIndex = 0;
}
//---------------------------------------------------------------------------
void __fastcall TFTPForm::mnuConnectClick(TObject *Sender)
{
    Connect();
}
//---------------------------------------------------------------------------
void __fastcall TFTPForm::mnuDisconnectClick(TObject *Sender)
{
    Disconnect();
}
//---------------------------------------------------------------------------
void __fastcall TFTPForm::SetupEvents(void)
{
    FClient->OnCertificateValidate = ClientCertificateValidate;
    FClient->OnCertificateNeededEx = ClientCertificateNeededEx;
    FClient->OnTextDataLine = ClientTextDataLine;
    FClient->OnControlSend = ClientControlSend;
    FClient->OnControlReceive = ClientControlReceive;
    FClient->OnSSLError = ClientSSLError;
}
//---------------------------------------------------------------------------
void __fastcall TFTPForm::ShowAbout(void)
{
    frmAbout->ShowModal();
}
//---------------------------------------------------------------------------
void __fastcall TFTPForm::btnPWDClick(TObject *Sender)
{
    if (FClient->Active)
    {
        try
        {
//            memoOutput->Text = memoOutput->Text + (AnsiString)"Current directory is: " + FClient->GetCurrentDir() + "\n";
            memoOutput->Lines->Add((AnsiString)"Current directory is: " + FClient->GetCurrentDir());

⌨️ 快捷键说明

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