diff --git a/android/Dockerfile b/android/Dockerfile index ac119a9..bd7bfe3 100644 --- a/android/Dockerfile +++ b/android/Dockerfile @@ -1,12 +1,5 @@ -FROM runmymind/docker-android-sdk:ubuntu-standalone-20230511 - -ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/android-sdk-linux/cmdline-tools/latest/bin:/opt/android-sdk-linux/cmdline-tools/tools/bin:/opt/android-sdk-linux/tools/bin:/opt/android-sdk-linux/build-tools/32.0.0:/opt/android-sdk-linux/platform-tools:/opt/android-sdk-linux/emulator:/opt/android-sdk-linux/bin - -RUN sdkmanager "system-images;android-33;google_apis;x86_64" -RUN echo no | avdmanager create avd -n virtual_dev -b google_apis/x86_64 -k "system-images;android-33;google_apis;x86_64" - -RUN apt-get update && apt-get install -y iproute2 iputils-ping iptables redsocks npm git +FROM pre_android/ready EXPOSE 3000 CMD bash /conf/docker-entrypoint.sh diff --git a/android/conf/docker-entrypoint.sh b/android/conf/docker-entrypoint.sh index 1470691..ea68137 100644 --- a/android/conf/docker-entrypoint.sh +++ b/android/conf/docker-entrypoint.sh @@ -1,14 +1,8 @@ -hashed_name=`openssl x509 -inform PEM -subject_hash_old -in /ca-cert.cer | head -1` - -emulator -avd virtual_dev -writable-system -no-window -no-audio & +bash /conf/start_culebra.sh npm i -C /code -cp /ca-cert.cer /$hashed_name.0 -bash /conf/install_cert.sh $hashed_name.0 -bash /conf/install_culebra.sh bash /conf/iptables_conf.sh redsocks -c /conf/redsocks.conf & bash /conf/wait_for_sd.sh -#wait for cert to be installed before launching socket server node /code/index.js #tail -f /dev/null diff --git a/android/conf/start_culebra.sh b/android/conf/start_culebra.sh new file mode 100644 index 0000000..6ceeda8 --- /dev/null +++ b/android/conf/start_culebra.sh @@ -0,0 +1,15 @@ +rm -f /opt/android-sdk-linux/.android/avd/virtual_dev.avd/*.lock +adb start-server +emulator -avd virtual_dev -writable-system -no-window -no-audio & +adb wait-for-device +adb emu avd snapshot load configured +adb wait-for-device + +export PATH=$PATH:/root/culebraDependencies +cd /root/culebra +./culebratester2 start-server & + +#wait for the server to start +while ! curl http://localhost:9987/v2/uiDevice/screenshot > /dev/null 2> /dev/null; do + sleep 0.1 +done \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index 100ac93..3481d48 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,38 +1,37 @@ services: proxy: build: ./proxy/ container_name: proxy networks: - rent_gen_android volumes: - $PWD/certificates:/root/.mitmproxy - $PWD/log:/log android: build: ./android/ container_name: android sysctls: - net.ipv6.conf.all.disable_ipv6=1 cap_add: - NET_ADMIN devices: - /dev/kvm networks: - rent_gen_android volumes: - $PWD/android/conf:/conf - - $PWD/certificates/mitmproxy-ca-cert.cer:/ca-cert.cer - $PWD/android/code:/code http_server: build: ./http_server/ container_name: http_server networks: - rent_gen_android volumes: - $PWD/http_server/code:/code - $PWD/log:/log ports: - 8080:8080 networks: rent_gen_android: driver: bridge \ No newline at end of file diff --git a/android/Dockerfile b/pre_android/Dockerfile similarity index 93% copy from android/Dockerfile copy to pre_android/Dockerfile index ac119a9..4789670 100644 --- a/android/Dockerfile +++ b/pre_android/Dockerfile @@ -1,12 +1,10 @@ FROM runmymind/docker-android-sdk:ubuntu-standalone-20230511 ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/android-sdk-linux/cmdline-tools/latest/bin:/opt/android-sdk-linux/cmdline-tools/tools/bin:/opt/android-sdk-linux/tools/bin:/opt/android-sdk-linux/build-tools/32.0.0:/opt/android-sdk-linux/platform-tools:/opt/android-sdk-linux/emulator:/opt/android-sdk-linux/bin RUN sdkmanager "system-images;android-33;google_apis;x86_64" RUN echo no | avdmanager create avd -n virtual_dev -b google_apis/x86_64 -k "system-images;android-33;google_apis;x86_64" RUN apt-get update && apt-get install -y iproute2 iputils-ping iptables redsocks npm git -EXPOSE 3000 - -CMD bash /conf/docker-entrypoint.sh +CMD bash /preconf/docker-entrypoint.sh diff --git a/pre_android/preconf/docker-entrypoint.sh b/pre_android/preconf/docker-entrypoint.sh new file mode 100644 index 0000000..6cfe71c --- /dev/null +++ b/pre_android/preconf/docker-entrypoint.sh @@ -0,0 +1,15 @@ +hashed_name=`openssl x509 -inform PEM -subject_hash_old -in /ca-cert.cer | head -1` + +adb start-server +emulator -avd virtual_dev -writable-system -no-window -no-audio & +cp /ca-cert.cer /$hashed_name.0 +bash /preconf/install_cert.sh $hashed_name.0 +bash /preconf/install_culebra.sh + +adb emu avd snapshot save configured + +adb emu kill +#to let the host know it finished installing +install -m 777 /dev/null /preconf/finished + +tail -f /dev/null \ No newline at end of file diff --git a/android/conf/install_cert.sh b/pre_android/preconf/install_cert.sh similarity index 100% rename from android/conf/install_cert.sh rename to pre_android/preconf/install_cert.sh diff --git a/android/conf/install_culebra.sh b/pre_android/preconf/install_culebra.sh similarity index 58% rename from android/conf/install_culebra.sh rename to pre_android/preconf/install_culebra.sh index 336d1a1..fde18d5 100644 --- a/android/conf/install_culebra.sh +++ b/pre_android/preconf/install_culebra.sh @@ -1,15 +1,10 @@ +adb wait-for-device cd /root git clone https://github.com/dtmilano/CulebraTester2-public culebra git clone https://gist.github.com/dtmilano/4537110 culebraDependencies export PATH=$PATH:/root/culebraDependencies cd culebra git checkout 4ce1987e7ec6ae627d8f33a1a3b59f684aff90c0 echo "/opt/android-sdk-linux" >> local.properties -./gradlew installDebug installDebugAndroidTest -./culebratester2 start-server & - -#wait for the server to start -while ! curl http://localhost:9987/v2/uiDevice/screenshot > /dev/null 2> /dev/null; do - sleep 0.1 -done \ No newline at end of file +./culebratester2 install \ No newline at end of file diff --git a/start.mjs b/start.mjs index c86e5e3..1e6ee3e 100644 --- a/start.mjs +++ b/start.mjs @@ -1,52 +1,94 @@ import { promises as fs } from "fs"; +async function sleep(time) { + return new Promise((resolve) => setTimeout(resolve, time)); +} + async function checkCertExistance() { return await Promise.all([ fs.access("./certificates/mitmproxy-ca-cert.cer", fs.constants.R_OK), fs.access("./certificates/mitmproxy-ca-cert.p12", fs.constants.R_OK), fs.access("./certificates/mitmproxy-ca-cert.pem", fs.constants.R_OK), fs.access("./certificates/mitmproxy-ca.p12"), fs.access("./certificates/mitmproxy-ca.pem"), fs.access("./certificates/mitmproxy-dhparam.pem", fs.constants.R_OK), ]); } async function generateCert() { //remove certs if they exist try { await $`rm -rf certificates`; } catch { throw new Error( "To remove certificates, and create new ones, this command must be run with sudo" ); } //iniciate docker which will create certs $`docker run --rm -v $PWD/certificates:/home/mitmproxy/.mitmproxy --name certGenerator mitmproxy/mitmproxy:9.0.1 mitmdump &`; //wait for certs to generate let generated = false; while (!generated) { try { await checkCertExistance(); generated = true; } catch {} } //kill docker container - await $`docker stop certGenerator`; + $`docker stop certGenerator`; +} + +async function generatePreAndroid() { + await $`docker build -t pre_android pre_android`; + $`docker run --rm -v $PWD/certificates/mitmproxy-ca-cert.cer:/ca-cert.cer -v $PWD/pre_android/preconf:/preconf --device=/dev/kvm --name pre_android_cont pre_android &`; + + console.log( + "installing tls certificate and culebra into the android pre-image" + ); + //the way of knowing when the culebra install is creating a file in the shared volume + let finished = false; + while (!finished) { + try { + await fs.access( + "./pre_android/preconf/finished", + fs.constants.R_OK + ), + $`rm -f ./pre_android/preconf/finished`; + finished = true; + } catch { + await sleep(100); + } + } + + await $`docker commit pre_android_cont pre_android/ready`; + $`docker stop pre_android_cont`; } if (process.argv.length !== 4) throw new Error("expected an argument"); else if (process.argv[3] === "up") { try { await checkCertExistance(); } catch { await generateCert(); } + + try { + await $`docker image inspect pre_android/ready > /dev/null 2> /dev/null`; + } catch { + await generatePreAndroid(); + } + await $`docker compose build`; await $`docker compose up`; } else if (process.argv[3] === "down") await $`docker compose down`; else if (process.argv[3] === "generateCert") { generateCert(); -} else throw new Error("expected [up | down | generateCert ] as argument"); +} else if (process.argv[3] === "generatePreAndroid") { + generatePreAndroid(); +} else + throw new Error( + "expected [up | down | generateCert | generatePreAndroid ] as argument" + );