Skip to content

Commit

Permalink
switch away from worker
Browse files Browse the repository at this point in the history
  • Loading branch information
ayunami2000 committed Dec 27, 2024
1 parent d48a8e5 commit 692b378
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 150 deletions.
13 changes: 4 additions & 9 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -189,23 +189,18 @@ <h1>Wispcraft Injector</h1>
const reader = new FileReader();
reader.onload = function (e) {
let content = e.target.result;
const index = content.toLowerCase().indexOf("<head>");
let index = content.toLowerCase().indexOf("<head>");
if (index == -1) {
alert("Invalid client file!");
fileElem.value = "";
fileElem.removeAttribute("disabled");
return;
}
index += 6;

let inject = `
<script>
let bundle = "${getWispcraftBundle()}";
let blob = new Blob([Uint8Array.from(atob(bundle).split("").map((c) => c.charCodeAt(0)))], {type: "application/javascript"});
let url = URL.createObjectURL(blob);
let script = document.createElement("script");
script.src = url;
document.head.appendChild(script);
script.onload = () => wispcraft.initMain(url);
<script type="text/javascript">
${atob(getWispcraftBundle())}
<${"/"}script>
`
content =
Expand Down
60 changes: 23 additions & 37 deletions src/connection/fakewebsocket.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
import { authstore, wispUrl, type AuthStore } from "..";
import { Connection } from ".";
import { authstore, wispUrl } from "..";
import { Buffer } from "../buffer";
import { showUI } from "../ui";
// @ts-ignore typescript sucks
import wispcraft from "./wispcraft.png";

class WispWS extends EventTarget {
inner: Connection;
url: string;
worker: Worker;

eaglerIn?: WritableStreamDefaultWriter<Buffer>;
eaglerOut?: ReadableStreamDefaultReader<Buffer | string>;
readyState: number;

constructor(uri: string, workeruri: string) {
constructor(uri: string) {
super();

this.url = uri;
this.worker = new Worker(workeruri);

this.inner = new Connection(uri, authstore);
this.readyState = WebSocket.CONNECTING;
this.worker.onmessage = async ({ data }) => {
this.eaglerIn = data.eaglerIn.getWriter();
this.eaglerOut = data.eaglerOut.getReader();
}

start() {
this.inner.forward(() => {
this.readyState = WebSocket.OPEN;
this.dispatchEvent(new Event("open"));

});
(async () => {
try {
while (true) {
const { done, value } = await this.eaglerOut!.read();
const { done, value } = await this.inner.eaglerOut.read();
if (done || !value) break;

this.dispatchEvent(
new MessageEvent("message", {
data: typeof value === "string" ? value : value.inner,
}),
})
);
}
this.readyState = WebSocket.CLOSING;
Expand All @@ -44,44 +42,32 @@ class WispWS extends EventTarget {
console.error(err);
this.dispatchEvent(new Event("error"));
}
};
}

start() {
this.worker.postMessage({
uri: this.url,
wisp: wispUrl,
authstore,
});
})();
}

send(chunk: Uint8Array | ArrayBuffer | string) {
let buf: Buffer;
if (typeof chunk == "string") {
if (chunk.toLowerCase() == "accept: motd") {
this.worker.postMessage({ ping: true });
this.inner.ping();
}
return;
} else if (chunk instanceof ArrayBuffer) {
buf = new Buffer(new Uint8Array(chunk), true);
} else {
buf = new Buffer(chunk, true);
}
if (!this.eaglerIn) throw new Error("not connected");
this.eaglerIn.write(buf);
this.inner.eaglerIn.write(buf);
}

close() {
if (
this.readyState == WebSocket.CLOSING ||
this.readyState == WebSocket.CLOSED
) {
if (this.readyState == WebSocket.CLOSING || this.readyState == WebSocket.CLOSED) {
return;
}
this.readyState = WebSocket.CLOSING;
try {
this.worker.postMessage({ close: true });
} catch (err) { }
this.inner.eaglerIn.abort();
} catch (err) {}
this.readyState = WebSocket.CLOSED;
}
}
Expand Down Expand Up @@ -145,7 +131,7 @@ class AutoWS extends EventTarget {
inner: WebSocket | WispWS | null;
url: string;

constructor(uri: string, workeruri: string, protocols?: string | string[]) {
constructor(uri: string, protocols?: string | string[]) {
super();
const url = new URL(uri);
this.inner = null;
Expand Down Expand Up @@ -195,7 +181,7 @@ class AutoWS extends EventTarget {
this.inner.removeEventListener("open", el3);
this.inner.removeEventListener("message", el);
}
this.inner = new WispWS(this.url, workeruri);
this.inner = new WispWS(this.url);
this.inner.addEventListener("close", el);
this.inner.addEventListener("error", el);
this.inner.addEventListener("open", el);
Expand Down Expand Up @@ -255,7 +241,7 @@ class AutoWS extends EventTarget {
}

const NativeWebSocket = WebSocket;
export function makeFakeWebSocket(workeruri: string): typeof WebSocket {
export function makeFakeWebSocket(): typeof WebSocket {
return new Proxy(WebSocket, {
construct(_target, [uri, protos]) {
let url = new URL(uri);
Expand All @@ -264,13 +250,13 @@ export function makeFakeWebSocket(workeruri: string): typeof WebSocket {
if (url.href == wispUrl) {
return new NativeWebSocket(uri, protos);
} else if (isCustomProtocol && url.hostname == "java") {
const ws = new WispWS(uri, workeruri);
const ws = new WispWS(uri);
ws.start();
return ws;
} else if (isCustomProtocol && url.hostname == "settings") {
return new SettingsWS();
} else {
return new AutoWS(uri, workeruri, protos);
return new AutoWS(uri, protos);
}
},
});
Expand Down
3 changes: 1 addition & 2 deletions src/connection/framer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,13 @@ export class Compressor {
}

export function eagerlyPoll<T>(
stream: ReadableStream<T>,
reader: ReadableStreamDefaultReader,
buffer: number,
cb: () => void
): ReadableStream<T> {
return new ReadableStream(
{
async start(controller) {
const reader = stream.getReader();
while (true) {
const { done, value } = await reader.read();
if (done || !value) break;
Expand Down
19 changes: 8 additions & 11 deletions src/connection/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EaglerProxy } from "../1.8";
import { connect_tcp, initWisp } from "./epoxy";
import { connect_tcp } from "./epoxy";
import { Buffer } from "../buffer";
import {
bufferTransformer,
Expand Down Expand Up @@ -41,11 +41,11 @@ function link<T>(): [ReadableStream<T>, WritableStream<T>] {

export class Connection {
// used by fake websocket
eaglerIn: WritableStream<Buffer>;
eaglerOut: ReadableStream<Buffer | string>;
eaglerIn: BytesWriter;
eaglerOut: BytesReader;

// linked to eaglerIn, has packets the client sends
processIn: ReadableStream<Buffer>;
processIn: BytesReader;
// linked to eaglerOut, has packets the server sends
processOut: BytesWriter;

Expand All @@ -56,17 +56,14 @@ export class Connection {

constructor(
uri: string,
wispurl: string,
private authStore: AuthStore,
) {
initWisp(wispurl);

const [processIn, eaglerIn] = link<Buffer>();
this.processIn = processIn;
this.eaglerIn = eaglerIn;
this.processIn = processIn.getReader();
this.eaglerIn = eaglerIn.getWriter();

const [eaglerOut, processOut] = link<Buffer>();
this.eaglerOut = eaglerOut;
this.eaglerOut = eaglerOut.getReader();
this.processOut = processOut.getWriter();

this.url = new URL(uri.slice(uri.toLowerCase().indexOf("://") + 3));
Expand Down Expand Up @@ -122,7 +119,7 @@ export class Connection {
.pipeThrough(bufferTransformer())
.pipeThrough(impl.decryptor.transform)
.pipeThrough(lengthTransformer())
.pipeThrough(impl.decompressor.transform),
.pipeThrough(impl.decompressor.transform).getReader(),
100,
() => backlog++,
).getReader();
Expand Down
Loading

0 comments on commit 692b378

Please sign in to comment.