
我設定了以下管道。這使我能夠查看透過管道傳遞的原始 HTTP 請求和回應。
筆記:我在下面使用 BSD nc
(這是 macOS 和 AmazonLinux 附帶的),但我也嘗試過 nmap ncat
,它可以採用相同的參數,並且socat
1.7.3.2 的行為是相同的。
socat
採用不同的參數,但管道是相同的。我將僅添加socat
一行,因為解釋是相同的。
單線:
mkfifo res; nc -kl 8888 < res | tee /dev/stderr | nc google.com 80 | tee res
或者
mkfifo res; socat tcp-listen:8888,reuseaddr,fork - < res | tee /dev/stderr | socat - tcp:google.com:80 | tee res
解釋:
mkfifo res; \ <-- Make a named pipe file with the name 'res'.
nc -kl 8888 < res \ <-- Open a socket listening on all interfaces at TCP port 8888. Use the 'res' pipe as input.
| tee /dev/stderr \ <-- Copy the request to stderr, so it's emitted by the terminal. stdout will be used as input for the next netcat.
| nc google.com 80 \ <-- Resolve google.com, connect to it on TCP port 80, and send what was received on stdin. The response goes to stdout.
| tee res <-- Copy the response to the 'res' pipe.
我在一個終端機中運行這個管道(管道終端),並用於curl
在另一個終端(捲曲終端):
curl --resolve google.com:8888:127.0.0.1 http://google.com:8888
在 macOS 和 Linux 上:
這捲曲終端正確接收並顯示回應。這管道終端顯示請求和回應。
在 macOS 上(macOS Mojave 10.14.3):
管道繼續運作。我可以使用 發送另一個請求curl
,並且可以在管道終端和響應兩個都終端。
在 Linux 上 (AmazonLinux 4.1.13):
管道繼續運作。curl
如果我發送另一個請求,則會掛起,並且我在任何地方都看不到該請求或回應。
版本:
蘋果系統
$ uname -a
Darwin ch007837.na.webmd.net 18.2.0 Darwin Kernel Version 18.2.0: Thu Dec 20 20:46:53 PST 2018; root:xnu-4903.241.1~1/RELEASE_X86_64 x86_64 i386 MacBookPro15,1 Darwin
$ curl --version
curl 7.64.1 (x86_64-apple-darwin18.2.0) libcurl/7.64.1 SecureTransport zlib/1.2.11
Release-Date: 2019-03-27
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IPv6 Largefile libz NTLM NTLM_WB SSL UnixSockets
$ bash --version | head -1
GNU bash, version 5.0.3(1)-release (x86_64-apple-darwin18.2.0)
$ ncat --version
Ncat: Version 7.70 ( https://nmap.org/ncat )
我不確定,nc
因為它沒有--version
標誌,但它隨作業系統一起提供。二進位檔案中有這個字串:
$ strings "$(which nc)" | tail -1
@(#)PROGRAM:nc PROJECT:netcat-42.200.1
Linux
$ uname -a
Linux ip-10-200-38-72 4.1.13-19.31.amzn1.x86_64 #1 SMP Wed Jan 20 00:25:47 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
$ curl --version
curl 7.40.0 (x86_64-redhat-linux-gnu) libcurl/7.40.0 NSS/3.19.1 Basic ECC zlib/1.2.8 libidn/1.18 libssh2/1.4.2
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz UnixSockets
$ bash --version | head -1
GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)
$ ncat --version
Ncat: Version 5.51 ( http://nmap.org/ncat )
$ yum list nc | tail -1 | sed 's/ \+/ /g'
nc.x86_64 1.84-24.8.amzn1 @amzn-main