enable and resolve some advanced clippy lints

This commit is contained in:
Miraty 2024-04-23 15:08:22 +02:00
parent 17c66abf6f
commit cabdcd53e7
5 changed files with 87 additions and 76 deletions

View File

@ -7,6 +7,19 @@ edition = "2021"
name = "updater"
path = "src/updater.rs"
[lints.rust]
unsafe_code = "forbid"
[lints.clippy]
blocks_in_conditions = "allow"
std_instead_of_core = "warn"
str_to_string = "warn"
string_to_string = "warn"
uninlined_format_args = "warn"
semicolon_if_nothing_returned = "warn"
wildcard_imports = "warn"
inefficient_to_string = "warn"
[dependencies]
asn-db2 = { version = "0.2", default-features = false }
dns-lookup = { version = "2.0", default-features = false }

View File

@ -1,5 +1,5 @@
use core::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use services::models::Services;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use texting_robots::{get_robots_url, Robot};
const USER_AGENT: &str = "ServiceListerBot";
@ -19,7 +19,7 @@ pub async fn check(url: &str, ipv: Option<u8>) -> Result<Services, String> {
.referer(false)
.local_address(local_address)
.user_agent(USER_AGENT)
.timeout(std::time::Duration::new(15, 0))
.timeout(core::time::Duration::new(15, 0))
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap();
@ -42,11 +42,11 @@ pub async fn check(url: &str, ipv: Option<u8>) -> Result<Services, String> {
let server = match response.headers().get("server") {
Some(header) => match header.to_str() {
Ok(h) => h,
Err(_) => return Err("Unstringable server header".to_string()),
Err(_) => return Err("Unstringable server header".to_owned()),
},
None => "",
}
.to_string();
.to_owned();
let response_body = response.text().await.unwrap();
Ok(Services {
@ -56,7 +56,7 @@ pub async fn check(url: &str, ipv: Option<u8>) -> Result<Services, String> {
if is_root {
"peertube"
} else {
return Err("Please only use root URL when adding software".to_string());
return Err("Please only use root URL when adding software".to_owned());
}
} else if response_body.contains(r#"<input type="text" id="padname" maxlength="50" autofocus x-webkit-speech>"#) {
"etherpad"
@ -75,23 +75,23 @@ pub async fn check(url: &str, ipv: Option<u8>) -> Result<Services, String> {
} else if response_body.contains(r#">Powered by Gitea</a>"#) {
"gitea"
} else {
return Err("Unknown software".to_string());
}.to_string(),
ipv6: "".to_string(),
ipv4: "".to_string(),
availability_ipv6: "".to_string(),
availability_ipv4: "".to_string(),
address_ipv6: "".to_string(),
address_ipv4: "".to_string(),
return Err("Unknown software".to_owned());
}.to_owned(),
ipv6: String::new(),
ipv4: String::new(),
availability_ipv6: String::new(),
availability_ipv4: String::new(),
address_ipv6: String::new(),
address_ipv4: String::new(),
})
}
Err(e) => Err(e.to_string()),
}
} else {
Err("Bot forbidden in /robots.txt".to_string())
Err("Bot forbidden in /robots.txt".to_owned())
}
} else {
Err("/robots.txt doesn't exist".to_string())
Err("/robots.txt doesn't exist".to_owned())
}
}

View File

