⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sbindysshserveriohandler9.pas

📁 著名的SecureBlackBox控件完整源码
💻 PAS
📖 第 1 页 / 共 4 页
字号:
      read GetCompressionAlgorithmCS;
    property EncryptionAlgorithmServerToClient : TSSHEncryptionAlgorithm
      read GetEncryptionAlgorithmSC;
    property EncryptionAlgorithmClientToServer : TSSHEncryptionAlgorithm
      read GetEncryptionAlgorithmCS;
    property MacAlgorithmServerToClient : TSSHMacAlgorithm read GetMacAlgorithmSC;
    property MacAlgorithmClientToServer : TSSHMacAlgorithm read GetMacAlgorithmCS;
  published
    { SSH server properties }
    property AuthenticationTypes : integer read GetAuthenticationTypes
      write SetAuthenticationTypes default SSH_AUTH_TYPE_PASSWORD;
    property CloseIfNoActiveTunnels : boolean read GetCloseIfNoActiveTunnels
      write SetCloseIfNoActiveTunnels default true;
    property ChangePwdPrompt : string read GetChangePwdPrompt write SetChangePwdPrompt;
    property CompressionLevel : integer read GetCompressionLevel
      write SetCompressionLevel default 9;
    property ForceCompression : boolean read GetForceCompression
      write SetForceCompression;
    property KeyStorage: TElSSHCustomKeyStorage read GetKeyStorage write
        SetKeyStorage;
    property SoftwareName : string read GetSoftwareName write SetSoftwareName;

    { SSH server events }
    property OnAuthAttempt: TSSHAuthAttemptEvent read GetOnAuthAttempt
      write SetOnAuthAttempt;
    property OnAuthFailed: TSSHAuthenticationFailedEvent read GetOnAuthFailed
      write SetOnAuthFailed;
    property OnAuthBanner: TSSHAuthBannerEvent read GetOnAuthBanner
      write SetOnAuthBanner;
    property OnAuthPassword: TSSHAuthPasswordEvent read GetOnAuthPassword
      write SetOnAuthPassword;
    property OnAuthPasswordChange: TSSHAuthPasswordChangeEvent read
      GetOnAuthPasswordChange write SetOnAuthPasswordChange;
    property OnAuthPublicKey: TSSHAuthPublicKeyEvent read GetOnAuthPublicKey
      write SetOnAuthPublicKey;
    property OnAuthHostbased: TSSHAuthHostbasedEvent read GetOnAuthHostbased
      write SetOnAuthHostbased;
    property OnAuthKeyboard: TSSHAuthKeyboardEvent read GetOnAuthKeyboard
      write SetOnAuthKeyboard;
    property OnAuthKeyboardResponse: TSSHAuthKeyboardResponseEvent
      read GetOnAuthKeyboardResponse write SetOnAuthKeyboardResponse;
    property OnBeforeOpenSession : TSSHBeforeOpenSessionEvent
      read GetOnBeforeOpenSession write SetOnBeforeOpenSession;
    property OnBeforeOpenShell : TSSHBeforeOpenShellEvent read
      GetOnBeforeOpenShell write SetOnBeforeOpenShell;
    property OnBeforeOpenCommand : TSSHBeforeOpenCommandEvent read
      GetOnBeforeOpenCommand write SetOnBeforeOpenCommand;
    property OnBeforeOpenSubsystem: TSSHBeforeOpenSubsystemEvent
      read GetOnBeforeOpenSubsystem write SetOnBeforeOpenSubsystem;
    property OnBeforeOpenClientForwarding: TSSHBeforeOpenClientForwardingEvent
      read GetOnBeforeOpenClientForwarding write SetOnBeforeOpenClientForwarding;
    property OnBeforeOpenX11Forwarding: TSSHBeforeOpenX11ForwardingEvent
      read GetOnBeforeOpenX11Forwarding write SetOnBeforeOpenX11Forwarding;
    property OnCloseConnection: TSSHCloseConnectionEvent
      read FOnCloseConnection write FOnCloseConnection;
    property OnError : TSSHErrorEvent read FOnError write FOnError;      
    property OnOpenSession: TSSHOpenSessionEvent read GetOnOpenSession
      write SetOnOpenSession;
    property OnOpenShell : TSSHOpenShellEvent read FOnOpenShell
      write FOnOpenShell default nil;
    property OnOpenCommand : TSSHOpenCommandEvent read FOnOpenCommand
      write FOnOpenCommand default nil;
    property OnOpenSubsystem : TSSHOpenSubsystemEvent read FOnOpenSubsystem
      write FOnOpenSubsystem default nil;
    property OnOpenClientForwarding: TSSHOpenClientForwardingEvent
      read FOnOpenClientForwarding write FOnOpenClientForwarding default nil;
    property OnOpenServerForwarding: TSSHOpenServerForwardingEvent
      read FOnOpenServerForwarding write FOnOpenServerForwarding default nil;
    property OnOpenX11Forwarding: TSSHOpenX11ForwardingEvent
      read FOnOpenX11Forwarding write FOnOpenX11Forwarding default nil;
    property OnServerForwardingFailed: TSSHServerForwardingFailedEvent
      read GetOnServerForwardingFailed write SetOnServerForwardingFailed;
    property OnServerForwardingRequest: TSSHServerForwardingRequestEvent
      read GetOnServerForwardingRequest write SetOnServerForwardingRequest;
    property OnServerForwardingCancel : TSSHServerForwardingCancelEvent
      read GetOnServerForwardingCancel write SetOnServerForwardingCancel;
    property OnX11ForwardingRequest : TSSHX11ForwardingRequestEvent
      read GetOnX11ForwardingRequest write SetOnX11ForwardingRequest;
    property OnTerminalRequest : TSSHTerminalRequestEvent
      read GetOnTerminalRequest write SetOnTerminalRequest;
    property OnOpenConnection : TSSHOpenConnectionEvent
      read GetOnOpenConnection write SetOnOpenConnection;

    property OnOpenChannel : TSSHIdOpenChannelEvent read
      FOnOpenChannel write FOnOpenChannel;
  end;

  EElIdNotImplementedException = class(EIdException);
  EElIdSSHException = class(EIdException);

  procedure Register;

