跳到主要内容

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

浏览文件

Invitation.java

82 行 · 3.5 KB历史下载
1package io.github.leexeo.cage.mobile;
2
3import android.util.Base64;
4import org.json.JSONObject;
5import java.io.ByteArrayOutputStream;
6import java.io.IOException;
7import java.io.InputStream;
8import java.net.URI;
9import java.nio.charset.StandardCharsets;
10import java.time.Instant;
11
12final class Invitation {
13 final String room, token;
14 final Instant expiry;
15
16 private Invitation(String room, String token, Instant expiry) {
17 this.room = room;
18 this.token = token;
19 this.expiry = expiry;
20 }
21
22 static final class Saved {
23 final String relay, pin, lookup;
24 Saved(JSONObject object) throws Exception {
25 if (object.length() != 4 || !Integer.valueOf(1).equals(object.opt("version"))) throw new IOException("连接文件版本无效。");
26 relay = relay(object.getString("relay"));
27 pin = pin(object.getString("host_pin"));
28 lookup = object.getString("lookup_token");
29 if (!capability(lookup)) throw new IOException("连接文件无效。");
30 }
31 }
32
33 static JSONObject read(InputStream stream) throws Exception {
34 if (stream == null) throw new IOException("无法读取邀请文件。");
35 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
36 byte[] chunk = new byte[1024];
37 int count;
38 while ((count = stream.read(chunk)) != -1) {
39 if (buffer.size() + count > 4096) throw new IOException("邀请文件过大。");
40 buffer.write(chunk, 0, count);
41 }
42 return new JSONObject(new String(buffer.toByteArray(), StandardCharsets.UTF_8));
43 }
44
45 static boolean capability(String value) {
46 if (!value.matches("[A-Za-z0-9_-]{43}")) return false;
47 int flags = Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP;
48 byte[] bytes = Base64.decode(value, flags);
49 return bytes.length == 32 && value.equals(Base64.encodeToString(bytes, flags));
50 }
51
52 static Invitation room(JSONObject object) throws Exception {
53 String room = object.getString("id"), token = object.getString("client_token");
54 Instant expiry = Instant.parse(object.getString("expires_at"));
55 if (object.length() != 3 || !capability(room) || !capability(token) || !expiry.isAfter(Instant.now())) {
56 throw new IOException("邀请无效或已过期。");
57 }
58 return new Invitation(room, token, expiry);
59 }
60
61 static String relay(String value) throws Exception {
62 URI uri = new URI(value.trim());
63 if (!"https".equalsIgnoreCase(uri.getScheme()) || uri.getHost() == null ||
64 uri.getRawUserInfo() != null || uri.getRawQuery() != null || uri.getRawFragment() != null ||
65 !(uri.getRawPath().isEmpty() || uri.getRawPath().equals("/")) || uri.getPort() == 0 || uri.getPort() > 65535) {
66 throw new IllegalArgumentException("请填写 HTTPS 中继地址,地址中只保留主机和端口。");
67 }
68 return new URI("https", null, uri.getHost(), uri.getPort(), null, null, null).toASCIIString();
69 }
70
71 static String pin(String value) {
72 String pin = value.trim();
73 if (pin.startsWith("sha256:")) {
74 String encoded = pin.substring(7);
75 try {
76 byte[] bytes = Base64.decode(encoded, Base64.NO_WRAP);
77 if (bytes.length == 32 && encoded.equals(Base64.encodeToString(bytes, Base64.NO_WRAP))) return pin;
78 } catch (IllegalArgumentException ignored) { }
79 }
80 throw new IllegalArgumentException("电脑指纹需要完整的 sha256: 值。");
81 }
82}