Show only connected peers of relevant device in status page

This commit is contained in:
Ioannis Dressos 2023-10-22 15:36:14 +03:00 committed by GitHub
parent 956496d840
commit db872c4261
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 26 deletions

View file

@ -2,6 +2,7 @@ package handler
import ( import (
"crypto/subtle" "crypto/subtle"
"path/filepath"
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
@ -805,6 +806,16 @@ func GlobalSettings(db store.IStore) echo.HandlerFunc {
} }
} }
func extractDeviceNameFromConfigPath(db store.IStore) string {
settings, err := db.GetGlobalSettings()
if err != nil {
log.Error("Cannot get global settings: ", err)
}
base := filepath.Base(settings.ConfigFilePath)
return strings.TrimSuffix(base, filepath.Ext(base))
}
// Status handler // Status handler
func Status(db store.IStore) echo.HandlerFunc { func Status(db store.IStore) echo.HandlerFunc {
type PeerVM struct { type PeerVM struct {
@ -835,6 +846,8 @@ func Status(db store.IStore) echo.HandlerFunc {
}) })
} }
deviceName := extractDeviceNameFromConfigPath(db)
devices, err := wgClient.Devices() devices, err := wgClient.Devices()
if err != nil { if err != nil {
return c.Render(http.StatusInternalServerError, "status.html", map[string]interface{}{ return c.Render(http.StatusInternalServerError, "status.html", map[string]interface{}{
@ -863,35 +876,37 @@ func Status(db store.IStore) echo.HandlerFunc {
conv := map[bool]int{true: 1, false: 0} conv := map[bool]int{true: 1, false: 0}
for i := range devices { for i := range devices {
devVm := DeviceVM{Name: devices[i].Name} if devices[i].Name == deviceName {
for j := range devices[i].Peers { devVm := DeviceVM{Name: devices[i].Name}
var allocatedIPs string for j := range devices[i].Peers {
for _, ip := range devices[i].Peers[j].AllowedIPs { var allocatedIPs string
if len(allocatedIPs) > 0 { for _, ip := range devices[i].Peers[j].AllowedIPs {
allocatedIPs += "</br>" if len(allocatedIPs) > 0 {
allocatedIPs += "</br>"
}
allocatedIPs += ip.String()
} }
allocatedIPs += ip.String() pVm := PeerVM{
} PublicKey: devices[i].Peers[j].PublicKey.String(),
pVm := PeerVM{ ReceivedBytes: devices[i].Peers[j].ReceiveBytes,
PublicKey: devices[i].Peers[j].PublicKey.String(), TransmitBytes: devices[i].Peers[j].TransmitBytes,
ReceivedBytes: devices[i].Peers[j].ReceiveBytes, LastHandshakeTime: devices[i].Peers[j].LastHandshakeTime,
TransmitBytes: devices[i].Peers[j].TransmitBytes, LastHandshakeRel: time.Since(devices[i].Peers[j].LastHandshakeTime),
LastHandshakeTime: devices[i].Peers[j].LastHandshakeTime, AllocatedIP: allocatedIPs,
LastHandshakeRel: time.Since(devices[i].Peers[j].LastHandshakeTime), Endpoint: devices[i].Peers[j].Endpoint.String(),
AllocatedIP: allocatedIPs, }
Endpoint: devices[i].Peers[j].Endpoint.String(), pVm.Connected = pVm.LastHandshakeRel.Minutes() < 3.
}
pVm.Connected = pVm.LastHandshakeRel.Minutes() < 3.
if _client, ok := m[pVm.PublicKey]; ok { if _client, ok := m[pVm.PublicKey]; ok {
pVm.Name = _client.Name pVm.Name = _client.Name
pVm.Email = _client.Email pVm.Email = _client.Email
}
devVm.Peers = append(devVm.Peers, pVm)
} }
devVm.Peers = append(devVm.Peers, pVm) sort.SliceStable(devVm.Peers, func(i, j int) bool { return devVm.Peers[i].Name < devVm.Peers[j].Name })
sort.SliceStable(devVm.Peers, func(i, j int) bool { return conv[devVm.Peers[i].Connected] > conv[devVm.Peers[j].Connected] })
devicesVm = append(devicesVm, devVm)
} }
sort.SliceStable(devVm.Peers, func(i, j int) bool { return devVm.Peers[i].Name < devVm.Peers[j].Name })
sort.SliceStable(devVm.Peers, func(i, j int) bool { return conv[devVm.Peers[i].Connected] > conv[devVm.Peers[j].Connected] })
devicesVm = append(devicesVm, devVm)
} }
} }

View file

@ -35,7 +35,6 @@ Connected Peers
{{ end}} {{ end}}
{{ range $dev := .devices }} {{ range $dev := .devices }}
<table class="table table-sm"> <table class="table table-sm">
<caption>List of connected peers for device with name {{ $dev.Name }} </caption>
<thead> <thead>
<tr> <tr>
<th scope="col">#</th> <th scope="col">#</th>