Skip to content

Commit

Permalink
Fixed interface stats for non linux os
Browse files Browse the repository at this point in the history
  • Loading branch information
UnAfraid committed Sep 7, 2023
1 parent 382ac1d commit 7cd61ce
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 36 deletions.
8 changes: 4 additions & 4 deletions pkg/wg/interface_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ func deleteInterface(name string) error {
return nil
}

func interfaceStats(name string) (*server.Stats, error) {
func interfaceStats(name string) (server.Stats, error) {
link, err := netlink.LinkByName(name)
if err != nil {
if os.IsNotExist(err) || errors.As(err, &netlink.LinkNotFoundError{}) {
return nil, nil
return server.Stats{}, nil
}
return nil, fmt.Errorf("failed to find link by name: %w", err)
return server.Stats{}, fmt.Errorf("failed to find link by name: %w", err)
}

statistics := link.Attrs().Statistics
return &server.Stats{
return server.Stats{
RxPackets: statistics.RxPackets,
TxPackets: statistics.TxPackets,
RxBytes: statistics.RxBytes,
Expand Down
8 changes: 6 additions & 2 deletions pkg/wg/interface_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

package wg

import (
"github.com/UnAfraid/wg-ui/pkg/server"
)

func configureInterface(name string, address string, mtu int) error {
return nil
}
Expand All @@ -10,8 +14,8 @@ func deleteInterface(name string) error {
return nil
}

func interfaceStats(name string) (*InterfaceStats, error) {
return nil, nil
func interfaceStats(name string) (server.Stats, error) {
return server.Stats{}, nil
}

func findForeignInterfaces(knownInterfaces []string) ([]ForeignInterface, error) {
Expand Down
31 changes: 1 addition & 30 deletions pkg/wg/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,43 +212,14 @@ func (s *service) updateServerStats() {
continue
}

stats, err := interfaceStats(svc.Name)
newInterfaceStats, err := interfaceStats(svc.Name)
if err != nil {
logrus.
WithError(err).
WithField("name", svc.Name).
Warn("failed to get interface stats")
continue
}
if stats == nil {
continue
}

newInterfaceStats := server.Stats{
RxPackets: stats.RxPackets,
TxPackets: stats.TxPackets,
RxBytes: stats.RxBytes,
TxBytes: stats.TxBytes,
RxErrors: stats.RxErrors,
TxErrors: stats.TxErrors,
RxDropped: stats.RxDropped,
TxDropped: stats.TxDropped,
Multicast: stats.Multicast,
Collisions: stats.Collisions,
RxLengthErrors: stats.RxLengthErrors,
RxOverErrors: stats.RxOverErrors,
RxCrcErrors: stats.RxCrcErrors,
RxFrameErrors: stats.RxFrameErrors,
RxFifoErrors: stats.RxFifoErrors,
RxMissedErrors: stats.RxMissedErrors,
TxAbortedErrors: stats.TxAbortedErrors,
TxCarrierErrors: stats.TxCarrierErrors,
TxFifoErrors: stats.TxFifoErrors,
TxHeartbeatErrors: stats.TxHeartbeatErrors,
TxWindowErrors: stats.TxWindowErrors,
RxCompressed: stats.RxCompressed,
TxCompressed: stats.TxCompressed,
}

if newInterfaceStats != svc.Stats {
updateOptions := &server.UpdateOptions{Stats: newInterfaceStats}
Expand Down

0 comments on commit 7cd61ce

Please sign in to comment.