@ -13,10 +13,10 @@ pub struct Config {
}
fn get_config_arg(kdl: &KdlDocument, key: &str) -> String {
kdl.get_arg(key)
.unwrap_or_else(|| panic!("config: missing {}", key))
.unwrap_or_else(|| panic!("config: missing {key}"))
.as_string()
.unwrap_or_else(|| panic!("config: {} must be a string", key))
.to_string()
.unwrap_or_else(|| panic!("config: {key} must be a string"))
.to_owned()
}
pub fn get_config() -> Config {
let kdl: KdlDocument = read_to_string("config.kdl")

View File

@ -3,12 +3,15 @@ extern crate rocket;
mod check;
use crate::check::check;
use services::{get_config, models::*, Config, Software};
use std::fs::File;
use ::services::schema::services::dsl::services;
use ::services::{
get_config,
models::{Scans, Services},
Config, Software,
};
use asn_db2::Database;
use diesel::prelude::*;
use diesel::{associations::HasTable, prelude::*};
use fluent_templates::FluentLoader;
use rocket::{
fairing::{Fairing, Info, Kind},
@ -23,7 +26,7 @@ use rocket_accept_language::{language, AcceptLanguage, LanguageIdentifier};
use rocket_sync_db_pools::{database, diesel};
use serde::Serialize;
use serde_json::json;
use std::io::BufReader;
use std::{fs::File, io::BufReader};
use tera::{Context, Tera};
use unic_langid::{langid, subtags::Language};
@ -140,12 +143,14 @@ async fn list_services(
al: &AcceptLanguage,
software: Option<Strict<Software>>,
) -> RawHtml<String> {
let services = conn
let mut templates: Vec<TemplateServices> = vec![];
for service in &conn
.run(|c| {
let mut request = services::schema::services::dsl::services.into_boxed();
let mut request = ::services::schema::services::dsl::services.into_boxed();
if let Some(s) = software {
request = request
.filter(services::schema::services::software.eq(s.to_string().to_lowercase()));
request = request.filter(
::services::schema::services::software.eq(s.to_string().to_lowercase()),
);
}
request
.limit(300)
@ -153,45 +158,43 @@ async fn list_services(
.load(c)
.unwrap()
})
.await;
let mut templates: Vec<TemplateServices> = vec![];
for service in &services {
.await
{
let mut ip_info = vec![];
let mut ip_combined: Vec<&str> = service.address_ipv6.split(',').collect();
ip_combined.append(&mut service.address_ipv4.split(',').collect());
ip_combined
.iter()
.filter(|ip| !ip.is_empty())
.for_each(|ip| {
.for_each(|&ip| {
match ipdb.lookup(ip.parse().unwrap()).unwrap() {
asn_db2::IpEntry::V6(info) => ip_info.push(IpInfo {
ip: ip.to_string(),
ip: ip.to_owned(),
subnet: info.subnet.to_string(),
asn: info.as_number.to_string(),
country: info.country.to_string(),
as_owner: info.owner.to_string(),
country: info.country.clone(),
as_owner: info.owner.clone(),
}),
asn_db2::IpEntry::V4(info) => ip_info.push(IpInfo {
ip: ip.to_string(),
ip: ip.to_owned(),
subnet: info.subnet.to_string(),
asn: info.as_number.to_string(),
country: info.country.to_string(),
as_owner: info.owner.to_string(),
country: info.country.clone(),
as_owner: info.owner.clone(),
}),
};
});
templates.push(TemplateServices {
url: service.url.to_string(),
software: service.software.to_string(),
server: service.server.to_string(),
ipv6: service.ipv6.to_string(),
ipv4: service.ipv4.to_string(),
availability_ipv6: service.availability_ipv6.to_string(),
availability_ipv4: service.availability_ipv4.to_string(),
url: service.url.clone(),
software: service.software.clone(),
server: service.server.clone(),
ipv6: service.ipv6.clone(),
ipv4: service.ipv4.clone(),
availability_ipv6: service.availability_ipv6.clone(),
availability_ipv4: service.availability_ipv4.clone(),
ip_info,
})
});
}
RawHtml(
@ -216,7 +219,7 @@ async fn list_scans(conn: DbConn, tera: &State<Tera>, al: &AcceptLanguage) -> Ra
&gen_context(
al,
json!({
"scans": conn.run(|c| services::schema::scans::dsl::scans
"scans": conn.run(|c| ::services::schema::scans::dsl::scans
.limit(1000)
.select(Scans::as_select())
.load(c)
@ -266,9 +269,6 @@ async fn add_service_post(
tera: &State<Tera>,
al: &AcceptLanguage,
) -> RawHtml<String> {
use ::services::schema::services::dsl::*;
use diesel::associations::HasTable;
let service = match check(submission.url, None).await {
Ok(service) => service,
Err(err) => {
@ -288,12 +288,12 @@ async fn add_service_post(
url: service.url,
software: service.software,
server: service.server,
ipv6: "".to_string(),
ipv4: "".to_string(),
availability_ipv6: "".to_string(),
availability_ipv4: "".to_string(),
address_ipv6: "".to_string(),
address_ipv4: "".to_string(),
ipv6: String::new(),
ipv4: String::new(),
availability_ipv6: String::new(),
availability_ipv4: String::new(),
address_ipv6: String::new(),
address_ipv4: String::new(),
})
.execute(c)
.unwrap()

View File

@ -5,10 +5,10 @@ use ::services::{
models::{Scans, Services},
schema::{
scans::{dsl::scans, installation},
services::dsl::*,
services::dsl::{self, services},
},
};
use std::net::IpAddr;
use core::net::IpAddr;
use diesel::{
associations::HasTable, Connection, ExpressionMethods, QueryDsl, RunQueryDsl, SelectableHelper,
@ -30,19 +30,19 @@ async fn main() {
.expect("Error loading services")
{
let result_ipv6 = match check(&service.url, Some(6)).await {
Ok(_) => "ok".to_string(),
Ok(_) => "ok".to_owned(),
Err(e) => e,
};
let result_ipv4 = match check(&service.url, Some(4)).await {
Ok(_) => "ok".to_string(),
Ok(_) => "ok".to_owned(),
Err(e) => e,
};
diesel::insert_into(scans::table())
.values(Scans {
id: None,
installation: service.url.to_string(),
installation: service.url.clone(),
timestamp: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
@ -54,7 +54,7 @@ async fn main() {
.unwrap();
let installation_scans = scans
.filter(installation.eq(service.url.to_string()))
.filter(installation.eq(service.url.clone()))
.limit(100)
.select(Scans::as_select())
.load(&mut conn)
@ -71,11 +71,9 @@ async fn main() {
let scans_nb = installation_scans.len();
let ips: Vec<std::net::IpAddr> =
match lookup_host(reqwest::Url::parse(&service.url).unwrap().domain().unwrap()) {
Ok(i) => i,
Err(_) => vec![],
};
let ips: Vec<IpAddr> =
lookup_host(reqwest::Url::parse(&service.url).unwrap().domain().unwrap())
.unwrap_or_default();
let service_new = check(&service.url, None).await.unwrap_or(service);
@ -91,17 +89,17 @@ async fn main() {
diesel::update(services.find(&service_new.url))
.set((
url.eq(&service_new.url),
software.eq(&service_new.software),
server.eq(&service_new.server),
ipv6.eq(&result_ipv6),
ipv4.eq(&result_ipv4),
availability_ipv6
dsl::url.eq(&service_new.url),
dsl::software.eq(&service_new.software),
dsl::server.eq(&service_new.server),
dsl::ipv6.eq(&result_ipv6),
dsl::ipv4.eq(&result_ipv4),
dsl::availability_ipv6
.eq(((ipv6_successes as f32 / scans_nb as f32 * 100.0) as u8).to_string()),
availability_ipv4
dsl::availability_ipv4
.eq(((ipv4_successes as f32 / scans_nb as f32 * 100.0) as u8).to_string()),
address_ipv6.eq(&addr_ipv6.join(",")),
address_ipv4.eq(&addr_ipv4.join(",")),
dsl::address_ipv6.eq(&addr_ipv6.join(",")),
dsl::address_ipv4.eq(&addr_ipv4.join(",")),
))
.execute(&mut conn)
.unwrap();