-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
120 lines (108 loc) · 3.97 KB
/
main.rs
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use obirt::server::entities::MemPeers;
use clap::{Arg, Command};
use rsa::pkcs1::{EncodeRsaPrivateKey, EncodeRsaPublicKey};
use obirt::client;
use obirt::server::{auther, router};
#[tokio::main]
async fn main() {
env_logger::builder()
.filter_level(log::LevelFilter::Trace)
.parse_env("LOG")
.init();
let matches = Command::new("Obirt")
.version(env!("CARGO_PKG_VERSION"))
.about(env!("CARGO_PKG_DESCRIPTION"))
.author(env!("CARGO_PKG_AUTHORS"))
.subcommand(
Command::new("client")
.about("Connect to a server")
.arg(
Arg::new("address")
.help("Server address")
.value_name("ADDRESS")
.index(1)
.required(true),
)
.arg(
Arg::new("auth-port")
.long("auth-port")
.help("Auth server port")
.value_name("AUTH_PORT")
.default_value("1120"),
)
.arg(
Arg::new("router-port")
.long("router-port")
.help("Router server port")
.value_name("ROUTER_PORT")
.default_value("9807"),
)
.arg(
Arg::new("interface")
.long("interface")
.short('i')
.help("Interface name")
.value_name("INTERFACE")
.default_value("obr0"),
),
)
.subcommand(Command::new("server").about("Start a server"))
.subcommand(
Command::new("keypair").about("Generate a new keypair").arg(
Arg::new("force")
.long("force")
.short('f')
.help("Force the generation of new the keypair")
.value_name("FORCE")
.value_parser(clap::value_parser!(bool))
.default_value("false"),
),
)
.subcommand(Command::new("info").about("Show information about the connection"))
.get_matches();
match matches.subcommand() {
Some(("client", command)) => {
let server = command.get_one::<String>("address").unwrap();
let auth_port = command.get_one::<String>("auth-port").unwrap();
let router_port = command.get_one::<String>("router-port").unwrap();
let interface = command.get_one::<String>("interface").unwrap();
client::connect(server, auth_port, router_port, interface).await;
}
Some(("server", _)) => {
let peers = MemPeers::default();
tokio::join!(auther::start(&peers), router::start(&peers));
}
Some(("keypair", command)) => {
let force = command.get_one::<bool>("force").unwrap();
if keypair(*force).await {
println!("Generated keypair");
} else {
println!("Keypair already exists");
}
}
_ => {
println!("No subcommand was used");
}
}
}
pub async fn keypair(force: bool) -> bool {
if !force {
if std::path::Path::new("./private.txt").exists() {
println!("Private key already exists");
return false;
}
if std::path::Path::new("./public.txt").exists() {
println!("Public key already exists");
return false;
}
}
let private = rsa::RsaPrivateKey::new(&mut rand::thread_rng(), 2048).unwrap();
private
.write_pkcs1_pem_file("./private.txt", rsa::pkcs8::LineEnding::LF)
.unwrap();
let public = private.to_public_key();
public
.write_pkcs1_pem_file("./public.txt", rsa::pkcs8::LineEnding::LF)
.unwrap();
return true;
}