1 Star 0 Fork 0

carlmax_my/console-core-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
wg_interface.go 3.43 KB
一键复制 编辑 原始数据 按行查看 历史
package wg_core
import (
"strings"
"gitee.com/carlmax_my/console-core-go/pkg/util"
"golang.zx2c4.com/wireguard/device"
)
// used's in clients(pop/connector/client), wg0's conf,
type WgInterface struct {
Interface string `json:"interface"` // WireGuard interface, wg0/wg1/wg2...
ListenPort int `json:"listen_port"` // [Interface]if this is server side(pop), then need this,
PrivateKey string `json:"private_key"` // [Interface][optional],base64 encoded, if empty then use client's
IP string `json:"ip"` // [Interface]self ip, get from controller
Network string `json:"network"` // [Interface]pop's network, cidr format
DNS string `json:"dns"` // [Interface][optional],
PublicKey string `json:"public_key"` // [自己的公钥]base64 encoded, 自己的公钥,和PrivateKey是一对, 不需要写在conf文件里,是给对方用的,对方用在自己的conf的peer里
PSK string `json:"psk"` // [同PublicKey]
Endpoint string `json:"endpoint"` // [同PublicKey][used in [Peer] peer is client who connect to here];[for server-side it self,this is useless]
AllowIps string `json:"allow_ips"` // [同PublicKey][used in [Peer] peer is client who connect to here];for peer, if use single allow-ip mode, then each ip of each peer is a single ip:1.1.1.1/32
// 因为是多peer模式,所以Endpoint和AllowIps是被这些peer共用的
ConfFile string `json:"conf_file"` // if empty, default value is {Interface}.conf
EthName string `json:"eth_name"` // default value is eth0, used in pop/connector, in PostUp/PostDown
Forward *bool `json:"forward"` // use forward, used in pop/connector
AgentType uint8 `json:"agent_type"` // [NOT A CONFIG],
Peers []WgPeer `json:"peers"` // ...
// ReplacePeers bool `json:"replace_peers"` // [NOT A CONFIG],
}
func (i *WgInterface) Assign(cfg *WgInterface) {
util.SetIfSourceNotEmptyInt(&i.ListenPort, &cfg.ListenPort)
util.SetIfSourceNotEmpty(&i.PrivateKey, &cfg.PrivateKey)
util.SetIfSourceNotEmpty(&i.IP, &cfg.IP)
util.SetIfSourceNotEmpty(&i.Network, &cfg.Network)
util.SetIfSourceNotEmpty(&i.DNS, &cfg.DNS)
util.SetIfSourceNotEmpty(&i.PublicKey, &cfg.PublicKey)
util.SetIfSourceNotEmpty(&i.PSK, &cfg.PSK)
util.SetIfSourceNotEmpty(&i.Endpoint, &cfg.Endpoint)
util.SetIfSourceNotEmpty(&i.AllowIps, &cfg.AllowIps)
if cfg.AgentType != 0 {
i.AgentType = cfg.AgentType
}
if len(cfg.Peers) > 0 {
i.Peers = cfg.Peers
}
}
func (i *WgInterface) AssignForce(cfg *WgInterface) {
i.ListenPort = cfg.ListenPort
i.PrivateKey = cfg.PrivateKey
i.IP = cfg.IP
i.Network = cfg.Network
i.DNS = cfg.DNS
i.PublicKey = cfg.PublicKey
i.PSK = cfg.PSK
i.Endpoint = cfg.Endpoint
i.AllowIps = cfg.AllowIps
i.AgentType = cfg.AgentType
i.Peers = cfg.Peers
}
func (i *WgInterface) GetIpCidr() string {
if i.IP == "" || strings.Contains(i.IP, "/") {
return i.IP
}
if i.Network != "" && strings.Contains(i.Network, "/") {
len := strings.Split(i.Network, "/")[1]
return i.IP + "/" + len
}
return i.IP + "/24"
}
func (i *WgInterface) GetIpWithoutCidr() string {
if !strings.Contains(i.IP, "/") {
return i.IP
}
return strings.Split(i.IP, "/")[0]
}
func (i *WgInterface) ParsePrivateKey() (*device.NoisePrivateKey, error) {
return ParsePrivateKey(i.PrivateKey)
}
func (i *WgInterface) PrivateKeyToHex() (string, error) {
return PrivateKeyToHex(i.PrivateKey)
}
func (c *WgInterface) FirstPeer() *WgPeer {
peer := c.Peers[0]
return &peer
}
马建仓 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.24

搜索帮助

0d507c66 1850385 C8b1a773 1850385