lib/nat, lib/upnp: IPv6 UPnP support (#9010)

This pull request allows syncthing to request an IPv6
[pinhole](https://en.wikipedia.org/wiki/Firewall_pinhole), addressing
issue #7406. This helps users who prefer to use IPv6 for hosting their
services or are forced to do so because of
[CGNAT](https://en.wikipedia.org/wiki/Carrier-grade_NAT). Otherwise,
such users would have to configure their firewall manually to allow
syncthing traffic to pass through while IPv4 users can use UPnP to take
care of network configuration already.

### Testing

I have tested this in a virtual machine setup with miniupnpd running on
the virtualized router. It successfully added an IPv6 pinhole when used
with IPv6 only, an IPv4 port mapping when used with IPv4 only and both
when dual-stack (IPv4 and IPv6) is used.

Automated tests could be added for SOAP responses from the router but
automatically testing this with a real network is likely infeasible.

### Documentation

https://docs.syncthing.net/users/firewall.html could be updated to
mention the fact that UPnP now works with IPv6, although this change is
more "behind the scenes".

---------

Co-authored-by: Simon Frei <freisim93@gmail.com>
Co-authored-by: bt90 <btom1990@googlemail.com>
Co-authored-by: André Colomb <github.com@andre.colomb.de>
This commit is contained in:
Maximilian
2023-12-11 07:36:18 +01:00
committed by GitHub
co-authored by Simon Frei bt90 André Colomb
parent 4c5528bd0e
commit 16db6fcf3d
9 changed files with 551 additions and 129 deletions
+11 -10
View File
@@ -17,24 +17,25 @@ import (
type MappingChangeSubscriber func()
type Mapping struct {
protocol Protocol
address Address
protocol Protocol
ipVersion IPVersion
address Address
extAddresses map[string]Address // NAT ID -> Address
extAddresses map[string][]Address // NAT ID -> Address
expires time.Time
subscribers []MappingChangeSubscriber
mut sync.RWMutex
}
func (m *Mapping) setAddressLocked(id string, address Address) {
l.Infof("New NAT port mapping: external %s address %s to local address %s.", m.protocol, address, m.address)
m.extAddresses[id] = address
func (m *Mapping) setAddressLocked(id string, addresses []Address) {
l.Infof("New external port opened: external %s address(es) %v to local address %s.", m.protocol, addresses, m.address)
m.extAddresses[id] = addresses
}
func (m *Mapping) removeAddressLocked(id string) {
addr, ok := m.extAddresses[id]
addresses, ok := m.extAddresses[id]
if ok {
l.Infof("Removing NAT port mapping: external %s address %s, NAT %s is no longer available.", m.protocol, addr, id)
l.Infof("Removing external open port: %s address(es) %v for gateway %s.", m.protocol, addresses, id)
delete(m.extAddresses, id)
}
}
@@ -73,7 +74,7 @@ func (m *Mapping) ExternalAddresses() []Address {
m.mut.RLock()
addrs := make([]Address, 0, len(m.extAddresses))
for _, addr := range m.extAddresses {
addrs = append(addrs, addr)
addrs = append(addrs, addr...)
}
m.mut.RUnlock()
return addrs
@@ -86,7 +87,7 @@ func (m *Mapping) OnChanged(subscribed MappingChangeSubscriber) {
}
func (m *Mapping) String() string {
return fmt.Sprintf("%s %s", m.protocol, m.address)
return fmt.Sprintf("%s/%s", m.address, m.protocol)
}
func (m *Mapping) GoString() string {