1 Star 0 Fork 0

carlmax_my/console-core-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
wgctrl.go 1.97 KB
一键复制 编辑 原始数据 按行查看 历史
carlmax_my 提交于 2024-12-02 21:32 . init project
package wg_core
import (
"errors"
"fmt"
"os"
"gitee.com/carlmax_my/console-core-go/pkg/file"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
func (p *WgPeer) GetWgTypesKeyBytes(encodedKey string) (*wgtypes.Key, error) {
key, err := wgtypes.ParseKey(encodedKey)
if err != nil {
return nil, err
}
return &key, nil
}
func (p *WgPeer) GetWgTypesPublicKeyBytes() (*wgtypes.Key, error) {
return p.GetWgTypesKeyBytes(p.PublicKey)
}
func (p *WgPeer) GetWgTypesPSKeyBytes() (*wgtypes.Key, error) {
return p.GetWgTypesKeyBytes(p.PSK)
}
func (p *WgInterface) GetWgTypesKeyBytes(encodedKey string) (*wgtypes.Key, error) {
key, err := wgtypes.ParseKey(encodedKey)
if err != nil {
return nil, err
}
return &key, nil
}
func (p *WgInterface) GetWgTypesPrivateKeyBytes() (*wgtypes.Key, error) {
return p.GetWgTypesKeyBytes(p.PrivateKey)
}
func ReadPrivateKey(keyFile string) (*wgtypes.Key, error) {
if _, b := file.IsExists(keyFile); b {
if bytes, err := os.ReadFile(keyFile); err != nil {
return nil, err
} else {
keyPair, err := wgtypes.ParseKey(string(bytes))
if err != nil {
return nil, err
}
return &keyPair, nil
}
}
return nil, errors.New("key file not exist")
}
func GenerateKeyPairByPath(keyFile string) (privateKey, publicKey string, err error) {
var keyPair wgtypes.Key
if keyFile != "" {
kp, err := ReadPrivateKey(keyFile)
if err != nil {
return "", "", fmt.Errorf("ReadPrivateKey error:%+v", err.Error())
}
keyPair = *kp
} else {
keyPair, err = wgtypes.GeneratePrivateKey()
if err != nil {
return "", "", fmt.Errorf("generate private key error:%s", err.Error())
}
}
privateKey = keyPair.String()
publicKey = keyPair.PublicKey().String()
return
}
func GenerateKeyPair() (privateKey, publicKey string, err error) {
keyPair, err := wgtypes.GeneratePrivateKey()
if err != nil {
return "", "", fmt.Errorf("generate private key error:%s", err.Error())
}
privateKey = keyPair.String()
publicKey = keyPair.PublicKey().String()
return
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/carlmax_my/console-core-go.git
git@gitee.com:carlmax_my/console-core-go.git
carlmax_my
console-core-go
console-core-go
v0.0.14

搜索帮助

0d507c66 1850385 C8b1a773 1850385