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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use anyhow::{Context, Result};
use clap::{App, Arg};
use native_tls::{Certificate, Identity, TlsConnector};
use openssl::{
    pkcs12::Pkcs12,
    pkey::{PKey, Private},
    x509::X509,
};
use rand::{distributions::Alphanumeric, Rng};
use std::{fs, io::Read, string::ToString};

#[derive(Clone, Debug)]
pub struct Redis {
    pub host: String,
    pub user: Option<String>,
    pub pass: Option<String>,
    pub v46: bool,
    pub port: u16,
    pub tls: native_tls::TlsConnector,
}

fn load_ca(filename: &str) -> Result<native_tls::Certificate> {
    let mut buf = Vec::new();
    fs::File::open(filename)?.read_to_end(&mut buf)?;
    Ok(Certificate::from_pem(&buf)?)
}

fn load_cert(filename: &str) -> Result<X509> {
    let mut buf = Vec::new();
    fs::File::open(filename)?.read_to_end(&mut buf)?;
    Ok(X509::from_pem(&buf)?)
}

fn load_key(filename: &str) -> Result<PKey<Private>> {
    let mut buf = Vec::new();
    fs::File::open(filename)?.read_to_end(&mut buf)?;
    Ok(PKey::private_key_from_pem(&buf)?)
}

fn is_file(s: String) -> Result<(), String> {
    if fs::metadata(&s).map_err(|e| e.to_string())?.is_file() {
        Ok(())
    } else {
        Err(format!(
            "cannot read the file: {}, verify file exist and is not a directory.",
            s
        ))
    }
}

fn random_string() -> String {
    rand::thread_rng()
        .sample_iter(&Alphanumeric)
        .take(32)
        .map(char::from)
        .collect()
}

fn is_num(s: String) -> Result<(), String> {
    if let Err(..) = s.parse::<usize>() {
        return Err(String::from("Not a valid number!"));
    }
    Ok(())
}

// returns (v46, port, pool)
pub fn new() -> Result<Redis> {
    let matches = App::new(env!("CARGO_PKG_NAME"))
        .version(env!("CARGO_PKG_VERSION"))
        .arg(
            Arg::with_name("redis")
                .takes_value(true)
                .default_value("127.0.0.1:6379")
                .help("redis host:port")
                .long("host")
                .required(true),
        )
        .arg(
            Arg::with_name("user")
                .takes_value(true)
                .help("redis user")
                .long("user")
                .short("u"),
        )
        .arg(
            Arg::with_name("pass")
                .takes_value(true)
                .help("redis password")
                .long("pass")
                .short("p"),
        )
        .arg(
            Arg::with_name("ca")
                .takes_value(true)
                .help("/path/to/ca.crt")
                .long("tls-ca-cert-file")
                .validator(is_file),
        )
        .arg(
            Arg::with_name("crt")
                .takes_value(true)
                .help("/path/to/redis.crt")
                .long("tls-cert-file")
                .validator(is_file)
                .required(true),
        )
        .arg(
            Arg::with_name("key")
                .takes_value(true)
                .help("/path/to/redis.key")
                .long("tls-key-file")
                .validator(is_file)
                .required(true),
        )
        .arg(
            Arg::with_name("port")
                .default_value("36379")
                .help("listening HTTP port")
                .long("http-port")
                .validator(is_num)
                .required(true),
        )
        .arg(
            Arg::with_name("v46")
                .help("listen in both IPv4 and IPv6")
                .long("46"),
        )
        .get_matches();

    let mut tls_builder = TlsConnector::builder();

    if matches.is_present("ca") {
        let pem_file = matches.value_of("ca").unwrap();
        let ca_cert = load_ca(pem_file)?;
        tls_builder.add_root_certificate(ca_cert);
    }

    if matches.is_present("crt") && matches.is_present("key") {
        let crt_file = matches.value_of("crt").unwrap();
        let client_cert: X509 = load_cert(crt_file)?;

        let key_file = matches.value_of("key").unwrap();
        let client_key = load_key(key_file)?;

        let builder = Pkcs12::builder();
        let pass = random_string();
        let pkcs12 = builder.build(&pass, "httpredis", &client_key, &client_cert)?;
        let pkcs12_der = pkcs12.to_der()?;
        let identity = Identity::from_pkcs12(&pkcs12_der, &pass)?;
        tls_builder.identity(identity);
    }

    tls_builder.danger_accept_invalid_certs(true);
    let tls = tls_builder.build()?;

    let host = matches
        .value_of("redis")
        .context("missing redis host:port")?;

    let host_port = host.split(':');
    let host = if host_port.count() == 2 {
        host.to_string()
    } else {
        format!("{}:6379", host)
    };

    let user = matches.value_of("user").map(ToString::to_string);

    let pass = matches.value_of("pass").map(ToString::to_string);

    Ok(Redis {
        host,
        user,
        pass,
        v46: matches.is_present("v46"),
        port: matches.value_of("port").unwrap().parse::<u16>()?,
        tls,
    })
}