implementation

resourcestring
  SConnectionError = 'SSH connection error';
  SNotConnected = 'Not connected';
  SConnectionClosed= 'Connection closed';

procedure Register;
begin
  RegisterComponents('SSHBlackbox', [TElIdSSHClientServerIOHandler,
    TElIdSSHServerIOHandler, TElIdSSHConnectionIOHandler]);
end;

{ TElIdSSHServerIOHandler }
constructor TElIdSSHServerIOHandler.Create(AOwner : TComponent);
var
  I : integer;
begin
  inherited;

  FAllowedSubsystems := TStringList.Create;
  FForceCompression := false;
  FCloseIfNoActiveTunnels := true;
  FAuthenticationTypes := SSH_AUTH_TYPE_PUBLICKEY or SSH_AUTH_TYPE_PASSWORD or
    SSH_AUTH_TYPE_HOSTBASED or SSH_AUTH_TYPE_KEYBOARD;
  FCompressionLevel := 6;
  FSoftwareName := '';
  FChangePwdPrompt := '';
  FKeyStorage := nil;

  for I := SSH_EA_FIRST to SSH_EA_LAST do
    FEncryptionAlgorithms[I] := true;
  FEncryptionAlgorithms[SSH_EA_3DES] := false;
  for I := SSH_CA_FIRST to SSH_CA_LAST do
    FCompressionAlgorithms[I] := true;
  {$ifdef DONT_USE_ZLIB}
  FCompressionAlgorithms[SSH_CA_ZLIB] := false;
  {$endif}
  for I := SSH_MA_FIRST to SSH_MA_LAST do
    FMACAlgorithms[I] := true;
  for I := SSH_PK_FIRST to SSH_PK_LAST do
    FPublicKeyAlgorithms[I] := true;
  for I := SSH_KEX_FIRST to SSH_KEX_LAST do
    FKexAlgorithms[I] := true;
  FCompressionLevel := 6;
end;

destructor TElIdSSHServerIOHandler.Destroy;
begin
  FreeAndNil(FAllowedSubsystems);

  inherited;
end;

function TElIdSSHServerIOHandler.GetCompressionAlgorithms(
  Index: TSSHCompressionAlgorithm): boolean;
begin
  Result := FCompressionAlgorithms[Index];
end;

function TElIdSSHServerIOHandler.GetEncryptionAlgorithms(
  Index: TSSHEncryptionAlgorithm): boolean;
begin
  Result := FEncryptionAlgorithms[Index];
end;

function TElIdSSHServerIOHandler.GetKexAlgorithms(
  Index: TSSHKexAlgorithm): boolean;
begin
  Result := FKexAlgorithms[Index];
end;

function TElIdSSHServerIOHandler.GetMACAlgorithms(
  Index: TSSHMacAlgorithm): boolean;
begin
  Result := FMACAlgorithms[Index];
end;

function TElIdSSHServerIOHandler.GetPublicKeyAlgorithms(
  Index: TSSHPublicKeyAlgorithm): boolean;
begin
  Result := FPublicKeyAlgorithms[Index];
end;

function TElIdSSHServerIOHandler.Accept(ASocket: TIdStackSocketHandle;
  AThread: TIdThread) : TIdIOHandler;
var
  IOHandler : TElIdSSHClientServerIOHandler;
  Index : integer;
