github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/supervisor/daemon/wireguard/monitor.go (about) 1 /* 2 * Copyright (C) 2020 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package wireguard 19 20 import ( 21 "fmt" 22 "sync" 23 "time" 24 25 "github.com/rs/zerolog/log" 26 27 "github.com/mysteriumnetwork/node/services/wireguard/endpoint/userspace" 28 "github.com/mysteriumnetwork/node/services/wireguard/wgcfg" 29 "github.com/mysteriumnetwork/node/supervisor/daemon/wireguard/wginterface" 30 ) 31 32 // Monitor creates/deletes the WireGuard interfaces and keeps track of them. 33 type Monitor struct { 34 interfaces map[string]*wginterface.WgInterface 35 mu sync.Mutex 36 } 37 38 // NewMonitor creates new WireGuard monitor instance. 39 func NewMonitor() *Monitor { 40 return &Monitor{ 41 interfaces: make(map[string]*wginterface.WgInterface), 42 } 43 } 44 45 // Up requests interface creation. 46 func (m *Monitor) Up(cfg wgcfg.DeviceConfig, uid string) (string, error) { 47 m.mu.Lock() 48 defer m.mu.Unlock() 49 50 if iface, exists := m.interfaces[cfg.IfaceName]; exists { 51 if err := iface.Reconfigure(cfg); err != nil { 52 return "", fmt.Errorf("failed to reconfigure interface %s: %w", cfg.IfaceName, err) 53 } 54 return iface.Name, nil 55 } 56 57 iface, err := wginterface.New(cfg, uid) 58 if err != nil { 59 return "", err 60 } 61 62 m.interfaces[iface.Name] = iface 63 return iface.Name, err 64 } 65 66 // Down requests interface deletion. 67 func (m *Monitor) Down(interfaceName string) error { 68 m.mu.Lock() 69 defer m.mu.Unlock() 70 71 iface, ok := m.interfaces[interfaceName] 72 if !ok { 73 return fmt.Errorf("interface %s not found", interfaceName) 74 } 75 76 iface.Down() 77 delete(m.interfaces, interfaceName) 78 return nil 79 } 80 81 // Stats requests interface statistics. 82 func (m *Monitor) Stats(interfaceName string) (wgcfg.Stats, error) { 83 m.mu.Lock() 84 defer m.mu.Unlock() 85 86 iface, ok := m.interfaces[interfaceName] 87 if !ok { 88 return wgcfg.Stats{}, fmt.Errorf("interface %s not found", interfaceName) 89 } 90 91 deviceState, err := userspace.ParseUserspaceDevice(iface.Device.IpcGetOperation) 92 if err != nil { 93 return wgcfg.Stats{}, fmt.Errorf("could not parse device state: %w", err) 94 } 95 96 for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(time.Second) { 97 stats, statErr := userspace.ParseDevicePeerStats(deviceState) 98 if err != nil { 99 err = statErr 100 log.Warn().Err(err).Msg("Failed to parse device stats, will try again") 101 } else { 102 return stats, nil 103 } 104 } 105 106 return wgcfg.Stats{}, err 107 }