Skip to content

Commit

Permalink
feat: allow specifying remote_url
Browse files Browse the repository at this point in the history
  • Loading branch information
datagutt committed Sep 21, 2023
1 parent dcd8bc5 commit 1eef969
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 10 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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.
26 changes: 21 additions & 5 deletions src/belabox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,24 @@ struct InnerMessage {
}

impl Belabox {
pub async fn connect(key: String) -> Result<Self, BelaboxError> {
pub async fn connect(url: String, key: String) -> Result<Self, BelaboxError> {
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,
Expand Down Expand Up @@ -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<broadcast::Sender<Message>>,
inner_rx: mpsc::UnboundedReceiver<InnerMessage>,
Expand All @@ -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
Expand Down Expand Up @@ -175,13 +191,13 @@ async fn run_loop(
}
}

async fn get_connection() -> WebSocketStream<MaybeTlsStream<TcpStream>> {
async fn get_connection(url: String) -> WebSocketStream<MaybeTlsStream<TcpStream>> {
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;
}
Expand Down
1 change: 1 addition & 0 deletions src/belabox/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ pub struct BelaState {
impl Bot {
pub async fn new(config: Settings) -> Result<Self, Error> {
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()));
Expand Down
13 changes: 11 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>,
pub monitor: Monitor,
Expand Down Expand Up @@ -120,13 +121,20 @@ impl Settings {
pub async fn ask_for_settings() -> Result<Self, ConfigError> {
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")
Expand Down Expand Up @@ -177,6 +185,7 @@ impl Settings {
}

let belabox = Belabox {
remote_url,
remote_key,
custom_interface_name,
monitor,
Expand Down

0 comments on commit 1eef969

Please sign in to comment.