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
handler
templates

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,6 +876,7 @@ 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 {
if devices[i].Name == deviceName {
devVm := DeviceVM{Name: devices[i].Name} devVm := DeviceVM{Name: devices[i].Name}
for j := range devices[i].Peers { for j := range devices[i].Peers {
var allocatedIPs string var allocatedIPs string
@ -894,6 +908,7 @@ func Status(db store.IStore) echo.HandlerFunc {
devicesVm = append(devicesVm, devVm) devicesVm = append(devicesVm, devVm)
} }
} }
}
return c.Render(http.StatusOK, "status.html", map[string]interface{}{ return c.Render(http.StatusOK, "status.html", map[string]interface{}{
"baseData": model.BaseData{Active: "status", CurrentUser: currentUser(c), Admin: isAdmin(c)}, "baseData": model.BaseData{Active: "status", CurrentUser: currentUser(c), Admin: isAdmin(c)},

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>