commit 9eaadaa940f80eb206dcc4aa713f7d9d7bfbb56b Author: doddy Date: Sun Jun 22 15:21:07 2025 +0200 init diff --git a/migrations/01-init/up.sql b/migrations/01-init/up.sql new file mode 100644 index 0000000..4686629 --- /dev/null +++ b/migrations/01-init/up.sql @@ -0,0 +1,13 @@ +CREATE TABLE PROSPECT( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + email TEXT UNIQUE, + password TEXT NOT NULL, + approved INTEGER NOT NULL +); + +CREATE TABLE car( + registration_plate TEXT PRIMARY KEY, + cost REAL NOT NULL, + bought_on TEXT NOT NULL +); \ No newline at end of file diff --git a/src/captcha/mod.rs b/src/captcha/mod.rs new file mode 100644 index 0000000..9691df5 --- /dev/null +++ b/src/captcha/mod.rs @@ -0,0 +1,37 @@ +use std::error::Error; +use url::Url; +use reqwest::StatusCode; +use serde::{Serialize, Deserialize}; + +const GOOGLE_API_URL: &str = "https://www.google.com/recaptcha/api/siteverify"; + +pub fn validate(secret_key: &String, token: &String) -> bool { + return match send_request(secret_key, token) { + Ok(is_success) => is_success, + Err(_e) => false + }; +} + +pub fn send_request(secret_key: &String, token: &String) -> Result> { + let url = Url::parse_with_params(GOOGLE_API_URL, + &[("secret", secret_key), ("response", token)])?; + + let resp = reqwest::blocking::get(url)?; + let status_code = resp.status(); + + if status_code != StatusCode::OK { + println!("nicht gut"); + Err("status code is not")?; + } + + let response: GoogleCaptchaResponse = serde_json::from_str(resp.text()?.as_str())?; + + Ok(response.success) +} + + +#[serde(rename_all = "camelCase")] +#[derive(Serialize, Deserialize)] +struct GoogleCaptchaResponse { + success: bool +} \ No newline at end of file diff --git a/src/common/mod.rs b/src/common/mod.rs new file mode 100644 index 0000000..5e69aaf --- /dev/null +++ b/src/common/mod.rs @@ -0,0 +1,10 @@ +use serde::{Serialize, Deserialize}; + +#[serde(rename_all = "camelCase")] +#[derive(Debug, Serialize, Deserialize)] +pub struct FreakProspect { + pub name: String, + pub password: String, + pub confirm_password: String, + pub email: String, +} diff --git a/src/db/mod.rs b/src/db/mod.rs new file mode 100644 index 0000000..d5f0f2e --- /dev/null +++ b/src/db/mod.rs @@ -0,0 +1,36 @@ +use include_dir::{include_dir, Dir}; +use lazy_static::lazy_static; +use crate::common::FreakProspect; +use bcrypt::{DEFAULT_COST, hash}; +use rusqlite::{Result, Connection}; +use rusqlite_migration::Migrations; + +const DB_PATH: &str = "~/freakshells.db"; +const DB_PROSPECT: &str = "prospect"; + +static MIGRATIONS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/migrations"); + +lazy_static! { + static ref MIGRATIONS: Migrations<'static> = + Migrations::from_directory(&MIGRATIONS_DIR).unwrap(); +} + +fn init_db() -> Connection { + let mut conn = Connection::open("./my_db.db3").unwrap(); + + // Update the database schema, atomically + MIGRATIONS.to_latest(&mut conn).unwrap(); + + conn +} + +pub fn store_prospect(prospect: FreakProspect) -> Result<()> { + let db = init_db(); + let hashed_password = hash(prospect.password, DEFAULT_COST).unwrap(); + db.execute( + "INSERT INTO PROSPECT (name, email, password, approved) VALUES (?1, ?2, ?3, 0)", + (prospect.name, prospect.email, hashed_password) + )?; + + Ok(()) +} diff --git a/src/mail/mod.rs b/src/mail/mod.rs new file mode 100644 index 0000000..be92a8b --- /dev/null +++ b/src/mail/mod.rs @@ -0,0 +1,35 @@ +use lettre::message::header::ContentType; +use lettre::transport::smtp::authentication::Credentials; +use lettre::{Message, SmtpTransport, Transport}; + +use std::io::Result; + +pub struct FreakMail { + from: String, + to: String, + subject: String, + body: String, +} + +pub fn send_mail(mail: FreakMail) -> Result<()> { + let email = Message::builder() + .from("Administrator FREAKSHELLS ".parse().unwrap()) + .to("dodiusz@gmail.com".parse().unwrap()) + .subject("Happy new year") + .header(ContentType::TEXT_HTML) + .body(String::from("Be happy!")) + .unwrap(); + + let creds = Credentials::new("freakshellsnet@gmail.com".to_owned(), "kivpysohxzydycnq".to_owned()); + + // Open a remote connection to gmail + let mailer = SmtpTransport::relay("smtp.gmail.com") + .unwrap() + .credentials(creds) + .build(); + + // Send the email + mailer.send(&email).unwrap(); + + Ok(()) +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..69746f3 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,42 @@ +use rocket::serde::{Serialize, json::Json}; +use rocket::response::status; + +use common::FreakProspect; + +mod captcha; +mod db; +mod common; +mod mail; + +#[macro_use] +extern crate rocket; + +#[derive(Serialize)] +#[serde(crate = "rocket::serde")] +struct GetResponse { + id: i32, + val: String +} + + +#[get("/")] +fn hello() -> &'static str { + let skey = String::from("cze"); + let tok = String::from("de"); + captcha::validate(&skey, &tok); + + db::store_prospect(FreakProspect { + name: String::from("asd"), + email: "test@test".to_string(), + password: "Test123#".to_string(), + confirm_password: "Test123#".to_string(), + }).unwrap(); + + "Hello world!" +} + +#[launch] +fn rocket() -> _ { + rocket::build().mount("/", routes![hello]) +} +