-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.rs
More file actions
252 lines (220 loc) · 7.46 KB
/
main.rs
File metadata and controls
252 lines (220 loc) · 7.46 KB
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
use std::{collections::HashMap, net::SocketAddr, net::IpAddr, sync::Arc};
use ::ring::rand::SystemRandom;
use axum::{extract::State, routing::get, Json, Router};
use log::{info, warn};
use serde::{Deserialize, Serialize};
use simplelog::{ColorChoice, LevelFilter, TermLogger, TerminalMode};
use sqlite::Connection;
use tokio::sync::Mutex;
use tower_http::cors::{Any, CorsLayer};
#[cfg(feature = "tls")]
use {axum_server::tls_rustls::RustlsConfig, rustls::crypto::ring};
pub use ofapi::util;
mod account;
mod auth;
mod cookie;
mod legacy;
mod moderation;
mod monitor;
mod rankinfo;
mod statics;
mod database;
mod email;
#[derive(Deserialize, Clone)]
struct CoreConfig {
server_name: String,
public_url: String,
db_path: String,
template_dir: String,
bind_ip: String,
port: Option<u16>,
}
#[allow(dead_code)]
#[derive(Deserialize, Clone)]
struct TlsConfig {
cert_path: String,
key_path: String,
port: Option<u16>,
}
#[derive(Deserialize, Clone)]
struct GameConfig {
versions: Vec<String>,
login_address: String,
custom_loading_screen: Option<bool>,
}
#[derive(Deserialize, Clone)]
struct Config {
core: CoreConfig,
tls: Option<TlsConfig>,
email: Option<email::EmailConfig>,
game: GameConfig,
monitor: Option<monitor::MonitorConfig>,
moderation: Option<moderation::ModerationConfig>,
rankinfo: Option<rankinfo::RankInfoConfig>,
account: Option<account::AccountConfig>,
auth: Option<auth::AuthConfig>,
cookie: Option<cookie::CookieConfig>,
legacy: Option<legacy::LegacyConfig>,
}
impl Config {
fn load() -> Self {
const CONFIG_PATH: &str = "config.toml";
let config = std::fs::read_to_string(CONFIG_PATH).expect("Failed to open config file");
toml::from_str(&config).expect("Failed to parse config file")
}
}
#[derive(Clone)]
struct AppState {
db: Arc<Mutex<Connection>>,
rng: Arc<SystemRandom>,
email_verifications: Arc<Mutex<HashMap<String, email::EmailVerification>>>,
temp_passwords: Arc<Mutex<HashMap<String, email::TempPassword>>>,
is_tls: bool,
config: Config,
}
impl AppState {
fn new(config: &Config) -> Self {
info!(
"SQLite version {}",
util::version_to_string(sqlite::version())
);
let conn = database::connect_to_db(&config.core.db_path);
Self {
db: Arc::new(Mutex::new(conn)),
rng: Arc::new(SystemRandom::new()),
email_verifications: Arc::new(Mutex::new(HashMap::new())),
temp_passwords: Arc::new(Mutex::new(HashMap::new())),
is_tls: false,
config: config.clone(),
}
}
}
#[tokio::main]
async fn main() {
// load config
let config = Config::load();
// init logging
TermLogger::init(
LevelFilter::Info,
simplelog::Config::default(),
TerminalMode::Mixed,
ColorChoice::Auto,
)
.expect("Failed to init logger");
info!("OFAPI v{}", env!("CARGO_PKG_VERSION"));
// init app state
let state = AppState::new(&config);
// register endpoints for both HTTP and HTTPS
let mut routes = Router::new().route("/", get(get_info));
routes = statics::register(routes);
if let Some(ref rankinfo_config) = config.rankinfo {
routes = rankinfo::register(routes, rankinfo_config);
}
if let Some(ref monitor_config) = config.monitor {
routes = monitor::register(routes, monitor_config);
}
if let Some(ref legacy_config) = config.legacy {
routes = legacy::register(
routes,
legacy_config,
config.game.versions.first(),
&config.core.template_dir,
)
}
// add CORS headers for non-sensitive APIs
let cors = CorsLayer::new().allow_origin(Any);
routes = routes.layer(cors);
// register HTTPS-only endpoints
let mut routes_tls = Router::new().merge(routes.clone());
if let Some(ref account_config) = config.account {
routes_tls = account::register(routes_tls, account_config);
}
if let Some(ref auth_config) = config.auth {
routes_tls = auth::register(routes_tls, auth_config, &state.rng);
}
if let Some(ref cookie_config) = config.cookie {
routes_tls = cookie::register(routes_tls, cookie_config);
}
if let Some(ref moderation_config) = config.moderation {
routes_tls = moderation::register(routes_tls, moderation_config);
}
// init both protocols
// N.B. these listen concurrently, but NOT in parallel (see tokio::join!)
let http = init_http(routes, &config, state.clone());
let https = init_https(routes_tls, &config, state);
let _ = tokio::join!(http, https);
}
async fn init_http(routes: Router<Arc<AppState>>, config: &Config, state: AppState) {
const DEFAULT_HTTP_PORT: u16 = 80;
let addr = SocketAddr::from((
config.core.bind_ip.parse::<IpAddr>().expect("Failed to parse bind_ip"),
config.core.port.unwrap_or(DEFAULT_HTTP_PORT)));
let app = routes.with_state(Arc::new(state));
info!("HTTP listening on {}", addr);
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
axum::serve(listener, app).await.unwrap();
}
async fn init_https(routes: Router<Arc<AppState>>, config: &Config, mut state: AppState) {
const DEFAULT_HTTPS_PORT: u16 = 443;
state.is_tls = true;
let app = routes.with_state(Arc::new(state));
let Some(ref tls_config) = config.tls else {
warn!("Missing or malformed TLS config. HTTPS disabled");
return;
};
let addr = SocketAddr::from((
config.core.bind_ip.parse::<IpAddr>().expect("Failed to parse bind_ip"),
tls_config.port.unwrap_or(DEFAULT_HTTPS_PORT)));
#[cfg(not(feature = "tls"))]
{
warn!("TLS APIs enabled but OFAPI was not compiled with the `tls` feature. Encryption should be done at the proxy level!");
}
info!("HTTPS listening on {}", addr);
#[cfg(feature = "tls")]
{
ring::default_provider().install_default().unwrap();
let rustls_cfg =
match RustlsConfig::from_pem_file(&tls_config.cert_path, &tls_config.key_path).await {
Err(e) => {
warn!("Failed to activate TLS ({}); HTTPS disabled", e);
return;
}
Ok(cfg) => cfg,
};
axum_server::bind_rustls(addr, rustls_cfg)
.serve(app.into_make_service())
.await
.unwrap();
}
#[cfg(not(feature = "tls"))]
{
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
axum::serve(listener, app).await.unwrap();
}
}
#[derive(Serialize, Deserialize)]
struct InfoResponse {
server_name: String,
api_version: String,
secure_apis_enabled: bool,
game_versions: Vec<String>,
login_address: String,
email_required: bool,
custom_loading_screen: bool,
}
async fn get_info(State(state): State<Arc<AppState>>) -> Json<InfoResponse> {
let info = InfoResponse {
server_name: state.config.core.server_name.clone(),
api_version: env!("CARGO_PKG_VERSION").to_string(),
secure_apis_enabled: state.is_tls,
game_versions: state.config.game.versions.clone(),
login_address: state.config.game.login_address.clone(),
email_required: state
.config
.account
.as_ref()
.map_or(false, |a| a.is_email_required()),
custom_loading_screen: state.config.game.custom_loading_screen.unwrap_or(false),
};
Json(info)
}