跳到主要内容

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

浏览文件

ip_pool.go

88 行 · 1.5 KB历史下载
1package tap
2
3import (
4 "errors"
5 "maps"
6 "math"
7 "net"
8 "sync"
9
10 "github.com/apparentlymart/go-cidr/cidr"
11)
12
13type IPPool struct {
14 base *net.IPNet
15 count uint64
16 leases map[string]string
17 lock sync.Mutex
18}
19
20func NewIPPool(base *net.IPNet) *IPPool {
21 return &IPPool{
22 base: base,
23 count: cidr.AddressCount(base),
24 leases: make(map[string]string),
25 }
26}
27
28func (p *IPPool) Leases() map[string]string {
29 p.lock.Lock()
30 defer p.lock.Unlock()
31 leases := map[string]string{}
32 maps.Copy(leases, p.leases)
33 return leases
34}
35
36func (p *IPPool) Mask() int {
37 ones, _ := p.base.Mask.Size()
38 return ones
39}
40
41func (p *IPPool) GetOrAssign(mac string) (net.IP, error) {
42 p.lock.Lock()
43 defer p.lock.Unlock()
44
45 for ip, candidate := range p.leases {
46 if candidate == mac {
47 return net.ParseIP(ip), nil
48 }
49 }
50
51 if p.count > math.MaxInt {
52 return nil, errors.New("IP pool exceeds maximum number of IP addresses")
53 }
54 for i := 1; i < int(p.count); i++ {
55 candidate, err := cidr.Host(p.base, i)
56 if err != nil {
57 continue
58 }
59 if _, ok := p.leases[candidate.String()]; !ok {
60 p.leases[candidate.String()] = mac
61 return candidate, nil
62 }
63 }
64 return nil, errors.New("cannot find available IP")
65}
66
67func (p *IPPool) Reserve(ip net.IP, mac string) {
68 p.lock.Lock()
69 defer p.lock.Unlock()
70
71 p.leases[ip.String()] = mac
72}
73
74func (p *IPPool) Release(given string) {
75 p.lock.Lock()
76 defer p.lock.Unlock()
77
78 var found string
79 for ip, mac := range p.leases {
80 if mac == given {
81 found = ip
82 break
83 }
84 }
85 if found != "" {
86 delete(p.leases, found)
87 }
88}