- 协议: InitMessage 增加 ip6/prefix6/server_ip6 字段(omitempty,向后兼容) - TUN: Device 接口新增 ConfigureIPv6, macOS 用 ifconfig inet6, Linux 用 ip -6 addr add - 路由: full 模式自动添加 ::/1+8000::/1, v6 服务器旁路 /128, resolveHosts 双栈解析 - 会话: setupTUN 解析 v6 字段并配置 TUN+路由, connectOnce 传 v6 给 stats/日志 - 统计: SetConnected(ip,ip6), Snapshot.AssignedIP6 - DB: connection_logs 增加 assigned_ip6 列 + migrateV3 幂等迁移 - UI: 状态卡片新增独立 IPv6 行, 始终可见(未获取显示 IPv6: —) - 文档: 同步 client-development.md 至服务端 IPv6 版本
71 lines
1.9 KiB
Go
71 lines
1.9 KiB
Go
//go:build darwin
|
|
|
|
package tun
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
|
|
"github.com/songgao/water"
|
|
)
|
|
|
|
type darwinDevice struct {
|
|
ifce *water.Interface
|
|
}
|
|
|
|
func createTUN(name string) (Device, error) {
|
|
cfg := water.Config{DeviceType: water.TUN}
|
|
cfg.Name = name
|
|
ifce, err := water.New(cfg)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create utun: %w", err)
|
|
}
|
|
return &darwinDevice{ifce: ifce}, nil
|
|
}
|
|
|
|
func (d *darwinDevice) Name() string { return d.ifce.Name() }
|
|
func (d *darwinDevice) Read(p []byte) (int, error) { return d.ifce.Read(p) }
|
|
func (d *darwinDevice) Write(p []byte) (int, error) { return d.ifce.Write(p) }
|
|
func (d *darwinDevice) Close() error { return d.ifce.Close() }
|
|
|
|
func (d *darwinDevice) Configure(localIP net.IP, prefix int, peerIP net.IP) error {
|
|
if localIP == nil {
|
|
return execCmd("ifconfig", d.Name(), "up")
|
|
}
|
|
inetType := "inet"
|
|
if localIP.To4() == nil {
|
|
inetType = "inet6"
|
|
}
|
|
localCidr := fmt.Sprintf("%s/%d", localIP.String(), prefix)
|
|
// ifconfig utunN inet <ip>/<prefix> <peer_ip> up
|
|
return execCmd("ifconfig", d.Name(), inetType, localCidr, peerIP.String(), "up")
|
|
}
|
|
|
|
func (d *darwinDevice) ConfigureIPv6(localIP6 net.IP, prefix6 int) error {
|
|
if localIP6 == nil {
|
|
return nil
|
|
}
|
|
// macOS utun IPv6 uses the plain address form (no peer aliasing):
|
|
// ifconfig utunN inet6 <ip6>/<prefix6> up
|
|
localCidr := fmt.Sprintf("%s/%d", localIP6.String(), prefix6)
|
|
return execCmd("ifconfig", d.Name(), "inet6", localCidr, "up")
|
|
}
|
|
|
|
func (d *darwinDevice) SetMTU(mtu int) error {
|
|
return execCmd("ifconfig", d.Name(), "mtu", fmt.Sprintf("%d", mtu))
|
|
}
|
|
|
|
// execCmd runs a command, forwarding stdout/stderr.
|
|
func execCmd(name string, arg ...string) error {
|
|
cmd := exec.Command(name, arg...)
|
|
cmd.Stdout = os.Stdout
|
|
cmd.Stderr = os.Stderr
|
|
if err := cmd.Run(); err != nil {
|
|
return fmt.Errorf("%s %s: %w", name, strings.Join(arg, " "), err)
|
|
}
|
|
return nil
|
|
}
|