begin
  IOHandler := TElIdSSHClientServerIOHandler.Create(nil);
  IOHandler.Open;

  if Assigned(AThread) then begin
    while not AThread.Stopped do begin
      if IOHandler.Binding.Select(ASocket, 100) then begin  // Wait for 100 ms
        if not IOHandler.Binding.Accept(ASocket) then FreeAndNil(IOHandler);
        Break;
      end;//if select
    end;
    if AThread.Stopped then
      FreeAndNil(IOHandler);
  end
  else begin
    if not IOHandler.Binding.Accept(ASocket) then
      FreeAndNil(IOHandler);
  end;

  if Assigned(IOHandler) then
  begin
    IOHandler.ForceCompression := FForceCompression;
    IOHandler.CloseIfNoActiveTunnels := FCloseIfNoActiveTunnels;
    IOHandler.AuthenticationTypes := FAuthenticationTypes;
    IOHandler.CompressionLevel := FCompressionLevel;
    if FSoftwareName <> '' then
      IOHandler.SoftwareName;
    if FChangePwdPrompt <> '' then  
      IOHandler.ChangePwdPrompt := FChangePwdPrompt;
    IOHandler.KeyStorage := FKeyStorage;
    IOHandler.OnAuthAttempt := FOnAuthAttempt;
    IOHandler.OnAuthFailed := FOnAuthFailed;
    IOHandler.OnAuthBanner := FOnAuthBanner;
    IOHandler.OnAuthPassword := FOnAuthPassword;
    IOHandler.OnAuthPasswordChange := FOnAuthPasswordChange;
    IOHandler.OnAuthPublicKey := FOnAuthPublicKey;
    IOHandler.OnAuthHostbased := FOnAuthHostbased;
    IOHandler.OnAuthKeyboard := FOnAuthKeyboard;
    IOHandler.OnAuthKeyboardResponse := FOnAuthKeyboardResponse;
    IOHandler.OnBeforeOpenSession := FOnBeforeOpenSession;
    IOHandler.OnBeforeOpenShell := FOnBeforeOpenShell;
    IOHandler.OnBeforeOpenCommand := FOnBeforeOpenCommand;
    IOHandler.OnBeforeOpenSubsystem := FOnBeforeOpenSubsystem;
    IOHandler.OnBeforeOpenClientForwarding := FOnBeforeOpenClientForwarding;
    IOHandler.OnBeforeOpenX11Forwarding := FOnBeforeOpenX11Forwarding;
    IOHandler.OnServerForwardingFailed := FOnServerForwardingFailed;
    IOHandler.OnServerForwardingRequest := FOnServerForwardingRequest;
    IOHandler.OnServerForwardingCancel := FOnServerForwardingCancel;
    IOHandler.OnX11ForwardingRequest := FOnX11ForwardingRequest;
    IOHandler.OnTerminalRequest := FOnTerminalRequest;
    IOHandler.OnOpenChannel := FOnOpenChannel;

    for Index := SSH_EA_FIRST to SSH_EA_LAST do
      IOHandler.EncryptionAlgorithms[Index] := FEncryptionAlgorithms[Index];
    for Index := SSH_CA_FIRST to SSH_CA_LAST do
      IOHandler.CompressionAlgorithms[Index] := FCompressionAlgorithms[Index];
    for Index := SSH_MA_FIRST to SSH_MA_LAST do
      IOHandler.MACAlgorithms[Index] := FMACAlgorithms[Index];
    for Index := SSH_KEX_FIRST to SSH_KEX_LAST do
      IOHandler.KexAlgorithms[Index] := FKexAlgorithms[Index];
    for Index := SSH_PK_FIRST to SSH_PK_LAST do
      IOHandler.PublicKeyAlgorithms[Index] := FPublicKeyAlgorithms[Index];

    IOHandler.AllowedSubsystems.Assign(FAllowedSubsystems);
  end;

  Result := IOHandler;
end;


procedure TElIdSSHServerIOHandler.SetCompressionAlgorithms(
  Index: TSSHCompressionAlgorithm; const Value: boolean);
begin
  FCompressionAlgorithms[Index] := Value;
end;

procedure TElIdSSHServerIOHandler.SetEncryptionAlgorithms(
  Index: TSSHEncryptionAlgorithm; const Value: boolean);
begin
  FEncryptionAlgorithms[Index] := Value;
end;

procedure TElIdSSHServerIOHandler.SetKexAlgorithms(Index: TSSHKexAlgorithm;
  const Value: boolean);
begin
  FKexAlgorithms[Index] := Value;
end;

procedure TElIdSSHServerIOHandler.SetMACAlgorithms(Index: TSSHMacAlgorithm;
  const Value: boolean);
begin
  FMACAlgorithms[Index] := Value;
end;

