| 1 | ## upnp ## |
| 2 | |
| 3 | package upnp provides a simple and opinionated interface to UPnP-enabled |
| 4 | routers, allowing users to forward ports and discover their external IP |
| 5 | address. Specific quirks: |
| 6 | |
| 7 | - When attempting to discover UPnP-enabled routers on the network, only the |
| 8 | first such router is returned. If you have multiple routers, this may cause |
| 9 | some trouble. |
| 10 | |
| 11 | - Forwarded ports are always symmetric, e.g. the router's port 9980 will be |
| 12 | mapped to the client's port 9980. This will be unacceptable for some purposes, |
| 13 | but symmetric mappings are the desired behavior 99% of the time, and they |
| 14 | simplify the API. |
| 15 | |
| 16 | - TCP and UDP protocols are forwarded together. |
| 17 | |
| 18 | - Ports are forwarded permanently. Some other implementations lease a port |
| 19 | mapping for a set duration, and then renew it periodically. This is nice, |
| 20 | because it means mappings won't stick around after they've served their |
| 21 | purpose. Unfortunately, some routers only support permanent mappings, so this |
| 22 | package has chosen to support the lowest common denominator. To un-forward a |
| 23 | port, you must use the Clear function (or do it manually). |
| 24 | |
| 25 | Once you've discovered your router, you can retrieve its address by calling |
| 26 | its Location method. This address can be supplied to Load to connect to the |
| 27 | router directly, which is much faster than calling Discover. |
| 28 | |
| 29 | See the [godoc](http://godoc.org/github.com/NebulousLabs/upnp) for full documentation. |
| 30 | |
| 31 | ## example ## |
| 32 | |
| 33 | ```go |
| 34 | import ( |
| 35 | "log" |
| 36 | "github.com/NebulousLabs/go-upnp" |
| 37 | ) |
| 38 | |
| 39 | func main() { |
| 40 | // connect to router |
| 41 | d, err := upnp.Discover() |
| 42 | if err != nil { |
| 43 | log.Fatal(err) |
| 44 | } |
| 45 | |
| 46 | // discover external IP |
| 47 | ip, err := d.ExternalIP() |
| 48 | if err != nil { |
| 49 | log.Fatal(err) |
| 50 | } |
| 51 | fmt.Println("Your external IP is:", ip) |
| 52 | |
| 53 | // forward a port |
| 54 | err = d.Forward(9001, "upnp test") |
| 55 | if err != nil { |
| 56 | log.Fatal(err) |
| 57 | } |
| 58 | |
| 59 | // un-forward a port |
| 60 | err = d.Clear(9001) |
| 61 | if err != nil { |
| 62 | log.Fatal(err) |
| 63 | } |
| 64 | |
| 65 | // record router's location |
| 66 | loc := d.Location() |
| 67 | |
| 68 | // connect to router directly |
| 69 | d, err = upnp.Load(loc) |
| 70 | if err != nil { |
| 71 | log.Fatal(err) |
| 72 | } |
| 73 | } |
| 74 | ``` |
| 75 | |
| 76 | ## motivation ## |
| 77 | |
| 78 | Port forwarding and external IP discovery are two of the most common tasks required when creating a P2P network. This requires talking to your local router, usually via the UPnP protocol. There are a number of existing packages that provide this functionality, but I found all of them lacking in some respect. The most robust implementation I found was huin's [goupnp](http://github.com/huin/goupnp), but its interface was clunky and required more boilerplate than I felt was necessary. So this package is really just a wrapper around the goupnp library, specifically tailored to the use cases listed above. |
| 79 | |
| 80 | This makes the name a bit of a misnomer, I know; UPnP is much broader than port forwarding. But `upnp` is much more search-friendly than `igdman` (Internet Gateway Device Manager). If you think of something better, I'd love to hear it. |