Skip to content

Commit

Permalink
fix(mobile): Add support for self signed certs in android. Fixes #381 (
Browse files Browse the repository at this point in the history
…#416)

Co-authored-by: MohamedBassem <me@mbassem.com>
  • Loading branch information
CrypticC3s4r and MohamedBassem authored Dec 28, 2024
1 parent a1a3a7e commit 82f1f61
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# dependencies
node_modules
.pnp
.pnpm-store
.pnp.js
.yarn/install-state.gz

Expand Down Expand Up @@ -53,4 +54,8 @@ data
# Idea
.idea
*.iml
.aider*

# Aider
.vscode

# VS-Code
1 change: 1 addition & 0 deletions apps/mobile/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"versionCode": 20
},
"plugins": [
"./plugins/trust-local-certs.js",
"./plugins/camera-not-required.js",
"expo-router",
[
Expand Down
9 changes: 9 additions & 0 deletions apps/mobile/plugins/network_security_config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
<certificates src="user" />
</trust-anchors>
</base-config>
</network-security-config>
43 changes: 43 additions & 0 deletions apps/mobile/plugins/trust-local-certs.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 82f1f61

Please sign in to comment.