跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

interface.go

138 行 · 4.1 KB历史下载
1package managedproxy
2
3import (
4 "context"
5 "errors"
6 "net"
7 "os"
8 "os/exec"
9 "runtime"
10 "strings"
11 "time"
12
13 "go.yaml.in/yaml/v3"
14)
15
16// PhysicalInterface uses the system's preferred reachable IPv4 hardware port.
17// A route through utun is not a physical uplink, even when it is the default.
18func PhysicalInterface(ctx context.Context) (string, error) {
19 if runtime.GOOS == "windows" {
20 return windowsPhysicalInterface(ctx)
21 }
22 if runtime.GOOS == "linux" {
23 return linuxPhysicalInterface(ctx)
24 }
25 if runtime.GOOS != "darwin" {
26 return "", errors.New("物理网卡识别当前支持 macOS")
27 }
28 ctx, cancel := context.WithTimeout(ctx, 4*time.Second)
29 defer cancel()
30 hardware, err := exec.CommandContext(ctx, "/usr/sbin/networksetup", "-listallhardwareports").Output()
31 if err != nil {
32 return "", errors.New("无法读取物理网卡,请检查主机网络后重试")
33 }
34 route, _ := exec.CommandContext(ctx, "/sbin/route", "-n", "get", "default").Output()
35 nwi, _ := exec.CommandContext(ctx, "/usr/sbin/scutil", "--nwi").Output()
36 usable := map[string]bool{}
37 interfaces, err := net.Interfaces()
38 if err != nil {
39 return "", err
40 }
41 for _, iface := range interfaces {
42 if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
43 continue
44 }
45 addresses, _ := iface.Addrs()
46 for _, address := range addresses {
47 ip, _, err := net.ParseCIDR(address.String())
48 if err == nil && ip.To4() != nil && !ip.IsLinkLocalUnicast() && !ip.IsUnspecified() {
49 usable[iface.Name] = true
50 }
51 }
52 }
53 return selectPhysicalInterface(string(route), string(hardware), string(nwi), usable)
54}
55
56func selectPhysicalInterface(route, hardware, nwi string, usable map[string]bool) (string, error) {
57 physical := map[string]bool{}
58 for _, line := range strings.Split(hardware, "\n") {
59 fields := strings.Fields(line)
60 if len(fields) == 2 && fields[0] == "Device:" && usable[fields[1]] {
61 physical[fields[1]] = true
62 }
63 }
64 // scutil lists interfaces in network priority order. Only its reachable
65 // IPv4 section is relevant to the proxy's IPv4 infrastructure transport.
66 inIPv4, candidate := false, ""
67 reachable := []string{}
68 for _, line := range strings.Split(nwi, "\n") {
69 line = strings.TrimSpace(line)
70 if strings.HasPrefix(line, "IPv4 network interface information") {
71 inIPv4 = true
72 } else if strings.HasPrefix(line, "IPv6 network interface information") {
73 break
74 }
75 if !inIPv4 {
76 continue
77 }
78 fields := strings.Fields(line)
79 if len(fields) >= 3 && fields[1] == ":" && fields[2] == "flags" {
80 candidate = fields[0]
81 }
82 if strings.HasPrefix(line, "reach") && strings.Contains(line, "(Reachable)") && physical[candidate] {
83 reachable = append(reachable, candidate)
84 }
85 }
86 for _, line := range strings.Split(route, "\n") {
87 fields := strings.Fields(line)
88 if len(fields) == 2 && fields[0] == "interface:" {
89 for _, name := range reachable {
90 if fields[1] == name {
91 return name, nil
92 }
93 }
94 }
95 }
96 if len(reachable) > 0 {
97 return reachable[0], nil
98 }
99 return "", errors.New("未找到可用的物理 IPv4 网卡;Cage 外网保持关闭,请连接网络后重试")
100}
101
102// BindInterface changes only the core's host transport. MATCH routing and
103// proxy credentials remain intact; this does not create a guest direct route.
104func BindInterface(data []byte, name string) ([]byte, error) {
105 if name == "" {
106 return nil, errors.New("缺少物理网卡,不能启动代理")
107 }
108 var config map[string]any
109 if err := yaml.Unmarshal(data, &config); err != nil || config == nil {
110 return nil, errors.New("代理运行配置无效")
111 }
112 config["interface-name"] = name
113 // Older imports may carry a node-level binding, which overrides the global.
114 if nodes, ok := config["proxies"].([]any); ok {
115 for _, item := range nodes {
116 if node, ok := item.(map[string]any); ok {
117 if _, bound := node["interface-name"]; bound {
118 node["interface-name"] = name
119 }
120 }
121 }
122 }
123 return yaml.Marshal(config)
124}
125
126func BoundInterface(path string) (string, error) {
127 data, err := os.ReadFile(path)
128 if err != nil {
129 return "", err
130 }
131 var config struct {
132 Interface string `yaml:"interface-name"`
133 }
134 if err := yaml.Unmarshal(data, &config); err != nil {
135 return "", err
136 }
137 return config.Interface, nil
138}