Skip to content

Commit

Permalink
feat: custom phcode:// protocol instead of tauri protocol for cachein…
Browse files Browse the repository at this point in the history
…g and csp header rewrite in future
  • Loading branch information
abose committed Oct 26, 2023
1 parent e6808f0 commit 591b8d1
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 63 deletions.
11 changes: 8 additions & 3 deletions src-build/ci-createDistReleaseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,18 @@ async function ciCreateDistReleaseConfig() {

console.log("write version in tauri.conf.json", tauriConfigPath);
configJson = JSON.parse(fs.readFileSync(tauriConfigPath));
configJson.build.distDir = '../phoenix/dist/'
// delete configJson.tauri.updater; // #uncomment_line_for_local_build_1
// delete configJson.tauri.bundle.windows.certificateThumbprint // #uncomment_line_for_local_build_1
configJson.build.distDir = '../phoenix/dist/';
// configJson.tauri.bundle.active = false; // #uncomment_line_for_local_build_1
configJson.package.version = phoenixVersion;
configJson.package.productName = _getProductName(configJson.package.productName, phoenixStage);
console.log("Product name is: ", configJson.package.productName);
configJson.tauri.windows[0].title = configJson.package.productName;
if(os.platform() === 'win32'){
configJson.tauri.windows[0].url = "https://phcode.localhost/";
} else {
configJson.tauri.windows[0].url = "phcode://localhost/";
}
console.log("Window Boot url is: ", configJson.tauri.windows[0].url);
configJson.tauri.updater.endpoints = [
`${UPDATE_NOTIFICATIONS_BASE_URL}${UPDATE_NOTIFICATION_LATEST_JSON_FILE_PATH[phoenixStage]}`
];
Expand Down
9 changes: 8 additions & 1 deletion src-build/createDistReleaseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fs from 'fs';
import chalk from 'chalk';

import {getPlatformDetails} from "./utils.js";
import os from "os";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
Expand All @@ -18,7 +19,13 @@ async function createDistReleaseConfig() {
let configJson = JSON.parse(fs.readFileSync(tauriConfigPath));
console.log(chalk.cyan("\n!Only creating executables. Creating msi, appimage and dmg installers are disabled in this build. If you want to create an installer, use: npm run tauri build manually after setting distDir in tauri conf!\n"));
configJson.tauri.bundle.active = false;
configJson.build.distDir = '../../phoenix/dist/'
configJson.build.distDir = '../../phoenix/dist/';
if(os.platform() === 'win32'){
configJson.tauri.windows[0].url = "https://phcode.localhost/";
} else {
configJson.tauri.windows[0].url = "phcode://localhost/";
}
console.log("Window Boot url is: ", configJson.tauri.windows[0].url);
console.log("Writing new local config json ", tauriLocalConfigPath);
fs.writeFileSync(tauriLocalConfigPath, JSON.stringify(configJson, null, 4));
}
Expand Down
9 changes: 8 additions & 1 deletion src-build/createDistTestReleaseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fs from 'fs';

import {getPlatformDetails} from "./utils.js";
import chalk from "chalk";
import os from "os";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
Expand All @@ -20,7 +21,13 @@ async function createDistTestReleaseConfig() {
configJson.tauri.windows[0].title = "phoenix-tester";
console.log(chalk.cyan("\n!Only creating executables. Creating msi, appimage and dmg installers are disabled in this build. If you want to create an installer, use: npm run tauri build manually after setting distDir in tauri conf!\n"));
configJson.tauri.bundle.active = false;
configJson.build.distDir = '../../phoenix/dist-test/'
configJson.build.distDir = '../../phoenix/dist-test/';
if(os.platform() === 'win32'){
configJson.tauri.windows[0].url = "https://phcode.localhost/";
} else {
configJson.tauri.windows[0].url = "phcode://localhost/";
}
console.log("Window Boot url is: ", configJson.tauri.windows[0].url);
console.log("Writing new local config json ", tauriLocalConfigPath);
fs.writeFileSync(tauriLocalConfigPath, JSON.stringify(configJson, null, 4));
}
Expand Down
7 changes: 7 additions & 0 deletions src-build/createSrcReleaseConfig.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {fileURLToPath} from "url";
import {dirname} from "path";
import fs from 'fs';
import * as os from 'os';

import {getPlatformDetails} from "./utils.js";
import chalk from "chalk";
Expand All @@ -19,6 +20,12 @@ async function createSrcReleaseConfig() {
console.log(chalk.cyan("\n!Only creating executables. Creating msi, appimage and dmg installers are disabled in this build. If you want to create an installer, use: npm run tauri build manually after setting distDir in tauri conf!\n"));
configJson.tauri.bundle.active = false;
configJson.build.distDir = '../../phoenix/src/'
if(os.platform() === 'win32'){
configJson.tauri.windows[0].url = "https://phcode.localhost/";
} else {
configJson.tauri.windows[0].url = "phcode://localhost/";
}
console.log("Window Boot url is: ", configJson.tauri.windows[0].url);
console.log("Writing new local config json ", tauriLocalConfigPath);
fs.writeFileSync(tauriLocalConfigPath, JSON.stringify(configJson, null, 4));
}
Expand Down
1 change: 1 addition & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ tauri-build = { version = "1.5.0", features = [] }
[dependencies]
serde_json = "1.0"
once_cell = "1.18.0"
percent-encoding = "2.1"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.5.2", features = [ "cli", "api-all", "updater", "devtools", "linux-protocol-headers"] }
winapi = { version = "0.3", features = ["fileapi"] }
Expand Down
37 changes: 37 additions & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]

