Format
This commit is contained in:
parent
df25d7882d
commit
bb26ff2e98
4 changed files with 689 additions and 661 deletions
|
|
@ -51,11 +51,12 @@ self.addEventListener("fetch", (event) => {
|
|||
(async function () {
|
||||
return await uv.fetch(event);
|
||||
})()
|
||||
)}
|
||||
else {
|
||||
);
|
||||
} else {
|
||||
event.respondWith(
|
||||
(async function () {
|
||||
return await fetch(event.request);
|
||||
})()
|
||||
)}
|
||||
);
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,9 +1,14 @@
|
|||
//Built from: https://github.com/motortruck1221/bare-as-module3 (commit: 36759f801e0009027878edecff156408b06404c6)
|
||||
(function (global, factory) {
|
||||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
||||
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
||||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.BareMod = {}));
|
||||
})(this, (function (exports) { 'use strict';
|
||||
typeof exports === "object" && typeof module !== "undefined"
|
||||
? factory(exports)
|
||||
: typeof define === "function" && define.amd
|
||||
? define(["exports"], factory)
|
||||
: ((global =
|
||||
typeof globalThis !== "undefined" ? globalThis : global || self),
|
||||
factory((global.BareMod = {})));
|
||||
})(this, function (exports) {
|
||||
"use strict";
|
||||
|
||||
// The user likely has overwritten all networking functions after importing bare-client
|
||||
// It is our responsibility to make sure components of Bare-Client are using native networking functions
|
||||
|
|
@ -12,12 +17,12 @@
|
|||
const WebSocket = globalThis.WebSocket;
|
||||
const WebSocketFields = {
|
||||
prototype: {
|
||||
send: WebSocket.prototype.send,
|
||||
send: WebSocket.prototype.send
|
||||
},
|
||||
CLOSED: WebSocket.CLOSED,
|
||||
CLOSING: WebSocket.CLOSING,
|
||||
CONNECTING: WebSocket.CONNECTING,
|
||||
OPEN: WebSocket.OPEN,
|
||||
OPEN: WebSocket.OPEN
|
||||
};
|
||||
|
||||
class BareError extends Error {
|
||||
|
|
@ -254,7 +259,7 @@
|
|||
* @returns MD5 string
|
||||
*/
|
||||
function binl2rstr(input) {
|
||||
let output = '';
|
||||
let output = "";
|
||||
const length32 = input.length * 32;
|
||||
for (let i = 0; i < length32; i += 8) {
|
||||
output += String.fromCharCode((input[i >> 5] >>> i % 32) & 0xff);
|
||||
|
|
@ -317,8 +322,8 @@
|
|||
* @returns Hex encoded string
|
||||
*/
|
||||
function rstr2hex(input) {
|
||||
const hexTab = '0123456789abcdef';
|
||||
let output = '';
|
||||
const hexTab = "0123456789abcdef";
|
||||
let output = "";
|
||||
for (let i = 0; i < input.length; i += 1) {
|
||||
const x = input.charCodeAt(i);
|
||||
output += hexTab.charAt((x >>> 4) & 0x0f) + hexTab.charAt(x & 0x0f);
|
||||
|
|
@ -404,10 +409,10 @@
|
|||
*/
|
||||
function splitHeaders(headers) {
|
||||
const output = new Headers(headers);
|
||||
if (headers.has('x-bare-headers')) {
|
||||
const value = headers.get('x-bare-headers');
|
||||
if (headers.has("x-bare-headers")) {
|
||||
const value = headers.get("x-bare-headers");
|
||||
if (value.length > MAX_HEADER_VALUE) {
|
||||
output.delete('x-bare-headers');
|
||||
output.delete("x-bare-headers");
|
||||
let split = 0;
|
||||
for (let i = 0; i < value.length; i += MAX_HEADER_VALUE) {
|
||||
const part = value.slice(i, i + MAX_HEADER_VALUE);
|
||||
|
|
@ -425,25 +430,25 @@
|
|||
*/
|
||||
function joinHeaders(headers) {
|
||||
const output = new Headers(headers);
|
||||
const prefix = 'x-bare-headers';
|
||||
const prefix = "x-bare-headers";
|
||||
if (headers.has(`${prefix}-0`)) {
|
||||
const join = [];
|
||||
for (const [header, value] of headers) {
|
||||
if (!header.startsWith(prefix)) {
|
||||
continue;
|
||||
}
|
||||
if (!value.startsWith(';')) {
|
||||
if (!value.startsWith(";")) {
|
||||
throw new BareError(400, {
|
||||
code: 'INVALID_BARE_HEADER',
|
||||
code: "INVALID_BARE_HEADER",
|
||||
id: `request.headers.${header}`,
|
||||
message: `Value didn't begin with semi-colon.`,
|
||||
message: `Value didn't begin with semi-colon.`
|
||||
});
|
||||
}
|
||||
const id = parseInt(header.slice(prefix.length + 1));
|
||||
join[id] = value.slice(1);
|
||||
output.delete(header);
|
||||
}
|
||||
output.set(prefix, join.join(''));
|
||||
output.set(prefix, join.join(""));
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
|
@ -458,22 +463,30 @@
|
|||
super(3, server);
|
||||
this.ws = new URL(this.base);
|
||||
this.http = new URL(this.base);
|
||||
if (this.ws.protocol === 'https:') {
|
||||
this.ws.protocol = 'wss:';
|
||||
}
|
||||
else {
|
||||
this.ws.protocol = 'ws:';
|
||||
if (this.ws.protocol === "https:") {
|
||||
this.ws.protocol = "wss:";
|
||||
} else {
|
||||
this.ws.protocol = "ws:";
|
||||
}
|
||||
}
|
||||
ready = true;
|
||||
async init() {
|
||||
this.ready = true;
|
||||
}
|
||||
connect(url, origin, protocols, requestHeaders, onopen, onmessage, onclose, onerror) {
|
||||
connect(
|
||||
url,
|
||||
origin,
|
||||
protocols,
|
||||
requestHeaders,
|
||||
onopen,
|
||||
onmessage,
|
||||
onclose,
|
||||
onerror
|
||||
) {
|
||||
const ws = new WebSocket(this.ws);
|
||||
const cleanup = () => {
|
||||
ws.removeEventListener('close', closeListener);
|
||||
ws.removeEventListener('message', messageListener);
|
||||
ws.removeEventListener("close", closeListener);
|
||||
ws.removeEventListener("message", messageListener);
|
||||
};
|
||||
const closeListener = () => {
|
||||
cleanup();
|
||||
|
|
@ -481,12 +494,14 @@
|
|||
const messageListener = (event) => {
|
||||
cleanup();
|
||||
// ws.binaryType is irrelevant when sending text
|
||||
if (typeof event.data !== 'string')
|
||||
throw new TypeError('the first websocket message was not a text frame');
|
||||
if (typeof event.data !== "string")
|
||||
throw new TypeError(
|
||||
"the first websocket message was not a text frame"
|
||||
);
|
||||
const message = JSON.parse(event.data);
|
||||
// finally
|
||||
if (message.type !== 'open')
|
||||
throw new TypeError('message was not of open type');
|
||||
if (message.type !== "open")
|
||||
throw new TypeError("message was not of open type");
|
||||
// onMeta({
|
||||
// protocol: message.protocol,
|
||||
// setCookies: message.setCookies,
|
||||
|
|
@ -497,35 +512,44 @@
|
|||
onmessage(ev.data);
|
||||
});
|
||||
};
|
||||
ws.addEventListener('close', closeListener);
|
||||
ws.addEventListener('message', messageListener);
|
||||
ws.addEventListener("close", closeListener);
|
||||
ws.addEventListener("message", messageListener);
|
||||
// CONNECTED TO THE BARE SERVER, NOT THE REMOTE
|
||||
ws.addEventListener('open', (event) => {
|
||||
ws.addEventListener(
|
||||
"open",
|
||||
(event) => {
|
||||
// getRequestHeaders().then((headers:any) =>
|
||||
WebSocketFields.prototype.send.call(ws, JSON.stringify({
|
||||
type: 'connect',
|
||||
WebSocketFields.prototype.send.call(
|
||||
ws,
|
||||
JSON.stringify({
|
||||
type: "connect",
|
||||
remote: url.toString(),
|
||||
protocols,
|
||||
headers: requestHeaders,
|
||||
forwardHeaders: [],
|
||||
}));
|
||||
forwardHeaders: []
|
||||
})
|
||||
);
|
||||
// );
|
||||
},
|
||||
// only block the open event once
|
||||
{ once: true });
|
||||
{ once: true }
|
||||
);
|
||||
return ws.send.bind(ws);
|
||||
}
|
||||
async request(remote, method, body, headers, signal) {
|
||||
const options = {
|
||||
credentials: 'omit',
|
||||
credentials: "omit",
|
||||
method: method,
|
||||
signal,
|
||||
signal
|
||||
};
|
||||
if (body !== undefined) {
|
||||
options.body = body;
|
||||
}
|
||||
options.headers = this.createBareHeaders(remote, headers);
|
||||
const response = await fetch(this.http + '?cache=' + md5(remote.toString()), options);
|
||||
const response = await fetch(
|
||||
this.http + "?cache=" + md5(remote.toString()),
|
||||
options
|
||||
);
|
||||
const readResponse = await this.readBareResponse(response);
|
||||
// const result: Response & Partial<BareResponse> = new Response(
|
||||
// statusEmpty.includes(readResponse.status!) ? undefined : response.body,
|
||||
|
|
@ -542,7 +566,7 @@
|
|||
body: response.body,
|
||||
headers: readResponse.headers,
|
||||
status: readResponse.status,
|
||||
statusText: readResponse.statusText,
|
||||
statusText: readResponse.statusText
|
||||
};
|
||||
}
|
||||
async readBareResponse(response) {
|
||||
|
|
@ -551,29 +575,32 @@
|
|||
}
|
||||
const responseHeaders = joinHeaders(response.headers);
|
||||
const result = {};
|
||||
const xBareStatus = responseHeaders.get('x-bare-status');
|
||||
if (xBareStatus !== null)
|
||||
result.status = parseInt(xBareStatus);
|
||||
const xBareStatusText = responseHeaders.get('x-bare-status-text');
|
||||
if (xBareStatusText !== null)
|
||||
result.statusText = xBareStatusText;
|
||||
const xBareHeaders = responseHeaders.get('x-bare-headers');
|
||||
if (xBareHeaders !== null)
|
||||
result.headers = JSON.parse(xBareHeaders);
|
||||
const xBareStatus = responseHeaders.get("x-bare-status");
|
||||
if (xBareStatus !== null) result.status = parseInt(xBareStatus);
|
||||
const xBareStatusText = responseHeaders.get("x-bare-status-text");
|
||||
if (xBareStatusText !== null) result.statusText = xBareStatusText;
|
||||
const xBareHeaders = responseHeaders.get("x-bare-headers");
|
||||
if (xBareHeaders !== null) result.headers = JSON.parse(xBareHeaders);
|
||||
return result;
|
||||
}
|
||||
createBareHeaders(remote, bareHeaders, forwardHeaders = [], passHeaders = [], passStatus = []) {
|
||||
createBareHeaders(
|
||||
remote,
|
||||
bareHeaders,
|
||||
forwardHeaders = [],
|
||||
passHeaders = [],
|
||||
passStatus = []
|
||||
) {
|
||||
const headers = new Headers();
|
||||
headers.set('x-bare-url', remote.toString());
|
||||
headers.set('x-bare-headers', JSON.stringify(bareHeaders));
|
||||
headers.set("x-bare-url", remote.toString());
|
||||
headers.set("x-bare-headers", JSON.stringify(bareHeaders));
|
||||
for (const header of forwardHeaders) {
|
||||
headers.append('x-bare-forward-headers', header);
|
||||
headers.append("x-bare-forward-headers", header);
|
||||
}
|
||||
for (const header of passHeaders) {
|
||||
headers.append('x-bare-pass-headers', header);
|
||||
headers.append("x-bare-pass-headers", header);
|
||||
}
|
||||
for (const status of passStatus) {
|
||||
headers.append('x-bare-pass-status', status.toString());
|
||||
headers.append("x-bare-pass-status", status.toString());
|
||||
}
|
||||
splitHeaders(headers);
|
||||
return headers;
|
||||
|
|
@ -581,6 +608,5 @@
|
|||
}
|
||||
|
||||
exports.BareClient = ClientV3;
|
||||
|
||||
}));
|
||||
});
|
||||
//# sourceMappingURL=bare.cjs.map
|
||||
|
|
|
|||
|
|
@ -21,7 +21,8 @@ function changeTransport(transport: string, wispUrl: string) {
|
|||
case "bare":
|
||||
localStorage.setItem("transport", "bare");
|
||||
console.log("Setting transport to Bare");
|
||||
const bare = localStorage.getItem("bare") || window.location.origin + "/bare/";
|
||||
const bare =
|
||||
localStorage.getItem("bare") || window.location.origin + "/bare/";
|
||||
console.log("Bare URL: " + bare);
|
||||
SetTransport("BareMod.BareClient", bare);
|
||||
break;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue