1
1
use std:: {
2
+ borrow:: Cow ,
2
3
io:: { Read , Write } ,
3
4
net:: { SocketAddr , TcpListener , TcpStream } ,
4
5
thread,
@@ -11,12 +12,6 @@ use tauri::{
11
12
12
13
const EXIT : [ u8 ; 4 ] = [ 1 , 3 , 3 , 7 ] ;
13
14
14
- /// Initializes the plugin.
15
- #[ must_use]
16
- pub fn init < R : Runtime > ( ) -> TauriPlugin < R > {
17
- Builder :: new ( "oauth" ) . build ( )
18
- }
19
-
20
15
/// Starts the localhost (using 127.0.0.1) server. Returns the port its listening on.
21
16
///
22
17
/// 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> {
33
28
/// # Panics
34
29
///
35
30
/// 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 ,
38
43
mut handler : F ,
39
44
) -> 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
+ } ?;
41
55
42
56
let port = listener. local_addr ( ) ?. port ( ) ;
43
57
44
58
thread:: spawn ( move || {
45
59
for conn in listener. incoming ( ) {
46
60
match conn {
47
61
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) {
49
63
// Using an empty string to communicate that a shutdown was requested.
50
64
if !url. is_empty ( ) {
51
65
handler ( url) ;
@@ -60,14 +74,11 @@ pub fn start<F: FnMut(String) + Send + 'static>(
60
74
}
61
75
}
62
76
} ) ;
77
+
63
78
Ok ( port)
64
79
}
65
80
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 > {
71
82
let mut buffer = [ 0 ; 4048 ] ;
72
83
if let Err ( io_err) = conn. read ( & mut buffer) {
73
84
log:: error!( "Error reading incoming connection: {}" , io_err. to_string( ) ) ;
0 commit comments