是否可以使用 UPnP 和 SOAP 映射端口,以绑定到我的 http 服务器正在侦听的本地端口?

发布于 2024-10-05 16:32:32 字数 431 浏览 4 评论 0原文

比如说,我位于 192.168.0.1 的路由器后面,它通向外部 IP,即 192.168.1.64(DSL 调制解调器),通向公共互联网地址。

是否可以使用 uPnP,通过 SOAP 请求网关,将本地 http 服务器端口映射到该公共互联网地址的任意端口?

抱歉,我在这件事上完全是新手。我正在网关上的端口 80 上使用 Putty 进行测试,使用 xml 发出 http。

POST /upnp/control3 HTTP/1.1 缓存控制:无缓存 连接:关闭 编译指示:无缓存 内容类型:text/xml;字符集=“utf-8” 用户代理:Microsoft-Windows/6.1 UPnP/1.0 SOAPAction: "urn:schemas-upnp-org:service:WANIPConnection:1#AddPortMapping" ... ..

谢谢。

Say, I'm behind a router at 192.168.0.1, which leads to external ip which is 192.168.1.64 (a dsl modem) which leads to a public internet address.

Is it possible using uPnP, via SOAP requests to gateway, to map my http server port local to an arbitrary port at that public internet address?

Sorry, I'm totally newbie on this matters. I'm testing using Putty on port 80 on gateway, issueing http with xml.

POST /upnp/control3 HTTP/1.1
Cache-Control: no-cache
Connection: Close
Pragma: no-cache
Content-Type: text/xml; charset="utf-8"
User-Agent: Microsoft-Windows/6.1 UPnP/1.0
SOAPAction: "urn:schemas-upnp-org:service:WANIPConnection:1#AddPortMapping"
...

..

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨落星ぅ辰 2024-10-12 16:32:32

您可以在名为“Puncher”的 Python 模块中看到一个很好的示例,它是 Coherence DNLA/UPnP 框架的一部分(在防火墙中“打孔”,duh!:o),它可以创建外部端口的端口映射(例如端口 8080)到您选择的内部端口:

https://gitlab .digitalcourage.de/coherence/puncher/-/blob/develop/puncher/base.py

Coherence 模块也可以作为 python-coherence 包在 Debian 上使用。如果您使用 ActiveState Python,在 Windows 上也是如此。

我假设您希望在您正在创建的内容中自动/以编程方式执行此操作,这就是为什么我向您指出上面的

Python 中的 Coherence DNLA/UPnP 框架

如果您只是想一次性手动执行此操作,可以使用一些优秀的命令行和 GUI 工具来创建 UPnP 端口映射。在 Mac 上,可以从创建者的 Port Map 和 TCMPortMapper 获取手动 GUI 工具亚伦理编辑。在 Windows 上,手动 GUI 工具位于 Deusty:适用于 Windows 的 UPnP 和 NAT-PMP PortMapper

我强烈建议您使用 Python 中的 Coherence,这会是一次有趣的编程体验。或者,许多使用网络的不同程序将具有 UPnP 模块来打开端口并将它们转发/映射到本地计算机。例如,μTorrent 就有一个,libtorrent/rtorrent 也有,其他许多也有。

是 C 和 Perl 语言),另请参阅以下具有 UPnP 客户端实现的库、示例和工具,以编程方式转发/映射路由器上的端口:

注意第二个链接,upnp.c 中trunk/libtransmission – Transmission,是来自流行的 BitTorrent 客户端“Transmission”的代码,它使用 miniupnp 库(第一个链接)。

我希望这已经回答了您的问题。祝你无论做什么都好运。

编辑: @kaneda:针对您的评论,如果我理解正确的话,您位于两个 NAT 后面,一个由您的 DSL 调制解调器负责,另一个由您的无线路由器负责。简单的 UPnP 在这里对您没有帮助,您需要能够在 DSL 调制解调器和路由器上进行 UPnP 端口映射(这可能是不可能的,因为您的路由器可能不会让 UPnP 请求离开其 WAN 端口),或者设置您的DSL 调制解调器或您的路由器来桥接而不是 NAT,这样您就只位于一个 NAT 后面。

编辑2: @kaneda:围绕双重 NAT 的另一种可能的方法是用 Linux 盒子替换路由器(例如使用 OpenWRT 或类似的),并在那里运行你的 upnp 代码来转发 DSL调制解调器端口,然后还在路由器后面的主机上运行基于 miniupnp 的代码以进一步打开路由器。然而,此时,由于您可以控制路由器,您不妨创建一个静态端口映射,并将其将所有 UPNP 协议请求传递到 DSL 调制解调器(通过告诉 iptables 有选择地转发/NAT 这些数据包回来/继续,这超出了本答案的范围)。

You can see a good example as part of the Coherence DNLA/UPnP framework in a Python module called "Puncher" ("punches" holes in your firewall, duh! :o) which can create port mappings of external ports (like port 8080) to internal ports of your choosing:

https://gitlab.digitalcourage.de/coherence/puncher/-/blob/develop/puncher/base.py

The Coherence modules are also available on Debian as the python-coherence package. On Windows as well if you use ActiveState Python.

I am assuming you want to do this automatically/programmatically as part of something you are creating, which is why I pointed you to the above

Coherence DNLA/UPnP framework in Python

If you just want to do this as a one-off manually, there are some excellent command-line and GUI tools to create UPnP port mappings. On Macs, a manual GUI tool is available at Port Map and TCMPortMapper from the creators of SubEtha Edit. On Windows, a manual GUI tool is available at Deusty: UPnP and NAT-PMP PortMapper for Windows.

I would strongly recommend you play with Coherence in Python, it can be a fun programming experience. Alternatively, many different programs that use the network will have UPnP modules to open up ports and forward/map them to your local machine. For example, µTorrent has one, as does libtorrent/rtorrent, as do many others.

See also the following libraries, examples, and tools with UPnP client implementations to programmatically forward/map ports on your router, if you are averse to Python (these are in C and Perl):

Note that the second link, upnp.c in trunk/libtransmission – Transmission, is code from the popular BitTorrent client "Transmission" which uses the miniupnp library (first link).

I hope this has answered your question. Best of luck with whatever you're doing.

EDIT: @kaneda: In response to your comment, you are behind two NATs, one by your DSL modem and one by your wireless router, if I understand you correctly. Simple UPnP will not help you here, you need to either be able to UPnP a port mapping on both the DSL modem and the router (probably impossible since your router probably won't let UPnP requests leave its WAN port), OR set either your DSL modem or your router to bridge rather than NAT, so that you are only behind one NAT.

EDIT 2: @kaneda: Another possible way around the double NATting is to replace your router with a Linux box (such as by using OpenWRT or similar) and to run your upnp code on there to forward the DSL modem port, then also run the miniupnp-based code on your host behind the router to further open the router. At that point, however, since you have control of the router you might as well just create a static port mapping and have it pass-through all UPNP protocol requests to the DSL modem (by telling iptables to selectively forward/NAT those packets back/forth, which is beyond the scope of this answer).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文