Skip to content

Commit

Permalink
style: fix clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemeowx2 committed Jun 22, 2021
1 parent c9311bf commit 7071019
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 39 deletions.
15 changes: 6 additions & 9 deletions protocol/raw/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl MapTable {
self.map.lock().insert(key, value);
}
pub fn get(&self, key: &SocketAddrV4) -> Option<SocketAddrV4> {
self.map.lock().get(key).map(|i| *i)
self.map.lock().get(key).copied()
}
}

Expand Down Expand Up @@ -85,16 +85,13 @@ fn set_src_addr<T: AsRef<[u8]> + AsMut<[u8]>>(
let src_addr = src_addr_v4.ip().to_owned().into();
let dst_addr = ip.dst_addr();
let port = src_addr_v4.port();
match ip.protocol() {
IpProtocol::Tcp => {
ip.set_src_addr(src_addr);
if let IpProtocol::Tcp = ip.protocol() {
ip.set_src_addr(src_addr);

let mut tcp = TcpPacket::new_checked(ip.payload_mut())?;
tcp.set_src_port(port);
let mut tcp = TcpPacket::new_checked(ip.payload_mut())?;
tcp.set_src_port(port);

tcp.fill_checksum(&src_addr.into(), &dst_addr.into());
}
_ => {}
tcp.fill_checksum(&src_addr.into(), &dst_addr.into());
};
ip.fill_checksum();

Expand Down
4 changes: 2 additions & 2 deletions protocol/raw/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub struct RawServer {
map: MapTable,
}

fn filter_packet(packet: &Packet, ethernet_addr: EthernetAddress, ip_addr: IpCidr) -> bool {
fn filter_packet(packet: &[u8], ethernet_addr: EthernetAddress, ip_addr: IpCidr) -> bool {
if let Ok(f) = EthernetFrame::new_checked(packet) {
let ether_accept = f.dst_addr() == ethernet_addr || f.dst_addr().is_broadcast();
ether_accept && {
Expand Down Expand Up @@ -174,7 +174,7 @@ impl RawServer {
let udp = nat
.entry(src)
.or_insert_with(|| UdpTunnel::new(net.clone(), src, send_raw.clone()));
if let Err(e) = udp.send_to(payload, dst.into()) {
if let Err(e) = udp.send_to(payload, dst) {
tracing::error!("Udp send_to {:?}", e);
nat.remove(&src);
}
Expand Down
2 changes: 1 addition & 1 deletion protocol/remote/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl PassiveProtocol {
};
let r = new_one.accept().await;
*listener = Some(new_one);
return r;
r
}
}

Expand Down
4 changes: 3 additions & 1 deletion protocol/remote/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ async fn process_channel(mut channel: Channel, net: Net, map: Map) -> Result<()>
}
}
CommandRequest::TcpAccept { id } => {
let target = map.get(id).ok_or(Error::Other("ID is not found".into()))?;
let target = map
.get(id)
.ok_or_else(|| Error::Other("ID is not found".into()))?;
connect_tcp(target, channel.into_inner()).await?;
}
}
Expand Down
2 changes: 1 addition & 1 deletion protocol/ss/src/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ async fn decrypt_payload_stream(
tracing::trace!("UDP packet got stream IV {:?}", ByteStr::new(iv));
let mut cipher = Cipher::new(method, key, iv);

assert_eq!(cipher.decrypt_packet(data), true);
assert!(cipher.decrypt_packet(data));

let (dn, addr) = parse_packet(data).await?;

Expand Down
12 changes: 6 additions & 6 deletions protocol/ss/src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ impl From<RDAddress> for WrapAddress {
}
}

impl Into<SSAddress> for WrapAddress {
fn into(self) -> SSAddress {
match self.0 {
impl From<WrapAddress> for SSAddress {
fn from(w: WrapAddress) -> Self {
match w.0 {
RDAddress::Domain(domain, port) => SSAddress::DomainNameAddress(domain, port),
RDAddress::SocketAddr(s) => SSAddress::SocketAddress(s),
}
Expand Down Expand Up @@ -129,10 +129,10 @@ pub enum Cipher {

impl_empty_net_resolve! { Cipher }

impl Into<CipherKind> for Cipher {
fn into(self) -> CipherKind {
impl From<Cipher> for CipherKind {
fn from(c: Cipher) -> Self {
let s: serde_json::Value =
serde_json::from_str(&serde_json::to_string(&self).unwrap()).unwrap();
serde_json::from_str(&serde_json::to_string(&c).unwrap()).unwrap();
CipherKind::from_str(s.as_str().unwrap()).unwrap()
}
}
Expand Down
5 changes: 2 additions & 3 deletions protocol/trojan/src/client/udp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
io::{Cursor, Write},
mem::replace,
mem::take,
net::SocketAddr,
sync::RwLock,
};
Expand Down Expand Up @@ -67,8 +67,7 @@ impl IUdpSocket for TrojanUdp {
let buf = if self.head.read().unwrap().len() > 0 {
let mut head = self.head.write().unwrap();
if head.len() > 0 {
let buf = replace(&mut *head, Vec::new());
buf
take(&mut *head)
} else {
Vec::new()
}
Expand Down
2 changes: 1 addition & 1 deletion protocol/trojan/src/tls/native-tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ impl TlsConnector {
.connect(self.sni.as_ref(), stream)
.await
.map_err(map_other)?;
Ok(stream.into())
Ok(stream)
}
}
4 changes: 2 additions & 2 deletions src/api_server/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn api(server: Server) -> impl Filter<Extract = impl warp::Reply, Error = Re
// TODO: read or write userdata by API
let userdata = &server
.userdata
.or(dirs::config_dir().map(|d| d.join("rabbit-digger")));
.or_else(|| dirs::config_dir().map(|d| d.join("rabbit-digger")));
let ctl = &server.controller;
let stopper: Arc<Mutex<Option<OnceConfigStopper>>> = Arc::new(Mutex::new(None));

Expand Down Expand Up @@ -86,7 +86,7 @@ pub fn routes(
.allow_headers(["authorization", "content-type"])
.allow_methods(["GET", "POST", "PUT", "DELETE"]);

return api(server).or(forward).with(cors);
api(server).or(forward).with(cors)
}

// Websocket /event
Expand Down
10 changes: 5 additions & 5 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ pub async fn generate_schema() -> Result<RootSchema> {
root.definitions.extend(schema.definitions);
}

let string_schema = {
let mut schema = SchemaObject::default();
schema.instance_type = Some(InstanceType::String.into());
schema.into()
};
let string_schema = SchemaObject {
instance_type: Some(InstanceType::String.into()),
..Default::default()
}
.into();
let net_schema = anyof_schema(nets);
let server_schema = anyof_schema(servers);

Expand Down
2 changes: 1 addition & 1 deletion src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct SelectNet(Net);

impl SelectNet {
pub fn new(config: SelectNetConfig) -> Result<Self> {
if config.list.len() == 0 {
if config.list.is_empty() {
return Err(Error::Other("select list is empty".into()));
}
let index = config.selected.min(config.list.len() - 1);
Expand Down
14 changes: 8 additions & 6 deletions src/translate/clash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,16 @@ impl Clash {

fn get_target(&self, target: &str) -> Result<String> {
if target == "DIRECT" {
return Ok(self.direct.clone().unwrap_or("local".to_string()));
return Ok(self.direct.clone().unwrap_or_else(|| "local".to_string()));
}
// TODO: noop is not reject, add blackhole net
if target == "REJECT" {
return Ok(self.direct.clone().unwrap_or("noop".to_string()));
return Ok(self.direct.clone().unwrap_or_else(|| "noop".to_string()));
}
let net_name = self.name_map.get(target);
net_name
.map(|i| i.to_string())
.ok_or(anyhow!("Name not found. clash name: {}", target))
.ok_or_else(|| anyhow!("Name not found. clash name: {}", target))
}

fn proxy_group_to_net(&self, p: ProxyGroup) -> Result<Net> {
Expand Down Expand Up @@ -135,7 +135,7 @@ impl Clash {

fn rule_to_rule(&self, r: &str) -> Result<rule_config::RuleItem> {
let bad_rule = || anyhow!("Bad rule.");
let mut ps = r.split(",");
let mut ps = r.split(',');
let mut ps_next = || ps.next().ok_or_else(bad_rule);
let rule_type = ps_next()?;
let item = match rule_type {
Expand Down Expand Up @@ -220,7 +220,7 @@ impl Clash {
.collect(),
|_, i: &ProxyGroup| Ok(i.proxies.clone()) as Result<_>,
)?
.ok_or(anyhow!("There is cyclic dependencies in proxy_groups"))?;
.ok_or_else(|| anyhow!("There is cyclic dependencies in proxy_groups"))?;

for (old_name, pg) in proxy_groups {
let name = self.proxy_group_name(&old_name);
Expand Down Expand Up @@ -258,7 +258,9 @@ impl Clash {
Server::new(
"socks5",
"local",
self.target.clone().unwrap_or(self.prefix("clash_rule")),
self.target
.clone()
.unwrap_or_else(|| self.prefix("clash_rule")),
json!({ "bind": format!("0.0.0.0:{}", clash_config.socks_port) }),
),
);
Expand Down

0 comments on commit 7071019

Please sign in to comment.