📄 sbindysshserveriohandler10.pas
字号:
unit SBIndySSHServerIOHandler10;
interface
{ if you are using Indy 10.1.1 or higher, please uncomment the following line }
//{$define INDY1011}
uses
SBUtils, SBSSHConstants, SBSSHCommon, SBSSHServer, SBSSHTerm, SBSSHKeyStorage,
IdGlobal, IdSocketHandle, IdThread, IdYarn, IdIOHandler, IdException,
IdComponent, IdServerIOHandler, IdServerIOHandlerSocket, IdIOHandlerSocket,
IdIOHandlerStack, IdTCPConnection, Classes, SysUtils;
type
TElIdSSHClientServerIOHandler = class;
TElIdSSHConnection = class;
TSSHIdOpenChannelEvent = procedure (Sender : TObject;
Connection : TElIdSSHConnection) of object;
TElIdSSHConnection = class(TIdTCPConnection)
private
FTunnelConnection : TElSSHTunnelConnection;
FSSHServer : TElSSHServer;
FScreenNumber: integer;
FCommand: string;
FAuthenticationProtocol: string;
FSubsystem: string;
FTerminalInfo: TElTerminalInfo;
FEnvironment: TStringList;
FSrcPort: Integer;
FDestPort: Integer;
FSrcHost: string;
FDestHost: string;
function GetTunnelType: TSSHTunnelType;
function GetData: pointer;
function GetExitMessage: string;
function GetExitSignal: string;
function GetExitStatus: integer;
procedure SetData(const Value: pointer);
protected
procedure Notification(AComponent : TComponent; Operation : TOperation); override;
procedure InitComponent; override;
procedure SetIOHandler(AValue: TIdIOHandler); override;
procedure SetConnection(ATunnelConnection : TElSSHTunnelConnection;
ASSHServer : TElSSHServer);
property Socket;
public
property TunnelType : TSSHTunnelType read GetTunnelType;
property Data : pointer read GetData write SetData;
property ExitStatus : integer read GetExitStatus;
property ExitSignal : string read GetExitSignal;
property ExitMessage : string read GetExitMessage;
{ tunnel properties }
property TerminalInfo : TElTerminalInfo read FTerminalInfo;
property Environment : TStringList read FEnvironment;
{ Command SSH connection }
property Command : string read FCommand;
{ Subsystem connection }
property Subsystem : string read FSubsystem;
{ port forwarding }
property SrcHost : string read FSrcHost;
property SrcPort: Integer read FSrcPort;
property DestHost : string read FDestHost;
property DestPort: Integer read FDestPort;
{ X11 forwarding }
property AuthenticationProtocol : string read FAuthenticationProtocol;
property ScreenNumber : integer read FScreenNumber;
end;
TElIdSSHConnectionIOHandler = class(TIdIOHandler)
protected
FTunnelConnection : TElSSHTunnelConnection;
FSSHServer : TElSSHServer;
FErrorOccured : boolean;
FActive : boolean;
procedure Notification(AComponent : TComponent; AOperation : TOperation); override;
procedure InitComponent; override;
procedure SetConnection(ATunnelConnection : TElSSHTunnelConnection;
ASSHServer : TElSSHServer);
procedure DoOnData(Sender : TObject; Buffer : pointer; Size : longint);
procedure DoOnError(Sender : TObject; ErrorCode : integer);
procedure DoOnClose(Sender : TObject; CloseType : TSSHCloseType);
public
destructor Destroy; override;
procedure Close; override;
procedure CheckForDisconnect(ARaiseExceptionIfDisconnected: boolean = true;
AIgnoreBuffer: boolean = false); override;
procedure CheckForDataOnSource(ATimeout : Integer = 0); override;
function Connected : boolean; override;
function Readable(AMSec: Integer = IdTimeoutDefault): Boolean; override;
function ReadFromSource(ARaiseExceptionIfDisconnected: Boolean;
ATimeout: Integer; ARaiseExceptionOnTimeout: Boolean): Integer; override;
{$ifndef INDY1011}
procedure WriteDirect(ABuffer : TIdBytes); override;
{$else}
procedure WriteDirect(var ABuffer : TIdBytes); override;
{$endif}
end;
TElIdSSHServerIOHandler = class(TIdServerIOHandlerSocket)
private
FForceCompression: boolean;
FCloseIfNoActiveTunnels: boolean;
FAuthenticationTypes: integer;
FCompressionLevel: integer;
FSoftwareName: string;
FChangePwdPrompt: string;
FKeyStorage: TElSSHCustomKeyStorage;
FOnAuthAttempt: TSSHAuthAttemptEvent;
FOnAuthBanner: TSSHAuthBannerEvent;
FOnAuthFailed: TSSHAuthenticationFailedEvent;
FOnAuthHostbased: TSSHAuthHostbasedEvent;
FOnAuthKeyboard: TSSHAuthKeyboardEvent;
FOnAuthKeyboardResponse: TSSHAuthKeyboardResponseEvent;
FOnAuthPasswordChange: TSSHAuthPasswordChangeEvent;
FOnAuthPassword: TSSHAuthPasswordEvent;
FOnAuthPublicKey: TSSHAuthPublicKeyEvent;
FOnCloseConnection: TSSHCloseConnectionEvent;
FEncryptionAlgorithms : array[SSH_EA_FIRST..SSH_EA_LAST] of boolean;
FCompressionAlgorithms : array[SSH_CA_FIRST..SSH_CA_LAST] of boolean;
FMACAlgorithms : array[SSH_MA_FIRST..SSH_MA_LAST] of boolean;
FKexAlgorithms : array[SSH_KEX_FIRST..SSH_KEX_LAST] of boolean;
FPublicKeyAlgorithms : array[SSH_PK_FIRST..SSH_PK_LAST] of boolean;
FOnOpenChannel: TSSHIdOpenChannelEvent;
FOnOpenConnection: TSSHOpenConnectionEvent;
FOnBeforeOpenClientForwarding: TSSHBeforeOpenClientForwardingEvent;
FOnBeforeOpenCommand: TSSHBeforeOpenCommandEvent;
FOnBeforeOpenSession: TSSHBeforeOpenSessionEvent;
FOnBeforeOpenShell: TSSHBeforeOpenShellEvent;
FOnBeforeOpenSubsystem: TSSHBeforeOpenSubsystemEvent;
FOnBeforeOpenX11Forwarding: TSSHBeforeOpenX11ForwardingEvent;
FOnServerForwardingCancel: TSSHServerForwardingCancelEvent;
FOnServerForwardingFailed: TSSHServerForwardingFailedEvent;
FOnServerForwardingRequest: TSSHServerForwardingRequestEvent;
FOnTerminalRequest: TSSHTerminalRequestEvent;
FOnX11ForwardingRequest: TSSHX11ForwardingRequestEvent;
function GetCompressionAlgorithms(
Index: TSSHCompressionAlgorithm): boolean;
function GetEncryptionAlgorithms(
Index: TSSHEncryptionAlgorithm): boolean;
function GetKexAlgorithms(Index: TSSHKexAlgorithm): boolean;
function GetMACAlgorithms(Index: TSSHMacAlgorithm): boolean;
function GetPublicKeyAlgorithms(
Index: TSSHPublicKeyAlgorithm): boolean;
procedure SetCompressionAlgorithms(Index: TSSHCompressionAlgorithm;
const Value: boolean);
procedure SetEncryptionAlgorithms(Index: TSSHEncryptionAlgorithm;
const Value: boolean);
procedure SetKexAlgorithms(Index: TSSHKexAlgorithm;
const Value: boolean);
procedure SetMACAlgorithms(Index: TSSHMacAlgorithm;
const Value: boolean);
procedure SetPublicKeyAlgorithms(Index: TSSHPublicKeyAlgorithm;
const Value: boolean);
procedure SetKeyStorage(const Value: TElSSHCustomKeyStorage);
protected
FAllowedSubsystems : TStringList;
procedure InitComponent; override;
procedure Notification(AComponent : TComponent; Operation : TOperation); override;
public
destructor Destroy; override;
function Accept(ASocket: TIdSocketHandle; AListenerThread: TIdThread;
AYarn: TIdYarn) : TIdIOHandler; override;
{ SSH server properties }
property AllowedSubsystems : TStringList read FAllowedSubsystems;
property EncryptionAlgorithms[Index : TSSHEncryptionAlgorithm] : boolean
read GetEncryptionAlgorithms write SetEncryptionAlgorithms;
property CompressionAlgorithms[Index : TSSHCompressionAlgorithm] : boolean
read GetCompressionAlgorithms write SetCompressionAlgorithms;
property MacAlgorithms[Index : TSSHMacAlgorithm] : boolean
read GetMACAlgorithms write SetMACAlgorithms;
property KexAlgorithms[Index : TSSHKexAlgorithm] : boolean
read GetKexAlgorithms write SetKexAlgorithms;
property PublicKeyAlgorithms[Index : TSSHPublicKeyAlgorithm] : boolean
read GetPublicKeyAlgorithms write SetPublicKeyAlgorithms;
published
{ SSH server properties }
property AuthenticationTypes : integer read FAuthenticationTypes
write FAuthenticationTypes default SSH_AUTH_TYPE_PASSWORD;
property CloseIfNoActiveTunnels : boolean read FCloseIfNoActiveTunnels
write FCloseIfNoActiveTunnels default true;
property ChangePwdPrompt : string read FChangePwdPrompt write FChangePwdPrompt;
property CompressionLevel : integer read FCompressionLevel
write FCompressionLevel default 9;
property ForceCompression : boolean read FForceCompression
write FForceCompression;
property KeyStorage: TElSSHCustomKeyStorage read FKeyStorage write
SetKeyStorage;
property SoftwareName : string read FSoftwareName write FSoftwareName;
{ SSH server events }
property OnAuthAttempt: TSSHAuthAttemptEvent read FOnAuthAttempt
write FOnAuthAttempt;
property OnAuthFailed: TSSHAuthenticationFailedEvent read FOnAuthFailed
write FOnAuthFailed;
property OnAuthBanner: TSSHAuthBannerEvent read FOnAuthBanner
write FOnAuthBanner;
property OnAuthPassword: TSSHAuthPasswordEvent read FOnAuthPassword
write FOnAuthPassword;
property OnAuthPasswordChange: TSSHAuthPasswordChangeEvent read
FOnAuthPasswordChange write FOnAuthPasswordChange;
property OnAuthPublicKey: TSSHAuthPublicKeyEvent read FOnAuthPublicKey
write FOnAuthPublicKey;
property OnAuthHostbased: TSSHAuthHostbasedEvent read FOnAuthHostbased
write FOnAuthHostbased;
property OnAuthKeyboard: TSSHAuthKeyboardEvent read FOnAuthKeyboard
write FOnAuthKeyboard;
property OnAuthKeyboardResponse: TSSHAuthKeyboardResponseEvent
read FOnAuthKeyboardResponse write FOnAuthKeyboardResponse;
property OnBeforeOpenSession : TSSHBeforeOpenSessionEvent
read FOnBeforeOpenSession write FOnBeforeOpenSession;
property OnBeforeOpenShell : TSSHBeforeOpenShellEvent read
FOnBeforeOpenShell write FOnBeforeOpenShell;
property OnBeforeOpenCommand : TSSHBeforeOpenCommandEvent read
FOnBeforeOpenCommand write FOnBeforeOpenCommand;
property OnBeforeOpenSubsystem: TSSHBeforeOpenSubsystemEvent
read FOnBeforeOpenSubsystem write FOnBeforeOpenSubsystem;
property OnBeforeOpenClientForwarding: TSSHBeforeOpenClientForwardingEvent
read FOnBeforeOpenClientForwarding write FOnBeforeOpenClientForwarding;
property OnBeforeOpenX11Forwarding: TSSHBeforeOpenX11ForwardingEvent
read FOnBeforeOpenX11Forwarding write FOnBeforeOpenX11Forwarding;
property OnCloseConnection: TSSHCloseConnectionEvent
read FOnCloseConnection write FOnCloseConnection;
property OnServerForwardingFailed: TSSHServerForwardingFailedEvent
read FOnServerForwardingFailed write FOnServerForwardingFailed;
property OnServerForwardingRequest: TSSHServerForwardingRequestEvent
read FOnServerForwardingRequest write FOnServerForwardingRequest;
property OnServerForwardingCancel : TSSHServerForwardingCancelEvent
read FOnServerForwardingCancel write FOnServerForwardingCancel;
property OnX11ForwardingRequest : TSSHX11ForwardingRequestEvent
read FOnX11ForwardingRequest write FOnX11ForwardingRequest;
property OnTerminalRequest : TSSHTerminalRequestEvent
read FOnTerminalRequest write FOnTerminalRequest;
property OnOpenConnection : TSSHOpenConnectionEvent
read FOnOpenConnection write FOnOpenConnection;
property OnOpenChannel : TSSHIdOpenChannelEvent read
FOnOpenChannel write FOnOpenChannel;
end;
TElIdSSHClientServerIOHandler = class(TIdIOHandlerStack)
private
FSSHServer : TElSSHServer;
FConnections : TList;
FErrorOccured : boolean;
FActive : boolean;
{ SSH server events }
FOnCloseConnection : TSSHCloseConnectionEvent;
FOnOpenShell: TSSHOpenShellEvent;
FOnOpenCommand: TSSHOpenCommandEvent;
FOnOpenSubsystem: TSSHOpenSubsystemEvent;
FOnOpenClientForwarding: TSSHOpenClientForwardingEvent;
FOnOpenServerForwarding: TSSHOpenServerForwardingEvent;
FOnOpenX11Forwarding: TSSHOpenX11ForwardingEvent;
FOnError: TSSHErrorEvent;
FOnOpenChannel : TSSHIdOpenChannelEvent;
function AddConnection(Connection : TElSSHTunnelConnection;
Server : TElSSHServer) : integer;
function GetActive: boolean;
function GetConnections(Index: integer): TElIdSSHConnection;
procedure DoReceive(Sender : TObject; Buffer : pointer; MaxSize : longint; out Written : longint);
procedure DoSend(Sender : TObject; Buffer : pointer; Size : longint);
{ SSH server events }
procedure DoCloseConnection(Sender : TObject);
procedure DoSSHError(Sender : TObject; ErrorCode : integer);
procedure DoOpenShell(Sender: TObject; Connection: TElSSHTunnelConnection);
procedure DoOpenCommand(Sender: TObject; Connection: TElSSHTunnelConnection;
const Command: string);
procedure DoOpenSubsystem(Sender: TObject; Connection: TElSSHTunnelConnection;
const Subsystem: string);
procedure DoOpenClientForwarding(Sender: TObject; Connection:
TElSSHTunnelConnection; const DestHost: string; DestPort: integer;
const SrcHost : string; SrcPort: integer);
procedure DoOpenServerForwarding(Sender: TObject;
Connection: TElSSHTunnelConnection);
procedure DoOpenX11Forwarding(Sender: TObject; Connection :
TElSSHTunnelConnection; const OrigIP : string; OrigPort: integer);
{ SSH server properties maping }
function GetAllowedSubsystems: TStringList;
function GetChangePwdPrompt: string;
function GetClientCloseReason: string;
function GetClientSoftwareName: string;
function GetUsername: string;
function GetAuthenticationTypes: integer;
function GetCloseIfNoActiveTunnels: boolean;
function GetCompressionAlgorithmCS: TSSHCompressionAlgorithm;
function GetCompressionAlgorithms(
Index: TSSHCompressionAlgorithm): boolean;
function GetCompressionAlgorithmSC: TSSHCompressionAlgorithm;
function GetCompressionLevel: integer;
function GetEncryptionAlgorithmCS: TSSHEncryptionAlgorithm;
function GetEncryptionAlgorithms(
Index: TSSHEncryptionAlgorithm): boolean;
function GetEncryptionAlgorithmSC: TSSHEncryptionAlgorithm;
function GetForceCompression: boolean;
function GetKexAlgorithm: TSSHKexAlgorithm;
function GetKexAlgorithms(Index: TSSHKexAlgorithm): boolean;
function GetKeyStorage: TElSSHCustomKeyStorage;
function GetMacAlgorithmCS: TSSHMacAlgorithm;
function GetMACAlgorithms(Index: TSSHMacAlgorithm): boolean;
function GetMacAlgorithmSC: TSSHMacAlgorithm;
function GetPublicKeyAlgorithm: TSSHPublicKeyAlgorithm;
function GetPublicKeyAlgorithms(
Index: TSSHPublicKeyAlgorithm): boolean;
function GetSoftwareName: string;
function GetOnAuthAttempt: TSSHAuthAttemptEvent;
function GetOnAuthBanner: TSSHAuthBannerEvent;
function GetOnAuthFailed: TSSHAuthenticationFailedEvent;
function GetOnAuthHostbased: TSSHAuthHostbasedEvent;
function GetOnAuthKeyboard: TSSHAuthKeyboardEvent;
function GetOnAuthKeyboardResponse: TSSHAuthKeyboardResponseEvent;
function GetOnAuthPassword: TSSHAuthPasswordEvent;
function GetOnAuthPasswordChange: TSSHAuthPasswordChangeEvent;
function GetOnAuthPublicKey: TSSHAuthPublicKeyEvent;
function GetOnBeforeOpenClientForwarding: TSSHBeforeOpenClientForwardingEvent;
function GetOnBeforeOpenCommand: TSSHBeforeOpenCommandEvent;
function GetOnBeforeOpenSession: TSSHBeforeOpenSessionEvent;
function GetOnBeforeOpenShell: TSSHBeforeOpenShellEvent;
function GetOnBeforeOpenSubsystem: TSSHBeforeOpenSubsystemEvent;
function GetOnBeforeOpenX11Forwarding: TSSHBeforeOpenX11ForwardingEvent;
function GetOnServerForwardingCancel: TSSHServerForwardingCancelEvent;
function GetOnServerForwardingFailed: TSSHServerForwardingFailedEvent;
function GetOnServerForwardingRequest: TSSHServerForwardingRequestEvent;
function GetOnTerminalRequest: TSSHTerminalRequestEvent;
function GetOnX11ForwardingRequest: TSSHX11ForwardingRequestEvent;
procedure SetOnAuthAttempt(const Value: TSSHAuthAttemptEvent);
procedure SetOnAuthBanner(const Value: TSSHAuthBannerEvent);
procedure SetOnAuthFailed(const Value: TSSHAuthenticationFailedEvent);
procedure SetOnAuthHostbased(const Value: TSSHAuthHostbasedEvent);
procedure SetOnAuthKeyboard(const Value: TSSHAuthKeyboardEvent);
procedure SetOnAuthKeyboardResponse(
const Value: TSSHAuthKeyboardResponseEvent);
procedure SetOnAuthPassword(const Value: TSSHAuthPasswordEvent);
procedure SetOnAuthPasswordChange(
const Value: TSSHAuthPasswordChangeEvent);
procedure SetOnAuthPublicKey(const Value: TSSHAuthPublicKeyEvent);
procedure SetOnBeforeOpenClientForwarding(
const Value: TSSHBeforeOpenClientForwardingEvent);
procedure SetOnBeforeOpenCommand(
const Value: TSSHBeforeOpenCommandEvent);
procedure SetOnBeforeOpenSession(
const Value: TSSHBeforeOpenSessionEvent);
procedure SetOnBeforeOpenShell(const Value: TSSHBeforeOpenShellEvent);
procedure SetOnBeforeOpenSubsystem(
const Value: TSSHBeforeOpenSubsystemEvent);
procedure SetOnBeforeOpenX11Forwarding(
const Value: TSSHBeforeOpenX11ForwardingEvent);
procedure SetOnOpenSession(const Value: TSSHOpenSessionEvent);
procedure SetOnServerForwardingCancel(
const Value: TSSHServerForwardingCancelEvent);
procedure SetOnServerForwardingFailed(
const Value: TSSHServerForwardingFailedEvent);
procedure SetOnServerForwardingRequest(
const Value: TSSHServerForwardingRequestEvent);
procedure SetOnTerminalRequest(const Value: TSSHTerminalRequestEvent);
procedure SetOnX11ForwardingRequest(
const Value: TSSHX11ForwardingRequestEvent);
procedure SetAuthenticationTypes(const Value: integer);
procedure SetChangePwdPrompt(const Value: string);
procedure SetCloseIfNoActiveTunnels(const Value: boolean);
procedure SetCompressionAlgorithms(Index: TSSHCompressionAlgorithm;
const Value: boolean);
procedure SetCompressionLevel(const Value: integer);
procedure SetEncryptionAlgorithms(Index: TSSHEncryptionAlgorithm;
const Value: boolean);
procedure SetForceCompression(const Value: boolean);
procedure SetKexAlgorithms(Index: TSSHKexAlgorithm;
const Value: boolean);
procedure SetKeyStorage(const Value: TElSSHCustomKeyStorage);
procedure SetMACAlgorithms(Index: TSSHMacAlgorithm;
const Value: boolean);
procedure SetPublicKeyAlgorithms(Index: TSSHPublicKeyAlgorithm;
const Value: boolean);
procedure SetSoftwareName(const Value: string);
function GetOnOpenSession: TSSHOpenSessionEvent;
function GetConnectionCount: integer;
function GetOnOpenConnection: TSSHOpenConnectionEvent;
procedure SetOnOpenConnection(const Value: TSSHOpenConnectionEvent);
protected
procedure InitComponent; override;
public
destructor Destroy; override;
property Connections[Index : integer] : TElIdSSHConnection read GetConnections;
property ConnectionCount : integer read GetConnectionCount;
{ IOHandler methods }
procedure AfterAccept; override;
//procedure CheckForDisconnect(ARaiseExceptionIfDisconnected: Boolean = True;
// AIgnoreBuffer: Boolean = False); override;
//procedure CheckForDataOnSource(ATimeout: Integer = 0); override;
procedure Close; override;
//function Readable(AMSec: Integer = IdTimeoutDefault): Boolean; override;
function ReadFromSource(ARaiseExceptionIfDisconnected: Boolean = True;
ATimeout: Integer = IdTimeoutDefault; ARaiseExceptionOnTimeout:
Boolean = True): Integer; override;
{$ifndef INDY1011}
procedure WriteDirect(ABuffer: TIdBytes); override;
{$else}
procedure WriteDirect(var ABuffer: TIdBytes); override;
{$endif}
{ SSH server methods }
procedure DataAvailable;
procedure OpenServerForwarding(const Address: string; Port : integer;
const SrcHost: string; SrcPort: integer; Data: pointer = nil);
{ SSH server properties }
property Active : boolean read GetActive;
property ClientCloseReason : string read GetClientCloseReason;
property ClientSoftwareName : string read GetClientSoftwareName;
property Username: string read GetUsername;
property AllowedSubsystems : TStringList read GetAllowedSubsystems;
property KexAlgorithm : TSSHKexAlgorithm read GetKexAlgorithm;
property PublicKeyAlgorithm : TSSHPublicKeyAlgorithm read GetPublicKeyAlgorithm;
property EncryptionAlgorithms[Index : TSSHEncryptionAlgorithm] : boolean
read GetEncryptionAlgorithms write SetEncryptionAlgorithms;
property CompressionAlgorithms[Index : TSSHCompressionAlgorithm] : boolean
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -