Skip to content

Commit

Permalink
Use the same allocator/endpoint code for windows platform as for unix
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Litvinov <[email protected]>
  • Loading branch information
Zensey committed Apr 30, 2024
1 parent ab5ba2e commit 30c75c6
Show file tree
Hide file tree
Showing 9 changed files with 0 additions and 132 deletions.
1 change: 0 additions & 1 deletion services/wireguard/connection/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ func (mce *mockConnectionEndpoint) ConfigureRoutes(_ net.IP) error { retur
func (mce *mockConnectionEndpoint) PeerStats() (wgcfg.Stats, error) {
return wgcfg.Stats{LastHandshake: time.Now(), BytesSent: 10, BytesReceived: 11}, nil
}
func (mce *mockConnectionEndpoint) ReleaseIP(ip net.IPNet) {}

type mockHandshakeWaiter struct {
err error
Expand Down
1 change: 0 additions & 1 deletion services/wireguard/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,4 @@ type ConnectionEndpoint interface {
Config() (ServiceConfig, error)
InterfaceName() string
Stop() error
ReleaseIP(ip net.IPNet)
}
6 changes: 0 additions & 6 deletions services/wireguard/endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package endpoint

import (
"fmt"
"net"

"github.com/pkg/errors"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -150,11 +149,6 @@ func (ce *connectionEndpoint) Config() (wg.ServiceConfig, error) {
return config, nil
}

// ReleaseIP releases an IP, so it could be reused later.
func (ce *connectionEndpoint) ReleaseIP(ip net.IPNet) {
ce.resourceAllocator.ReleaseIPNet(ip)
}

// Stop closes wireguard client and destroys wireguard network interface.
func (ce *connectionEndpoint) Stop() error {
if err := ce.wgClient.Close(); err != nil {
Expand Down
2 changes: 0 additions & 2 deletions services/wireguard/endpoint/endpoint_unix.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build !windows

/*
* Copyright (C) 2019 The "MysteriumNetwork/node" Authors.
*
Expand Down
14 changes: 0 additions & 14 deletions services/wireguard/endpoint/endpoint_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,3 @@
*/

package endpoint

import (
"net"

"github.com/rs/zerolog/log"
)

func (ce *connectionEndpoint) consumerIP(subnet net.IPNet) net.IP {
ipnet, err := ce.resourceAllocator.AllocateIPNet()
if err != nil {
log.Error().Err(err).Msgf("Failed to allocate IPNet")
}
return ipnet.IP
}
2 changes: 0 additions & 2 deletions services/wireguard/resources/allocator_unix.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build !windows

/*
* Copyright (C) 2018 The "MysteriumNetwork/node" Authors.
*
Expand Down
101 changes: 0 additions & 101 deletions services/wireguard/resources/allocator_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,104 +16,3 @@
*/

package resources

import (
"net"
"sync"

"github.com/mysteriumnetwork/node/core/port"
"github.com/pkg/errors"
)

// MaxConnections sets the limit to the maximum number of wireguard connections.
// x.x.x.0, x.x.x.1 and x.x.x.255 are reserved
var MaxConnections = 253

type portSupplier interface {
Acquire() (port.Port, error)
}

// Allocator is mock wireguard resource handler.
// It will manage lists of network interfaces names, IP addresses and port for endpoints.
type Allocator struct {
IPAddresses map[int]struct{}
mu sync.Mutex

portSupplier portSupplier
subnet net.IPNet
}

// NewAllocator creates new resource pool for wireguard connection.
func NewAllocator(portSupplier portSupplier, subnet net.IPNet) *Allocator {
return &Allocator{
IPAddresses: make(map[int]struct{}),

portSupplier: portSupplier,
subnet: subnet,
}
}

// AbandonedInterfaces is not required for Windows implementation and left here just to satisfy the interface.
func (a *Allocator) AbandonedInterfaces() ([]net.Interface, error) {
return nil, nil
}

// AllocateInterface provides available name for the wireguard network interface.
func (a *Allocator) AllocateInterface() (string, error) {
return interfacePrefix, nil
}

// AllocateIPNet provides available IP address for the wireguard connection.
func (a *Allocator) AllocateIPNet() (net.IPNet, error) {
a.mu.Lock()
defer a.mu.Unlock()

availableOctetMin := 2
availableOctetMax := MaxConnections + 2

for i := availableOctetMin; i < availableOctetMax; i++ {
if _, ok := a.IPAddresses[i]; !ok {
a.IPAddresses[i] = struct{}{}
return calcIPNet(a.subnet, i), nil
}
}
return net.IPNet{}, errors.New("no more unused subnets")
}

// AllocatePort provides available UDP port for the wireguard endpoint.
func (a *Allocator) AllocatePort() (int, error) {
p, err := a.portSupplier.Acquire()
return int(p), err
}

// ReleaseInterface is not required for Windows implementation and left here just to satisfy the interface.
func (a *Allocator) ReleaseInterface(iface string) error {
return nil
}

// ReleaseIPNet releases IP address.
func (a *Allocator) ReleaseIPNet(ipnet net.IPNet) error {
a.mu.Lock()
defer a.mu.Unlock()

ip4 := ipnet.IP.To4()
if ip4 == nil {
return errors.New("allocated subnet not found")
}

i := int(ip4[3])
if _, ok := a.IPAddresses[i]; !ok {
return errors.New("allocated subnet not found")
}

delete(a.IPAddresses, i)
return nil
}

func calcIPNet(ipnet net.IPNet, index int) net.IPNet {
ip := make(net.IP, len(ipnet.IP))
copy(ip, ipnet.IP)
ip = ip.To4()
ip[3] = byte(index)
return net.IPNet{IP: ip, Mask: net.IPv4Mask(255, 255, 255, 0)}
}
4 changes: 0 additions & 4 deletions services/wireguard/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"encoding/json"
"fmt"
"net"
"runtime"
"sync"
"time"

Expand Down Expand Up @@ -190,9 +189,6 @@ func (m *Manager) ProvideConfig(sessionID string, sessionConfig json.RawMessage,
if err := conn.Stop(); err != nil {
log.Error().Err(err).Msg("Failed to stop connection endpoint")
}
if runtime.GOOS == "windows" {
conn.ReleaseIP(config.Consumer.IPAddress)
}

if err := m.resourcesAllocator.ReleaseIPNet(providerConfig.Subnet); err != nil {
log.Error().Err(err).Msg("Failed to release IP network")
Expand Down
1 change: 0 additions & 1 deletion services/wireguard/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ func (mce *mockConnectionEndpoint) ConfigureRoutes(_ net.IP) error { retur
func (mce *mockConnectionEndpoint) PeerStats() (wgcfg.Stats, error) {
return wgcfg.Stats{LastHandshake: time.Now()}, nil
}
func (mce *mockConnectionEndpoint) ReleaseIP(ip net.IPNet) {}

func newManagerStub(pub, out, country string) *Manager {
dnsHandler, _ := dns.ResolveViaSystem()
Expand Down

0 comments on commit 30c75c6

Please sign in to comment.