简体   繁体   English

使用C#在复杂网络上添加NAT UPnP映射

[英]Add a NAT UPnP mapping on a complex network using C#

I am creating an application that listens for connections (server) from a different application (client) via a tcp connection. 我正在创建一个应用程序,通过tcp连接从不同的应用程序(客户端)侦听连接(服务器)。 So if both application where to be on the same network it will be easy to establish the connection. 因此,如果两个应用程序位于同一网络上,则很容易建立连接。 so I am trying to add a method to the server so that users do not have to open ports on their routers in order to make the app work when using it over the internet. 所以我正在尝试向服务器添加一个方法,以便用户不必在其路由器上打开端口,以便在通过Internet使用时使应用程序正常工作。 so let me show you a few different scenarios: 那么让我向您展示几个不同的场景:

1)---------------------------------------------------SCENARIO_1------------------------------------------------------------- 1)------------------------------------------------ --- SCENARIO_1 ---------------------------------------------- ---------------

the server is a computer in a house. 服务器是一个房子里的电脑。 That computer is connected to a router and that router is connected to the internet. 该计算机连接到路由器,该路由器连接到互联网。 The client is a computer on a office that has access to the internet as well. 客户端是办公室中的计算机,也可以访问互联网。

for this scenario to work, before I used to open the ports on the house router and forward them to the server computer. 为了这个场景工作,我以前打开家用路由器上的端口并将它们转发到服务器计算机。 on the client I just had to supply the correct IP address and same port in order to establish the connection. 在客户端我只需要提供正确的IP地址和相同的端口,以建立连接。

now I avoided opening the ports on the house router with a really cool technique: 现在我用一种非常酷的技术避免打开家用路由器上的端口:

first I add this reference: 首先我添加此引用:

在此输入图像描述

then I tell the router which ports I want to be forwarded to my computer (the server): 然后我告诉路由器我想将哪些端口转发到我的计算机(服务器):

    NATUPNPLib.UPnPNATClass upnpnat;
    NATUPNPLib.IStaticPortMappingCollection mappings;

    public ServerExample()
    {
        InitializeComponent();

        //                           server local IP address
        mappings.Add(1300, "TCP", 1300, "192.168.0.120", true, "my server");
        //.... etc
       ..

when doing this I am able to connect from my office computer by providing port 1300, the WAN ip address of the server computer which is a different one. 当我这样做时,我能够通过提供端口1300来连接我的办公室计算机,该服务器计算机的WAN IP地址是另一个。 (that address can be found at: whatismyipaddress.com) THE COOL THING IS THAT I DO NOT HAVE TO DO ANY CONFIGURATION ON THE ROUTER!!! (该地址可在以下网站找到:whatismyipaddress.com)冷却是因为我不需要在路由器上进行任何配置!

2)-----------------------------------------------SCENARIO_2---------------------------------------------- 2)----------------------------------------------- SCENARIO_2 ----------------------------------------------

know here comes my problem. 知道这里是我的问题。 The server happens to be on a office. 服务器碰巧在办公室。 that server is connected to router A, router A is connected to router B and router B is connected to the internet. 该服务器连接到路由器A,路由器A连接到路由器B,路由器B连接到互联网。 In other this example is like the last example on the server with an extra router in between. 在其他示例中,这就像服务器上的最后一个示例,其间有一个额外的路由器。 and the client is somewhere else. 而客户是其他地方。 we do not care how the network setup is on the client computer as long as it has internet access. 只要它具有互联网访问权限,我们就不关心客户端计算机上的网络设置。

the way I used to solve this was by forwarding the packages from router x to the server computer and also port forwarding the traffic from port x of router B to the ip address of router A. when setting up that configuration I was able to establish a connection with the client when providing the WAN ip address of the server. 我以前解决这个问题的方法是将软件包从路由器x转发到服务器计算机,并将流量从路由器B的端口x转发到路由器A的IP地址。在设置该配置时,我能够建立一个提供服务器的WAN IP地址时与客户端的连接。 (the ip address that is shown at: whatismyipaddress.com when retrieved from the network of the office). (从办公室网络检索时显示的ip地址:whatismyipaddress.com)。

so it will be nice if I can avoid doing all this configuration on the routers and do something similar to: 所以,如果我可以避免在路由器上进行所有这些配置并执行类似的操作,那将是很好的:

    public delegate void SomeDelegate(string parameters);

    NATUPNPLib.UPnPNATClass upnpnat;
    NATUPNPLib.IStaticPortMappingCollection mappings;

    public ServerExample()
    {
        InitializeComponent();

        //                           server local IP address
        mappings.Add(1300, "TCP", 1300, "192.168.150.141", true, "my server");
        mappings.Add(1300, "TCP", 1300, "another ip", true, "router's ip");

so I have been playing around with that and I have not been able to make it work. 所以我一直在玩这个,我无法让它发挥作用。 also it will be nice if I could find out if the server connection is like scenario 1 with c# or scenario 2 or maybe a different scenario so that the users setting up the server do not have to specify all that info. 如果我能够确定服务器连接是否与c#或方案2的方案1相似,或者可能是不同的方案,那么设置服务器的用户不必指定所有信息也会很好。 for example I am sure that limewire does something similar. 例如,我确信limewire做了类似的事情。


Edit: 编辑:

here is scenario 1 illustrated: 这里是方案1说明:

在此输入图像描述

and here is scenario 2 illustrated 这里是方案2

在此输入图像描述

The feature that you are using is called Universal Plug and Play (UPnP) which is basically a way for a device to tell another device how to communicate with it. 您正在使用的功能称为通用即插即用(UPnP) ,它基本上是设备告诉其他设备如何与之通信的一种方式。 What you are doing is telling a computer to tell the router what ports you want forwarded to it. 你正在做的是告诉计算机告诉路由器你想要转发给它的端口。 Unfortunately UPnP is a non-routable protocol which means that it can't jump across routers. 不幸的是,UPnP是一种不可路由的协议,这意味着它不能跳过路由器。 So the short answer is that doing what you are trying will not work with multiple routers without manually configuring the ones "farthest" away from the client. 所以简短的回答是,如果没有手动配置离客户端“最远”的路由器,那么做你正在尝试的东西将不适用于多个路由器。

It might be possible to do something with Internet Gateway Device protocol (IDG) but you'd have to research that more and the library you are targeting doesn't support that. 可能可以使用Internet网关设备协议(IDG)执行某些操作,但您必须进行更多研究,并且您要定位的库不支持该功能。 Post this question to Server Fault and you might get a better answer. 将此问题发布到Server Fault ,您可能会得到更好的答案。 (Don't post your code, just explain the basics of your problem, that you're trying to have a machine register itself with a router using UPnP which is working but you have another router in front of that that you want to automatically forward, too.) (不要发布你的代码,只是解释你的问题的基础知识,你试图让一台机器注册自己使用UPnP的路由器工作,但你有另一台路由器,你想要自动转发也是。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM