From 82f1f61c6b3dc9b4c073e57049dd46967be18013 Mon Sep 17 00:00:00 2001 From: CrypticC3s4r <27031384+CrypticC3s4r@users.noreply.github.com> Date: Sat, 28 Dec 2024 20:12:19 +0100 Subject: [PATCH] fix(mobile): Add support for self signed certs in android. Fixes #381 (#416) Co-authored-by: MohamedBassem --- .gitignore | 7 ++- apps/mobile/app.json | 1 + .../plugins/network_security_config.xml | 9 ++++ apps/mobile/plugins/trust-local-certs.js | 43 +++++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 apps/mobile/plugins/network_security_config.xml create mode 100644 apps/mobile/plugins/trust-local-certs.js diff --git a/.gitignore b/.gitignore index 8bc17e431..53b7c2171 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ # dependencies node_modules .pnp +.pnpm-store .pnp.js .yarn/install-state.gz @@ -53,4 +54,8 @@ data # Idea .idea *.iml -.aider* + +# Aider +.vscode + +# VS-Code diff --git a/apps/mobile/app.json b/apps/mobile/app.json index 87db856fb..c1dc6bf9d 100644 --- a/apps/mobile/app.json +++ b/apps/mobile/app.json @@ -51,6 +51,7 @@ "versionCode": 20 }, "plugins": [ + "./plugins/trust-local-certs.js", "./plugins/camera-not-required.js", "expo-router", [ diff --git a/apps/mobile/plugins/network_security_config.xml b/apps/mobile/plugins/network_security_config.xml new file mode 100644 index 000000000..bb6ab93df --- /dev/null +++ b/apps/mobile/plugins/network_security_config.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/apps/mobile/plugins/trust-local-certs.js b/apps/mobile/plugins/trust-local-certs.js new file mode 100644 index 000000000..402750342 --- /dev/null +++ b/apps/mobile/plugins/trust-local-certs.js @@ -0,0 +1,43 @@ +const { AndroidConfig, withAndroidManifest } = require("@expo/config-plugins"); +const { Paths } = require("@expo/config-plugins/build/android"); +const path = require("path"); +const fs = require("fs"); +const fsPromises = fs.promises; + +const { getMainApplicationOrThrow } = AndroidConfig.Manifest; + +const withTrustLocalCerts = (config) => { + return withAndroidManifest(config, async (config) => { + config.modResults = await setCustomConfigAsync(config, config.modResults); + return config; + }); +}; + +async function setCustomConfigAsync(config, androidManifest) { + const src_file_pat = path.join(__dirname, "network_security_config.xml"); + const res_file_path = path.join( + await Paths.getResourceFolderAsync(config.modRequest.projectRoot), + "xml", + "network_security_config.xml", + ); + + const res_dir = path.resolve(res_file_path, ".."); + + if (!fs.existsSync(res_dir)) { + await fsPromises.mkdir(res_dir); + } + + try { + await fsPromises.copyFile(src_file_pat, res_file_path); + } catch (e) { + throw e; + } + + const mainApplication = getMainApplicationOrThrow(androidManifest); + mainApplication.$["android:networkSecurityConfig"] = + "@xml/network_security_config"; + + return androidManifest; +} + +module.exports = withTrustLocalCerts;