-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.rs
224 lines (180 loc) · 5.95 KB
/
api.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
use crate::discord::DiscordUser;
use crate::models::{Image, NewImage};
use axum::extract::{Query, State};
use axum::{
routing::{post, put},
Json, Router,
};
use axum_macros::debug_handler;
use chrono::NaiveDateTime;
use diesel::{
r2d2,
r2d2::ConnectionManager,
sql_types::{Bytea, Float},
FromSqlRow, PgConnection, RunQueryDsl,
};
use hyper::StatusCode;
use img_hash::{image, HasherConfig, ImageHash};
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
ops::DerefMut,
sync::{Arc, Mutex},
};
pub struct Api;
type SharedConnectionManager = Arc<Mutex<r2d2::Pool<ConnectionManager<PgConnection>>>>;
type PooledConnection = r2d2::PooledConnection<ConnectionManager<PgConnection>>;
#[derive(Clone)]
pub(crate) struct Config {
pub bot_token: String,
pub db_pool: SharedConnectionManager,
}
impl Api {
pub async fn run() -> Result<(), Box<dyn std::error::Error>> {
let bot_token = std::env::var("BOT_TOKEN").expect("BOT_TOKEN must be set");
let db_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let db_manager = ConnectionManager::<PgConnection>::new(db_url.clone());
let db_pool = r2d2::Pool::builder()
.build(db_manager)
.expect("Failed to create pool.");
let db_pool = Arc::new(Mutex::new(db_pool));
let app = Router::new()
.route("/phishing/check/image", post(check_image))
.route("/phishing/submit/image", put(create_image))
.with_state(Config { bot_token, db_pool });
axum::Server::bind(&"127.0.0.1:7000".parse().unwrap())
.serve(app.into_make_service())
.await?;
Ok(())
}
}
#[derive(Serialize, FromSqlRow)]
struct FoundHashResponse {
category: String,
score: f32,
added_by: i64,
added_at: NaiveDateTime,
}
#[derive(Deserialize)]
struct SubmitImageResponse {
url: String,
category: String,
added_by: i64,
md5: Option<String>,
}
async fn create_image(
State(config): State<Config>,
_user: DiscordUser,
Json(mut body): Json<SubmitImageResponse>,
) -> StatusCode {
let pg_connection = &mut config.db_pool.clone().lock().unwrap().get().unwrap();
let hasher = HasherConfig::new().to_hasher();
if body.url.contains("discord") {
let idx = body.url.find("?size=");
if let Some(idx) = idx {
let url = body.url[..idx].to_string() + "?size=1024";
body.url = url;
}
}
let res = reqwest::get(&body.url).await.unwrap();
let bytes = res.bytes().await.map_err(|_| StatusCode::BAD_REQUEST);
if bytes.is_err() {
return StatusCode::BAD_REQUEST;
}
let bytes = bytes.unwrap();
let image = image::load_from_memory(&bytes).unwrap();
let hash = hasher.hash_image(&image).as_bytes().to_vec();
let image = NewImage {
source: body.url.as_str(),
category: body.category.as_str(),
added_by: body.added_by,
md5_hash: body.md5.as_ref().map(|s| s.as_str()),
phash: hash,
};
diesel::insert_into(crate::schema::images::table)
.values(&image)
.execute(pg_connection)
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
.map(|c| {
if c > 0 {
StatusCode::CREATED
} else {
StatusCode::BAD_REQUEST
}
})
.unwrap()
}
// Write an API endpoint (POST /check/image) that takes an avatar hash as a query parameter, and requires authentication.
async fn check_image(
State(pg): State<Config>,
Query(query): Query<HashMap<String, String>>,
_user: DiscordUser,
) -> Result<Json<FoundHashResponse>, StatusCode> {
let hash = query.get("hash").ok_or(StatusCode::BAD_REQUEST)?;
let id = query.get("id").ok_or(StatusCode::BAD_REQUEST)?;
let _threshold = query
.get("threshold")
.and_then(|t| t.parse::<f32>().ok())
.unwrap_or(0.95);
let fetch = reqwest::get(format!(
"https://cdn.discordapp.com/avatars/{}/{}.png?size=256",
id, hash
))
.await;
let image = fetch
.map_err(|_| StatusCode::BAD_REQUEST)?
.bytes()
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let image = compute_hash(image.to_vec()).ok_or(StatusCode::BAD_REQUEST)?;
let _hash_bytes = image.as_bytes().to_vec();
let pg_connection = &mut pg.db_pool.clone().lock().unwrap().get().unwrap();
get_most_similar_image(_hash_bytes, _threshold, pg_connection)
.ok_or(StatusCode::NOT_FOUND)
.map(|(image, score)| {
Json(FoundHashResponse {
category: image.category,
score,
added_by: image.added_by,
added_at: image.added,
})
})
}
fn get_most_similar_image(
bytes: Vec<u8>,
threshold: f32,
pg_conn: &mut PooledConnection,
) -> Option<(Image, f32)> {
use diesel::dsl::*;
let conn = pg_conn.deref_mut();
// Find the most similar image in the database using the <-> operator
let image = sql_query(include_str!("../sql/phash.sql"))
.bind::<Bytea, _>(&bytes)
.bind::<Float, _>(threshold)
.load::<Image>(conn)
.ok()?
.first_mut()
.map(|i| i.clone());
if let Some(img) = image {
use bitvec::prelude::*;
let dist_vec = img
.phash
.iter()
.zip(bytes)
.map(|(a, b)| *a ^ b)
.collect::<Vec<u8>>();
let dist = dist_vec.as_bits::<Msb0>().count_ones();
// Normalize the distance between 1 and 0
let dist_normalized = 1.0 - (dist as f32 / dist_vec.len() as f32);
return Some((img, dist_normalized));
}
None
}
fn compute_hash(image: Vec<u8>) -> Option<ImageHash> {
let image = match image::load_from_memory(&image) {
Ok(image) => image,
Err(_) => return None,
};
let config = HasherConfig::new().to_hasher();
Some(config.hash_image(&image))
}