Archive incari-client (client, 26.3, clean)
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
plugins {
|
||||
application
|
||||
}
|
||||
|
||||
// A development stand-in for Incari's online service (ADR 0058): :online's in-process fake behind a
|
||||
// real WebSocket, so the client's account screens can be tried without the real service.
|
||||
// Keeps everything in memory, checks nothing that matters, and must never face the internet. Never
|
||||
// shipped.
|
||||
|
||||
dependencies {
|
||||
implementation(testFixtures(project(":online")))
|
||||
implementation(libs.netty.buffer)
|
||||
implementation(libs.netty.transport)
|
||||
implementation(libs.netty.codec.base)
|
||||
implementation(libs.netty.codec.http)
|
||||
implementation(libs.netty.handler)
|
||||
implementation(libs.gson)
|
||||
}
|
||||
|
||||
application {
|
||||
applicationName = "incari-onlinedev"
|
||||
mainClass = "incari.tools.onlinedev.Main"
|
||||
}
|
||||
|
||||
tasks.named<JavaExec>("run") {
|
||||
// `./gradlew :tools:onlinedev:run --args="--listen 8090"`, then the page with
|
||||
// ?online=ws://localhost:8090/v1/ws, or the desktop with -Pincari.online.url=ws://localhost:8090/v1/ws.
|
||||
standardInput = System.`in`
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// A second Incari player for trying the account screens and the chat menu (ADR 0058, ADR 0059)
|
||||
// against tools/onlinedev or the online service. Signs up as <username> (password "peer password long"),
|
||||
// then runs each command in turn; events from the service are printed as they come.
|
||||
//
|
||||
// INCARI_ONLINE_URL=ws://127.0.0.1:8090/v1/ws node tools/onlinedev/peer.mjs Alex_Builds \
|
||||
// befriend:FrostTester "say:FrostTester:hello there" wait:2000
|
||||
//
|
||||
// Commands: befriend:<username>, accept (every request received), say:<friend>:<text> (waits up to
|
||||
// two minutes for the friendship), settings:<everyone|friends|nobody>,
|
||||
// presence:<serverHash>:<name>, wait:<ms>. Development only.
|
||||
import { createHash } from "node:crypto";
|
||||
const [, , username, ...commands] = process.argv;
|
||||
const ws = new WebSocket(process.env.INCARI_ONLINE_URL ?? "ws://127.0.0.1:8090/v1/ws");
|
||||
let id = 0;
|
||||
const waiting = new Map();
|
||||
const ask = (t, body = {}) => new Promise((resolve) => { const n = ++id; waiting.set(n, resolve); ws.send(JSON.stringify({ t, id: n, ...body })); });
|
||||
ws.onmessage = (e) => { const m = JSON.parse(e.data); if (m.id && waiting.has(m.id)) { waiting.get(m.id)(m); waiting.delete(m.id); } else console.log("event", e.data); };
|
||||
const zeros = (h) => { let n = 0; for (const b of h) { if (b === 0) { n += 8; continue; } n += Math.clz32(b) - 24; break; } return n; };
|
||||
const b64 = (s) => Buffer.from(s.replace(/-/g, "+").replace(/_/g, "/"), "base64");
|
||||
ws.onopen = async () => {
|
||||
await ask("hello", { v: 1, client: "peer" });
|
||||
const c = await ask("challenge");
|
||||
const ch = b64(c.challenge);
|
||||
let nonce = 0;
|
||||
for (;; nonce++) { const buf = Buffer.alloc(24); ch.copy(buf); buf.writeBigUInt64BE(BigInt(nonce), 16); const h = createHash("sha256").update(buf).digest(); if (zeros(h) >= c.bits) break; }
|
||||
const r = await ask("signup", { username, password: "peer password long", challenge: c.challenge, nonce });
|
||||
console.log("signup", r.t);
|
||||
for (const cmd of commands) {
|
||||
const [verb, ...rest] = cmd.split(":");
|
||||
if (verb === "befriend") console.log(verb, JSON.stringify(await ask("befriend", { username: rest[0] })));
|
||||
if (verb === "settings") console.log(verb, JSON.stringify(await ask("settings", { messages: rest[0] })));
|
||||
if (verb === "presence") console.log(verb, JSON.stringify(await ask("presence", { serverHash: rest[0], name: rest[1] })));
|
||||
if (verb === "wait") await new Promise((res) => setTimeout(res, Number(rest[0])));
|
||||
if (verb === "accept") { const f = await ask("friends"); for (const q of f.requests) if (q.incoming) console.log("accept", JSON.stringify(await ask("accept", { rid: q.rid }))); }
|
||||
if (verb === "say") { let fr; for (let i = 0; i < 240 && !fr; i++) { const f = await ask("friends"); fr = f.friends.find((x) => x.username.toLowerCase() === rest[0].toLowerCase()); if (!fr) await new Promise((res) => setTimeout(res, 500)); } console.log("say", JSON.stringify(await ask("send", { fid: fr.fid, text: rest.slice(1).join(":") }))); }
|
||||
}
|
||||
setTimeout(() => {}, 1 << 30);
|
||||
};
|
||||
@@ -0,0 +1,133 @@
|
||||
package incari.tools.onlinedev;
|
||||
|
||||
import incari.online.FakeService;
|
||||
import incari.platform.api.net.NetConnection;
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.EventLoopGroup;
|
||||
import io.netty.channel.MultiThreadIoEventLoopGroup;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import io.netty.channel.nio.NioIoHandler;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||
import io.netty.handler.codec.http.HttpObjectAggregator;
|
||||
import io.netty.handler.codec.http.HttpServerCodec;
|
||||
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
|
||||
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* Serves {@code :online}'s fake service on {@code ws://localhost:<port>/v1/ws}, one Netty thread for
|
||||
* every connection (the fake is single-threaded). Accounts, sessions and presence live in memory and
|
||||
* are gone when it stops. Sign-up's proof of work asks for {@link FakeService#BITS} bits.
|
||||
* {@code --release <version>} names a latest release in every {@code hello} answer, and each
|
||||
* {@code --note <change>} a line of its changelog.
|
||||
*/
|
||||
// Writes are fire-and-forget: a failed one closes its channel.
|
||||
@SuppressWarnings("FutureReturnValueIgnored")
|
||||
public final class Main {
|
||||
|
||||
private Main() {}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
int port = 8090;
|
||||
String release = null;
|
||||
List<String> notes = new ArrayList<>();
|
||||
for (int i = 0; i + 1 < args.length; i++) {
|
||||
if (args[i].equals("--listen")) {
|
||||
port = Integer.parseInt(args[i + 1]);
|
||||
} else if (args[i].equals("--release")) {
|
||||
// The latest release hello names, for the title screen's update notice.
|
||||
release = args[i + 1];
|
||||
} else if (args[i].equals("--note")) {
|
||||
notes.add(args[i + 1]);
|
||||
}
|
||||
}
|
||||
FakeService service = new FakeService();
|
||||
service.announceRelease(release, notes.toArray(String[]::new));
|
||||
// One thread: the fake is used by one thread at a time.
|
||||
EventLoopGroup group = new MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory());
|
||||
Channel server = new ServerBootstrap()
|
||||
.group(group)
|
||||
.channel(NioServerSocketChannel.class)
|
||||
.childHandler(new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
protected void initChannel(SocketChannel channel) {
|
||||
channel.pipeline()
|
||||
.addLast(new HttpServerCodec())
|
||||
.addLast(new HttpObjectAggregator(16 * 1024))
|
||||
.addLast(new WebSocketServerProtocolHandler("/v1/ws", null, false, 16 * 1024))
|
||||
.addLast(new Client(service));
|
||||
}
|
||||
})
|
||||
.bind("127.0.0.1", port)
|
||||
.sync()
|
||||
.channel();
|
||||
System.out.println(
|
||||
"Incari online dev service on ws://127.0.0.1:" + port + "/v1/ws (memory only; Ctrl+C stops)");
|
||||
server.closeFuture().sync();
|
||||
}
|
||||
|
||||
/** One client's WebSocket, handed to the fake as if it had opened it. */
|
||||
private static final class Client extends SimpleChannelInboundHandler<TextWebSocketFrame> {
|
||||
|
||||
private final FakeService service;
|
||||
private @Nullable NetConnection socket;
|
||||
|
||||
Client(FakeService service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void userEventTriggered(ChannelHandlerContext ctx, Object event) {
|
||||
if (!(event instanceof WebSocketServerProtocolHandler.HandshakeComplete)) {
|
||||
ctx.fireUserEventTriggered(event);
|
||||
return;
|
||||
}
|
||||
Channel channel = ctx.channel();
|
||||
System.out.println("Connected: " + channel.remoteAddress());
|
||||
socket = service.openWebSocket("ws://local", new NetConnection.Listener() {
|
||||
@Override
|
||||
public void connected() {}
|
||||
|
||||
@Override
|
||||
public void received(ByteBuffer data) {}
|
||||
|
||||
@Override
|
||||
public void receivedText(String text) {
|
||||
channel.writeAndFlush(new TextWebSocketFrame(text));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closed(@Nullable Throwable cause) {
|
||||
channel.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame frame) {
|
||||
NetConnection open = socket;
|
||||
if (open != null) {
|
||||
String text = frame.text();
|
||||
// The type only: never the message, which may hold a password.
|
||||
System.out.println("<- " + text.replaceAll("^\\{\"t\":\"([a-z]+)\".*", "$1"));
|
||||
open.sendText(text);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) {
|
||||
NetConnection open = socket;
|
||||
if (open != null) {
|
||||
service.disconnected(open);
|
||||
}
|
||||
System.out.println("Disconnected: " + ctx.channel().remoteAddress());
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
/** A development stand-in for Incari's online service (ADR 0058). Never shipped. */
|
||||
@NullMarked
|
||||
package incari.tools.onlinedev;
|
||||
|
||||
import org.jspecify.annotations.NullMarked;
|
||||
Reference in New Issue
Block a user