feat: custom port support for relay

This commit is contained in:
Erica Marigold 2023-07-20 17:01:35 +05:30
parent a45cfc1889
commit 8bce588246
No known key found for this signature in database
GPG key ID: 23CD97ABBBCC5ED2
3 changed files with 15 additions and 6 deletions

View file

@ -29,5 +29,8 @@ pub enum Commands {
password: String, password: String,
}, },
/// Start relay server /// Start relay server
Relay {}, Relay {
#[clap(value_parser, required = false)]
port: Option<u16>,
},
} }

View file

@ -28,8 +28,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
println!("Receiving password {}", password); println!("Receiving password {}", password);
receive(password).await? receive(password).await?
} }
Commands::Relay {} => { Commands::Relay { port } => {
serve().await?; serve(port.to_owned()).await?;
} }
} }
Ok(()) Ok(())

View file

@ -2,6 +2,7 @@ use crate::handshake::Handshake;
use anyhow::Result; use anyhow::Result;
use bytes::Bytes; use bytes::Bytes;
use std::collections::HashMap; use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::Arc; use std::sync::Arc;
use tokio::io::{copy, AsyncWriteExt}; use tokio::io::{copy, AsyncWriteExt};
use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf}; use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf};
@ -87,9 +88,14 @@ impl Client {
} }
} }
pub async fn serve() -> Result<()> { pub async fn serve(port: Option<u16>) -> Result<()> {
let addr = "127.0.0.1:8080".to_string(); let port: u16 = match port {
let listener = TcpListener::bind(&addr).await?; Some(port) => port,
None => 8080u16,
};
let addr = SocketAddr::from(([127, 0, 0, 1], port));
let listener = TcpListener::bind(addr).await?;
let state = Arc::new(Mutex::new(Shared::new())); let state = Arc::new(Mutex::new(Shared::new()));
let (tx, _rx) = broadcast::channel::<Bytes>(100); let (tx, _rx) = broadcast::channel::<Bytes>(100);
println!("Listening on: {}", addr); println!("Listening on: {}", addr);