如何让 openssl 在监听时直接从命令行响应 http/s

如何让 openssl 在监听时直接从命令行响应 http/s

如何让 openssl s_server 直接从命令行或服务器本身回复每个 http(s) 请求(服务器使用 centOS)?那可能吗?

从 openssl s_server 的 -help 命令中我看到有一个 -HTTP 标志应该用于:

 -WWW          - Respond to a 'GET /<path> HTTP/1.0' with file./<path> 
 -HTTP         - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>
                  with the assumption it contains a complete HTTP response.

这可以解决我的问题吗?如果是这样,并且由于我找不到任何如何使用此标志的示例,我会很高兴知道如何准确使用它。

我尝试运行的命令很简单:

openssl s_server -key key.pem -cert cert.pem -msg

提前致谢!

答案1

使用-WWW-HTTPs_server充当使用当前目录中的文件的静态内容 HTTPS 服务器。这是我用于演示的完整设置。

$ openssl req -x509 -nodes -newkey rsa -keyout key.pem -out cert.pem -subj /CN=localhost
$ echo 'hello, world.' >index.txt
$ openssl s_server -key key.pem -cert cert.pem -WWW
Using default temp DH parameters
Using default temp ECDH parameters
ACCEPT

s_server现在正在端口 4433 上等待 HTTPS 请求。您可以从另一个 shell 向s_server使用发出请求curl

$ curl -kv https://localhost:4433/index.txt
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 4433 (#0)
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server key exchange (12):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* Server certificate:
*    subject: CN=localhost
*    start date: 2015-06-01 15:29:02 GMT
*    expire date: 2015-07-01 15:29:02 GMT
*    issuer: CN=localhost
*    SSL certificate verify result: self signed certificate (18), continuing anyway.
> GET /index.txt HTTP/1.1
> User-Agent: curl/7.38.0
> Host: localhost:4433
> Accept: */*
> 
* HTTP 1.0, assume close after body
< HTTP/1.0 200 ok
< Content-type: text/plain
< 
hello, world.
* Closing connection 0
* SSLv3, TLS alert, Client hello (1):

$ curl -k https://localhost:4433/not-existence
Error opening 'not-existence'
140226499298960:error:02001002:system library:fopen:No such file or directory:bss_file.c:169:fopen('not-existence','r')
140226499298960:error:2006D080:BIO routines:BIO_new_file:no such file:bss_file.c:172:

在每个请求上s_server打印请求的路径并再次等待下一个请求。

FILE:index.txt
ACCEPT

如果您想根据请求运行脚本(如 CGI 那样),您可能需要使用其他工具,例如索卡特

$ echo 'echo "Your request is $(head -1)"' > server.sh
$ socat openssl-listen:4433,cert=cert.pem,key=key.pem,verify=0,reuseaddr,fork exec:"bash server.sh"

结果是:

$ curl -k https://localhost:4433/index.txt
Your request is GET /index.txt HTTP/1.1

相关内容