简体   繁体   English

indy ssl delphi 服务器

[英]indy ssl delphi server

I use Delphi 10.1 Berlin and Indy 10.6.2, 32bit on a Windows Server 2012.我在 Windows Server 2012 上使用 Delphi 10.1 Berlin 和 Indy 10.6.2,32 位。

I implemented a server based on TIdHTTPServer .我实现了一个基于TIdHTTPServer的服务器。 Works like a charm for many years.多年来一直像魅力一样工作。

Now my customer wants the traffic secured.现在我的客户希望流量得到保障。 SSL is an area I have little knowledge of. SSL 是一个我知之甚少的领域。

There are several helpful pointers on the web that have helped me make CA certificate and key files with OpenSSL.网络上有几个有用的指针帮助我使用 OpenSSL 制作 CA 证书和密钥文件。 And from several examples I've put together the code below, using TIdServerIOHandlerSSLOpenSSL .从几个示例中,我使用TIdServerIOHandlerSSLOpenSSL将下面的代码放在一起。 The cert/key files are in the exe directory and so are the OpenSSL dlls ssleay32 and libeay32.证书/密钥文件位于 exe 目录中,OpenSSL dll ssleay32 和 libeay32 也是如此。

The server responds to http://localhost:8080 but there is no response when addressing it via https://localhost .服务器响应http://localhost:8080但通过https://localhost寻址时没有响应。 It behaves as if the TIdServerIOHandlerSSLOpenSSL is not there at all.它的行为就像TIdServerIOHandlerSSLOpenSSL根本不存在一样。 (The IOHandler does read the cert/key files and it complains when I remove the OpenSSL DLLs). (IOHandler 确实读取了证书/密钥文件,并且在我删除 OpenSSL DLL 时它会抱怨)。 It is as if I've forgotten to throw a switch somewhere.就好像我忘记在某处扔开关一样。

The analysis of Windows Network Diagnostics (in IEdge) is 'The device or resource (localhost) is not set up to accept connections on port "https".' Windows 网络诊断(在 IEdge 中)的分析是“设备或资源 (localhost) 未设置为接受端口“https”上的连接。

I tried to log a message via the OnConnect event, but that stage is never reached with HTTPS.我试图通过OnConnect事件记录一条消息,但 HTTPS 从未达到该阶段。

I have run out of ideas, and can not find relevant suggestions on the web.我的想法已经用完了,在网上也找不到相关的建议。

Here is my code (the components are all declared in code):这是我的代码(组件都在代码中声明):

procedure TServerForm.FormCreate(Sender: TObject);
var
  ServerSSLIOHandler: TIdServerIOHandlerSSLOpenSSL;
  rootdir           : string;
begin
  inherited;

  rootdir:=ExtractFilePath(Application.ExeName);

  ServerSSLIOHandler:=TIdServerIOHandlerSSLOpenSSL.Create(self);
  ServerSSLIOhandler.SSLOptions.RootCertFile:=rootdir+'ca.cert.pem';
  ServerSSLIOhandler.SSLOptions.CertFile:=rootdir+'localhost.cert.pem';
  ServerSSLIOhandler.SSLOptions.KeyFile:=rootdir+'localhost.key.pem';
  ServerSSLIOhandler.SSLOptions.Method:=sslvSSLv23;
  ServerSSLIOhandler.SSLOptions.Mode:=sslmServer;
  ServerSSLIOhandler.OnGetPassword:=NIL;
  ServerSSLIOhandler.OnVerifyPeer:=OnVerifyPeer;

  HTTPServer:=TIdHTTPServer.Create(self);
  HTTPServer.IOhandler:=ServerSSLIOHandler;
  HTTPserver.Bindings.Add.Port:=443;
  HTTPserver.Bindings.Add.Port:=8080;
  HTTPServer.Active:=True;
  HTTPServer.AutoStartSession:=True;
  HTTPServer.SessionTimeOut:=1200000;
  HTTPserver.OnQuerySSLPort:=OnQuerySSLPort;
  HTTPServer.OnCommandGet:=HTTPServerCommandGet;
  ...
end;

procedure TServerForm.OnQuerySSLPort(APort: Word; var VUseSSL: Boolean);
// This will not be called when the request is a HTTPS request
// It facilitates the use of the server for testing via HTTP://localhost:8080 (i.e. without SSL)
begin
  VUseSSL := (APort<>8080);
end;

function TServerForm.OnVerifyPeer(Certificate: TIdX509; AOk: Boolean; ADepth, AError: Integer): Boolean;
begin
  result:=AOk;
end;

Thank you.谢谢你。 Remy's remark about OnConnect and his suggestion to use netstat did the trick. Remy 关于 OnConnect 的评论和他使用netstat建议起到了作用。 Ie it lead me to discover that the problem was elsewhere.即它让我发现问题出在别处。 In the past I had to move away from port 80 because it became in use by a Windows service.过去我不得不离开端口 80,因为它被 Windows 服务使用。 From then on I specified a port number (8080) in an ini file and acted as follows.从那时起,我在 ini 文件中指定了一个端口号 (8080) 并执行如下操作。 Code:代码:

prt:=parameters.ReadInteger('settings','port',80);
if prt<>HTTPserver.DefaultPort
   then begin HTTPserver.Active:=false;
              HTTPserver.Bindings.Clear;
              HTTPserver.DefaultPort:=prt;
              HTTPserver.Active:=true;
        end;

Since this piece of code was still there, obviously only the specified port (8080) was active.由于这段代码仍然存在,显然只有指定的端口(8080)处于活动状态。 netstat revealed that immediately. netstat立即透露了这一点。 Will you believe that I am very happy with your quick response!您会相信我对您的快速回复感到非常满意吗!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM