Skip to content

Commit 44490ca

Browse files
committed
change ca save path to $HOME
1 parent 8015a48 commit 44490ca

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ authors = ["Tyan Boot"]
77
openssl = "0.10.0"
88
clap = "2.32.0"
99
rand = "0.5.4"
10+
dirs = "1.0.2"
1011

1112
[target.x86_64-pc-windows-msvc.dependencies]
1213
gdi32-sys = "0.2.0"

src/cert.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
extern crate dirs;
12
extern crate openssl;
23
extern crate rand;
34

45
use std::fs::File;
6+
use std::fs::DirBuilder;
57
use std::io::Read;
68
use std::io::Write;
79
use std::net::IpAddr;
@@ -21,6 +23,8 @@ use self::openssl::x509::*;
2123

2224
use self::rand::prelude::random;
2325

26+
use super::utils;
27+
2428
pub struct Cert {
2529
ca: Option<X509>,
2630

@@ -41,7 +45,7 @@ impl Cert {
4145
}
4246

4347
pub fn init(&mut self, name: &str, length: u32, force: bool) {
44-
if (Path::new("ca.key").exists() || Path::new("ca.pem").exists()) && !force {
48+
if (Path::new(&utils::key_path()).exists() || Path::new(&utils::ca_path()).exists()) && !force {
4549
eprintln!(
4650
"{}",
4751
"old ca exist, use -f or --force to force init a new ca"
@@ -55,8 +59,8 @@ impl Cert {
5559
}
5660

5761
pub fn load_ca(&mut self) -> bool {
58-
let ca = File::open("ca.pem");
59-
let key = File::open("ca.key");
62+
let ca = File::open(utils::ca_path());
63+
let key = File::open(utils::key_path());
6064

6165
if let (Ok(mut ca), Ok(mut key)) = (ca, key) {
6266
let mut buf = Vec::<u8>::new();
@@ -130,8 +134,7 @@ impl Cert {
130134
x509.append_extension(sid).unwrap();
131135

132136
let mut aid = AuthorityKeyIdentifier::new();
133-
let aid = aid
134-
.keyid(true)
137+
let aid = aid.keyid(true)
135138
.build(&x509.x509v3_context(None, None))
136139
.unwrap();
137140
x509.append_extension(aid).unwrap();
@@ -145,17 +148,25 @@ impl Cert {
145148
}
146149

147150
pub fn save_ca(&mut self) {
151+
let easycert_dir = utils::easycert_dir();
152+
153+
let easycert_dir = Path::new(&easycert_dir);
154+
155+
if !easycert_dir.exists() {
156+
DirBuilder::new().create(easycert_dir).unwrap();
157+
}
158+
148159
if let Some(ref ca) = self.ca {
149160
let pem = ca.to_pem().unwrap();
150161

151-
let mut file = File::create("ca.pem").unwrap();
162+
let mut file = File::create(utils::ca_path()).unwrap();
152163
file.write(pem.as_slice()).unwrap();
153164
}
154165

155166
if let Some(ref key) = self.pkey {
156167
let key = key.private_key_to_pem_pkcs8().unwrap();
157168

158-
let mut file = File::create("ca.key").unwrap();
169+
let mut file = File::create(utils::key_path()).unwrap();
159170
file.write(key.as_slice()).unwrap();
160171
}
161172
}

src/utils.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
extern crate dirs;
2+
13
use std::env;
24
use std::str;
35

6+
use self::dirs::home_dir;
7+
48
#[cfg(target_os = "linux")]
59
extern "C" {
610
fn gethostname(name: *mut u8, len: usize) -> u32;
@@ -28,3 +32,15 @@ pub fn hostname() -> String {
2832
"tyanboot".to_string()
2933
}
3034
}
35+
36+
pub fn easycert_dir() -> String {
37+
return format!("{}/.easycert", home_dir().unwrap().to_str().unwrap());
38+
}
39+
40+
pub fn ca_path() -> String {
41+
return format!("{}/{}", easycert_dir(), "ca.pem");
42+
}
43+
44+
pub fn key_path() -> String {
45+
return format!("{}/{}", easycert_dir(), "ca.key");
46+
}

0 commit comments

Comments
 (0)