1 Star 0 Fork 0

carlmax_my/console-core-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
peer.go 1.84 KB
一键复制 编辑 原始数据 按行查看 历史
carlmax_my 提交于 2024-12-02 21:32 . init project
package wg_core
import (
"gitee.com/carlmax_my/console-core-go/pkg/ip_util"
"golang.zx2c4.com/wireguard/device"
)
type WgPeer struct {
AgentType uint8 `json:"agent_type"` // [NOT A CONFIG],
Address string `json:"address"` // [NOT A CONFIG],peer's ip address, if client want to ping connected pop, then need to provide this
PublicKey string `json:"public_key"` // [Peer][required],base64 encoded, 对方的公钥
PSK string `json:"psk"` // [Peer][optional],base64 encoded, 对方的
Endpoint string `json:"endpoint"` // [Peer][optional],对方的IP地址和端口号,用于建立连接, for pop, endpoint is other pop, if single pop, then del this;for connector/client,endpoint is pop
AllowedIPs string `json:"allowed_ips"` // [Peer][optional],指定允许对方使用的IP范围
// not by response
Remove bool `json:"remove"` // [Peer], used by uapi
ReplaceAllowedIPs bool `json:"replace_allowed_ips"` // [Peer], used by uapi
}
func (p *WgPeer) AssignForce(peer *WgPeer) {
p.AgentType = peer.AgentType
p.Address = peer.Address
p.PublicKey = peer.PublicKey
p.PSK = peer.PSK
p.Endpoint = peer.Endpoint
p.AllowedIPs = peer.AllowedIPs
p.Remove = peer.Remove
p.ReplaceAllowedIPs = peer.ReplaceAllowedIPs
}
func (p *WgPeer) ParsePublicKey() (*device.NoisePublicKey, error) {
return ParsePublicKey(p.PublicKey)
}
func (p *WgPeer) PublicKeyToHex() (string, error) {
return PublicKeyToHex(p.PublicKey)
}
func GetWgPeer(allowIps string, publicKey string, psk string, toRemove bool) (peerConfig *WgPeer, err error) {
allowedIPs, err := ip_util.GetAllowedIPsAsStr(allowIps)
if err != nil {
return nil, err
}
peerConfig = &WgPeer{
PublicKey: publicKey,
PSK: psk,
Remove: toRemove,
AllowedIPs: allowedIPs,
ReplaceAllowedIPs: false,
}
return peerConfig, nil
}
马建仓 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.2

搜索帮助

0d507c66 1850385 C8b1a773 1850385