Add rx_bytes and tx_bytes fields to Device type and API responses. Add formatDataSize helper for human-readable byte formatting with units from B to TB. Add Received and Sent columns to devices table in admin UI with formatted traffic totals. Add traffic metrics display to device action panel. Add TelemetrySnapshot and PeerTelemetry types for gateway runtime stats. Add gateway telemetry endpoint at POST /gateway
66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"nexavpn/backend/internal/wireguard"
|
|
)
|
|
|
|
type Service struct {
|
|
repo Repository
|
|
}
|
|
|
|
func NewService(repo Repository) *Service {
|
|
return &Service{repo: repo}
|
|
}
|
|
|
|
func (s *Service) List(ctx context.Context) ([]Gateway, error) {
|
|
return s.repo.List(ctx)
|
|
}
|
|
|
|
func (s *Service) SelectActive(ctx context.Context) (Gateway, error) {
|
|
return s.repo.FirstActive(ctx)
|
|
}
|
|
|
|
func (s *Service) BuildSyncBundle(ctx context.Context, gatewayID string) (wireguard.GatewayBundle, error) {
|
|
id, err := uuid.Parse(gatewayID)
|
|
if err != nil {
|
|
return wireguard.GatewayBundle{}, err
|
|
}
|
|
return s.repo.BuildSyncBundle(ctx, id)
|
|
}
|
|
|
|
func (s *Service) Update(ctx context.Context, gatewayID string, input UpdateRequest) (Gateway, error) {
|
|
id, err := uuid.Parse(gatewayID)
|
|
if err != nil {
|
|
return Gateway{}, err
|
|
}
|
|
return s.repo.Update(ctx, id, input)
|
|
}
|
|
|
|
func (s *Service) StoreTelemetry(ctx context.Context, gatewayID string, snapshot TelemetrySnapshot) error {
|
|
id, err := uuid.Parse(gatewayID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return s.repo.StoreTelemetry(ctx, id, snapshot)
|
|
}
|
|
|
|
func (s *Service) Bootstrap(ctx context.Context, input BootstrapRequest) (Gateway, error) {
|
|
if input.Name == "" {
|
|
input.Name = "primary-gateway"
|
|
}
|
|
if input.ListenPort == 0 {
|
|
input.ListenPort = 51820
|
|
}
|
|
if input.VPNCIDR == "" {
|
|
input.VPNCIDR = "100.96.0.0/24"
|
|
}
|
|
if len(input.DNSServers) == 0 {
|
|
input.DNSServers = []string{"10.20.0.53"}
|
|
}
|
|
return s.repo.UpsertByName(ctx, input)
|
|
}
|