Skip to content

Commit

Permalink
ifacewatcher: handle wireguard ifaces separately
Browse files Browse the repository at this point in the history
  • Loading branch information
USA-RedDragon committed Feb 12, 2024
1 parent e636935 commit 58143b5
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions internal/ifacewatcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ func (w *Watcher) watch() {
if (strings.HasPrefix(iface.Name, "wg") || strings.HasPrefix(iface.Name, "tun")) && !ifaceContainsNetInterface(w.interfaces, iface) {
fmt.Printf("Interface %s is now present\n", iface.Name)
tunnel := w.findTunnel(iface)
if tunnel == nil {
fmt.Printf("No tunnel found for interface %s\n", iface.Name)
continue
}
err = w.Stats.Add(iface.Name)
if err != nil {
fmt.Println(err)
Expand Down Expand Up @@ -136,14 +140,29 @@ func (w *Watcher) findTunnel(iface net.Interface) *models.Tunnel {
continue
}
ip = ip.To4()
ip[3] -= 2 // AREDN tunnel IPs are always the interface IP - 2 if a client
tun, err := models.FindTunnelByIP(w.db, ip)
if err != nil {
ip[3] += 1 // AREDN tunnel IPs are always the interface IP - 1 if a server
var tun models.Tunnel
if strings.HasPrefix(iface.Name, "wg") {
var err error
tun, err = models.FindTunnelByIP(w.db, ip) // AREDN tunnel IPs are always the interface IP if a server
if err != nil {
ip[3] += 1 // AREDN tunnel IPs are always the interface IP + 1 if a client
tun, err = models.FindTunnelByIP(w.db, ip)
if err != nil {
fmt.Println(err)
continue
}
}
} else {
var err error
ip[3] -= 2 // AREDN tunnel IPs are always the interface IP - 2 if a client
tun, err = models.FindTunnelByIP(w.db, ip)
if err != nil {
fmt.Println(err)
continue
ip[3] += 1 // AREDN tunnel IPs are always the interface IP - 1 if a server
tun, err = models.FindTunnelByIP(w.db, ip)
if err != nil {
fmt.Println(err)
continue
}
}
}
return &tun
Expand Down

0 comments on commit 58143b5

Please sign in to comment.