| 1 | package io.github.leexeo.cage.mobile; |
| 2 | |
| 3 | import android.util.Base64; |
| 4 | import org.json.JSONObject; |
| 5 | import java.io.ByteArrayOutputStream; |
| 6 | import java.io.IOException; |
| 7 | import java.io.InputStream; |
| 8 | import java.net.URI; |
| 9 | import java.nio.charset.StandardCharsets; |
| 10 | import java.time.Instant; |
| 11 | |
| 12 | final 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 | } |