-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rs
284 lines (243 loc) · 7.57 KB
/
main.rs
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#[macro_use]
extern crate rocket;
use std::net::IpAddr;
use std::num::NonZeroU32;
use std::path::Path;
use std::time::{SystemTime, UNIX_EPOCH};
use url::Url;
use governor::{DefaultKeyedRateLimiter, Quota, RateLimiter};
use rand::distributions::Alphanumeric;
use rand::Rng;
use rocket::fairing::AdHoc;
use rocket::fs::FileServer;
use rocket::futures::TryStreamExt;
use rocket::http::Status;
use rocket::response::status::{Created, Custom, NotFound};
use rocket::response::Redirect;
use rocket::serde::{json::Json, Deserialize, Serialize};
use rocket::{fairing, Build, Request, Rocket, State};
use rocket::fs::NamedFile;
use rocket_dyn_templates::{context, Template};
use rocket_db_pools::{sqlx, Connection, Database};
type Result<T, E = rocket::response::Debug<sqlx::Error>> = std::result::Result<T, E>;
const ID_LENGTH: u8 = 6;
const MAX_URL_LENGTH: usize = 2048;
#[derive(Database)]
#[database("route_db")]
struct Routes(sqlx::SqlitePool);
#[derive(Serialize, Deserialize)]
struct Route {
#[serde(skip_deserializing)]
id: String,
destination: String,
expires: i64,
}
struct RateLimitState {
limiter: DefaultKeyedRateLimiter<IpAddr>,
}
fn generate_alphanumeric_string(length: u8) -> String {
rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(length as usize)
.map(char::from)
.collect()
}
fn get_unix_epoch() -> i64 {
let start = SystemTime::now();
let since_the_epoch = start
.duration_since(UNIX_EPOCH)
.expect("Time went backwards!");
since_the_epoch.as_millis() as i64
}
fn is_valid_route_id(route_id: &str) -> bool {
route_id.len() == ID_LENGTH as usize && route_id.chars().all(|c| c.is_ascii_alphanumeric())
}
#[post("/api/create", data = "<post>")]
async fn create(
limiter_state: &State<RateLimitState>, mut db: Connection<Routes>, ip_addr: IpAddr,
mut post: Json<Route>,
) -> Result<Created<Json<Route>>, Custom<String>> {
let rate_limit_result = limiter_state.limiter.check_key(&ip_addr);
if rate_limit_result.is_err() {
return Err(Custom(
Status::TooManyRequests,
String::from("You have reached the limit of routes you may create!"),
));
}
// Check URL length
if post.destination.len() > MAX_URL_LENGTH {
return Err(Custom(
Status::BadRequest,
format!(
"URL exceeds maximum length of {} characters",
MAX_URL_LENGTH
),
));
}
post.id = String::new();
for _ in 0..64 {
post.id = generate_alphanumeric_string(ID_LENGTH);
let exists_result =
sqlx::query!("SELECT * FROM routes WHERE route_id = ? LIMIT 1", post.id)
.fetch(&mut **db)
.try_collect::<Vec<_>>()
.await;
match exists_result {
Ok(records) if records.is_empty() => break,
Ok(_) | Err(_) => continue,
}
}
if post.id.is_empty() {
return Err(Custom(
Status::InternalServerError,
String::from("Couldn't find space for your route. Sorry!"),
));
}
let current_unix_epoch = get_unix_epoch();
let one_year_later = current_unix_epoch + 365 * 24 * 60 * 60 * 1000;
if post.expires < current_unix_epoch {
return Err(Custom(
Status::BadRequest,
String::from("Route set to expire in the past!"),
));
}
if post.expires > one_year_later {
return Err(Custom(
Status::BadRequest,
String::from("Route expiry date must be within one year from now!"),
));
}
let parsed_destination = Url::parse(&post.destination).map_err(|_| {
Custom(
Status::BadRequest,
String::from("URL destination is invalid!"),
)
})?;
if parsed_destination.scheme() != "http" && parsed_destination.scheme() != "https" {
return Err(Custom(
Status::BadRequest,
String::from("Non-http(s) URLs are not allowed!"),
));
}
let insert_result = sqlx::query!(
"INSERT INTO routes (route_id, destination, expires) VALUES (?, ?, ?)",
post.id,
post.destination,
post.expires
)
.execute(&mut **db)
.await;
match insert_result {
Ok(_) => Ok(Created::new(format!("/{}", post.id)).body(post)),
Err(_) => Err(Custom(
Status::InternalServerError,
String::from("Ran into issues while adding route to database."),
)),
}
}
#[get("/<route_id>")]
async fn route(mut db: Connection<Routes>, route_id: &str) -> Result<Redirect, NotFound<Template>> {
if !is_valid_route_id(route_id) {
return Err(NotFound(Template::render(
"error",
context! {
error_code: 404,
reason: "Invalid route ID."
},
)));
}
let lookup_result = sqlx::query!("SELECT * FROM routes WHERE route_id = ?", route_id)
.fetch_one(&mut **db)
.await;
match lookup_result {
Ok(record) => {
let epoch = get_unix_epoch();
if epoch > record.expires {
sqlx::query!("DELETE FROM routes WHERE route_id = ?", record.route_id)
.execute(&mut **db)
.await
.expect("Tried to expire route that doesn't exist?");
return Err(NotFound(Template::render(
"error",
context! {
error_code: 404,
reason: "Route doesn't exist."
},
)));
}
Ok(Redirect::temporary(record.destination))
}
Err(_) => Err(NotFound(Template::render(
"error",
context! {
error_code: 404,
reason: "Route doesn't exist."
},
))),
}
}
#[get("/")]
fn index() -> Template {
Template::render("index", context! {})
}
#[get("/favicon.ico")]
async fn favicon() -> Option<NamedFile> {
NamedFile::open(Path::new("static/").join("favicon.ico"))
.await
.ok()
}
async fn run_migrations(rocket: Rocket<Build>) -> fairing::Result {
match Routes::fetch(&rocket) {
Some(db) => match sqlx::migrate!("db/migrations").run(&**db).await {
Ok(_) => Ok(rocket),
Err(e) => {
error!("Failed to initialize SQLx database: {}", e);
Err(rocket)
}
},
None => Err(rocket),
}
}
#[catch(404)]
fn not_found() -> Template {
Template::render(
"error",
context! {
error_code: 404,
reason: "404 Not Found"
},
)
}
#[catch(429)]
fn too_many_requests() -> Template {
Template::render(
"error",
context! {
error_code: 429,
reason: "429 Too Many Requests"
},
)
}
#[catch(500)]
fn internal_error(err: Status, _req: &Request) -> Template {
Template::render(
"error",
context! {
error_code: 500,
reason: err.reason().unwrap()
},
)
}
#[launch]
fn rocket() -> _ {
rocket::build()
.attach(Routes::init())
.attach(AdHoc::try_on_ignite("SQLx Migrations", run_migrations))
.attach(Template::fairing())
.manage(RateLimitState {
limiter: RateLimiter::keyed(Quota::per_minute(NonZeroU32::new(1).unwrap())),
})
.mount("/static", FileServer::from("static"))
.mount("/", routes![index, favicon, route, create])
.register("/", catchers![not_found, too_many_requests, internal_error])
}