@ai-ecoverse/slicc-kernel 1.115.0 → 1.116.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2305,6 +2305,12 @@ var Incoming = class {
2305
2305
  this.buf = this.buf.subarray(n);
2306
2306
  return out;
2307
2307
  }
2308
+ async firstLine(max, signal) {
2309
+ while (this.buf.length < max && !this.buf.includes(10) && await this.fill(signal)) ;
2310
+ const end = this.buf.indexOf(10);
2311
+ const bytes = this.buf.subarray(0, end >= 0 ? end + 1 : Math.min(max, this.buf.length));
2312
+ return String.fromCharCode(...bytes);
2313
+ }
2308
2314
  async peek(signal) {
2309
2315
  if (this.buf.length === 0) await this.fill(signal);
2310
2316
  return this.buf[0];
@@ -4708,6 +4714,14 @@ var LoopbackNet = class {
4708
4714
  facades = /* @__PURE__ */ new Map();
4709
4715
  nextEphemeral = EPHEMERAL_FIRST;
4710
4716
  route;
4717
+ origins;
4718
+ useOrigins(serve) {
4719
+ this.origins = serve;
4720
+ }
4721
+ serveOrigin(conn, origin) {
4722
+ if (!this.origins) throw new KernelError("ENETUNREACH");
4723
+ void this.origins(conn, origin).catch(() => conn.close());
4724
+ }
4711
4725
  useUplink(route) {
4712
4726
  this.route = route;
4713
4727
  }
@@ -6227,7 +6241,7 @@ function satisfies(version, range2) {
6227
6241
  }
6228
6242
 
6229
6243
  // src/version.ts
6230
- var KERNEL_VERSION = true ? "1.115.0" : "0.0.0-dev";
6244
+ var KERNEL_VERSION = true ? "1.116.0" : "0.0.0-dev";
6231
6245
  var OS_RELEASE = "6.0.0-slicc";
6232
6246
  function procVersion() {
6233
6247
  return `Linux version ${OS_RELEASE} (slicc-kernel ${KERNEL_VERSION}) #1 SMP PREEMPT_DYNAMIC
@@ -7587,6 +7601,9 @@ var TLS_HANDSHAKE = 22;
7587
7601
  var TOKEN2 = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
7588
7602
  var NO_WEBSOCKET2 = "WebSocket not supported by this transport (fetchTransport needs { webSocket: true })";
7589
7603
  var REALM_PROXY_PORT = 3128;
7604
+ var REQUEST_LINE_MAX = 8192;
7605
+ var REQUEST_LINE = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+ \S+ HTTP\/1\.[01]\r?\n$/;
7606
+ var NOT_HTTP = "not HTTP/1.1: TLS through the transport carries only HTTP/1.1 and WebSocket";
7590
7607
  var DEFAULT_LIMITS = {
7591
7608
  maxConnections: 64,
7592
7609
  maxHead: 64 * 1024,
@@ -7875,6 +7892,26 @@ var RealmProxy = class {
7875
7892
  conn.close();
7876
7893
  }
7877
7894
  }
7895
+ async serveOrigin(conn, origin) {
7896
+ if (this.stop.signal.aborted) {
7897
+ conn.close();
7898
+ return;
7899
+ }
7900
+ this.connections.add(conn);
7901
+ const idle = { ms: this.limits.idleMs };
7902
+ const incoming = new Incoming(timedSource(conn, idle, this.stop.signal));
7903
+ try {
7904
+ const line = await incoming.firstLine(REQUEST_LINE_MAX).catch(() => "");
7905
+ if (line !== "" && !REQUEST_LINE.test(line)) {
7906
+ await this.refuse(conn, new HttpError(400, NOT_HTTP));
7907
+ return;
7908
+ }
7909
+ await this.requests({ sink: conn, incoming, origin, socket: conn, idle });
7910
+ } finally {
7911
+ this.connections.delete(conn);
7912
+ conn.close();
7913
+ }
7914
+ }
7878
7915
  async requests(ctx) {
7879
7916
  try {
7880
7917
  for (; ; ) {
@@ -14373,7 +14410,7 @@ async function writeCaFile(fs, ca, locks = globalThis.navigator?.locks) {
14373
14410
  function enableNetwork(net, options) {
14374
14411
  let proxy;
14375
14412
  const transport = options.transport ?? missingTransport();
14376
- net.activate({ family: "inet", host: "127.0.0.1", port: REALM_PROXY_PORT }, () => {
14413
+ const start2 = () => {
14377
14414
  const tls = new TlsTerminator({
14378
14415
  ca: options.ca,
14379
14416
  engine: options.engine,
@@ -14392,6 +14429,11 @@ function enableNetwork(net, options) {
14392
14429
  if (proxy === started) proxy = void 0;
14393
14430
  await tls.close();
14394
14431
  });
14432
+ };
14433
+ net.activate({ family: "inet", host: "127.0.0.1", port: REALM_PROXY_PORT }, start2);
14434
+ net.useOrigins((conn, origin) => {
14435
+ if (!proxy) start2();
14436
+ return proxy.serveOrigin(conn, origin);
14395
14437
  });
14396
14438
  return () => proxy;
14397
14439
  }
@@ -16307,7 +16349,8 @@ var SOCKET_OPS = [
16307
16349
  "sock-setopt",
16308
16350
  "sock-resolve",
16309
16351
  "sock-sendto",
16310
- "sock-recvfrom"
16352
+ "sock-recvfrom",
16353
+ "sock-tls"
16311
16354
  ];
16312
16355
  function socketAt(fds, fd) {
16313
16356
  const file = anySocketAt(fds, fd);
@@ -16379,6 +16422,8 @@ async function socketSyscall(req, proc) {
16379
16422
  if (addresses.length === 0) throw new KernelError("ENOENT");
16380
16423
  return ok3(addresses);
16381
16424
  }
16425
+ case "sock-tls":
16426
+ return ok3(tlsOverTransport(req, proc));
16382
16427
  default:
16383
16428
  return datagram(req, proc);
16384
16429
  }
@@ -16482,6 +16527,48 @@ async function accept(proc, fd, nonblock) {
16482
16527
  const socket = await listener.accept(waits ? proc.blocking() : void 0, nonblock);
16483
16528
  return { fd: proc.fds.install(new OpenFile(socket), 3), peer: socket.peer };
16484
16529
  }
16530
+ var NOT_HTTP_PORTS = /* @__PURE__ */ new Set([
16531
+ 465,
16532
+ 563,
16533
+ 585,
16534
+ 636,
16535
+ 853,
16536
+ 989,
16537
+ 990,
16538
+ 992,
16539
+ 993,
16540
+ 994,
16541
+ 995,
16542
+ 5061,
16543
+ 5671,
16544
+ 6697,
16545
+ 8883
16546
+ ]);
16547
+ var HOST_NAME = /^[A-Za-z0-9.-]+$|^\[?[0-9A-Fa-f:.]+\]?$/;
16548
+ function tlsOverTransport(req, proc) {
16549
+ if (req.ca) throw new KernelError("ENOTSUP");
16550
+ if (req.alpn && req.alpn.length > 0 && !req.alpn.includes("http/1.1")) {
16551
+ throw new KernelError("EPROTONOSUPPORT");
16552
+ }
16553
+ if (!Number.isInteger(req.port) || req.port < 1 || req.port > 65535)
16554
+ throw new KernelError("EINVAL");
16555
+ if (NOT_HTTP_PORTS.has(req.port)) throw new KernelError("EPROTONOSUPPORT");
16556
+ const servername = req.servername || req.host;
16557
+ if (!HOST_NAME.test(servername)) throw new KernelError("EINVAL");
16558
+ if (isLoopbackHost(servername) || isLoopbackHost(req.host)) throw new KernelError("ENOTSUP");
16559
+ const host = servername.includes(":") && !servername.startsWith("[") ? `[${servername}]` : servername;
16560
+ const origin = req.port === 443 ? `https://${host}` : `https://${host}:${req.port}`;
16561
+ const [mine, theirs] = KernelSocket.pair(proc.net, "inet", proc.cred?.());
16562
+ mine.peer = { family: "inet", host: req.host, port: req.port };
16563
+ try {
16564
+ proc.net.serveOrigin(theirs, origin);
16565
+ } catch (err) {
16566
+ mine.close();
16567
+ theirs.close();
16568
+ throw err;
16569
+ }
16570
+ return { fd: proc.fds.install(new OpenFile(mine), 3), alpnProtocol: "http/1.1", servername };
16571
+ }
16485
16572
 
16486
16573
  // src/kernel/vfs-file.ts
16487
16574
  var O_ACCMODE = 3;