25 lines
1.5 KiB
JavaScript
25 lines
1.5 KiB
JavaScript
|
|
const { withAndroidManifest, withDangerousMod } = require('expo/config-plugins');
|
||
|
|
const fs = require('node:fs/promises');
|
||
|
|
const path = require('node:path');
|
||
|
|
const { environment } = require('../config/environment.cjs');
|
||
|
|
// Android denies cleartext globally; allow only the exact configured internal hosts.
|
||
|
|
module.exports = (config, options) => {
|
||
|
|
const env = environment();
|
||
|
|
const hosts = [...new Set(options?.hosts || [])];
|
||
|
|
const expected = [...new Set([env.apiUrl, env.updateOrigin].filter(s => s.startsWith('http:')).map(s => new URL(s).hostname))];
|
||
|
|
if (JSON.stringify(hosts) !== JSON.stringify(expected)) throw Error('Native network hosts must match the environment config');
|
||
|
|
config = withAndroidManifest(config, cfg => {
|
||
|
|
const app = cfg.modResults.manifest.application[0];
|
||
|
|
app.$['android:networkSecurityConfig'] = '@xml/cmvr_network_security';
|
||
|
|
app.$['android:usesCleartextTraffic'] = 'false';
|
||
|
|
return cfg;
|
||
|
|
});
|
||
|
|
return withDangerousMod(config, ['android', async cfg => {
|
||
|
|
const dir = path.join(cfg.modRequest.platformProjectRoot, 'app/src/main/res/xml');
|
||
|
|
await fs.mkdir(dir, { recursive: true });
|
||
|
|
await fs.writeFile(path.join(dir, 'cmvr_network_security.xml'),
|
||
|
|
`<?xml version="1.0" encoding="utf-8"?><network-security-config><base-config cleartextTrafficPermitted="false"/>${hosts.length ? `<domain-config cleartextTrafficPermitted="true">${hosts.map(h => `<domain includeSubdomains="false">${h}</domain>`).join('')}</domain-config>` : ''}</network-security-config>`);
|
||
|
|
return cfg;
|
||
|
|
}]);
|
||
|
|
};
|