feat: use internal resolver for wg network
This commit is contained in:
53
stats.go
53
stats.go
@@ -3,8 +3,6 @@ package main
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -12,11 +10,12 @@ import (
|
||||
"golang.zx2c4.com/wireguard/device"
|
||||
)
|
||||
|
||||
func statsHandler(next http.Handler, dev *device.Device) http.Handler {
|
||||
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Host != "" || r.URL.Path != "/stats" {
|
||||
next.ServeHTTP(rw, r)
|
||||
return
|
||||
func stats(dev *device.Device) func() (any, error) {
|
||||
return func() (any, error) {
|
||||
var buf bytes.Buffer
|
||||
if err := dev.IpcGetOperation(&buf); err != nil {
|
||||
logger.Errorf("Get device config: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
stats := struct {
|
||||
@@ -30,30 +29,22 @@ func statsHandler(next http.Handler, dev *device.Device) http.Handler {
|
||||
NumGoroutine: runtime.NumGoroutine(),
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := dev.IpcGetOperation(&buf); err != nil {
|
||||
logger.Errorf("Get device config: %v", err)
|
||||
rw.WriteHeader(http.StatusInternalServerError)
|
||||
} else {
|
||||
scanner := bufio.NewScanner(&buf)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if prefix := "endpoint="; strings.HasPrefix(line, prefix) {
|
||||
stats.Endpoint = strings.TrimPrefix(line, prefix)
|
||||
}
|
||||
if prefix := "last_handshake_time_sec="; strings.HasPrefix(line, prefix) {
|
||||
stats.LastHandshakeTimestamp, _ = strconv.ParseInt(strings.TrimPrefix(line, prefix), 10, 64)
|
||||
}
|
||||
if prefix := "rx_bytes="; strings.HasPrefix(line, prefix) {
|
||||
stats.ReceivedBytes, _ = strconv.ParseInt(strings.TrimPrefix(line, prefix), 10, 64)
|
||||
}
|
||||
if prefix := "tx_bytes="; strings.HasPrefix(line, prefix) {
|
||||
stats.SentBytes, _ = strconv.ParseInt(strings.TrimPrefix(line, prefix), 10, 64)
|
||||
}
|
||||
scanner := bufio.NewScanner(&buf)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if prefix := "endpoint="; strings.HasPrefix(line, prefix) {
|
||||
stats.Endpoint = strings.TrimPrefix(line, prefix)
|
||||
}
|
||||
if prefix := "last_handshake_time_sec="; strings.HasPrefix(line, prefix) {
|
||||
stats.LastHandshakeTimestamp, _ = strconv.ParseInt(strings.TrimPrefix(line, prefix), 10, 64)
|
||||
}
|
||||
if prefix := "rx_bytes="; strings.HasPrefix(line, prefix) {
|
||||
stats.ReceivedBytes, _ = strconv.ParseInt(strings.TrimPrefix(line, prefix), 10, 64)
|
||||
}
|
||||
if prefix := "tx_bytes="; strings.HasPrefix(line, prefix) {
|
||||
stats.SentBytes, _ = strconv.ParseInt(strings.TrimPrefix(line, prefix), 10, 64)
|
||||
}
|
||||
resp, _ := json.MarshalIndent(stats, "", " ")
|
||||
rw.Header().Set("Content-Type", "application/json")
|
||||
_, _ = rw.Write(append(resp, '\n'))
|
||||
}
|
||||
})
|
||||
return stats, nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user