Linux 使用netcat(nc)作为HTTP代理服务器并监控
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4053876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using netcat (nc) as an HTTP proxy server and monitor
提问by Landon Kuhn
Is it possible to use the Unix netcat (nc) program to create a TCP proxy server and monitor? I would like all TCP traffic to be passed across the pipe, as well as sent to stdout for monitoring. Note this will be used for monitoring HTTP traffic between a hardware device and an HTTP server.
是否可以使用Unix netcat(nc)程序创建TCP代理服务器并进行监控?我希望所有 TCP 流量都通过管道传递,并发送到标准输出进行监控。请注意,这将用于监视硬件设备和 HTTP 服务器之间的 HTTP 流量。
回答by Jé Queue
Sure, you canuse it or a faucet|hose
pair, but why do that when you can have a minimal Apache instance do the exact same thing and provide a more feature-full set of analysis?
当然,您可以使用它或一faucet|hose
对,但是当您可以使用最小的 Apache 实例做完全相同的事情并提供更多功能完整的分析集时,为什么要这样做呢?
回答by Spacedman
Not netcat on its own, since it would have to interpret the HTTP request and pass it on. For example, an HTTP request through a proxy starts with:
不是 netcat 本身,因为它必须解释 HTTP 请求并将其传递。例如,通过代理的 HTTP 请求以以下开头:
GET http://www.example.org/HTTP/1.1
获取http://www.example.org/HTTP/1.1
which your proxy then has to go, 'okay, I gotta connect to example.org and GET /'.
然后你的代理必须去,'好吧,我必须连接到example.org和GET /'。
Now this could maybe be done by piping the nc output into a script which parses the HTTP req and then calls 'wget' to get the page, then slurp that back through netcat... oh heck, why?
现在这可以通过将 nc 输出传输到一个脚本中来完成,该脚本解析 HTTP 请求,然后调用 'wget' 来获取页面,然后通过 netcat 将其返回......哦,哎呀,为什么?
Apache, or squid can probably do the job.
Apache 或鱿鱼可能可以完成这项工作。
回答by Tom Anderson
Yeah, should be possible.
是的,应该是可以的。
When i [asked about writing a web server in bash1on a newsgroup, i ended up with two decent ideas. One was to use xinetd as the actual server, and have it run a shell script for each connection; in your case, the script could then use tee and nc to forward and log the connection (with some file descriptor trickery to get a tee on each stream, i think). The other was to use socat, which effectively lets you write a fully operational server, with listening sockets and handler subprocesses, in bash; again, you would want tee and netcat to do the logging and proxying.
当我 [询问在新闻组上使用bash 1编写 Web 服务器的问题时,我最终得到了两个不错的想法。一种是使用 xinetd 作为实际服务器,并让它为每个连接运行一个 shell 脚本;在你的情况下,脚本然后可以使用 tee 和 nc 来转发和记录连接(我认为使用一些文件描述符技巧来在每个流上获取一个 tee)。另一个是使用 socat,它可以有效地让您在 bash 中编写一个完全可操作的服务器,具有侦听套接字和处理程序子进程;同样,您希望 tee 和 netcat 进行日志记录和代理。
If you want a proper proxy server, than as @Spacedman says, you'd need to interpret the request line, but that's easy enough - read the first line, apply cut -d ' ' -f 2
to get the URL, some sed or shell string operators to pull out the domain and port, and continue. If you know upfront that all traffic is going to one endpoint, though, then you can hardwire it.
如果你想要一个合适的代理服务器,就像@Spacedman 所说的那样,你需要解释请求行,但这很容易 - 阅读第一行,申请cut -d ' ' -f 2
获取 URL,一些 sed 或 shell 字符串运算符来提取域和端口,然后继续。但是,如果您预先知道所有流量都将流向一个端点,那么您可以对其进行硬接线。
回答by symcbean
It'd be tricky to set up so it worked properly. A better solution would be to use a proper proxy (e.g. squid) or just sniff the traffic (wireshark, pastmon).
设置起来很棘手,所以它可以正常工作。更好的解决方案是使用适当的代理(例如鱿鱼)或仅嗅探流量(wireshark、pastmon)。
回答by Antoine Martin
Just had the need yesterday. You can find the answer here (french) : http://www.linux-france.org/~mdecore/linux/doc/memo2/node168.html
昨天刚需要。你可以在这里找到答案(法语):http: //www.linux-france.org/~mdecore/linux/doc/memo2/node168.html
mknod backpipe p
nc -l -p 80 < backpipe | tee -a in | nc localhost 8080 | tee -a out.html > backpipe
This listens on port 80 and redirect on port 8080. Incoming traffic will be present in the in
file, outgoing traffic in the out.html
file.The named pipe is needed for the connection to be bi-directional.
这在端口 80 上侦听并在端口 8080 上重定向。传入流量将出现在in
文件中,传出流量将出现在文件中。out.html
命名管道是双向连接所必需的。