reorganize client code

This commit is contained in:
ading2210 2024-01-02 01:25:04 -05:00
parent 27aa9ff74c
commit 643fc8e463
8 changed files with 32 additions and 26 deletions

19
client/build.sh Executable file
View file

@ -0,0 +1,19 @@
#!/bin/bash
INCLUDE_DIR="build/curl-wasm/include/"
LIB_DIR="build/curl-wasm/lib/"
CACERT_FILE="cacert.pem"
OUT_FILE="out/libcurl.js"
EXPORTED_FUNCS="_main"
COMPILER_OPTIONS="-o $OUT_FILE -Os -lcurl -lssl -lcrypto -I $INCLUDE_DIR -L $LIB_DIR"
EMSCRIPTEN_OPTIONS="-lwebsocket.js -sWEBSOCKET_URL=wss://debug.ading.dev/ws -pthread -sPROXY_TO_PTHREAD -sEXPORTED_FUNCTIONS=$EXPORTED_FUNCS --preload-file $CACERT_FILE"
if [ ! -f $CACERT_FILE ]; then
wget "https://curl.se/ca/cacert.pem" -O $CACERT_FILE
fi
COMPILE_CMD="emcc main.c $COMPILER_OPTIONS $EMSCRIPTEN_OPTIONS"
echo $COMPILE_CMD
$COMPILE_CMD
mv out/libcurl.data ./

10
client/index.html Normal file
View file

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<script src="./out/libcurl.js"></script>
<script src="./main.js"></script>
</head>
<body>
<p>emscripten tests</p>
</body>
</html>

34
client/main.c Normal file
View file

@ -0,0 +1,34 @@
#include <stdio.h>
#include <curl/curl.h>
int main() {
CURL *curl;
CURLcode res;
char* url = "https://ading.dev/";
printf("downloading %s\n", url);
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_PROXY, "socks5h://127.0.0.1:1234");
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
curl_easy_setopt(curl, CURLOPT_CAINFO, "/cacert.pem");
curl_easy_setopt(curl, CURLOPT_CAPATH, "/cacert.pem");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}

16
client/main.js Normal file
View file

@ -0,0 +1,16 @@
const cacert_path = "./out/cacert.peem";
const websocket_url = `wss://${location.hostname}/ws`;
function allocate_str(str) {
return allocate(intArrayFromString(str), ALLOC_NORMAL);
}
async function main() {
}
window.onload = () => {
console.log("page loaded, waiting for emscripten module load");
//Module.websocket.url = websocket_url;
Module.onRuntimeInitialized = main;
};

26
client/tools/curl.sh Executable file
View file

@ -0,0 +1,26 @@
#!/bin/bash
#compile openssl for use with emscripten
CORE_COUNT=$(nproc --all)
PREFIX=$(realpath build/curl-wasm)
OPENSSL_PREFIX=$(realpath build/openssl-wasm)
mkdir -p $PREFIX
rm -rf curl
git clone -b master --depth=1 https://github.com/curl/curl
cd curl
autoreconf -fi
emconfigure ./configure --host i686-linux --prefix=$PREFIX --disable-shared --disable-threaded-resolver --without-libpsl --disable-netrc --disable-ipv6 --disable-tftp --disable-ntlm-wb --with-ssl=$OPENSSL_PREFIX
emmake make -j$CORE_COUNT CFLAGS="-pthread"
rm -rf $PREFIX/include/*
rm -rf $PREFIX/lib/*
mkdir -p $PREFIX/include
mkdir -p $PREFIX/lib
cp -r include/curl $PREFIX/include
cp lib/.libs/libcurl.a $PREFIX/lib
cp -r $OPENSSL_PREFIX/* $PREFIX
cd ..

24
client/tools/openssl.sh Executable file
View file

@ -0,0 +1,24 @@
#!/bin/bash
#compile openssl for use with emscripten
CORE_COUNT=$(nproc --all)
PREFIX=$(realpath build/openssl-wasm)
mkdir -p $PREFIX
rm -rf openssl
git clone -b master --depth=1 https://github.com/openssl/openssl
cd openssl
emconfigure ./Configure linux-x32 --prefix=$PREFIX -no-asm -static -no-afalgeng -no-dso -DOPENSSL_SYS_NETWARE -DSIG_DFL=0 -DSIG_IGN=0 -DHAVE_FORK=0 -DOPENSSL_NO_AFALGENG=1 -DOPENSSL_NO_SPEED=1 -DOPENSSL_NO_DYNAMIC_ENGINE -DDLOPEN_FLAG=0
sed -i 's|^CROSS_COMPILE.*$|CROSS_COMPILE=|g' Makefile
emmake make -j$CORE_COUNT build_generated libssl.a libcrypto.a
rm -rf $PREFIX/include/*
rm -rf $PREFIX/lib/*
mkdir -p $PREFIX/include
mkdir -p $PREFIX/lib
cp -r include/openssl $PREFIX/include
cp libcrypto.a libssl.a $PREFIX/lib
cd ..