Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Run obfuscated builds #36

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions dist/server/php.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ function retrievePhpIniSettings() {
cwd: appPath,
env
};
return yield (0, util_1.promisify)(child_process_1.execFile)(state_1.default.php, ['artisan', 'native:php-ini'], phpOptions);
let command = ['artisan', 'native:php-ini'];
if (runningSecureBuild()) {
command.unshift((0, path_1.join)(appPath, 'build', '__nativephp_app_bundle'));
}
return yield (0, util_1.promisify)(child_process_1.execFile)(state_1.default.php, command, phpOptions);
});
}
exports.retrievePhpIniSettings = retrievePhpIniSettings;
Expand All @@ -61,11 +65,18 @@ function retrieveNativePHPConfig() {
cwd: appPath,
env
};
return yield (0, util_1.promisify)(child_process_1.execFile)(state_1.default.php, ['artisan', 'native:config'], phpOptions);
let command = ['artisan', 'native:config'];
if (runningSecureBuild()) {
command.unshift((0, path_1.join)(appPath, 'build', '__nativephp_app_bundle'));
}
return yield (0, util_1.promisify)(child_process_1.execFile)(state_1.default.php, command, phpOptions);
});
}
exports.retrieveNativePHPConfig = retrieveNativePHPConfig;
function callPhp(args, options, phpIniSettings = {}) {
if (args[0] === 'artisan' && runningSecureBuild()) {
args.unshift((0, path_1.join)(appPath, 'build', '__nativephp_app_bundle'));
}
let defaultIniSettings = {
'memory_limit': '512M',
'curl.cainfo': state_1.default.caCert,
Expand All @@ -75,6 +86,9 @@ function callPhp(args, options, phpIniSettings = {}) {
Object.keys(iniSettings).forEach(key => {
args.unshift('-d', `${key}=${iniSettings[key]}`);
});
if (parseInt(process.env.SHELL_VERBOSITY) > 0) {
console.log('Calling PHP', state_1.default.php, args);
}
return (0, child_process_1.spawn)(state_1.default.php, args, {
cwd: options.cwd,
env: Object.assign(Object.assign({}, process.env), options.env),
Expand Down Expand Up @@ -112,7 +126,6 @@ function ensureAppFoldersAreAvailable() {
function startQueueWorker(secret, apiPort, phpIniSettings = {}) {
const env = {
APP_ENV: process.env.NODE_ENV === 'development' ? 'local' : 'production',
APP_DEBUG: process.env.NODE_ENV === 'development' ? 'true' : 'false',
NATIVEPHP_STORAGE_PATH: storagePath,
NATIVEPHP_DATABASE_PATH: databaseFile,
NATIVEPHP_API_URL: `http://localhost:${apiPort}/api/`,
Expand Down Expand Up @@ -146,7 +159,7 @@ function getPath(name) {
function getDefaultEnvironmentVariables(secret, apiPort) {
return {
APP_ENV: process.env.NODE_ENV === 'development' ? 'local' : 'production',
APP_DEBUG: process.env.NODE_ENV === 'development' ? 'true' : 'false',
LARAVEL_STORAGE_PATH: storagePath,
NATIVEPHP_STORAGE_PATH: storagePath,
NATIVEPHP_DATABASE_PATH: databaseFile,
NATIVEPHP_API_URL: `http://localhost:${apiPort}/api/`,
Expand All @@ -164,6 +177,9 @@ function getDefaultEnvironmentVariables(secret, apiPort) {
NATIVEPHP_RECENT_PATH: getPath('recent'),
};
}
function runningSecureBuild() {
return (0, fs_1.existsSync)((0, path_1.join)(appPath, 'build', '__nativephp_app_bundle'));
}
function serveApp(secret, apiPort, phpIniSettings) {
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
const appPath = getAppPath();
Expand All @@ -176,18 +192,20 @@ function serveApp(secret, apiPort, phpIniSettings) {
env
};
const store = new electron_store_1.default();
callPhp(['artisan', 'storage:link', '--force'], phpOptions, phpIniSettings);
if (store.get('migrated_version') !== electron_1.app.getVersion() && process.env.NODE_ENV !== 'development') {
if (!runningSecureBuild()) {
callPhp(['artisan', 'storage:link', '--force'], phpOptions, phpIniSettings);
}
if (store.get('migrated_version') !== electron_1.app.getVersion() && (process.env.NODE_ENV !== 'development' || runningSecureBuild())) {
console.log('Migrating database...');
callPhp(['artisan', 'migrate', '--force'], phpOptions, phpIniSettings);
store.set('migrated_version', electron_1.app.getVersion());
}
if (process.env.NODE_ENV === 'development') {
if (process.env.NODE_ENV === 'development' && !runningSecureBuild()) {
console.log('Skipping Database migration while in development.');
console.log('You may migrate manually by running: php artisan native:migrate');
}
const phpPort = yield getPhpPort();
const serverPath = (0, path_1.join)(appPath, 'vendor', 'laravel', 'framework', 'src', 'Illuminate', 'Foundation', 'resources', 'server.php');
let serverPath = (0, path_1.join)(appPath, 'build', '__nativephp_app_bundle');
const phpServer = callPhp(['-S', `127.0.0.1:${phpPort}`, serverPath], {
cwd: (0, path_1.join)(appPath, 'public'),
env
Expand Down
67 changes: 50 additions & 17 deletions src/server/php.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,24 @@ async function getPhpPort() {
}

async function retrievePhpIniSettings() {
const env = {
NATIVEPHP_RUNNING: 'true',
NATIVEPHP_STORAGE_PATH: storagePath,
NATIVEPHP_DATABASE_PATH: databaseFile,
};
const env = {
NATIVEPHP_RUNNING: 'true',
NATIVEPHP_STORAGE_PATH: storagePath,
NATIVEPHP_DATABASE_PATH: databaseFile,
};

const phpOptions = {
cwd: appPath,
env
};
const phpOptions = {
cwd: appPath,
env
};

let command = ['artisan', 'native:php-ini'];

return await promisify(execFile)(state.php, ['artisan', 'native:php-ini'], phpOptions);
if (runningSecureBuild()) {
command.unshift(join(appPath, 'build', '__nativephp_app_bundle'));
}

return await promisify(execFile)(state.php, command, phpOptions);
}

async function retrieveNativePHPConfig() {
Expand All @@ -49,10 +55,20 @@ async function retrieveNativePHPConfig() {
env
};

return await promisify(execFile)(state.php, ['artisan', 'native:config'], phpOptions);
let command = ['artisan', 'native:config'];

if (runningSecureBuild()) {
command.unshift(join(appPath, 'build', '__nativephp_app_bundle'));
}

return await promisify(execFile)(state.php, command, phpOptions);
}

function callPhp(args, options, phpIniSettings = {}) {
if (args[0] === 'artisan' && runningSecureBuild()) {
args.unshift(join(appPath, 'build', '__nativephp_app_bundle'));
}

let defaultIniSettings = {
'memory_limit': '512M',
'curl.cainfo': state.caCert,
Expand All @@ -65,6 +81,10 @@ function callPhp(args, options, phpIniSettings = {}) {
args.unshift('-d', `${key}=${iniSettings[key]}`);
});

if (parseInt(process.env.SHELL_VERBOSITY) > 0) {
console.log('Calling PHP', state.php, args)
}

return spawn(
state.php,
args,
Expand Down Expand Up @@ -122,7 +142,7 @@ function ensureAppFoldersAreAvailable() {
function startQueueWorker(secret, apiPort, phpIniSettings = {}) {
const env = {
APP_ENV: process.env.NODE_ENV === 'development' ? 'local' : 'production',
APP_DEBUG: process.env.NODE_ENV === 'development' ? 'true' : 'false',
// APP_DEBUG: process.env.NODE_ENV === 'development' ? 'true' : 'false',
NATIVEPHP_STORAGE_PATH: storagePath,
NATIVEPHP_DATABASE_PATH: databaseFile,
NATIVEPHP_API_URL: `http://localhost:${apiPort}/api/`,
Expand Down Expand Up @@ -161,7 +181,8 @@ function getPath(name: string) {
function getDefaultEnvironmentVariables(secret, apiPort) {
return {
APP_ENV: process.env.NODE_ENV === 'development' ? 'local' : 'production',
APP_DEBUG: process.env.NODE_ENV === 'development' ? 'true' : 'false',
// APP_DEBUG: process.env.NODE_ENV === 'development' ? 'true' : 'false',
LARAVEL_STORAGE_PATH: storagePath,
NATIVEPHP_STORAGE_PATH: storagePath,
NATIVEPHP_DATABASE_PATH: databaseFile,
NATIVEPHP_API_URL: `http://localhost:${apiPort}/api/`,
Expand All @@ -180,6 +201,10 @@ function getDefaultEnvironmentVariables(secret, apiPort) {
};
}

function runningSecureBuild() {
return existsSync(join(appPath, 'build', '__nativephp_app_bundle'))
}

function serveApp(secret, apiPort, phpIniSettings): Promise<ProcessResult> {
return new Promise(async (resolve, reject) => {
const appPath = getAppPath();
Expand All @@ -201,23 +226,31 @@ function serveApp(secret, apiPort, phpIniSettings): Promise<ProcessResult> {

// Make sure the storage path is linked - as people can move the app around, we
// need to run this every time the app starts
callPhp(['artisan', 'storage:link', '--force'], phpOptions, phpIniSettings)
if (! runningSecureBuild()) {
callPhp(['artisan', 'storage:link', '--force'], phpOptions, phpIniSettings)
}

// Migrate the database
if (store.get('migrated_version') !== app.getVersion() && process.env.NODE_ENV !== 'development') {
if (store.get('migrated_version') !== app.getVersion() && (process.env.NODE_ENV !== 'development' || runningSecureBuild())) {
console.log('Migrating database...')
callPhp(['artisan', 'migrate', '--force'], phpOptions, phpIniSettings)
store.set('migrated_version', app.getVersion())
}

if (process.env.NODE_ENV === 'development') {
if (process.env.NODE_ENV === 'development' && ! runningSecureBuild()) {
console.log('Skipping Database migration while in development.')
console.log('You may migrate manually by running: php artisan native:migrate')
}

const phpPort = await getPhpPort();

const serverPath = join(appPath, 'vendor', 'laravel', 'framework', 'src', 'Illuminate', 'Foundation', 'resources', 'server.php')
let serverPath = join(appPath, 'build', '__nativephp_app_bundle');

// if (process.env.NODE_ENV !== 'production' || ! runningSecureBuild()) {
// console.log('* * * Running from source * * *');
// serverPath = join(appPath, 'vendor', 'laravel', 'framework', 'src', 'Illuminate', 'Foundation', 'resources', 'server.php');
// }

const phpServer = callPhp(['-S', `127.0.0.1:${phpPort}`, serverPath], {
cwd: join(appPath, 'public'),
env
Expand Down
Loading