1 Star 0 Fork 0

张旭 / wgctl-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
main.go 1.54 KB
一键复制 编辑 原始数据 按行查看 历史
张旭 提交于 2023-06-27 09:51 . initial commit
// Command wgctrl is a testing utility for interacting with WireGuard via package
// wgctrl.
package main
import (
"flag"
"fmt"
"log"
"net"
"strings"
"gitee.com/aurawing/wgctl-go"
"gitee.com/aurawing/wgctl-go/wgtypes"
)
func main() {
flag.Parse()
c, err := wgctrl.New()
if err != nil {
log.Fatalf("failed to open wgctrl: %v", err)
}
defer c.Close()
var devices []*wgtypes.Device
if device := flag.Arg(0); device != "" {
d, err := c.Device(device)
if err != nil {
log.Fatalf("failed to get device %q: %v", device, err)
}
devices = append(devices, d)
} else {
devices, err = c.Devices()
if err != nil {
log.Fatalf("failed to get devices: %v", err)
}
}
for _, d := range devices {
printDevice(d)
for _, p := range d.Peers {
printPeer(p)
}
}
}
func printDevice(d *wgtypes.Device) {
const f = `interface: %s (%s)
public key: %s
private key: (hidden)
listening port: %d
`
fmt.Printf(
f,
d.Name,
d.Type.String(),
d.PublicKey.String(),
d.ListenPort)
}
func printPeer(p wgtypes.Peer) {
const f = `peer: %s
endpoint: %s
allowed ips: %s
latest handshake: %s
transfer: %d B received, %d B sent
`
fmt.Printf(
f,
p.PublicKey.String(),
// TODO(mdlayher): get right endpoint with getnameinfo.
p.Endpoint.String(),
ipsString(p.AllowedIPs),
p.LastHandshakeTime.String(),
p.ReceiveBytes,
p.TransmitBytes,
)
}
func ipsString(ipns []net.IPNet) string {
ss := make([]string, 0, len(ipns))
for _, ipn := range ipns {
ss = append(ss, ipn.String())
}
return strings.Join(ss, ", ")
}
1
https://gitee.com/aurawing/wgctl-go.git
git@gitee.com:aurawing/wgctl-go.git
aurawing
wgctl-go
wgctl-go
0ea3fbe6c6cb

搜索帮助