Skip to content

Commit

Permalink
Better smtp ports management
Browse files Browse the repository at this point in the history
  • Loading branch information
UwUDev committed Oct 28, 2024
1 parent 2f5d3e0 commit ced3842
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 40 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "mail-sink"
version = "0.1.0"
edition = "2021"
authors = ["UwUDev"]

[dependencies]
tokio = { version = "1.28.0", features = ["full"] }
Expand Down
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,13 @@ Run Mail Sink with specific options:
```

### Options:
| short | long | value | description |
|-------|------------------------|-----------|-----------------------------------------------------|
| -h | --help | | Show help message. |
| -p | --smtp-port | SMTP PORT | Set the SMTP port. Default: `2525` |
| -s | --secondary-smtp-port | SMTP PORT | Bind the SMTP server to a secondary port. |
| | --http-port | HTTP PORT | Set the HTTP port. Default: `8080` |
| -k | --key | KEY | The key to access the API. Default: `prouteur` |
| -V | --version | | Print version. |
| short | long | value | description |
|-------|------------------------|------------|-----------------------------------------------------------|
| -h | --help | | Show help message. |
| -p | --smtp-port | SMTP PORTS | Set the SMTP port. Default: `2525` Example: `25,587,465` |
| | --http-port | HTTP PORT | Set the HTTP port. Default: `8080` |
| -k | --key | KEY | The key to access the API. Default: `prouteur` |
| -V | --version | | Print version. |

## Panel
The panel is accessible via `/panel?k=your_key`
Expand Down
15 changes: 7 additions & 8 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@ pub struct Args {
#[arg(long, short)]
pub help: bool,

#[arg(short = 'p', long, default_value = "2525", value_name = "SMTP PORT")]
pub smtp_port: u16,

#[arg(
short,
short = 'p',
long,
help = "Allows to bind the SMTP server to a secondary port",
value_name = "SMTP PORT"
default_value = "2525",
value_name = "SMTP PORTS",
help = "Example: `25,587,465`"
)]
pub secondary_smtp_port: Option<u16>,
pub smtp_port: String,


#[arg(long, default_value = "8080", value_name = "HTTP PORT")]
pub http_port: u16,
pub http_ports: u16,

#[arg(
short,
Expand Down
40 changes: 16 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ async fn main() -> Result<(), SharedError> {
if args.help {
Printer::new(Args::command())
.with("introduction", INTRO)
.without("author")
.print_help();

print_api_usage();
Expand All @@ -36,25 +35,23 @@ async fn main() -> Result<(), SharedError> {

let db_clone = db.clone();
let tls_clone = tls_config.clone();
let smtp_handle =
task::spawn(
async move { run_smtp_service(tls_clone.clone(), db_clone, args.smtp_port).await },
);
args.smtp_port.split(',')
.map(|port| port.parse::<u16>().expect("Wrong ports"))
.for_each(|port| {
let tls = tls_clone.clone();
let db = db_clone.clone();
task::spawn(
async move { run_smtp_service(tls, db, port).await },
);
});


let db_clone = db.clone();
let key = args.key.clone();
let service_handle =
task::spawn(async move { run_http_service(db_clone, args.http_port, key.clone()).await });
task::spawn(async move { run_http_service(db_clone, args.http_ports, key.clone()).await });


let secondary_smtp_handle = if let Some(port) = args.secondary_smtp_port {
let tls_config = tls_config.clone();
let db = db.clone();
Some(task::spawn(async move {
run_smtp_service(tls_config, db, port).await
}))
} else {
None
};

match args.lifetime {
Some(lifetime) => {
Expand All @@ -66,18 +63,13 @@ async fn main() -> Result<(), SharedError> {

println!(
"Panel: http://localhost:{}/panel?k={}",
args.http_port, args.key
args.http_ports, args.key
);

// wait for all services to complete (it should never happen)
match secondary_smtp_handle {
Some(handle) => {
let _ = tokio::try_join!(smtp_handle, service_handle, handle)?;
}
None => {
let _ = tokio::try_join!(smtp_handle, service_handle)?;
}
}

let _ = tokio::try_join!(service_handle)?;


eprintln!("All services have completed unexpectedly ...");

Expand Down

0 comments on commit ced3842

Please sign in to comment.