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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
 * Copyright (C) 2022  Aravinth Manivannan <realaravinth@batsense.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
use std::env;
use std::path::Path;

use config::{Config, ConfigError, Environment, File};
use derive_more::Display;
use log::{debug, warn};
use serde::{Deserialize, Serialize};
use url::Url;

#[derive(Debug, Clone, Deserialize)]
pub struct Server {
    pub port: u32,
    pub domain: String,
    pub cookie_secret: String,
    pub ip: String,
    pub url_prefix: Option<String>,
    pub proxy_has_tls: bool,
}

#[derive(Debug, Clone, Deserialize)]
pub struct Captcha {
    pub salt: String,
    pub gc: u64,
    pub runners: Option<usize>,
    pub queue_length: usize,
    pub enable_stats: bool,
    pub default_difficulty_strategy: DefaultDifficultyStrategy,
}

#[derive(Debug, Clone, Deserialize)]
pub struct DefaultDifficultyStrategy {
    pub avg_traffic_difficulty: u32,
    pub broke_my_site_traffic_difficulty: u32,
    pub peak_sustainable_traffic_difficulty: u32,
    pub duration: u32,
}

#[derive(Debug, Clone, Deserialize)]
pub struct Smtp {
    pub from: String,
    pub reply: String,
    pub url: String,
    pub username: String,
    pub password: String,
    pub port: u16,
}

impl Server {
    #[cfg(not(tarpaulin_include))]
    pub fn get_ip(&self) -> String {
        format!("{}:{}", self.ip, self.port)
    }
}

//#[derive(Debug, Clone, Deserialize)]
//struct DatabaseBuilder {
//    pub port: u32,
//    pub hostname: String,
//    pub username: String,
//    pub password: String,
//    pub name: String,
//}

//impl DatabaseBuilder {
//    #[cfg(not(tarpaulin_include))]
//    fn extract_database_url(url: &Url) -> Self {
//        debug!("Database name: {}", url.path());
//        let mut path = url.path().split('/');
//        path.next();
//        let name = path.next().expect("no database name").to_string();
//        DatabaseBuilder {
//            port: url.port().expect("Enter database port").into(),
//            hostname: url.host().expect("Enter database host").to_string(),
//            username: url.username().into(),
//            password: url.password().expect("Enter database password").into(),
//            name,
//        }
//    }
//}

#[derive(Deserialize, Serialize, Display, PartialEq, Clone, Debug)]
#[serde(rename_all = "lowercase")]
pub enum DBType {
    #[display(fmt = "postgres")]
    Postgres,
    #[display(fmt = "maria")]
    Maria,
}

impl DBType {
    fn from_url(url: &Url) -> Result<Self, ConfigError> {
        match url.scheme() {
            "mysql" => Ok(Self::Maria),
            "postgres" => Ok(Self::Postgres),
            _ => Err(ConfigError::Message("Unknown database type".into())),
        }
    }
}

#[derive(Debug, Clone, Deserialize)]
pub struct Database {
    pub url: String,
    pub pool: u32,
    pub database_type: DBType,
}

#[derive(Debug, Clone, Deserialize)]
pub struct Redis {
    pub url: String,
    pub pool: u32,
}

#[derive(Debug, Clone, Deserialize)]
pub struct Settings {
    pub debug: bool,
    pub commercial: bool,
    pub database: Database,
    pub redis: Option<Redis>,
    pub server: Server,
    pub captcha: Captcha,
    pub source_code: String,
    pub smtp: Option<Smtp>,
    pub allow_registration: bool,
    pub allow_demo: bool,
}

#[cfg(not(tarpaulin_include))]
impl Settings {
    pub fn new() -> Result<Self, ConfigError> {
        let mut s = Config::new();

        const CURRENT_DIR: &str = "./config/default.toml";
        const ETC: &str = "/etc/mcaptcha/config.toml";

        s.set("capatcha.enable_stats", true.to_string())
            .expect("unable to set capatcha.enable_stats default config");

        if let Ok(path) = env::var("MCAPTCHA_CONFIG") {
            s.merge(File::with_name(&path))?;
        } else if Path::new(CURRENT_DIR).exists() {
            // merging default config from file
            s.merge(File::with_name(CURRENT_DIR))?;
        } else if Path::new(ETC).exists() {
            s.merge(File::with_name(ETC))?;
        } else {
            log::warn!("configuration file not found");
        }

        s.merge(Environment::with_prefix("MCAPTCHA").separator("_"))?;

        check_url(&s);

        match env::var("PORT") {
            Ok(val) => {
                s.set("server.port", val).unwrap();
            }
            Err(e) => warn!("couldn't interpret PORT: {}", e),
        }

        match env::var("DATABASE_URL") {
            Ok(val) => {
                let url = Url::parse(&val).expect("couldn't parse Database URL");
                s.set("database.url", url.to_string()).unwrap();
                let database_type = DBType::from_url(&url).unwrap();
                s.set("database.database_type", database_type.to_string())
                    .unwrap();
            }
            Err(e) => {
                set_database_url(&mut s);
            }
        }

        // setting default values
        #[cfg(test)]
        s.set("database.pool", 2.to_string())
            .expect("Couldn't set database pool count");

        match s.try_into() {
            Ok(val) => Ok(val),
            Err(e) => Err(ConfigError::Message(format!("\n\nError: {}. If it says missing fields, then please refer to https://github.com/mCaptcha/mcaptcha#configuration to learn more about how mcaptcha reads configuration\n\n", e))),
        }
    }
}

#[cfg(not(tarpaulin_include))]
fn check_url(s: &Config) {
    let url = s
        .get::<String>("source_code")
        .expect("Couldn't access source_code");

    Url::parse(&url).expect("Please enter a URL for source_code in settings");
}

#[cfg(not(tarpaulin_include))]
fn set_database_url(s: &mut Config) {
    s.set(
        "database.url",
        format!(
            r"postgres://{}:{}@{}:{}/{}",
            s.get::<String>("database.username")
                .expect("Couldn't access database username"),
            s.get::<String>("database.password")
                .expect("Couldn't access database password"),
            s.get::<String>("database.hostname")
                .expect("Couldn't access database hostname"),
            s.get::<String>("database.port")
                .expect("Couldn't access database port"),
            s.get::<String>("database.name")
                .expect("Couldn't access database name")
        ),
    )
    .expect("Couldn't set database url");
}

//#[cfg(test)]
//mod tests {
//    use super::*;
//
//    #[test]
//    fn url_prefix_test() {
//        let mut settings = Settings::new().unwrap();
//        assert!(settings.server.url_prefix.is_none());
//        settings.server.url_prefix = Some("test".into());
//        settings.server.check_url_prefix();
//        settings.server.url_prefix = Some("    ".into());
//        settings.server.check_url_prefix();
//        assert!(settings.server.url_prefix.is_none());
//    }
//
//    #[test]
//    fn smtp_config_works() {
//        let settings = Settings::new().unwrap();
//        assert!(settings.smtp.is_some());
//        assert_eq!(settings.smtp.as_ref().unwrap().password, "password");
//        assert_eq!(settings.smtp.as_ref().unwrap().username, "admin");
//    }
//}