working reclaim tls shim

This commit is contained in:
ading2210 2024-10-03 07:59:35 -04:00
parent bcda300153
commit 0cab209e9d
8 changed files with 1289 additions and 9 deletions

3
.gitignore vendored
View file

@ -3,4 +3,5 @@
/client/fragments/tmp
/server/.venv
node_modules
node_modules
dist

View file

@ -77,6 +77,14 @@ echo $COMPILE_CMD
$COMPILE_CMD
mv $COMPILED_FILE $WASM_FILE || true
#build tls shim
echo "bundling tls shim"
if [[ "$*" == *"release"* ]]; then
npx webpack --mode=production
else
npx webpack
fi
#merge compiled emscripten module and wrapper code
cp $JAVSCRIPT_DIR/main.js $OUT_FILE
sed -i "/__emscripten_output__/r $MODULE_FILE" $OUT_FILE
@ -90,7 +98,6 @@ sed -i "s/__library_version__/$VERSION/" $OUT_FILE
WISP_VERSION=$(cat $WISP_CLIENT/package.json | jq -r '.version')
sed -i "s/__wisp_version__/$WISP_VERSION/" $OUT_FILE
#js files are inserted in reverse order
sed -i "/__extra_libraries__/r $JAVSCRIPT_DIR/ftp.js" $OUT_FILE
sed -i "/__extra_libraries__/r $JAVSCRIPT_DIR/tls_socket.js" $OUT_FILE
@ -107,6 +114,7 @@ sed -i "/__extra_libraries__/r $JAVSCRIPT_DIR/logger.js" $OUT_FILE
sed -i "/__extra_libraries__/r $WISP_CLIENT/polyfill.js" $OUT_FILE
sed -i "/__extra_libraries__/r $WISP_CLIENT/wisp.js" $OUT_FILE
sed -i "/__extra_libraries__/r dist/tls-shim.js" $OUT_FILE
#apply patches
python3 tools/patch_js.py $FRAGMENTS_DIR $OUT_FILE

View file

@ -2,18 +2,20 @@
ws ?= ?new WebSocketConstructor\(url, ?opts\)
*/
try {
let transport;
if (api.transport === "wisp") {
ws = new WispWebSocket(url);
transport = new WispWebSocket(url);
}
else if (api.transport === "wsproxy") {
ws = new WebSocket(url);
transport = new WebSocket(url);
}
else if (typeof api.transport === "string") {
throw new TypeError("invalid transport type");
}
else { //custom transports
ws = new api.transport(url);
transport = new api.transport(url);
}
ws = new tls_shim.ReclaimTLSSocket(url, transport);
}
catch (e) {
error_msg("Error while creating a TCP connection: " + e);

View file

@ -154,7 +154,8 @@ api = {
set logger(func) {logger = func},
onload() {},
events: new EventTarget()
events: new EventTarget(),
tls_shim: tls_shim
};
return api;

1115
client/package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
{
"name": "libcurl.js",
"version": "0.6.16",
"name": "libcurl.js-reclaimtls",
"version": "0.6.16-reclaimtls",
"description": "A port of libcurl to WebAssembly, for proxying HTTPS requests from the browser with full TLS encryption",
"main": "libcurl.mjs",
"exports": {
@ -16,5 +16,12 @@
"bugs": {
"url": "https://github.com/ading2210/libcurl.js/issues"
},
"homepage": "https://github.com/ading2210/libcurl.js"
"homepage": "https://github.com/ading2210/libcurl.js",
"dependencies": {
"@reclaimprotocol/tls": "^0.0.2"
},
"devDependencies": {
"webpack": "^5.95.0"
},
"sideEffects": false
}

129
client/tls/index.mjs Normal file
View file

@ -0,0 +1,129 @@
import { makeTLSClient } from '@reclaimprotocol/tls'
export class ReclaimTLSSocket extends EventTarget {
constructor(url, socket) {
//set up websocket interface
super();
this.url = url;
this.socket = socket;
this.protocols = [];
this.binaryType = "blob";
this.onopen = () => {};
this.onerror = () => {};
this.onmessage = () => {};
this.onclose = () => {};
this.CONNECTING = 0;
this.OPEN = 1;
this.CLOSING = 2;
this.CLOSED = 3;
//parse host and port
let url_split = this.url.split("/");
let wsproxy_path = url_split.pop().split(":");
this.host = wsproxy_path[0];
this.port = parseInt(wsproxy_path[1]);
//create tls client
this.tls = makeTLSClient({
host: this.host,
verifyServerCertificate: true,
write: async ({header, content}) => {
var message = new Uint8Array(header.length + content.length);
message.set(header);
message.set(content, header.length);
this.socket.send(message);
},
onHandshake: () => {
this.status = this.OPEN;
this.emit_event(new Event("open"));
},
onApplicationData: (data) => {
let converted;
if (typeof data === "string") {
converted = data;
}
else { //binary frame received as uint8array
if (this.binaryType == "blob") {
converted = new Blob(data);
}
else if (this.binaryType == "arraybuffer") {
converted = data.buffer;
}
else {
throw new TypeError("invalid binaryType string");
}
}
this.emit_event(new MessageEvent("message", {data: converted}));
},
onTlsEnd: () => {
this.status = this.CLOSED;
this.emit_event(new CloseEvent("close"));
this.socket.close();
}
})
//set up socket callbacks
this.socket.binaryType = "arraybuffer";
this.socket.onopen = () => {
this.tls.startHandshake();
}
this.socket.onmessage = (event) => {
this.tls.handleReceivedBytes(new Uint8Array(event.data));
}
this.socket.onclose = () => {
if (this.tls.hasEnded) return;
this.tls.end();
}
this.socket.onerror = () => {
this.status = this.CLOSED;
this.emit_event(new Event("error"));
}
this.status = this.CONNECTING;
}
send(data) {
if (this.status === this.CONNECTING) {
throw new DOMException("websocket not ready yet");
}
if (this.status === this.CLOSED) {
return;
}
if (data instanceof Blob) {
(async () => {
let array_buffer = await data.arrayBuffer();
this.send(new Uint8Array(array_buffer));
})();
return;
}
this.tls.write(data_to_array(data));
}
emit_event(event) {
this.dispatchEvent(event);
if (typeof this["on" + event.type] === "function")
this["on" + event.type](event);
}
close() {
this.status = this.CLOSING;
this.tls.end();
}
get readyState() {
return this.status;
}
get bufferedAmount() {
return 0;
}
get protocol() {
return this.protocols[0] || "";
}
get extensions() {
return "";
}
}

17
client/webpack.config.js Normal file
View file

@ -0,0 +1,17 @@
module.exports = {
name: "tls-shim",
entry: "./tls/index.mjs",
output: {
filename: "tls-shim.js",
library: {
name: "tls_shim",
type: "var"
}
},
mode: "development",
resolve: {
fallback: {
crypto: false
}
}
}