Skip to content

Commit d6d5f37

Browse files
feat: add --root-certificate-path option for mobile dev (#13358)
* feat: add `--root-certificate-path` option for mobile dev lets you use a HTTPS development server example usage: ``` cargo install tauri-cli --git https://github.com/tauri-apps/tauri --branch feat/mobile-dev-cert cargo tauri android dev --open --root-certificate-path "/Users/lucas/Library/Application Support/mkcert/rootCA.pem" --features tauri/rustls-tls ``` * Apply suggestions from code review Co-authored-by: Fabian-Lars <[email protected]> --------- Co-authored-by: Fabian-Lars <[email protected]>
1 parent 7261a14 commit d6d5f37

File tree

5 files changed

+68
-1
lines changed

5 files changed

+68
-1
lines changed

.changes/load-cert-mobile.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": minor:feat
3+
---
4+
5+
Load root certificate from CLI-set environment variable and use it on the mobile dev server proxy.

.changes/mobile-dev-root-cert.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": minor:feat
3+
"@tauri-apps/cli": minor:feat
4+
---
5+
6+
Added `--root-certificate-path` option to `android dev` and `ios dev` to be able to connect to HTTPS dev servers.

crates/tauri-cli/src/mobile/android/dev.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ pub struct Options {
104104
/// e.g. `tauri android dev -- [runnerArgs]`.
105105
#[clap(last(true))]
106106
pub args: Vec<String>,
107+
/// Path to the certificate file used by your dev server. Required for mobile dev when using HTTPS.
108+
#[clap(long, env = "TAURI_DEV_ROOT_CERTIFICATE_PATH")]
109+
pub root_certificate_path: Option<PathBuf>,
107110
}
108111

109112
impl From<Options> for DevOptions {
@@ -138,6 +141,13 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
138141

139142
fn run_command(options: Options, noise_level: NoiseLevel) -> Result<()> {
140143
delete_codegen_vars();
144+
// setup env additions before calling env()
145+
if let Some(root_certificate_path) = &options.root_certificate_path {
146+
std::env::set_var(
147+
"TAURI_DEV_ROOT_CERTIFICATE",
148+
std::fs::read_to_string(root_certificate_path).context("failed to read certificate file")?,
149+
);
150+
}
141151

142152
let tauri_config = get_tauri_config(
143153
tauri_utils::platform::Target::Android,

crates/tauri-cli/src/mobile/ios/dev.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ pub struct Options {
109109
/// e.g. `tauri ios dev -- [runnerArgs]`.
110110
#[clap(last(true))]
111111
pub args: Vec<String>,
112+
/// Path to the certificate file used by your dev server. Required for mobile dev when using HTTPS.
113+
#[clap(long, env = "TAURI_DEV_ROOT_CERTIFICATE_PATH")]
114+
pub root_certificate_path: Option<PathBuf>,
112115
}
113116

114117
impl From<Options> for DevOptions {
@@ -142,6 +145,14 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
142145
}
143146

144147
fn run_command(options: Options, noise_level: NoiseLevel) -> Result<()> {
148+
// setup env additions before calling env()
149+
if let Some(root_certificate_path) = &options.root_certificate_path {
150+
std::env::set_var(
151+
"TAURI_DEV_ROOT_CERTIFICATE",
152+
std::fs::read_to_string(root_certificate_path).context("failed to read certificate file")?,
153+
);
154+
}
155+
145156
let env = env()?;
146157
let device = if options.open {
147158
None

crates/tauri/src/protocol/tauri.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,42 @@ fn get_response<R: Runtime>(
114114
decoded_path.trim_start_matches('/')
115115
);
116116

117-
let mut proxy_builder = reqwest::ClientBuilder::new()
117+
let mut client = reqwest::ClientBuilder::new();
118+
119+
if url.starts_with("https://") {
120+
// we can't load env vars at runtime, gotta embed them in the lib
121+
if let Some(cert_pem) = option_env!("TAURI_DEV_ROOT_CERTIFICATE") {
122+
#[cfg(any(
123+
feature = "native-tls",
124+
feature = "native-tls-vendored",
125+
feature = "rustls-tls"
126+
))]
127+
{
128+
log::info!("adding dev server root certificate");
129+
client = client.add_root_certificate(
130+
reqwest::Certificate::from_pem(cert_pem.as_bytes())
131+
.expect("failed to parse TAURI_DEV_ROOT_CERTIFICATE"),
132+
);
133+
}
134+
135+
#[cfg(not(any(
136+
feature = "native-tls",
137+
feature = "native-tls-vendored",
138+
feature = "rustls-tls"
139+
)))]
140+
{
141+
log::warn!(
142+
"the dev root-certificate-path option was provided, but you must enable one of the following Tauri features in Cargo.toml: native-tls, native-tls-vendored, rustls-tls"
143+
);
144+
}
145+
} else {
146+
log::warn!(
147+
"loading HTTPS URL; you might need to provide a certificate via the `dev --root-certificate-path` option. You must enable one of the following Tauri features in Cargo.toml: native-tls, native-tls-vendored, rustls-tls"
148+
);
149+
}
150+
}
151+
152+
let mut proxy_builder = client
118153
.build()
119154
.unwrap()
120155
.request(request.method().clone(), &url);

0 commit comments

Comments
 (0)