aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKunoiSayami <[email protected]>2021-05-07 02:40:50 +0800
committerKunoiSayami <[email protected]>2021-05-07 02:40:50 +0800
commit3a8881fbaab4d18b865a44198db9c195041cbff2 (patch)
tree35a78bbeaabc43ac505f3da5f75ec5ee92b0ca97
parent1bd3bfc91fda539ed236c5bb701441b7436d4ead (diff)
fix: Fix database can't initialize properly
-rw-r--r--Cargo.lock54
-rw-r--r--Cargo.toml3
-rw-r--r--src/datastructures.rs1
-rw-r--r--src/main.rs52
4 files changed, 25 insertions, 85 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 1444813..1a5e885 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -344,7 +344,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cgit-simple-authentication"
-version = "0.1.0"
+version = "0.1.1"
dependencies = [
"anyhow",
"base64",
@@ -359,7 +359,6 @@ dependencies = [
"serde_json",
"sha2",
"sqlx",
- "syslog",
"tokio 1.5.0",
"toml",
"url",
@@ -420,10 +419,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b"
[[package]]
-name = "cpuid-bool"
-version = "0.1.2"
+name = "cpufeatures"
+version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8aebca1129a03dc6dc2b127edd729435bbc4a37e1d5f4d7513165089ceb02634"
+checksum = "5cd5a7748210e7ec1a9696610b1015e6e31fbf58f77a160801f124bd1c36592a"
[[package]]
name = "crc"
@@ -525,15 +524,6 @@ dependencies = [
]
[[package]]
-name = "error-chain"
-version = "0.12.4"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2d2f06b9cac1506ece98fe3231e3cc9c4410ec3d5b1f24ae1c8946f0742cdefc"
-dependencies = [
- "version_check",
-]
-
-[[package]]
name = "event-listener"
version = "2.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1150,9 +1140,9 @@ checksum = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de"
[[package]]
name = "openssl-sys"
-version = "0.9.62"
+version = "0.9.63"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "fa52160d45fa2e7608d504b7c3a3355afed615e6d8b627a74458634ba21b69bd"
+checksum = "b6b0d6fb7d80f877617dfcb014e605e2b5ab2fb0afdf27935219bb6bd984cb98"
dependencies = [
"autocfg",
"cc",
@@ -1430,9 +1420,9 @@ dependencies = [
[[package]]
name = "redox_syscall"
-version = "0.2.7"
+version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "85dd92e586f7355c633911e11f77f3d12f04b1b1bd76a198bd34ae3af8341ef2"
+checksum = "742739e41cd49414de871ea5e549afb7e2a3ac77b589bcbebe8c82fab37147fc"
dependencies = [
"bitflags",
]
@@ -1560,13 +1550,13 @@ checksum = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d"
[[package]]
name = "sha2"
-version = "0.9.3"
+version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "fa827a14b29ab7f44778d14a88d3cb76e949c45083f7dbfa507d0cb699dc12de"
+checksum = "d8f6b75b17576b792bef0db1bcc4b8b8bcdf9506744cf34b974195487af6cff2"
dependencies = [
"block-buffer 0.9.0",
"cfg-if 1.0.0",
- "cpuid-bool",
+ "cpufeatures",
"digest 0.9.0",
"opaque-debug 0.3.0",
]
@@ -1734,18 +1724,6 @@ dependencies = [
]
[[package]]
-name = "syslog"
-version = "5.0.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "9a5d8ef1b679c07976f3ee336a436453760c470f54b5e7237556728b8589515d"
-dependencies = [
- "error-chain",
- "libc",
- "log",
- "time",
-]
-
-[[package]]
name = "tap"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1804,16 +1782,6 @@ dependencies = [
]
[[package]]
-name = "time"
-version = "0.1.43"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438"
-dependencies = [
- "libc",
- "winapi 0.3.9",
-]
-
-[[package]]
name = "tinyvec"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index 5349a40..167c42e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "cgit-simple-authentication"
-version = "0.1.0"
+version = "0.1.1"
authors = ["KunoiSayami <[email protected]>"]
edition = "2018"
@@ -13,7 +13,6 @@ serde_derive = "1"
serde = { version = "1", features = ["derive"] }
anyhow = "1"
toml = "0.5"
-syslog = "5.0.0"
sqlx = { version = "0.5", features = [ "json", "sqlite", "runtime-actix-native-tls"] }
rand = "0.7"
clap = "2.33"
diff --git a/src/datastructures.rs b/src/datastructures.rs
index 12e23a2..6897bd3 100644
--- a/src/datastructures.rs
+++ b/src/datastructures.rs
@@ -33,6 +33,7 @@ const DEFAULT_DATABASE_LOCATION: &str = "/etc/cgit/auth.db";
pub struct Config {
pub cookie_ttl: u64,
database: String,
+ //access_node: hashmap,
}
impl Default for Config {
diff --git a/src/main.rs b/src/main.rs
index 1925d1e..9ef6c2e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -21,12 +21,9 @@
mod database;
mod datastructures;
-use std::fs::OpenOptions;
use anyhow::Result;
-use std::io::{Write, stdin, Read};
-use std::{process, env};
-use log::LevelFilter;
-use syslog::{Formatter3164, Facility, BasicLogger};
+use std::io::{stdin, Read};
+use std::env;
use clap::{Arg, App, SubCommand, ArgMatches};
use rand::Rng;
use serde::{Serialize};
@@ -73,9 +70,9 @@ struct Meta<'a> {
redirect: &'a str,
}
-// Verify username and password via gogs.
+
async fn verify_login(cfg: &Config, data: &FormData) -> Result<bool> {
- let mut conn = sqlx::SqliteConnection::connect("/tmp/database.db").await?;
+ let mut conn = sqlx::SqliteConnection::connect(cfg.get_database_location()).await?;
let password_sha = data.get_password_sha256()?;
let ret = sqlx::query(r#"SELECT 1 FROM "accounts" WHERE "user" = ? AND "password" = ? "#)
.bind(data.get_user())
@@ -141,6 +138,11 @@ async fn cmd_authenticate_cookie(
async fn cmd_init(cfg: Config) -> Result<()> {
+ log::trace!("{}", cfg.get_database_location());
+ let loc = std::path::Path::new(cfg.get_database_location());
+ if ! loc.exists() {
+ std::fs::File::create(loc)?;
+ }
let mut conn = sqlx::SqliteConnection::connect(cfg.get_database_location()).await?;
@@ -153,8 +155,10 @@ async fn cmd_init(cfg: Config) -> Result<()> {
sqlx::query(database::current::CREATE_TABLES)
.execute(&mut conn)
.await?;
+ log::info!("Initialize the database successfully");
}
+
Ok(())
}
@@ -174,7 +178,6 @@ async fn cmd_authenticate_post(
let key = format!("{}_{}", get_current_timestamp(), rand_int());
let value = rand_str(COOKIE_LENGTH);
- // TODO: same here
let redis_conn = redis::Client::open("redis://127.0.0.1/")?;
let mut conn = redis_conn.get_async_connection().await?;
conn.set_ex::<_, _, i32>(format!("cgit_auth_{}", key), &value, cfg.cookie_ttl as usize).await?;
@@ -223,11 +226,6 @@ async fn cmd_body(matches: &ArgMatches<'_>, _cfg: Config) {
.unwrap();
}
-// Processing the `body` called by cron.
-fn cmd_expire(_matches: &ArgMatches, cfg: Config) {
- unimplemented!();
-}
-
async fn async_main(arg_matches: ArgMatches<'_>, cfg: Config) -> Result<i32>{
match arg_matches.subcommand() {
@@ -242,9 +240,6 @@ async fn async_main(arg_matches: ArgMatches<'_>, cfg: Config) -> Result<i32>{
("body", Some(matches)) => {
cmd_body(matches, cfg).await;
}
- ("expire", Some(matches)) => {
- cmd_expire(matches, cfg);
- }
("init", Some(matches)) => {
cmd_init(cfg).await?;
}
@@ -255,29 +250,7 @@ async fn async_main(arg_matches: ArgMatches<'_>, cfg: Config) -> Result<i32>{
fn main() -> Result<()>{
-
- let mut file = OpenOptions::new()
- .create(true)
- .append(true)
- .open("/tmp/output")?;
- let args = std::env::args().collect::<Vec<String>>().join(" ");
- //args.push("\n".into());
- file.write_all(args.as_bytes())?;
- file.write_all("\n".as_bytes())?;
- //println!(r#"<meta name="test">"#);
-
-
- let formatter = Formatter3164 {
- facility: Facility::LOG_USER,
- hostname: None,
- process: "cgit-gogs-auth-filter".into(),
- pid: process::id() as i32,
- };
-
- let logger = syslog::unix(formatter).expect("could not connect to syslog");
- if let Ok(()) = log::set_boxed_logger(Box::new(BasicLogger::new(logger))) {
- log::set_max_level(LevelFilter::Debug);
- }
+ env_logger::init();
// Prints each argument on a separate line
for (nth, argument) in env::args().enumerate() {
@@ -317,7 +290,6 @@ fn main() -> Result<()>{
.args(sub_args),
)
.subcommand(SubCommand::with_name("init").about("Init sqlite database"))
- //.subcommand(SubCommand::with_name("expire").about("Check and clean all expired cookies"))
.get_matches();
// Load filter configurations