delphimvcframework/samples/sslserver/SSLSample.dpr

89 lines
2.4 KiB
ObjectPascal
Raw Normal View History

// JCL_DEBUG_EXPERT_GENERATEJDBG OFF
// JCL_DEBUG_EXPERT_INSERTJDBG OFF
// JCL_DEBUG_EXPERT_DELETEMAPFILE OFF
2014-03-03 14:45:55 +01:00
program SSLSample;
{$APPTYPE CONSOLE}
uses
System.SysUtils,
IdHTTPWebBrokerBridge,
IdSSLOpenSSL,
System.IOUtils,
2014-03-03 14:45:55 +01:00
Web.WebReq,
Web.HTTPApp,
Web.WebBroker,
WebModuleUnit1 in 'WebModuleUnit1.pas' {WebModule1: TWebModule} ,
MyControllerU in 'MyControllerU.pas',
MyObjectsU in 'MyObjectsU.pas';
{$R *.res}
type
TGetSSLPassword = class
procedure OnGetSSLPassword(var APassword: {$IF CompilerVersion < 27}AnsiString{$ELSE}string{$ENDIF});
2014-03-03 14:45:55 +01:00
end;
procedure TGetSSLPassword.OnGetSSLPassword(var APassword: {$IF CompilerVersion < 27}AnsiString{$ELSE}string{$ENDIF});
2014-03-03 14:45:55 +01:00
begin
APassword := '';
end;
procedure RunServer(APort: Integer);
var
LServer: TIdHTTPWebBrokerBridge;
LGetSSLPassword: TGetSSLPassword;
LIOHandleSSL: TIdServerIOHandlerSSLOpenSSL;
begin
Writeln(Format('Starting DMVCFramework HTTPS Server or port %d', [APort]));
LGetSSLPassword := nil;
LServer := TIdHTTPWebBrokerBridge.Create(nil);
try
LGetSSLPassword := TGetSSLPassword.Create;
LIOHandleSSL := TIdServerIOHandlerSSLOpenSSL.Create(LServer);
LIOHandleSSL.SSLOptions.CertFile := 'cacert.pem';
2014-03-03 14:45:55 +01:00
LIOHandleSSL.SSLOptions.RootCertFile := '';
LIOHandleSSL.SSLOptions.KeyFile := 'privkey.pem';
2014-03-03 14:45:55 +01:00
LIOHandleSSL.OnGetPassword := LGetSSLPassword.OnGetSSLPassword;
LServer.IOHandler := LIOHandleSSL;
LServer.DefaultPort := APort;
LServer.Active := True;
Writeln('Press RETURN to stop the server');
ReadLn;
2014-03-03 14:45:55 +01:00
finally
LServer.Free;
LGetSSLPassword.Free;
end;
end;
const
OPENSSL_LIBS: array of string = ['libeay32.dll', 'ssleay32.dll'];
procedure CheckOPENSSLLibs;
var
lOpenSSLLib: String;
begin
// Just a check for
for lOpenSSLLib in OPENSSL_LIBS do
begin
Write('Checking ', lOpenSSLLib, '...');
if not TFile.Exists(lOpenSSLLib) then
raise Exception.CreateFmt('Required OPENSSL library not found in the exe folder: %s' + sLineBreak +
'Download INDY compatible OpenSSL Libraries from http://indy.fulgan.com/SSL/', [lOpenSSLLib]);
Writeln('OK');
end;
end;
2014-03-03 14:45:55 +01:00
begin
CheckOPENSSLLibs;
2014-03-03 14:45:55 +01:00
try
if WebRequestHandler <> nil then
WebRequestHandler.WebModuleClass := WebModuleClass;
RunServer(443 { standard https port } );
2014-03-03 14:45:55 +01:00
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end
end.