跳到主要内容

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

浏览文件

config.go

118 行 · 3.1 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package usernet
5
6import (
7 "fmt"
8 "net"
9 "path/filepath"
10
11 "github.com/apparentlymart/go-cidr/cidr"
12
13 "github.com/lima-vm/lima/v2/pkg/limatype/dirnames"
14 "github.com/lima-vm/lima/v2/pkg/networks"
15 "github.com/lima-vm/lima/v2/pkg/osutil"
16)
17
18type SockType = string
19
20const (
21 FDSock = "fd"
22 QEMUSock = "qemu"
23 EndpointSock = "ep"
24)
25
26// Sock returns a usernet socket based on name and sockType.
27func Sock(name string, sockType SockType) (string, error) {
28 dir, err := dirnames.LimaNetworksDir()
29 if err != nil {
30 return "", err
31 }
32 return SockWithDirectory(filepath.Join(dir, name), name, sockType)
33}
34
35// SockWithDirectory return a usernet socket based on dir, name and sockType.
36func SockWithDirectory(dir, name string, sockType SockType) (string, error) {
37 if name == "" {
38 name = "default"
39 }
40 sockPath := filepath.Join(dir, fmt.Sprintf("%s_%s.sock", name, sockType))
41 if len(sockPath) >= osutil.UnixPathMax {
42 return "", fmt.Errorf("usernet socket path %#q too long: must be less than UNIX_PATH_MAX=%d characters, but is %d",
43 sockPath, osutil.UnixPathMax, len(sockPath))
44 }
45 return sockPath, nil
46}
47
48// PIDFile returns a path for usernet PID file.
49func PIDFile(name string) (string, error) {
50 dir, err := dirnames.LimaNetworksDir()
51 if err != nil {
52 return "", err
53 }
54 return filepath.Join(dir, name, fmt.Sprintf("usernet_%s.pid", name)), nil
55}
56
57// SubnetCIDR returns a subnet in form of net.IPNet for the given network name.
58func SubnetCIDR(name string) (*net.IPNet, error) {
59 cfg, err := networks.LoadConfig()
60 if err != nil {
61 return nil, err
62 }
63 err = cfg.Check(name)
64 if err != nil {
65 return nil, err
66 }
67 _, ipNet, err := netmaskToCidr(cfg.Networks[name].Gateway, cfg.Networks[name].NetMask)
68 if err != nil {
69 return nil, err
70 }
71 return ipNet, err
72}
73
74// Subnet returns a subnet net.IP for the given network name.
75func Subnet(name string) (net.IP, error) {
76 cfg, err := networks.LoadConfig()
77 if err != nil {
78 return nil, err
79 }
80 err = cfg.Check(name)
81 if err != nil {
82 return nil, err
83 }
84 _, ipNet, err := netmaskToCidr(cfg.Networks[name].Gateway, cfg.Networks[name].NetMask)
85 if err != nil {
86 return nil, err
87 }
88 return ipNet.IP, err
89}
90
91// GatewayIP returns the 2nd IP for the given subnet.
92func GatewayIP(subnet net.IP) string {
93 return cidr.Inc(cidr.Inc(subnet)).String()
94}
95
96// DNSIP returns the 3rd IP for the given subnet.
97func DNSIP(subnet net.IP) string {
98 return cidr.Inc(cidr.Inc(cidr.Inc(subnet))).String()
99}
100
101// Leases returns a leases file based on network name.
102func Leases(name string) (string, error) {
103 dir, err := dirnames.LimaNetworksDir()
104 if err != nil {
105 return "", err
106 }
107 sockPath := filepath.Join(filepath.Join(dir, name), "leases.json")
108 if len(sockPath) >= osutil.UnixPathMax {
109 return "", fmt.Errorf("usernet leases path %#q too long: must be less than UNIX_PATH_MAX=%d characters, but is %d",
110 sockPath, osutil.UnixPathMax, len(sockPath))
111 }
112 return sockPath, nil
113}
114
115func netmaskToCidr(baseIP, netMask net.IP) (net.IP, *net.IPNet, error) {
116 size, _ := net.IPMask(netMask.To4()).Size()
117 return net.ParseCIDR(fmt.Sprintf("%s/%d", baseIP.String(), size))
118}