2020-08-22 16:34:28 +02:00
use serde_json ::Value ;
2020-08-19 01:21:42 +02:00
use std ::fs ::File ;
use std ::io ::Read ;
use std ::io ::{ self , BufRead , BufReader } ;
use std ::path ::Path ;
// payload limit set to 5MiB
2020-08-22 15:38:42 +02:00
pub const PAYLOAD_LIMIT : usize = 10_000_000 ;
pub const PROXY_TIMEOUT : u64 = 15 ;
2020-08-19 01:21:42 +02:00
pub const CONFIG_FILE : & str = " ./config.toml " ;
2020-11-03 19:05:08 +01:00
pub const CONFIG_VERSION : u8 = 2 ;
2020-08-19 01:21:42 +02:00
pub const ADJ_LIST_FILE : & str = " ./adj-list.txt " ;
pub const NAME_LIST_FILE : & str = " ./name-list.txt " ;
pub const LOC_FILE : & str = " ./lang.json " ;
2021-03-24 19:48:59 +01:00
pub const USER_AGENT : & str = " Actix-web " ;
2021-03-24 19:48:17 +01:00
2020-08-19 01:21:42 +02:00
lazy_static! {
pub static ref CONFIG : Config = Config ::init ( ) ;
pub static ref ADJ_LIST : Vec < String > =
lines_from_file ( ADJ_LIST_FILE ) . expect ( " Failed to load adjectives list " ) ;
pub static ref NAME_LIST : Vec < String > =
lines_from_file ( NAME_LIST_FILE ) . expect ( " Failed to load names list " ) ;
pub static ref LOC : Value = init_lang ( ) ;
}
2020-09-16 22:08:19 +02:00
// Open LOC_FILE and store it in memory (LOC)
2020-08-19 01:21:42 +02:00
fn init_lang ( ) -> Value {
let mut file = File ::open ( LOC_FILE ) . expect ( " init_lang: Can't open translations file " ) ;
let mut data = String ::new ( ) ;
file . read_to_string ( & mut data )
. expect ( " init_lang: Can't read translations file " ) ;
serde_json ::from_str ( & data ) . expect ( " init_lang(): Can't parse translations file " )
}
2020-09-16 22:08:19 +02:00
// Open a file from its path
2020-08-19 01:21:42 +02:00
fn lines_from_file ( filename : impl AsRef < Path > ) -> io ::Result < Vec < String > > {
BufReader ::new ( File ::open ( filename ) ? ) . lines ( ) . collect ( )
}
#[ derive(Deserialize) ]
pub struct Config {
pub listening_address : String ,
pub listening_port : u16 ,
pub sncf_url : String ,
pub database_path : String ,
pub nextcloud_url : String ,
pub admin_username : String ,
pub admin_password : String ,
pub prune_days : u16 ,
pub debug_mode : bool ,
2020-11-03 19:05:08 +01:00
pub cookie_key : String ,
2020-08-19 01:21:42 +02:00
pub config_version : u8 ,
}
// totally not copypasted from rs-short
impl Config {
2020-09-16 22:08:19 +02:00
// open and parse CONFIG_FILE
2020-08-19 01:21:42 +02:00
pub fn init ( ) -> Self {
let mut conffile = File ::open ( CONFIG_FILE ) . expect (
r #" Config file config.toml not found.
Please create it using config . toml . sample . " #,
) ;
let mut confstr = String ::new ( ) ;
conffile
. read_to_string ( & mut confstr )
. expect ( " Couldn't read config to string " ) ;
2020-11-03 19:05:08 +01:00
toml ::from_str ( & confstr ) . expect ( " Couldn't deserialize the config. Please update at https://git.42l.fr/neil/sncf/wiki/Upgrade-from-a-previous-version --- Error " )
2020-08-19 01:21:42 +02:00
}
2020-09-16 22:08:19 +02:00
// if config.config_version doesn't match the hardcoded version,
// ask the admin to manually upgrade its config file
2020-08-19 01:21:42 +02:00
pub fn check_version ( & self ) {
if self . config_version ! = CONFIG_VERSION {
2020-08-31 16:02:10 +02:00
eprintln! ( " Your configuration file is obsolete! \n Please update it following the instructions in https://git.42l.fr/neil/sncf/wiki/Upgrade-from-a-previous-version and update its version to {} . " , CONFIG_VERSION ) ;
2020-08-19 01:21:42 +02:00
panic! ( ) ;
}
}
}
2020-11-04 20:04:29 +01:00
pub fn get_csrf_key ( ) -> [ u8 ; 32 ] {
let mut key : [ u8 ; 32 ] = Default ::default ( ) ;
key . copy_from_slice ( & CONFIG . cookie_key . clone ( ) . into_bytes ( ) [ .. 32 ] ) ;
key
}