1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//! # Client Side
//!

mod test;
mod upload;
mod download;

use std::fs::OpenOptions;
use clap::{Parser, Subcommand};
use log::{LevelFilter};
use simplelog::*;

use crate::download::download;
use crate::test::send_test_requests;
use crate::upload::upload;

#[derive(Parser, Debug)]
#[command(name = "DNSTP")]
#[command(author, version, about, long_about = None)]
struct Args {
    #[clap(subcommand)]
    command: Command,
}

#[derive(Debug, Subcommand)]
enum Command {
    /// Send test requests on loop to the server
    Test {
        #[clap(flatten)]
        net_options: NetSettings
    },
    /// Upload data to the remote server
    Upload {
        #[clap(flatten)]
        net_options: NetSettings,
        #[arg(short, long)]
        key: Option<Vec<String>>,
        #[arg(short, long)]
        value: Vec<String>
    },
    /// Download a payload from the remote server
    Download {
        #[clap(flatten)]
        net_options: NetSettings
    }
}

#[derive(Parser, Debug)]
struct NetSettings {
    /// Server address to send requests to
    #[arg(short, long)]
    address: String,
    /// Base domain server is operating on
    #[arg(long)]
    base_domain: String,
    /// Sub-domain to handle key handling when requested
    #[arg(long, default_value = "static")]
    key_endpoint: String,
}

fn main() {
    CombinedLogger::init(
        vec![
            TermLogger::new(LevelFilter::Info, Config::default(), TerminalMode::Mixed, ColorChoice::Auto),
            WriteLogger::new(LevelFilter::Trace, Config::default(), OpenOptions::new()
                .read(true)
                .write(true)
                .append(true)
                .create(true)
                .open("dnstp.log").unwrap()),
        ]
    ).unwrap();

    let args = Args::parse();

    match args.command {
        Command::Test { net_options } => {
            send_test_requests(net_options);
        }
        Command::Upload { net_options, key, value } => {
            upload(net_options, key, value);
        }
        Command::Download { net_options } => {
            download(net_options);
        }
    }
}