rebuild: split core into separate repo; move wg-go integration to internal/ctr/corebind
- go.mod: require git.zkcoi.com/zkcoi/meshray/core, replace => ../Meshray - internal/ctr/corebind: EnhancedBind + registry + adapter (wg-go integration isolated) - cmd/mr-wg migrated from old core/cmd/meshray-core - core/ removed; CHANGELOG updated; fix checkdb vet warning
This commit is contained in:
+52
-12
@@ -8,8 +8,8 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
contract "git.zkcoi.com/zkcoi/meshray-contract"
|
||||
"go.uber.org/zap"
|
||||
"golang.zx2c4.com/wireguard/conn"
|
||||
"golang.zx2c4.com/wireguard/device"
|
||||
"golang.zx2c4.com/wireguard/tun"
|
||||
"golang.zx2c4.com/wireguard/wgctrl"
|
||||
@@ -48,7 +48,8 @@ func NewWGManager(logger *zap.Logger) *WGManager {
|
||||
}
|
||||
|
||||
// CreateDevice 创建 WireGuard 设备
|
||||
func (m *WGManager) CreateDevice(networkID string, subnet string, listenPort int) error {
|
||||
// meshMode: "native" 使用 wireguard-go 默认 Bind 直连;"enhanced" 使用 meshray-core 注册的自定义 Bind 接管收发
|
||||
func (m *WGManager) CreateDevice(networkID string, subnet string, listenPort int, meshMode string) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
@@ -63,7 +64,8 @@ func (m *WGManager) CreateDevice(networkID string, subnet string, listenPort int
|
||||
zap.String("network_id", networkID),
|
||||
zap.String("device", deviceName),
|
||||
zap.String("subnet", subnet),
|
||||
zap.Int("listen_port", listenPort))
|
||||
zap.Int("listen_port", listenPort),
|
||||
zap.String("mesh_mode", meshMode))
|
||||
|
||||
// 生成密钥对
|
||||
privateKey, err := wgtypes.GeneratePrivateKey()
|
||||
@@ -76,7 +78,7 @@ func (m *WGManager) CreateDevice(networkID string, subnet string, listenPort int
|
||||
zap.String("public_key", publicKey.String()))
|
||||
|
||||
// 使用用户态模式启动 wireguard-go 进程,并获取资源引用
|
||||
tunDev, wgDev, err := m.startUserModeWGProcessWithRefs(deviceName, privateKey, listenPort)
|
||||
tunDev, wgDev, err := m.startUserModeWGProcessWithRefs(deviceName, privateKey, listenPort, networkID, meshMode)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -356,19 +358,43 @@ type WGStatus struct {
|
||||
}
|
||||
|
||||
// ListPeers 获取设备下所有 Peer 信息
|
||||
// 除内存记录外,还会从 wgctrl 读取对端真实 Endpoint,作为增强模式 P2P 建连的候选地址来源
|
||||
func (m *WGManager) ListPeers(networkID string) ([]PeerInfo, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
|
||||
device, ok := m.devices[networkID]
|
||||
if !ok {
|
||||
m.mu.RUnlock()
|
||||
return nil, fmt.Errorf("网络 %s 的设备不存在", networkID)
|
||||
}
|
||||
// 复制内存中的 peer 列表,避免返回内部共享引用
|
||||
peers := make([]PeerInfo, len(device.Peers))
|
||||
copy(peers, device.Peers)
|
||||
m.mu.RUnlock()
|
||||
|
||||
return device.Peers, nil
|
||||
// 从 wgctrl 读取真实 Endpoint(若已配置)
|
||||
client, err := wgctrl.New()
|
||||
if err == nil {
|
||||
defer client.Close()
|
||||
if dev, err := client.Device(device.Name); err == nil {
|
||||
for i := range peers {
|
||||
for _, wp := range dev.Peers {
|
||||
if wp.PublicKey.String() == peers[i].PublicKey {
|
||||
if wp.Endpoint != nil {
|
||||
peers[i].Endpoint = wp.Endpoint.String()
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return peers, nil
|
||||
}
|
||||
|
||||
// UpdatePeerEndpoint 更新 Peer 的 Endpoint
|
||||
// 注意:增强模式(meshray-core 接管收发)下不应再改写 peer Endpoint 为回环端口,
|
||||
// 否则 WG 入站源地址匹配失败;此方法仅用于设置对端真实地址或原生模式。
|
||||
func (m *WGManager) UpdatePeerEndpoint(networkID string, publicKey string, newEndpoint string) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@@ -434,7 +460,7 @@ func (m *WGManager) UpdatePeerEndpoint(networkID string, publicKey string, newEn
|
||||
|
||||
// startUserModeWGProcess 用户态模式启动 wireguard-go(保存资源引用)
|
||||
func (m *WGManager) startUserModeWGProcess(deviceName string, privateKey wgtypes.Key, listenPort int) error {
|
||||
tunDev, wgDev, err := m.startUserModeWGProcessWithRefs(deviceName, privateKey, listenPort)
|
||||
tunDev, wgDev, err := m.startUserModeWGProcessWithRefs(deviceName, privateKey, listenPort, "", "native")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -445,10 +471,12 @@ func (m *WGManager) startUserModeWGProcess(deviceName string, privateKey wgtypes
|
||||
}
|
||||
|
||||
// startUserModeWGProcessWithRefs 用户态模式启动 wireguard-go,返回资源引用
|
||||
func (m *WGManager) startUserModeWGProcessWithRefs(deviceName string, privateKey wgtypes.Key, listenPort int) (tun.Device, *device.Device, error) {
|
||||
// meshMode 决定 Bind 来源:native 用 wireguard-go 默认 Bind;enhanced 用 meshray-core 注册的自定义 Bind
|
||||
func (m *WGManager) startUserModeWGProcessWithRefs(deviceName string, privateKey wgtypes.Key, listenPort int, networkID string, meshMode string) (tun.Device, *device.Device, error) {
|
||||
m.logger.Info("使用用户态模式启动 WireGuard",
|
||||
zap.String("device", deviceName),
|
||||
zap.Int("port", listenPort))
|
||||
zap.Int("port", listenPort),
|
||||
zap.String("mesh_mode", meshMode))
|
||||
|
||||
// 1. 创建 TUN 设备并保存引用
|
||||
tunDevice, err := tun.CreateTUN(deviceName, 1420)
|
||||
@@ -456,8 +484,20 @@ func (m *WGManager) startUserModeWGProcessWithRefs(deviceName string, privateKey
|
||||
return nil, nil, fmt.Errorf("创建 TUN 设备失败:%w", err)
|
||||
}
|
||||
|
||||
// 2. 创建 UDP bind
|
||||
bind := conn.NewDefaultBind()
|
||||
// 2. 创建 UDP bind:根据组网模式选择
|
||||
// - 原生模式:wireguard-go 默认 Bind(标准直连,无需 meshray-core)
|
||||
// - 增强模式:优先使用 meshray-core 注册的自定义 Bind(接管收发,根治 Endpoint 旁路缺陷);
|
||||
// 若默认构建未引入 meshray-core,则返回错误,增强模式不可用。
|
||||
bindFactory, err := contract.GetBindFactory(meshMode == "enhanced")
|
||||
if err != nil {
|
||||
tunDevice.Close()
|
||||
return nil, nil, fmt.Errorf("获取 Bind 工厂失败(增强模式需 meshray-core):%w", err)
|
||||
}
|
||||
bind, err := bindFactory(listenPort, networkID, m.logger)
|
||||
if err != nil {
|
||||
tunDevice.Close()
|
||||
return nil, nil, fmt.Errorf("创建 Bind 失败:%w", err)
|
||||
}
|
||||
|
||||
// 3. 创建 WireGuard device 并保存引用
|
||||
logger := &device.Logger{
|
||||
|
||||
Reference in New Issue
Block a user