跳到主要内容

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

浏览文件

Program.cs

58 行 · 4.2 KB历史下载
1using Cage.GuestClipboard;
2
3static void Check(bool value, string message) { if(!value) throw new Exception(message); }
4var events = new List<InputStroke>();
5var unlocked = true; var failKey = -1; var failUp = false; var focused = true;
6var state = new NativeInputCoordinator(stroke =>
7{
8 if(stroke.Value == failKey || stroke.Up && failUp) return false;
9 events.Add(stroke); return true;
10});
11void Validate() { if(!focused) throw new InvalidDataException("unfocused"); }
12bool ReleaseAllowed() => unlocked;
13void Emit(NativeInputCoordinator.Lease lease, params InputStroke[] strokes) => state.Emit(lease, strokes, Validate, ReleaseAllowed);
14
15var a = state.Open(); var b = state.Open();
16Emit(a, InputStroke.Key(17,false), InputStroke.Button(1,false));
17Emit(b, InputStroke.Key(17,false), InputStroke.Button(1,false));
18var before = events.Count; state.Close(a);
19Check(events.Count == before && state.KeyHeld(17) && state.Buttons(b) == 1, "Disconnect released another viewer's key or button.");
20state.Close(b);
21Check(events.Skip(before).Count() == 2 && events.Skip(before).All(e => e.Up), "The last owner did not emit exactly its two ups.");
22
23var held = state.Open(); Emit(held, InputStroke.Key(17,false));
24var foreign = state.Open(); before = events.Count; Emit(foreign, InputStroke.Key(17,true));
25Check(events.Count == before && state.KeyHeld(17), "An unowned up released a foreign key.");
26state.Close(foreign);
27var macro = state.Open(); Emit(macro, InputStroke.Key(17,false), InputStroke.Key(86,false), InputStroke.Key(86,true), InputStroke.Key(17,true)); state.Close(macro);
28Check(state.KeyHeld(17), "A paste macro consumed the viewer's held modifier.");
29focused = false; before = events.Count; state.Close(held);
30Check(events.Count == before+1 && events[^1].Up, "Cleanup required foreground focus or emitted text."); focused = true;
31
32var partial = state.Open(); failKey = 86;
33try { Emit(partial, InputStroke.Key(17,false), InputStroke.Key(86,false)); throw new Exception("Partial injection did not fail."); } catch(InvalidDataException) { }
34failKey = -1; before = events.Count; state.Close(partial);
35Check(events.Count == before+1 && events[^1] == InputStroke.Key(17,true), "Partial injection invented an unobserved down or lost the successful modifier.");
36
37var locked = state.Open(); Emit(locked, InputStroke.Key(17,false), InputStroke.Button(4,false));
38unlocked = false; before = events.Count; state.Close(locked); state.Retry();
39Check(events.Count == before && state.KeyHeld(17), "Lock cleanup injected into another desktop or discarded a pending up.");
40unlocked = true; state.Retry();
41Check(events.Skip(before).Count() == 2 && events.Skip(before).All(e => e.Up) && !state.KeyHeld(17), "Unlock did not release pending ups.");
42
43var changedDocument = state.Open(); Emit(changedDocument, InputStroke.Key(17,false), InputStroke.Key(16,false));
44unlocked=false; focused=false; before=events.Count; state.ReleaseInputs(changedDocument,[InputStroke.Key(17,true)]);
45Check(events.Count==before && state.KeyHeld(17) && state.KeyHeld(16), "A changed document lost the pending modifier or another live key.");
46unlocked=true; state.Retry();
47Check(!state.KeyHeld(17) && state.KeyHeld(16) && events[^1]==InputStroke.Key(17,true), "A document-change retry released more than the requested own key.");
48state.Close(changedDocument); focused=true;
49
50var failedRelease = state.Open(); Emit(failedRelease, InputStroke.Key(18,false)); failUp = true; before = events.Count; state.Close(failedRelease);
51Check(events.Count == before && state.KeyHeld(18), "A failed up was recorded as successful."); failUp = false; state.Retry();
52Check(!state.KeyHeld(18) && events[^1] == InputStroke.Key(18,true), "A failed up was not retried.");
53
54var reset = state.Open(); Emit(reset, InputStroke.Key(16,false)); state.Reset(reset); Emit(reset, InputStroke.Key(16,false), InputStroke.Key(16,true)); state.Close(reset);
55before = events.Count;
56try { Emit(reset, InputStroke.Key(17,false)); throw new Exception("A closed lease accepted a late down."); } catch(InvalidDataException) { }
57Check(events.Count == before, "A late down was injected after lease cleanup.");
58Console.WriteLine("Native input leases passed: shared owners, foreign ups, macro, focus change, partial injection, lock, failed release, reset and late down.");