Skip to content

Commit f479c28

Browse files
committed
feat!: add start_with_config() to allow configuring ports.
BREAKING CHANGE: remove response arg from start()
1 parent b06eb34 commit f479c28

File tree

3 files changed

+39
-26
lines changed

3 files changed

+39
-26
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ httparse = "1"
1212
log = "0.4"
1313
url = "2"
1414
tauri = { version = "1" }
15+
serde = "1"
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
#![cfg_attr(
2-
all(not(debug_assertions), target_os = "windows"),
3-
windows_subsystem = "windows"
2+
all(not(debug_assertions), target_os = "windows"),
3+
windows_subsystem = "windows"
44
)]
55

66
use tauri::{command, Window};
77
use tauri_plugin_oauth::start;
88

99
#[command]
1010
async fn start_server(window: Window) -> Result<u16, String> {
11-
start(None, move |url| {
12-
// Because of the unprotected localhost port, you must verify the URL here.
13-
// Preferebly send back only the token, or nothing at all if you can handle everything else in Rust.
14-
let _ = window.emit("redirect_uri", url);
15-
}).map_err(|err| err.to_string())
11+
start(move |url| {
12+
// Because of the unprotected localhost port, you must verify the URL here.
13+
// Preferebly send back only the token, or nothing at all if you can handle everything else in Rust.
14+
let _ = window.emit("redirect_uri", url);
15+
})
16+
.map_err(|err| err.to_string())
1617
}
1718

1819
fn main() {
19-
tauri::Builder::default()
20-
.invoke_handler(tauri::generate_handler![start_server])
21-
.run(tauri::generate_context!())
22-
.expect("error while running tauri application");
20+
tauri::Builder::default()
21+
.invoke_handler(tauri::generate_handler![start_server])
22+
.run(tauri::generate_context!())
23+
.expect("error while running tauri application");
2324
}

src/lib.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::{
2+
borrow::Cow,
23
io::{Read, Write},
34
net::{SocketAddr, TcpListener, TcpStream},
45
thread,
@@ -11,12 +12,6 @@ use tauri::{
1112

1213
const EXIT: [u8; 4] = [1, 3, 3, 7];
1314

14-
/// Initializes the plugin.
15-
#[must_use]
16-
pub fn init<R: Runtime>() -> TauriPlugin<R> {
17-
Builder::new("oauth").build()
18-
}
19-
2015
/// Starts the localhost (using 127.0.0.1) server. Returns the port its listening on.
2116
///
2217
/// Because of the unprotected localhost port, you _must_ verify the URL in the handler function.
@@ -33,19 +28,38 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
3328
/// # Panics
3429
///
3530
/// The seperate server thread can panic if its unable to send the html response to the client. This may change after more real world testing.
36-
pub fn start<F: FnMut(String) + Send + 'static>(
37-
response: Option<&'static str>,
31+
pub fn start<F: FnMut(String) + Send + 'static>(handler: F) -> Result<u16, std::io::Error> {
32+
start_with_config(OauthConfig::default(), handler)
33+
}
34+
35+
#[derive(Default, serde::Deserialize)]
36+
pub struct OauthConfig {
37+
pub ports: Option<Vec<u16>>,
38+
pub response: Option<Cow<'static, str>>,
39+
}
40+
41+
pub fn start_with_config<F: FnMut(String) + Send + 'static>(
42+
config: OauthConfig,
3843
mut handler: F,
3944
) -> Result<u16, std::io::Error> {
40-
let listener = TcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 0)))?;
45+
let listener = match config.ports {
46+
Some(ports) => TcpListener::bind(
47+
ports
48+
.iter()
49+
.map(|p| SocketAddr::from(([127, 0, 0, 1], *p)))
50+
.collect::<Vec<SocketAddr>>()
51+
.as_slice(),
52+
),
53+
None => TcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 0))),
54+
}?;
4155

4256
let port = listener.local_addr()?.port();
4357

4458
thread::spawn(move || {
4559
for conn in listener.incoming() {
4660
match conn {
4761
Ok(conn) => {
48-
if let Some(url) = handle_connection(conn, response, port) {
62+
if let Some(url) = handle_connection(conn, config.response.as_deref(), port) {
4963
// Using an empty string to communicate that a shutdown was requested.
5064
if !url.is_empty() {
5165
handler(url);
@@ -60,14 +74,11 @@ pub fn start<F: FnMut(String) + Send + 'static>(
6074
}
6175
}
6276
});
77+
6378
Ok(port)
6479
}
6580

66-
fn handle_connection(
67-
mut conn: TcpStream,
68-
response: Option<&'static str>,
69-
port: u16,
70-
) -> Option<String> {
81+
fn handle_connection(mut conn: TcpStream, response: Option<&str>, port: u16) -> Option<String> {
7182
let mut buffer = [0; 4048];
7283
if let Err(io_err) = conn.read(&mut buffer) {
7384
log::error!("Error reading incoming connection: {}", io_err.to_string());

0 commit comments

Comments
 (0)