自动启用从 Internet 访问端口 4900 的方法

发布于 2024-12-21 07:51:54 字数 140 浏览 4 评论 0原文

我正在编写一个在端口 4900 上运行的自定义 p2p 程序。在某些情况下,当用户位于路由器后面时,无法从互联网访问此端口。

是否有一种自动方式可以从互联网访问该端口。我不太确定其他 p2p 应用程序是如何工作的。

有人能解释一下吗?

I am writing a custom p2p program that runs on port 4900. In some cases when the person is behind a router, this port is not accessible from the internet.

Is there an automatic way of enabling the access to the port from the internet. I am not really sure of how other p2p applications work.

Can anyone please throw some light on this?

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

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

发布评论

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

评论(6

姜生凉生 2024-12-28 07:51:54

简而言之,P2P 连接。假设我们在这里讨论的是 UDP。经过一些调整,以下步骤也可以应用于 TCP。

  1. 枚举所有本地 IP 地址(通常只有 1 个)。为每个具有 IP 地址的适配器在给定端口号**上创建 UDP 套接字。

  2. 对于步骤 1 中创建的每个套接字,请使用同一套接字联系 STUN 或 TURN 服务器,以发现您的外部 IP 地址并发现内部端口号映射到 NAT 外部的内容(并不总是相同的端口值) 。也就是说,您的本地地址 192.168.1.2:4900 对于外界来说可能是 128.11.12.13:8888。当使用相同的本地端口到其他 IP 地址时,某些 NAT 并不总是使用相同的端口映射。 TURN 还将为您提供一个“中继地址”。如果路由器支持该协议,您还可以使用 UPNP 直接从路由器获取端口映射地址。

  3. 通过集合服务(SIP、XMPP、即时消息、Web 服务、电子邮件、带字符串的杯子),将您的地址候选列表发布到服务或向其他客户端发送通知,说:“嘿,我想与你联系”。此消息包括在步骤 1 和 2 中收集的所有“候选地址”(IP 和端口对)。

  4. 远程客户端在收到连接邀请后,也会执行上述步骤 1 和 2。然后通过他收到邀请者候选人名单的同一渠道发回他的候选人名单。

  5. 打孔步骤。两个客户端开始通过 UDP 向对方的候选地址发送测试消息,并在各自的一端侦听相同的消息。每当收到消息时,请回复消息来源的地址。最终,客户端会发现他们有一对地址,他们也可以可靠地发送数据报。通常,一个端点最终决定与哪个地址对(套接字)进行通信,并且协议有助于该端点告诉另一个端点这一决定。

**- 通常最好不要依赖 P2P 客户端的众所周知的端口。因为同一 NAT 或防火墙后面的两个客户端不可能同时使用您的软件。

以下是一些需要探索的技术的快速总结。

STUN - 是一个简单的服务器和协议,供 NAT/路由后面的客户端发现其外部信息IP和端口映射是。

TURN 是 STUN 的扩展,但支持防火墙和 NAT 阻止直接连接的 P2P 连接场景的中继连接。

ICE 是一组使用 STUN 和 TURN 设置 P2P 连接的步骤。 ICE 是上述步骤 1-5 的正式协议。 ICE 上的两组优秀幻灯片是 这里此处

WebRTC 是 ICE 标准的变体,也是使用 STUN 和 TURN 进行 P2P 会话的参考库。

UPNP + Internet 网关设备协议 - 某些路由器支持此协议,以便主机自动获取端口映射。

libnice 是一个用于 Linux 的开源 C 库(并且可以在 Windows 上运行),它实现了 ICE。

PJNATHPJSIP 编码库套件。它是 ICE 堆栈(C 代码)的良好实现,并且已被移植到很多平台。 (Windows、Linux、Mac、iOS、Symbian 以及即将推出的 Android)。

最后,我有一个明目张胆的插件供您使用我的 STUN 服务器代码库

P2P connectivity in a nutshell. Assume we're talking about UDP here. The steps below can also be applied to TCP with some adjustments.

  1. Enumerate all your local IP addresses (usually only 1). Create a UDP socket on a given port number** for each adapter with an IP address.

  2. For each socket created in step 1, contact a STUN or TURN server with that same socket to discover your external IP address and to discover what the internal port number maps to outside of the NAT (it's not always the same port value). That is, your local address 192.168.1.2:4900 might be 128.11.12.13:8888 to the outside world. And some NATs don't always use the same port mapping when using the same local port to other IP addresses. TURN will also provide you a "relay address". You can also use UPNP to get a port mapped address directly from your router, if it supports that protocol.

  3. Through a rendezvous service (SIP, XMPP, instant message, web service, email, cups with strings), publish your address candidate list to a service or send a notification to the other client that says, "hey, I want to connect with you". This message includes all the "address candidates" (ip and port pairs) collected in steps 1 and 2.

  4. The remote client, upon receiving the invite to connect, performs step 1 and 2 above as well. Then sends back his candidate list through the same channel that he received the inviter's candidate list on.

  5. Hole punching step. Both clients, start sending test messages over UDP to the other side's address candidates and listening for the same messages on their end. Whenever a messages is received, reply back to the address from which it came. Eventually, the clients will discover that they have a pair of addresses that they can reliably send datagrams too. Typically, one endpoint makes the final decision on which address pair (sockets) to communicate with and the protocol facilitates this endpoint telling the other endpoint this decision.

**- usually best to not to rely on a well known port for P2P clients. Because two clients behind the same NAT or firewall would not likely be able to use your software at the same time.

Here is a quick summary of some technologies to explore.

STUN - Is a simple server and protocol for clients behind a NAT/route to discover what their external IP and port mappings are.

TURN is an expansion to STUN, but supports relaying for P2P connectivity scenarios where firewalls and NATs prevent direct connections.

ICE is a set of steps by which STUN and TURN are used for setting up a P2P connection. ICE is a formal protocol for steps 1-5 above. Two excellent set of slides on ICE are here and here.

WebRTC is a variant of the ICE standard as well as a reference library for make P2P sessions with STUN and TURN.

UPNP + Internet Gateway Device Protocol - Some routers support this for hosts to automatically obtain port mappings.

libnice is an open source C library for Linux (and might work on windows) that implements ICE.

PJNATH is a library within the PJSIP suite of coding libraries. It is a good implementation of an ICE stack (C code) and has been ported to a lot of platforms. (Windows, Linux, Mac, iOS, Symbian, and soon Android).

And finally, I have a blatant plug for you to use my STUN server code base.

生死何惧 2024-12-28 07:51:54

某些情况下有解决方案,请参阅 UPnP: https://en.wikipedia.org/wiki/ Universal_Plug_and_Play#NAT_traversal

我的家庭路由器允许这样做,基本上,NAT 可以根据计算机的正确请求自动配置。

我不会指望这会大大提高您的可用性,因为没有多少路由器既支持它又启用它。

编辑:@David 建议针对 UPnP 的 .NET 库提出这个问题: 是否有适用于.NET(C# 或 VB.NET)的 UPnP 库?

There are solutions in some cases, see UPnP: https://en.wikipedia.org/wiki/Universal_Plug_and_Play#NAT_traversal

My home router allows this, basically, the NAT can be configured automatically by the proper request from the computer.

I would not count on this to provide a big improvement in your availability, because not that many routers both support that and have it enabled.

EDIT: @David suggested this SO question for a .NET library for UPnP: Is there a UPnP Library for .NET (C# or VB.NET)?

陌上芳菲 2024-12-28 07:51:54

我将使用 WebRTC 技术作为此类应用程序的开源框架。

官方网站

事实上,它是一个开源项目,支持同行所需的所有内容开箱即用的点对点技术:

  • ICE 和 STUN(NAT 穿越)
  • DTLS 和 SRTP(安全)
  • AVPF,确保流媒体质量。

I would use WebRTC technology as an open source framework for such application.

Official Website

In fact it is an open source project which supports all necessary for peer-to-peer technologies out of the box:

  • ICE and STUN (NAT traversal)
  • DTLS and SRTP (security)
  • AVPF for quality of streaming.
黄昏下泛黄的笔记 2024-12-28 07:51:54

这可能比您正在寻找的稍微复杂一些,但 TCP 打孔是一种应该可行的技术。 http://en.wikipedia.org/wiki/TCP_hole_punching

或者,UPnP 非常适合路由器/支持它的防火墙。

This may be a little more complicated than what you're looking for, but TCP Hole Punching is a technique that should work. http://en.wikipedia.org/wiki/TCP_hole_punching

Alternatively, UPnP works great for routers / firewalls that support it.

墨洒年华 2024-12-28 07:51:54

您还有另一个选项,即NAT 端口映射协议 (NAT-PMP)
NAT-PMP 被 Skype 或 BitTorrent P2P 客户端等 VoIP 应用程序广泛使用。

You have another option that is NAT Port Mapping Protocol (NAT-PMP)
NAT-PMP is widely used by VoIP applications like Skype or BitTorrent P2P clients.

Spring初心 2024-12-28 07:51:54

对于简单的开始,我建议查看打孔技术。精彩视频此处。但请确保它并不总是与网络拓扑相关。这解决了 ICE 技术,该技术发现如何建立连接。

For the simple beginnig I would recommend to see hole punching technique. Great video here. But be sure it will not always work relating the network topology. This solves the ICE technique which discovers how connection could be established.

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