heiher / natmap

TCP/UDP port mapping for full cone NAT
MIT License
1.38k stars 103 forks source link

[Feature Request] UPnP Server #20

Closed rankaiyx closed 1 year ago

rankaiyx commented 1 year ago

Add upnp function to the router so that port mapping can be completed automatically. This should be an exciting feature.

heiher commented 1 year ago

Is possible to do this in hook script? add port mapping rule by call upnp client util (e.g. miniupnpc).

rankaiyx commented 1 year ago

Both methods can do this by plug-in or by integrating a upnp server. We can try both methods and see which is easier to implement.

rankaiyx commented 1 year ago

I found a upnp project that may be of reference value. https://github.com/ofekp/TinyUPnP

rankaiyx commented 1 year ago

https://github.com/easion/tinynatpmpd

rankaiyx commented 1 year ago

https://www.rfc-editor.org/rfc/rfc6886.html

"Correspondingly, NAT-PMP allows a client to request a desired external port, and if that external port is already in use by some other client, the NAT-PMP server will instead assign some other available external port. UPnP IGD does not do this. "

This gives us the opportunity to return the actual external port to the client.

OpportunityLiu commented 1 year ago

I've made a simple implementation with nodejs.

https://github.com/OpportunityLiu/nat-mapmp

rankaiyx commented 1 year ago

I've made a simple implementation with nodejs.

https://github.com/OpportunityLiu/nat-mapmp Great! We need a usage document.

OpportunityLiu commented 1 year ago

Great! We need a usage document.

I've added readme and releases. You can also use ./nat-mapmp.mjs --help to see all options.

image

image

OpportunityLiu commented 1 year ago

https://www.rfc-editor.org/rfc/rfc6886.html

"Correspondingly, NAT-PMP allows a client to request a desired external port, and if that external port is already in use by some other client, the NAT-PMP server will instead assign some other available external port. UPnP IGD does not do this. "

This gives us the opportunity to return the actual external port to the client.

https://datatracker.ietf.org/doc/html/rfc6886#section-9

Basically, run UPnP IGD server behind NAT is impossible, as well as PCP with PREFER_FAILURE support (which is designed for working with IGD, see https://datatracker.ietf.org/doc/html/rfc6887#section-13.2)

As rfc6970 Universal Plug and Play (UPnP) Internet Gateway Device - Port Control Protocol Interworking Function suggests, an IGD-PCP server will respond ConflictInMappingEntry for AddPortMapping() request, which will cause an IGD client trying all 65536 ports, and results in nothing.