syncthing/lib/nat/structs.go
Maximilian 16db6fcf3d
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>
2023-12-11 07:36:18 +01:00

132 lines
3.1 KiB
Go

// Copyright (C) 2015 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
package nat
import (
"fmt"
"net"
"time"
"github.com/syncthing/syncthing/lib/sync"
)
type MappingChangeSubscriber func()
type Mapping struct {
protocol Protocol
ipVersion IPVersion
address Address
extAddresses map[string][]Address // NAT ID -> Address
expires time.Time
subscribers []MappingChangeSubscriber
mut sync.RWMutex
}
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) {
addresses, ok := m.extAddresses[id]
if ok {
l.Infof("Removing external open port: %s address(es) %v for gateway %s.", m.protocol, addresses, id)
delete(m.extAddresses, id)
}
}
func (m *Mapping) clearAddresses() {
m.mut.Lock()
change := len(m.extAddresses) > 0
for id, addr := range m.extAddresses {
l.Debugf("Clearing mapping %s: ID: %s Address: %s", m, id, addr)
delete(m.extAddresses, id)
}
m.expires = time.Time{}
m.mut.Unlock()
if change {
m.notify()
}
}
func (m *Mapping) notify() {
m.mut.RLock()
for _, subscriber := range m.subscribers {
subscriber()
}
m.mut.RUnlock()
}
func (m *Mapping) Protocol() Protocol {
return m.protocol
}
func (m *Mapping) Address() Address {
return m.address
}
func (m *Mapping) ExternalAddresses() []Address {
m.mut.RLock()
addrs := make([]Address, 0, len(m.extAddresses))
for _, addr := range m.extAddresses {
addrs = append(addrs, addr...)
}
m.mut.RUnlock()
return addrs
}
func (m *Mapping) OnChanged(subscribed MappingChangeSubscriber) {
m.mut.Lock()
m.subscribers = append(m.subscribers, subscribed)
m.mut.Unlock()
}
func (m *Mapping) String() string {
return fmt.Sprintf("%s/%s", m.address, m.protocol)
}
func (m *Mapping) GoString() string {
return m.String()
}
// Checks if the mappings local IP address matches the IP address of the gateway
// For example, if we are explicitly listening on 192.168.0.12, there is no
// point trying to acquire a mapping on a gateway to which the local IP is
// 10.0.0.1. Fallback to true if any of the IPs is not there.
func (m *Mapping) validGateway(ip net.IP) bool {
if m.address.IP == nil || ip == nil || m.address.IP.IsUnspecified() || ip.IsUnspecified() {
return true
}
return m.address.IP.Equal(ip)
}
// Address is essentially net.TCPAddr yet is more general, and has a few helper
// methods which reduce boilerplate code.
type Address struct {
IP net.IP
Port int
}
func (a Address) Equal(b Address) bool {
return a.Port == b.Port && a.IP.Equal(b.IP)
}
func (a Address) String() string {
var ipStr string
if a.IP == nil {
ipStr = net.IPv4zero.String()
} else {
ipStr = a.IP.String()
}
return net.JoinHostPort(ipStr, fmt.Sprintf("%d", a.Port))
}
func (a Address) GoString() string {
return a.String()
}