procedure TElIdSSHServerIOHandler.SetPublicKeyAlgorithms(
  Index: TSSHPublicKeyAlgorithm; const Value: boolean);
begin
  FPublicKeyAlgorithms[Index] := Value;
end;

procedure TElIdSSHServerIOHandler.SetKeyStorage(
  const Value: TElSSHCustomKeyStorage);
begin
  FKeyStorage := Value;

  if Assigned(FKeyStorage) then
  begin
    FKeyStorage.FreeNotification(Self);
  end;
end;

procedure TElIdSSHServerIOHandler.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited;

  if (AComponent = FKeyStorage) and (Operation = opRemove) then
  begin
    FKeyStorage := nil;
  end;
end;

{ TElIdSSHClientServerIOHandler }

constructor TElIdSSHClientServerIOHandler.Create(AOwner : TComponent);
begin
  inherited;

  FInputBuffer := TIdManagedBuffer.Create;
  FSSHServer := TElSSHServer.Create(nil);
  FConnections := TList.Create;
  FActive := false;
  FErrorOccured := false;  
end;

procedure TElIdSSHClientServerIOHandler.Close;
begin
  if FSSHServer.Active and FActive then
  begin
    if FInputBuffer.Size = 0 then
    begin
      FActive := false;
      FSSHServer.Close;

      while FSSHServer.Active and (not FErrorOccured) do
        DataAvailable;
    end;    
  end;

  inherited;
end;

destructor TElIdSSHClientServerIOHandler.Destroy;
begin
  inherited;

  FreeAndNil(FInputBuffer);
  FreeAndNil(FConnections);
  FreeAndNil(FSSHServer);  
end;

procedure TElIdSSHClientServerIOHandler.DoOpenClientForwarding(
  Sender: TObject; Connection: TElSSHTunnelConnection;
  const DestHost: string; DestPort: integer; const SrcHost: string;
  SrcPort: integer);
var
  Index : integer;
begin
  Index := AddConnection(Connection, TElSSHServer(Sender));

  if (Index >= 0) then
  begin
    Connections[Index].FDestHost := DestHost;
    Connections[Index].FDestPort := DestPort;
    Connections[Index].FSrcHost := SrcHost;
    Connections[Index].FSrcPort := SrcPort;
  end;

  if Assigned(FOnOpenChannel) then
    FOnOpenChannel(Self, Connections[Index]);

  if Assigned(FOnOpenClientForwarding) then
    FOnOpenClientForwarding(Sender, Connection, DestHost, DestPort, SrcHost, SrcPort);
end;

procedure TElIdSSHClientServerIOHandler.DoOpenCommand(Sender: TObject;
  Connection: TElSSHTunnelConnection; const Command: string);
var
  Index : integer;
begin
  Index := AddConnection(Connection, TElSSHServer(Sender));

  if Index >= 0 then
    Connections[Index].FCommand := Command;

  if Assigned(FOnOpenChannel) then
    FOnOpenChannel(Self, Connections[Index]);

  if Assigned(FOnOpenCommand) then
    FOnOpenCommand(Sender, Connection, Command);
end;

procedure TElIdSSHClientServerIOHandler.DoOpenServerForwarding(
  Sender: TObject; Connection: TElSSHTunnelConnection);
var
  Index : integer;
begin
  Index := AddConnection(Connection, TElSSHServer(Sender));

  if Assigned(FOnOpenChannel) then
    FOnOpenChannel(Self, Connections[Index]);

  if Assigned(FOnOpenServerForwarding) then
    FOnOpenServerForwarding(Sender, Connection);
end;

procedure TElIdSSHClientServerIOHandler.DoOpenShell(Sender: TObject;
  Connection: TElSSHTunnelConnection);
var
  Index : integer;
begin
  Index := AddConnection(Connection, TElSSHServer(Sender));

  if Assigned(FOnOpenChannel) then
    FOnOpenChannel(Self, Connections[Index]);

  if Assigned(FOnOpenShell) then
    FOnOpenShell(Sender, Connection);
end;

procedure TElIdSSHClientServerIOHandler.DoOpenSubsystem(Sender: TObject;
  Connection: TElSSHTunnelConnection; const Subsystem: string);
var
  Index : integer;
begin
  Index := AddConnection(Connection, TElSSHServer(Sender));
  if Index >= 0 then
    Connections[Index].FSubsystem := Subsystem;

  if Assigned(FOnOpenChannel) then
    FOnOpenChannel(Self, Connections[Index]);


  if Assigned(FOnOpenSubsystem) then
    FOnOpenSubsystem(Sender, Connection, Subsystem);
end;

procedure TElIdSSHClientServerIOHandler.DoOpenX11Forwarding(
  Sender: TObject; Connection: TElSSHTunnelConnection;
  const OrigIP: string; OrigPort: integer);
var

⌨️ 快捷键说明

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