diff --git a/README.md b/README.md index 290caf5..9e9dabc 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ A chat bot alternative to control [belaUI](https://github.com/BELABOX/belaUI) in ## How do i run this? -Just download the latest binary from [releases](https://github.com/715209/belabot/releases) and execute it. +Just download the latest binary from [releases](https://github.com/IRLServer/belabot/releases) and execute it. ## Config @@ -73,6 +73,7 @@ Example of the config that will be automatically generated upon running the bina ```JSON "belabox": { + "remote_url": "wss://remote.belabox.net/ws/remote", "remote_key": "key", "custom_interface_name": { "eth0": "Something", @@ -144,4 +145,7 @@ After running the executable successfully you can use the following commands in ## Disclaimer -This is a third party tool, please do not ask for help on the BELABOX discord server. Instead, join the [NOALBS Community Server](https://discord.gg/efWu5HWM2u) for all your questions. +This is a third party tool, please do not ask for help on the BELABOX or NOALBS discord server. Instead, join the [IRLServer discord](https://irlserver.com/discord) for all your questions. + +Thanks to [NOALBS](https://discord.gg/efWu5HWM2u) for the original code. I am hoping to contribute to the Belabox and open-source community in any way i can. +Please use official services when possible. diff --git a/src/belabox.rs b/src/belabox.rs index f0c7bb0..6d7f538 100644 --- a/src/belabox.rs +++ b/src/belabox.rs @@ -57,13 +57,24 @@ struct InnerMessage { } impl Belabox { - pub async fn connect(key: String) -> Result { + pub async fn connect(url: String, key: String) -> Result { let (inner_tx, inner_rx) = mpsc::unbounded_channel(); let (message_tx, _) = broadcast::channel(100); let message_tx = Arc::new(message_tx); + let url = if url.is_empty() { + BELABOX_WS.to_string() + } else { + url + }; + let auth = requests::Remote::AuthKey { key, version: 6 }; - let run_handle = tokio::spawn(run_loop(auth, message_tx.clone(), inner_rx)); + if is_belabox_cloud(url.clone()).await { + info!("Connecting to BELABOX Cloud"); + } else { + info!("Connecting to {}", url); + } + let run_handle = tokio::spawn(run_loop(url, auth, message_tx.clone(), inner_rx)); Ok(Self { run_handle, @@ -133,7 +144,12 @@ impl Belabox { } } +async fn is_belabox_cloud(url: String) -> bool { + url.contains(BELABOX_WS) +} + async fn run_loop( + url: String, auth: requests::Remote, message_tx: Arc>, inner_rx: mpsc::UnboundedReceiver, @@ -143,7 +159,7 @@ async fn run_loop( tokio::spawn(handle_requests(inner_rx, request_write.clone())); loop { - let ws_stream = get_connection().await; + let ws_stream = get_connection(url.clone()).await; let (mut write, read) = ws_stream.split(); // Authenticate @@ -175,13 +191,13 @@ async fn run_loop( } } -async fn get_connection() -> WebSocketStream> { +async fn get_connection(url: String) -> WebSocketStream> { let mut retry_grow = 1; loop { info!("Connecting"); - if let Ok((ws_stream, _)) = tokio_tungstenite::connect_async(BELABOX_WS).await { + if let Ok((ws_stream, _)) = tokio_tungstenite::connect_async(url.clone()).await { info!("Connected"); break ws_stream; } diff --git a/src/belabox/messages.rs b/src/belabox/messages.rs index 3087f77..41bb053 100644 --- a/src/belabox/messages.rs +++ b/src/belabox/messages.rs @@ -41,6 +41,7 @@ pub struct RemoteEncoder { #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] pub struct Config { pub password_hash: String, + pub remote_url: String, pub remote_key: String, pub max_br: u32, pub delay: i32, diff --git a/src/bot.rs b/src/bot.rs index 14c94a8..8bc12d2 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -42,7 +42,13 @@ pub struct BelaState { impl Bot { pub async fn new(config: Settings) -> Result { let twitch = Arc::new(Twitch::run(config.twitch.clone()).await?); - let belabox = Arc::new(Belabox::connect(config.belabox.remote_key.to_owned()).await?); + let belabox = Arc::new( + Belabox::connect( + config.belabox.remote_url, + config.belabox.remote_key.to_owned(), + ) + .await?, + ); // Create state to store BELABOX information let bela_state = Arc::new(RwLock::new(BelaState::default())); diff --git a/src/config.rs b/src/config.rs index 9800239..a299b81 100644 --- a/src/config.rs +++ b/src/config.rs @@ -25,6 +25,7 @@ pub struct Settings { #[derive(Serialize, Deserialize, Debug, Clone, Default)] #[serde(default)] pub struct Belabox { + pub remote_url: String, pub remote_key: String, pub custom_interface_name: HashMap, pub monitor: Monitor, @@ -120,13 +121,20 @@ impl Settings { pub async fn ask_for_settings() -> Result { println!("Please paste your BELABOX Cloud remote URL below"); - let remote_key: String = input() + let remote: String = input() .msg("URL: ") + .add_err_test(|u: &String| !u.is_empty(), "No url found, please try again") .add_err_test( |u: &String| u.contains("?key="), "No key found, please try again", ) - .get() + .get(); + let remote_url: String = remote + .split("?key=") + .nth(0) + .expect("No URL found") + .to_string(); + let remote_key: String = remote .split("?key=") .nth(1) .expect("No key found") @@ -177,6 +185,7 @@ impl Settings { } let belabox = Belabox { + remote_url, remote_key, custom_interface_name, monitor,