mainform.pas

来自「著名的SecureBlackBox控件完整源码」· PAS 代码 · 共 173 行

PAS
173
字号
unit MainForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, ExtCtrls, StdCtrls, ForwardingMain, ImgList, SBUtils;

type
  TfrmMain = class(TForm)
    pConnProps: TPanel;
    pClient: TPanel;
    pLog: TPanel;
    lvLog: TListView;
    lvConnections: TListView;
    lblSSHProps: TLabel;
    lblHost: TLabel;
    lblPort: TLabel;
    editHost: TEdit;
    editPort: TEdit;
    lblUsername: TLabel;
    editUsername: TEdit;
    lblPassword: TLabel;
    editPassword: TEdit;
    lblForwardingProps: TLabel;
    lblLocalPort: TLabel;
    editLocalPort: TEdit;
    lblDestination: TLabel;
    editDestHost: TEdit;
    editDestPort: TEdit;
    btnStart: TButton;
    imgConnections: TImageList;
    imgLog: TImageList;
    procedure btnStartClick(Sender: TObject);
  private
    FSession : TSSHSession;
    procedure RefreshConnectionInfo(Item: TListItem);
    procedure OnLog(Sender: TObject; const S : string; Error : boolean);
    procedure OnConnectionOpen(Sender: TObject; Conn: TSSHForwardingThread);
    procedure OnConnectionChange(Sender: TObject; Conn: TSSHForwardingThread);
    procedure OnConnectionRemove(Sender: TObject; Conn: TSSHForwardingThread);
    procedure OnSessionTerminate(Sender: TObject);
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.DFM}

procedure TfrmMain.btnStartClick(Sender: TObject);
begin
  if FSession = nil then
  begin
    FSession := TSSHSession.Create;
    FSession.Host := editHost.Text;
    FSession.Port := StrToIntDef(editPort.Text, 22);
    FSession.Username := editUsername.Text;
    FSession.Password := editPassword.Text;
    FSession.ForwardedPort := StrToIntDef(editLocalPort.Text, 5001);
    FSession.DestHost := editDestHost.Text;
    FSession.DestPort := StrToIntDef(editDestPort.Text, 5001);
    FSession.OnLog := OnLog;
    FSession.OnConnectionOpen := OnConnectionOpen;
    FSession.OnConnectionChange := OnConnectionChange;
    FSession.OnConnectionRemove := OnConnectionRemove;
    FSession.OnTerminate := OnSessionTerminate;
    FSession.Run;
    btnStart.Caption := 'Stop';
  end
  else
  begin
    btnStart.Enabled := false;
    FSession.Terminate;
  end;
end;

procedure TfrmMain.OnLog(Sender: TObject; const S : string; Error : boolean);
var
  Item : TListItem;
begin
  Item := lvLog.Items.Add();
  Item.Caption := TimeToStr(Now);
  Item.SubItems.Add(S);
  if Error then
    Item.ImageIndex := 1
  else
    Item.ImageIndex := 0;
end;

procedure TfrmMain.OnConnectionOpen(Sender: TObject; Conn: TSSHForwardingThread);
var
  Item : TListItem;
begin
  Item := lvConnections.Items.Add;
  Item.Data := Conn;
  RefreshConnectionInfo(Item);
end;

procedure TfrmMain.OnConnectionChange(Sender: TObject; Conn: TSSHForwardingThread);
var
  Item : TListItem;
begin
  Item := lvConnections.FindData(0, Conn, true, false);
  if Item <> nil then
    RefreshConnectionInfo(Item);
end;

procedure TfrmMain.OnConnectionRemove(Sender: TObject; Conn: TSSHForwardingThread);
var
  Item : TListItem;
begin
  Item := lvConnections.FindData(0, Conn, true, false);
  if Item <> nil then
    lvConnections.Items.Delete(Item.Index);
end;

procedure TfrmMain.RefreshConnectionInfo(Item: TListItem);
var
  Fwd : TSSHForwardingThread;
  S : string;
begin
  Item.SubItems.Clear;
  Fwd := Item.Data;
  Item.Caption := Fwd.Host;
  Item.SubItems.Add(IntToStr(Fwd.Sent));
  Item.SubItems.Add(IntToStr(Fwd.Received));
  S := '';
  case Fwd.InState of
    ifsActive : S := 'Active';
    ifsClosing : S := 'Closing';
    ifsClosed : S := 'Closed';
  end;
  Item.SubItems.Add(S);
  S := '';
  case Fwd.OutState of
    ofsEstablishing : S := 'Establishing';
    ofsActive : S := 'Active';
    ofsClosing : S := 'Closing';
    ofsClosed : S := 'Closed';
  end;
  Item.SubItems.Add(S);
  if (Fwd.InState = ifsActive) and (Fwd.OutState = ofsActive) then
    Item.ImageIndex := 1
  else if (Fwd.OutState = ofsEstablishing) then
    Item.ImageIndex := 0
  else
    Item.ImageIndex := 2;
end;

procedure TfrmMain.OnSessionTerminate(Sender: TObject);
begin
  FreeAndNil(FSession);
  btnStart.Enabled := true;
  btnStart.Caption := 'Start';
end;

initialization

SetLicenseKey('ADDCD14AD06709806817E0B3D7BFD0A2222D536FE156466C5D5FE65DB5DEAE76' + 
  'FFDEBC07E915A5751C12C01C783958872A38E4A5EDA140E7247E0F2E56442A3C' + 
  'F3E9347AD8FDE52083A0DFC86BC00ECB0FD0CF1B51159A2BCB84F6EA6349EF47' + 
  '5C15A59AFCC55F7C3AAD26C279628B5D91B1DC94BD2385354A70CCA3B76101D9' + 
  'F41C84A639FC3CCE4BA8F0CC4A66DCD150114A3F58C1AD46B7B94643741BC20A' + 
  '8DCA83AB921480951B423CAA19EF1863A47CA2C3422E7E5634BED98939A5AE43' + 
  'DE1E4BAD79E66D8A5C973B3455656C8C9B6FF024FADD6CDA02D0F506D98493C8' + 
  'BD1ED7B237DB75FA31F2C82654490CDDDEE24E19939137B9E1DB05508733B22F');

end.

⌨️ 快捷键说明

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