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
// Copyright (C) 2022  Aravinth Manivannan <realaravinth@batsense.net>
// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::RwLock;
use std::time::Duration;

use reqwest::Client;
use serde::{Deserialize, Serialize};
use tokio::task::JoinHandle;
use tokio::time::sleep;

use crate::errors::*;
use crate::settings::Settings;
use crate::AppData;

#[async_trait::async_trait]
trait SurveyClientTrait {
    async fn start_job(&self, data: AppData) -> ServiceResult<JoinHandle<()>>;
    async fn register(&self, data: &AppData) -> ServiceResult<()>;
}

#[derive(Clone, Debug, Default)]
pub struct SecretsStore {
    store: Arc<RwLock<HashMap<String, String>>>,
}

impl SecretsStore {
    pub fn get(&self, key: &str) -> Option<String> {
        let r = self.store.read().unwrap();
        r.get(key).map(|x| x.to_owned())
    }

    pub fn rm(&self, key: &str) {
        let mut w = self.store.write().unwrap();
        w.remove(key);
        drop(w);
    }

    pub fn set(&self, key: String, value: String) {
        let mut w = self.store.write().unwrap();
        w.insert(key, value);
        drop(w);
    }
}

struct Survey {
    settings: Settings,
    client: Client,
    secrets: SecretsStore,
}
impl Survey {
    fn new(settings: Settings, secrets: SecretsStore) -> Self {
        Survey {
            client: Client::new(),
            settings,
            secrets,
        }
    }
}

#[async_trait::async_trait]
impl SurveyClientTrait for Survey {
    async fn start_job(&self, data: AppData) -> ServiceResult<JoinHandle<()>> {
        let fut = async move {
            loop {
                sleep(Duration::new(data.settings.survey.rate_limit, 0)).await;
                //                if let Err(e) = Self::delete_demo_user(&data).await {
                //                    log::error!("Error while deleting demo user: {:?}", e);
                //                }
                //                if let Err(e) = Self::register_demo_user(&data).await {
                //                    log::error!("Error while registering demo user: {:?}", e);
                //                }
            }
        };
        let handle = tokio::spawn(fut);
        Ok(handle)
    }
    async fn register(&self, data: &AppData) -> ServiceResult<()> {
        let protocol = if self.settings.server.proxy_has_tls {
            "https://"
        } else {
            "http://"
        };
        #[derive(Serialize)]
        struct MCaptchaInstance {
            url: url::Url,
            secret: String,
        }

        let this_instance_url =
            url::Url::parse(&format!("{protocol}{}", self.settings.server.domain))?;
        for url in self.settings.survey.nodes.iter() {
            // mCaptcha/survey must send this token while uploading secret to authenticate itself
            // this token must be sent to mCaptcha/survey with the registration payload
            let secret_upload_auth_token = crate::api::v1::mcaptcha::get_random(20);

            let payload = MCaptchaInstance {
                url: this_instance_url.clone(),
                secret: secret_upload_auth_token.clone(),
            };

            // SecretsStore will store auth tokens generated by both mCaptcha/mCaptcha and
            // mCaptcha/survey
            //
            // Storage schema:
            // - mCaptcha/mCaptcha generated auth token: (<auth_token>, <survey_instance_url>)
            // - mCaptcha/survey generated auth token (<survey_instance_url>, <auth_token)
            self.secrets.set(secret_upload_auth_token, url.to_string());
            self.client
                .post(url.clone())
                .json(&payload)
                .send()
                .await
                .unwrap();
        }
        Ok(())
    }
}