代码拉取完成,页面将自动刷新
// 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, ", ")
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。