extern crate percent_encoding;
use tauri::http::ResponseBuilder;
use tauri::GlobalWindowEvent;
mod init;
mod utilities;
Expand Down Expand Up @@ -55,6 +57,41 @@ fn process_window_event(event: &GlobalWindowEvent) {

fn main() {
tauri::Builder::default()
.register_uri_scheme_protocol("phcode", move |app, request| { // can't use `tauri` because that's already in use
let path = request.uri().strip_prefix("phcode://localhost").unwrap();
let path = percent_encoding::percent_decode(path.as_bytes())
.decode_utf8_lossy()
.to_string();
// Remove query string and fragment
let path_without_query_or_fragment = path.split('?').next().unwrap_or(&path);
let final_path = path_without_query_or_fragment.split('#').next().unwrap_or(path_without_query_or_fragment).to_string();

let asset_option = app.asset_resolver().get(final_path.clone());
if asset_option.is_none() {
let not_found_response = ResponseBuilder::new()
.status(404)
.mimetype("text/html")
.body("Asset not found".as_bytes().to_vec())
.unwrap();
return Ok(not_found_response);
}

let asset = asset_option.unwrap();

#[cfg(windows)]
let window_origin = "https://phcode.localhost";
#[cfg(not(windows))]
let window_origin = "phcode://localhost";

let builder = ResponseBuilder::new()
.header("Access-Control-Allow-Origin", window_origin)
.header("Origin", window_origin)
.header("Cache-Control", "private, max-age=31536000, immutable") // 1 year cache age expiry
.mimetype(&asset.mime_type);

let response = builder.body(asset.bytes)?;
Ok(response)
})
.plugin(tauri_plugin_fs_extra::init())
.on_window_event(|event| process_window_event(&event))
.invoke_handler(tauri::generate_handler![
Expand Down
109 changes: 51 additions & 58 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,70 +16,47 @@
"all": true,
"fs": {
"all": true,
"scope": [
"$APPCONFIG/**",
"$APPDATA/**",
"$APPLOCALDATA/**",
"$APPCACHE/**",
"$APPLOG/**",
"$AUDIO/**",
"$CACHE/**",
"$CONFIG/**",
"$DATA/**",
"$LOCALDATA/**",
"$DESKTOP/**",
"$DOCUMENT/**",
"$DOWNLOAD/**",
"$EXE/**",
"$FONT/**",
"$HOME/**",
"$PICTURE/**",
"$PUBLIC/**",
"$RUNTIME/**",
"$TEMPLATE/**",
"$VIDEO/**",
"$RESOURCE/**",
"$APP/**",
"$LOG/**",
"$TEMP/**",
"**",
"**/**",
"$APPCONFIG/**/*.*",
"$APPDATA/**/*.*",
"$APPLOCALDATA/**/*.*",
"$APPCACHE/**/*.*",
"$APPLOG/**/*.*",
"$AUDIO/**/*.*",
"$CACHE/**/*.*",
"$CONFIG/**/*.*",
"$DATA/**/*.*",
"$LOCALDATA/**/*.*",
"$DESKTOP/**/*.*",
"$DOCUMENT/**/*.*",
"$DOWNLOAD/**/*.*",
"$EXE/**/*.*",
"$FONT/**/*.*",
"$HOME/**/*.*",
"$PICTURE/**/*.*",
"$PUBLIC/**/*.*",
"$RUNTIME/**/*.*",
"$TEMPLATE/**/*.*",
"$VIDEO/**/*.*",
"$RESOURCE/**/*.*",
"$APP/**/*.*",
"$LOG/**/*.*",
"$TEMP/**/*.*",
"**/*.*",
"**/**/*.*"
]
"scope":{
"allow": [
"$APPCONFIG/**",
"$APPDATA/**",
"$APPLOCALDATA/**",
"$APPCACHE/**",
"$APPLOG/**",
"$AUDIO/**",
"$CACHE/**",
"$CONFIG/**",
"$DATA/**",
"$LOCALDATA/**",
"$DESKTOP/**",
"$DOCUMENT/**",
"$DOWNLOAD/**",
"$EXE/**",
"$FONT/**",
"$HOME/**",
"$PICTURE/**",
"$PUBLIC/**",
"$RUNTIME/**",
"$TEMPLATE/**",
"$VIDEO/**",
"$RESOURCE/**",
"$APP/**",
"$LOG/**",
"$TEMP/**",
"**",
"**/**"
],
"requireLiteralLeadingDot": false
}
},
"protocol": {
"asset": true,
"assetScope": {
"allow": [
"$APPLOCALDATA/assets/**",
"$APPLOCALDATA/assets/**/*.*"
]
],
"requireLiteralLeadingDot": false
}
},
"shell": {
Expand Down Expand Up @@ -241,7 +218,23 @@
},
"security": {
"csp": null,
"dangerousDisableAssetCspModification": true
"dangerousDisableAssetCspModification": true,
"dangerousRemoteDomainIpcAccess": [
{
"scheme": "phcode",
"domain": "localhost",
"enableTauriAPI": true,
"plugins": ["fs-extra"],
"windows": ["main"]
},
{
"scheme": "https",
"domain": "phcode.localhost",
"enableTauriAPI": true,
"plugins": ["fs-extra"],
"windows": ["main"]
}
]
},
"updater": {
"active": true,
Expand Down

0 comments on commit 591b8d1

Please sign in to comment.