Save username/password, clean up proxy startup

This commit is contained in:
Travis Burtrum 2022-10-02 22:30:56 -04:00
parent b1042f55ea
commit 74d9837402
5 changed files with 112 additions and 22 deletions

67
dist/index.html vendored
View File

@ -23,21 +23,80 @@
// access the pre-bundled global API functions
const { invoke } = window.__TAURI__.tauri;
invoke('start_proxy').then((port) => {
console.log('start_proxy Completed!, port: ' + port);
function addCredentials (login, password) {
localStorage.setItem('login', login);
localStorage.setItem('password', password);
}
function getCredentials () {
const credentials = {}
credentials.login = localStorage.getItem('login') || '';
if (credentials.login) {
credentials.password = localStorage.getItem('password');
}
return credentials;
}
function removeCredentials () {
localStorage.removeItem('login');
localStorage.removeItem('password');
}
converse.plugins.add('converse-desktop-credentials', {
initialize () {
console.log("converse-desktop-credentials - initialize");
const { _converse } = this;
const { api } = _converse;
api.listen.on('afterResourceBinding', () => {
console.log("converse-desktop-credentials - afterResourceBinding");
if (_converse.connection.pass) {
addCredentials(
_converse.bare_jid,
_converse.connection.pass
);
}
});
api.listen.on('logout', () => {
console.log("converse-desktop-credentials - logout");
removeCredentials();
});
}
});
const { login, password } = getCredentials();
console.log('login: ' + login);
invoke('proxy_port').then((port) => {
console.log('proxy_port: ' + port);
converse.initialize({
auto_login: login && password,
jid: login,
password: password,
authentication: 'login',
auto_away: 300,
auto_reconnect: true,
//websocket_url: 'wss://burtrum.org/xmpp-websocket/',
websocket_url: 'ws://127.0.0.1:' + port + '/xmpp-websocket/',
assets_path: './dist/',
discover_connection_methods: false,
message_archiving: 'always',
play_sounds: false,
view_mode: 'fullscreen'
view_mode: 'fullscreen',
loglevel: 'debug',
muc_respect_autojoin: true,
muc_show_logs_before_join: true,
whitelisted_plugins: ['converse-desktop-credentials']
}).catch((reason) => {
document.body.innerText = "error starting converse: " + reason;
console.log(document.body.innerText);
});
}).catch((reason) => {
document.body.innerText = "error starting proxy: " + reason;
console.log(document.body.innerText);
});
</script>
</body>

1
src-tauri/Cargo.lock generated
View File

@ -336,6 +336,7 @@ dependencies = [
name = "converse-tauri"
version = "0.1.0"
dependencies = [
"once_cell",
"serde",
"serde_json",
"tauri",

View File

@ -19,6 +19,7 @@ serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.1.1", features = ["api-all", "devtools", "system-tray"] }
once_cell = "1"
tokio = { version = "1.9", features = ["net"] }
xmpp-proxy = { git = "https://github.com/moparisthebest/xmpp-proxy", default-features = false, features = ["c2s-outgoing", "tls", "quic", "websocket", "tls-ca-roots-bundled"] }

View File

@ -3,6 +3,7 @@
windows_subsystem = "windows"
)]
use once_cell::sync::OnceCell;
use std::sync::Arc;
use tauri::{
CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem,
@ -13,9 +14,15 @@ use xmpp_proxy::{
outgoing::spawn_outgoing_listener,
};
static PROXY_PORT: OnceCell<Result<u16, &'static str>> = OnceCell::new();
#[tauri::command]
async fn start_proxy() -> u16 {
println!("starting proxy");
fn proxy_port() -> Result<u16, &'static str> {
println!("returning proxy_port");
*PROXY_PORT.wait()
}
async fn start_proxy() -> Result<u16, std::io::Error> {
let outgoing_cfg = OutgoingConfig {
// limit incoming stanzas to this many bytes, default to ejabberd's default
// https://github.com/processone/ejabberd/blob/master/ejabberd.yml.example#L32
@ -23,14 +30,27 @@ async fn start_proxy() -> u16 {
max_stanza_size_bytes: 262_144,
certs_key: Arc::new(CertsKey {}),
};
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
// try to listen to a specific port (10032), but otherwise listen on any available
let listener = match TcpListener::bind("127.0.0.1:10032").await {
Ok(listener) => listener,
Err(_) => TcpListener::bind("127.0.0.1:0").await?,
};
let port = listener.local_addr()?.port();
spawn_outgoing_listener(listener, outgoing_cfg);
println!("started proxy at port: {port}");
port
Ok(port)
}
fn main() {
tauri::async_runtime::spawn(async {
PROXY_PORT
.set(
start_proxy()
.await
.map_err(|e| Box::leak(Box::new(format!("{:?}", e))).as_str()),
)
.expect("this is the only place that calls set");
});
let tray_menu = SystemTrayMenu::new()
.add_item(CustomMenuItem::new("show".to_string(), "Show"))
.add_native_item(SystemTrayMenuItem::Separator)
@ -39,6 +59,15 @@ fn main() {
.add_item(CustomMenuItem::new("quit".to_string(), "Quit"));
let system_tray = SystemTray::new().with_menu(tray_menu);
tauri::Builder::default()
.setup(|app| {
#[cfg(debug_assertions)] // only include this code on debug builds
{
let window = app.get_window("main").unwrap();
window.open_devtools();
window.close_devtools();
}
Ok(())
})
.system_tray(system_tray)
.on_system_tray_event(|app, event| match event {
SystemTrayEvent::LeftClick {
@ -81,7 +110,7 @@ fn main() {
},
_ => {}
})
.invoke_handler(tauri::generate_handler![start_proxy])
.invoke_handler(tauri::generate_handler![proxy_port])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}