1 Star 0 Fork 0

张旭/wgctl-go

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
wgtest.go 1.80 KB
一键复制 编辑 原始数据 按行查看 历史
张旭 提交于 2023-06-27 14:41 +08:00 . add PubKey struct and related function
package wgtest
import (
"encoding/hex"
"fmt"
"net"
"gitee.com/aurawing/wgctl-go/wgtypes"
)
// MustCIDR converts CIDR string s into a net.IPNet or panics.
func MustCIDR(s string) net.IPNet {
_, cidr, err := net.ParseCIDR(s)
if err != nil {
panicf("wgtest: failed to parse CIDR: %v", err)
}
return *cidr
}
// MustHexKey decodes a hex string s as a key or panics.
func MustHexKey(s string) wgtypes.Key {
b, err := hex.DecodeString(s)
if err != nil {
panicf("wgtest: failed to decode hex key: %v", err)
}
k, err := wgtypes.NewKey(b)
if err != nil {
panicf("wgtest: failed to create key: %v", err)
}
return k
}
// MustHexKey decodes a hex string s as a key or panics.
func MustHexPubKey(s string) wgtypes.PubKey {
b, err := hex.DecodeString(s)
if err != nil {
panicf("wgtest: failed to decode hex key: %v", err)
}
k, err := wgtypes.NewPubKey(b)
if err != nil {
panicf("wgtest: failed to create key: %v", err)
}
return k
}
// MustPresharedKey generates a preshared key or panics.
func MustPresharedKey() wgtypes.Key {
k, err := wgtypes.GenerateKey()
if err != nil {
panicf("wgtest: failed to generate preshared key: %v", err)
}
return k
}
// MustPrivateKey generates a private key or panics.
func MustPrivateKey() wgtypes.Key {
k, err := wgtypes.GeneratePrivateKey()
if err != nil {
panicf("wgtest: failed to generate private key: %v", err)
}
return k
}
// MustPublicKey generates a public key or panics.
func MustPublicKey() wgtypes.PubKey {
return MustPrivateKey().PublicKey()
}
// MustUDPAddr parses s as a UDP address or panics.
func MustUDPAddr(s string) *net.UDPAddr {
a, err := net.ResolveUDPAddr("udp", s)
if err != nil {
panicf("wgtest: failed to resolve UDP address: %v", err)
}
return a
}
func panicf(format string, a ...interface{}) {
panic(fmt.Sprintf(format, a...))
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/aurawing/wgctl-go.git
git@gitee.com:aurawing/wgctl-go.git
aurawing
wgctl-go
wgctl-go
dd260be11735

搜索帮助