-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
108a0f7
commit 0e61267
Showing
12 changed files
with
208 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//go:build linux | ||
// +build linux | ||
|
||
package wireguardlinux | ||
|
||
const VPN_INTERFACE_NAME = "vpn" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//go:build linux | ||
// +build linux | ||
|
||
package stats | ||
|
||
import "time" | ||
|
||
type PeerStat struct { | ||
Timestamp time.Time `json:"timestamp"` | ||
PublicKey string `json:"publicKey"` | ||
LastHandshakeTime time.Time `json:"lastHandshakeTime"` | ||
ReceiveBytes int64 `json:"receiveBytes"` | ||
TransmitBytes int64 `json:"transmitBytes"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//go:build linux | ||
// +build linux | ||
|
||
package stats | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
wireguardlinux "github.com/in4it/wireguard-server/pkg/wireguard/linux" | ||
) | ||
|
||
func GetStats() ([]PeerStat, error) { | ||
c, available, err := wireguardlinux.New() | ||
if err != nil { | ||
return []PeerStat{}, fmt.Errorf("cannot start wireguardlinux client: %s", err) | ||
} | ||
if !available { | ||
return []PeerStat{}, fmt.Errorf("wireguard linux client not available") | ||
} | ||
device, err := c.Device(wireguardlinux.VPN_INTERFACE_NAME) | ||
if err != nil { | ||
return []PeerStat{}, fmt.Errorf("wireguard linux device 'vpn' not found: %s", err) | ||
} | ||
|
||
peerStats := make([]PeerStat, len(device.Peers)) | ||
|
||
for k, peer := range device.Peers { | ||
peerStats[k] = PeerStat{ | ||
Timestamp: time.Now(), | ||
PublicKey: peer.PublicKey.String(), | ||
LastHandshakeTime: peer.LastHandshakeTime, | ||
ReceiveBytes: peer.ReceiveBytes, | ||
TransmitBytes: peer.TransmitBytes, | ||
} | ||
} | ||
return peerStats, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
//go:build linux | ||
// +build linux | ||
|
||
package wireguard | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"path" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/in4it/wireguard-server/pkg/logging" | ||
"github.com/in4it/wireguard-server/pkg/storage" | ||
"github.com/in4it/wireguard-server/pkg/wireguard/linux/stats" | ||
) | ||
|
||
const RUN_STATS_INTERVAL = 5 | ||
const TIMESTAMP_FORMAT = "2006-01-02T15:04:05" | ||
|
||
func RunStats(storage storage.Iface) { | ||
err := storage.EnsurePath(storage.ConfigPath(VPN_STATS_DIR)) | ||
if err != nil { | ||
logging.ErrorLog(fmt.Errorf("could not create stats path: %s. Stats disabled", err)) | ||
return | ||
} | ||
for { | ||
err := runStats(storage) | ||
if err != nil { | ||
logging.ErrorLog(fmt.Errorf("run stats error: %s", err)) | ||
} | ||
time.Sleep(RUN_STATS_INTERVAL * time.Minute) | ||
} | ||
} | ||
|
||
func runStats(storage storage.Iface) error { | ||
peerStats, err := stats.GetStats() | ||
if err != nil { | ||
return fmt.Errorf("Could not get WireGuard stats: %s", err) | ||
} | ||
|
||
peerConfigs, err := GetAllPeerConfigs(storage) | ||
|
||
statsEntries := []StatsEntry{} | ||
|
||
for _, stat := range peerStats { | ||
for _, peerConfig := range peerConfigs { | ||
if stat.PublicKey == peerConfig.PublicKey { | ||
user, connectionID := splitUserAndConnectionID(peerConfig.ID) | ||
statsEntries = append(statsEntries, StatsEntry{ | ||
User: user, | ||
ConnectionID: connectionID, | ||
TransmitBytes: stat.TransmitBytes, | ||
ReceiveBytes: stat.ReceiveBytes, | ||
LastHandshakeTime: stat.LastHandshakeTime, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
statsCsv := statsToCsv(statsEntries) | ||
|
||
peerConfigPath := storage.ConfigPath(path.Join(VPN_STATS_DIR, "user-"+time.Now().Format("2006-01-02"))) | ||
err = storage.AppendFile(peerConfigPath, statsCsv) | ||
if err != nil { | ||
return fmt.Errorf("could not append stats to file (%s): %s", peerConfigPath, err) | ||
} | ||
return nil | ||
} | ||
|
||
func splitUserAndConnectionID(id string) (string, string) { | ||
split := strings.Split(id, "-") | ||
if len(split) == 1 { | ||
return id, "" | ||
} | ||
return strings.Join(split[:len(split)-1], "-"), split[len(split)-1] | ||
} | ||
|
||
func statsToCsv(statsEntries []StatsEntry) []byte { | ||
var res bytes.Buffer | ||
|
||
for _, statsEntry := range statsEntries { | ||
res.WriteString(strings.Join([]string{statsEntry.Timestamp.Format(TIMESTAMP_FORMAT), statsEntry.User, statsEntry.ConnectionID, strconv.FormatInt(statsEntry.ReceiveBytes, 10), strconv.FormatInt(statsEntry.TransmitBytes, 10), statsEntry.LastHandshakeTime.Format(TIMESTAMP_FORMAT)}, ",") + "\n") | ||
} | ||
return res.Bytes() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters