Skip to content

Commit

Permalink
Migration to rust 1.67
Browse files Browse the repository at this point in the history
  • Loading branch information
zdz committed Jan 28, 2023
1 parent 10a8f13 commit 3a520f0
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 55 deletions.
2 changes: 1 addition & 1 deletion client/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ fn main() {
rustc_version().unwrap()
);
}
println!("cargo:rustc-env=APP_VERSION={}", app_version);
println!("cargo:rustc-env=APP_VERSION={app_version}");
}
5 changes: 2 additions & 3 deletions client/src/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ use stat_common::server_status::StatRequest;

use crate::sample_all;
use crate::Args;
use crate::INTERVAL_MS;

// TODO TLS
// TODO mTLS

pub async fn report(args: &Args, stat_base: &mut StatRequest) -> anyhow::Result<()> {
if !vec![stat_base.online4, stat_base.online6].iter().any(|&x| x) {
Expand Down Expand Up @@ -68,6 +67,6 @@ pub async fn report(args: &Args, stat_base: &mut StatRequest) -> anyhow::Result<
}
});

thread::sleep(Duration::from_millis(INTERVAL_MS));
thread::sleep(Duration::from_secs(args.report_interval));
}
}
4 changes: 2 additions & 2 deletions client/src/vnstat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ pub fn get_traffic(args: &Args) -> Result<(u64, u64, u64, u64)> {
let b = str::from_utf8(&a)?;
let j: VnstatJson = serde_json::from_str(b).unwrap_or_else(|e| {
error!("{:?}", e);
panic!("invalid vnstat json `{}", b)
panic!("invalid vnstat json `{b}")
});
calc_traffic(j, false, args)
} else if args.vnstat_mr > 1 && args.vnstat_mr <= 28 {
Expand All @@ -174,7 +174,7 @@ pub fn get_traffic(args: &Args) -> Result<(u64, u64, u64, u64)> {
let b = str::from_utf8(&a)?;
let j: VnstatJson = serde_json::from_str(b).unwrap_or_else(|e| {
error!("{:?}", e);
panic!("invalid vnstat json `{}", b)
panic!("invalid vnstat json `{b}")
});
calc_traffic(j, true, args)
} else {
Expand Down
2 changes: 1 addition & 1 deletion common/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn main() {
Utc::now().format("%Y-%m-%d %H:%M:%S %Z")
);
}
println!("cargo:rustc-env=APP_VERSION={}", app_version);
println!("cargo:rustc-env=APP_VERSION={app_version}");

std::env::set_var("PROTOC", protobuf_src::protoc());
tonic_build::configure()
Expand Down
2 changes: 1 addition & 1 deletion server/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ fn main() {
rustc_version().unwrap()
);
}
println!("cargo:rustc-env=APP_VERSION={}", app_version);
println!("cargo:rustc-env=APP_VERSION={app_version}");
}
2 changes: 1 addition & 1 deletion server/src/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn check_auth(req: Request<()>) -> Result<Request<()>, Status> {
pub async fn serv_grpc(addr: &str) -> anyhow::Result<()> {
let sock_addr = addr.parse().unwrap();
let sss = ServerStatusSrv::default();
eprintln!("🚀 listening on grpc://{}", sock_addr);
eprintln!("🚀 listening on grpc://{sock_addr}");
let svc = ServerStatusServer::with_interceptor(sss, check_auth);
Server::builder()
.add_service(svc)
Expand Down
56 changes: 28 additions & 28 deletions server/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ pub async fn init_client(req: Request<Body>) -> Result<Response<Body>> {
domain = host.to_string();
})
});
server_url = format!("{}://{}/report", scheme, domain);
server_url = format!("{scheme}://{domain}/report");
}

let debug = params.get("debug").map(|p| p.eq("1")).unwrap_or(false);
Expand Down Expand Up @@ -175,15 +175,15 @@ pub async fn init_client(req: Request<Body>) -> Result<Response<Body>> {
let exclude_iface = params.get("exclude-iface").unwrap_or(&invalid);

// build client opts
let mut client_opts = format!(r#"-a "{}" -p "{}""#, server_url, pass);
let mut client_opts = format!(r#"-a "{server_url}" -p "{pass}""#);
if debug {
client_opts.push_str(" -d");
}
if vnstat {
client_opts.push_str(" -n");
}
if 1 < vnstat_mr && vnstat_mr <= 28 {
let _ = write!(client_opts, r#" --vnstat-mr {}"#, vnstat_mr);
let _ = write!(client_opts, r#" --vnstat-mr {vnstat_mr}"#);
}
if disable_ping {
client_opts.push_str(" --disable-ping");
Expand All @@ -195,39 +195,39 @@ pub async fn init_client(req: Request<Body>) -> Result<Response<Body>> {
client_opts.push_str(" --disable-extra");
}
if weight > 0 {
let _ = write!(client_opts, r#" -w {}"#, weight);
let _ = write!(client_opts, r#" -w {weight}"#);
}
if !gid.is_empty() {
let _ = write!(client_opts, r#" -g "{}""#, gid);
let _ = write!(client_opts, r#" --alias "{}""#, alias);
let _ = write!(client_opts, r#" -g "{gid}""#);
let _ = write!(client_opts, r#" --alias "{alias}""#);
}
if !uid.is_empty() {
let _ = write!(client_opts, r#" -u "{}""#, uid);
let _ = write!(client_opts, r#" -u "{uid}""#);
}
if !notify {
client_opts.push_str(" --disable-notify");
}
if !host_type.is_empty() {
let _ = write!(client_opts, r#" -t "{}""#, host_type);
let _ = write!(client_opts, r#" -t "{host_type}""#);
}
if !location.is_empty() {
let _ = write!(client_opts, r#" --location "{}""#, location);
let _ = write!(client_opts, r#" --location "{location}""#);
}
if !cm.is_empty() && cm.contains(":") {
let _ = write!(client_opts, r#" --cm "{}""#, cm);
if !cm.is_empty() && cm.contains(':') {
let _ = write!(client_opts, r#" --cm "{cm}""#);
}
if !ct.is_empty() && ct.contains(":") {
let _ = write!(client_opts, r#" --ct "{}""#, ct);
if !ct.is_empty() && ct.contains(':') {
let _ = write!(client_opts, r#" --ct "{ct}""#);
}
if !cu.is_empty() && cu.contains(":") {
let _ = write!(client_opts, r#" --cu "{}""#, cu);
if !cu.is_empty() && cu.contains(':') {
let _ = write!(client_opts, r#" --cu "{cu}""#);
}

if !iface.is_empty() {
let _ = write!(client_opts, r#" --iface "{}""#, iface);
let _ = write!(client_opts, r#" --iface "{iface}""#);
}
if !exclude_iface.is_empty() {
let _ = write!(client_opts, r#" --exclude-iface "{}""#, exclude_iface);
let _ = write!(client_opts, r#" --exclude-iface "{exclude_iface}""#);
}

Ok(jinja::render_template(
Expand Down Expand Up @@ -326,16 +326,16 @@ pub async fn get_detail(req: Request<Body>) -> Result<Response<Body>> {
.as_ref()
.map(|o| {
let mut s = String::new();
s.push_str(format!("version: {}\n", o.version).as_str());
s.push_str(format!("host_name: {}\n", o.host_name).as_str());
s.push_str(format!("os_name: {}\n", o.os_name).as_str());
s.push_str(format!("os_arch: {}\n", o.os_arch).as_str());
s.push_str(format!("os_family: {}\n", o.os_family).as_str());
s.push_str(format!("os_release: {}\n", o.os_release).as_str());
s.push_str(format!("kernel_version: {}\n", o.kernel_version).as_str());
s.push_str(format!("cpu_num: {}\n", o.cpu_num).as_str());
s.push_str(format!("cpu_brand: {}\n", o.cpu_brand).as_str());
s.push_str(format!("cpu_vender_id: {}", o.cpu_vender_id).as_str());
s.push_str(&format!("version: {}\n", o.version));
s.push_str(&format!("host_name: {}\n", o.host_name));
s.push_str(&format!("os_name: {}\n", o.os_name));
s.push_str(&format!("os_arch: {}\n", o.os_arch));
s.push_str(&format!("os_family: {}\n", o.os_family));
s.push_str(&format!("os_release: {}\n", o.os_release));
s.push_str(&format!("kernel_version: {}\n", o.kernel_version));
s.push_str(&format!("cpu_num: {}\n", o.cpu_num));
s.push_str(&format!("cpu_brand: {}\n", o.cpu_brand));
s.push_str(&format!("cpu_vender_id: {}", o.cpu_vender_id));
s
})
.unwrap_or_default();
Expand Down Expand Up @@ -372,7 +372,7 @@ pub async fn get_detail(req: Request<Body>) -> Result<Response<Body>> {
host.uptime_str,
ip_info.query,
sys_info,
format!("{}\n{}", addrs, isp)
format!("{addrs}\n{isp}")
]);
} else {
table.add_row(row![
Expand Down
4 changes: 2 additions & 2 deletions server/src/jinja.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ where
T: Into<String> + std::fmt::Display,
S: Into<String>,
{
let name = format!("{}.{}", kind, tag);
let name = format!("{kind}.{tag}");
JINJA_ENV
.lock()
.as_mut()
Expand All @@ -24,7 +24,7 @@ where
}

pub fn render_template<'a>(kind: &'a str, tag: &'a str, ctx: Value, trim: bool) -> Result<String> {
let name = format!("{}.{}", kind, tag);
let name = format!("{kind}.{tag}");
Ok(JINJA_ENV
.lock()
.map(|e| {
Expand Down
4 changes: 2 additions & 2 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,11 @@ async fn main() -> Result<()> {
let http_service = make_service_fn(|_| async { Ok::<_, GenericError>(service_fn(main_service_func)) });

let http_addr = G_CONFIG.get().unwrap().http_addr.parse()?;
eprintln!("🚀 listening on http://{}", http_addr);
eprintln!("🚀 listening on http://{http_addr}");
let server = Server::bind(&http_addr).serve(http_service);
let graceful = server.with_graceful_shutdown(shutdown_signal());
if let Err(e) = graceful.await {
eprintln!("server error: {}", e);
eprintln!("server error: {e}");
}

Ok(())
Expand Down
10 changes: 5 additions & 5 deletions server/src/notifier/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl crate::notifier::Notifier for Log {

let dt = Local::now().format("%Y-%m-%d").to_string();
let log_file = Path::new(&self.config.log_dir)
.join(format!("ssr.log.{}", dt))
.join(format!("ssr.log.{dt}"))
.to_string_lossy()
.to_string();

Expand All @@ -61,23 +61,23 @@ impl crate::notifier::Notifier for Log {
.append(true)
.open(&log_file)
.await
.unwrap_or_else(|_| panic!("can't create log `{}", log_file));
.unwrap_or_else(|_| panic!("can't create log `{log_file}"));

let _ = file
.write(content.as_bytes())
.await
.unwrap_or_else(|_| panic!("can't write log `{}", log_file));
.unwrap_or_else(|_| panic!("can't write log `{log_file}"));

if !content.ends_with('\n') {
let _ = file
.write(b"\n")
.await
.unwrap_or_else(|_| panic!("can't write log `{}", log_file));
.unwrap_or_else(|_| panic!("can't write log `{log_file}"));
}

file.flush()
.await
.unwrap_or_else(|_| panic!("can't flush log `{}", log_file));
.unwrap_or_else(|_| panic!("can't flush log `{log_file}"));
});
Ok(())
}
Expand Down
3 changes: 1 addition & 2 deletions server/src/notifier/webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ impl Webhook {
o.engine.register_fn("now_str", now_str);

for r in o.config.receiver.iter() {
let ast = o.engine.compile(&r.script).unwrap();

if r.enabled {
let ast = o.engine.compile(&r.script).unwrap();
o.ast_list.push(Some(ast));
} else {
o.ast_list.push(None);
Expand Down
6 changes: 2 additions & 4 deletions server/src/notifier/wechat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,8 @@ impl crate::notifier::Notifier for WeChat {
if let Ok(json_data) = json_res {
if let Some(access_token) = json_data.get("access_token") {
if let Some(token) = access_token.as_str() {
let req_url = format!(
"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={}",
token
);
let req_url =
format!("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={token}");
let req_data = serde_json::json!({
"touser": "@all",
"agentid": agent_id,
Expand Down
4 changes: 2 additions & 2 deletions server/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl StatsMgr {
// uptime str
let day = (stat_t.uptime as f64 / 3600.0 / 24.0) as i64;
if day > 0 {
stat_t.uptime_str = format!("{} 天", day);
stat_t.uptime_str = format!("{day} 天");
} else {
stat_t.uptime_str = format!(
"{:02}:{:02}:{:02}",
Expand Down Expand Up @@ -256,7 +256,7 @@ impl StatsMgr {
let os_r = sys_info.os_release.to_lowercase();
for s in OS_LIST.iter() {
if os_r.contains(s) {
write!(o.labels, ";os={}", s);
write!(o.labels, ";os={s}");
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion web/jinja/client-init.jinja.sh
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function download_client() {
cd ${SSR_WORKSPACE}
rm -rf client-*.zip stat_* | true

say "start downloading the stat_client"
say "start download the stat_client"

if [ "${SSR_CN}" = true ]; then
say "using cn mirror: coding.net"
Expand Down

0 comments on commit 3a520f0

Please sign in to comment.