-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from gongzhengyang/feature-global-improve
Feature global improve
- Loading branch information
Showing
19 changed files
with
256 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
.idea/ | ||
Cargo.lock | ||
tmp | ||
temp.rs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,37 @@ | ||
use serde::Serialize; | ||
use thiserror::Error; | ||
use snafu::prelude::*; | ||
use snafu::Location; | ||
use std::num::ParseIntError; | ||
|
||
#[derive(Error, Debug, Serialize, PartialEq)] | ||
#[derive(Snafu, Debug)] | ||
#[snafu(visibility(pub))] | ||
pub enum APPError { | ||
#[error("split failed because ip or port format is error")] | ||
SplitIpPortFailed, | ||
#[error("port args format is error")] | ||
PortFormatError, | ||
#[error("port couldn't be empty")] | ||
#[snafu(display("split failed because ip or port format is error {}", value))] | ||
SplitIpPortFailed { location: Location, value: String }, | ||
|
||
#[snafu(display("port args format is error {}", value))] | ||
PortFormat { location: Location, value: String }, | ||
|
||
#[snafu(display("port value parse failed {}", source))] | ||
PortParse { | ||
location: Location, | ||
source: ParseIntError, | ||
value: String, | ||
}, | ||
|
||
#[snafu(display("port couldn't be empty"))] | ||
PortIsEmpty, | ||
#[error("unix ulimits soft limit is bigger than hard limit")] | ||
|
||
#[snafu(display("unix ulimits soft limit is bigger than hard limit"))] | ||
ULimitSoftBiggerThanHard, | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::err::APPError; | ||
|
||
#[test] | ||
fn error_response() { | ||
for i in 0..3 { | ||
println!("{:?}", result_with_value(i)); | ||
} | ||
} | ||
|
||
fn result_with_value(value: u8) -> anyhow::Result<()> { | ||
if value.eq(&1) { | ||
return Err(APPError::SplitIpPortFailed.into()); | ||
} | ||
Ok(()) | ||
} | ||
#[snafu(display("Option value is None"))] | ||
OptionEmpty { location: Location }, | ||
|
||
#[snafu(display("common io error {}", source))] | ||
CommonIo { | ||
location: Location, | ||
source: std::io::Error, | ||
}, | ||
} | ||
|
||
pub type Result<T> = std::result::Result<T, APPError>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,70 @@ | ||
use std::net::Ipv4Addr; | ||
|
||
use pnet::datalink::{Channel, MacAddr}; | ||
use pnet::packet::ethernet::EtherTypes; | ||
use pnet::packet::ethernet::MutableEthernetPacket; | ||
use cached::proc_macro::cached; | ||
use pnet::datalink::{Channel, MacAddr, NetworkInterface}; | ||
use pnet::packet::{ | ||
icmp::echo_request::MutableEchoRequestPacket, ip::IpNextHeaderProtocols, | ||
ethernet::{EtherTypes, MutableEthernetPacket}, | ||
icmp::echo_request::MutableEchoRequestPacket, | ||
ip::IpNextHeaderProtocols, | ||
ipv4::MutableIpv4Packet, | ||
MutablePacket, Packet, | ||
}; | ||
use pnet::packet::{MutablePacket, Packet}; | ||
use snafu::{OptionExt, ResultExt}; | ||
|
||
use crate::interfaces; | ||
use crate::interfaces::interface_normal_running; | ||
use crate::err::{CommonIoSnafu, OptionEmptySnafu, Result}; | ||
use crate::interfaces::{self, interface_normal_running}; | ||
|
||
use super::common; | ||
|
||
pub fn send_with_interface(target_ip: Ipv4Addr) { | ||
#[cached(time = 60)] | ||
pub fn running_interface_with_ip() -> Vec<(NetworkInterface, Ipv4Addr)> { | ||
pnet::datalink::interfaces() | ||
.into_iter() | ||
.filter(interface_normal_running) | ||
.filter_map(|interface| interfaces::get_interface_ipv4(&interface).map(|x| (interface, x))) | ||
.collect() | ||
} | ||
|
||
pub fn send_with_interface(target_ip: Ipv4Addr) -> Result<()> { | ||
tracing::debug!("{target_ip} send by specific interface"); | ||
for interface in pnet::datalink::interfaces() { | ||
if !interface_normal_running(&interface) { | ||
continue; | ||
} | ||
if let Some(source_ip) = interfaces::get_interface_ipv4(&interface) { | ||
let mut header = [0u8; common::ICMP_LEN]; | ||
let mut icmp_packet = MutableEchoRequestPacket::new(&mut header).unwrap(); | ||
common::modify_icmp_packet(&mut icmp_packet); | ||
|
||
let mut ipv4_header = [0u8; common::IPV4_LEN]; | ||
let mut ipv4_packet = MutableIpv4Packet::new(&mut ipv4_header).unwrap(); | ||
ipv4_packet.set_version(4); | ||
ipv4_packet.set_header_length(5); | ||
ipv4_packet.set_dscp(4); | ||
ipv4_packet.set_ecn(1); | ||
ipv4_packet.set_ttl(64); | ||
ipv4_packet.set_next_level_protocol(IpNextHeaderProtocols::Icmp); | ||
ipv4_packet.set_source(source_ip); | ||
ipv4_packet.set_destination(target_ip); | ||
ipv4_packet.set_total_length(common::IPV4_LEN as u16); | ||
ipv4_packet.set_payload(icmp_packet.packet_mut()); | ||
ipv4_packet.set_checksum(pnet::packet::ipv4::checksum(&ipv4_packet.to_immutable())); | ||
|
||
let mut ethernet_buffer = [0u8; common::ETHERNET_LEN]; | ||
let mut ethernet_packet = MutableEthernetPacket::new(&mut ethernet_buffer).unwrap(); | ||
|
||
ethernet_packet.set_destination(MacAddr::broadcast()); | ||
ethernet_packet.set_source(interface.mac.unwrap()); | ||
ethernet_packet.set_ethertype(EtherTypes::Ipv4); | ||
ethernet_packet.set_payload(ipv4_packet.packet_mut()); | ||
|
||
let (mut sender, _) = match pnet::datalink::channel(&interface, Default::default()) { | ||
Ok(Channel::Ethernet(tx, rx)) => (tx, rx), | ||
Ok(_) => panic!("Unknown channel type"), | ||
Err(e) => panic!("Error happened {}", e), | ||
}; | ||
sender | ||
.send_to(ethernet_packet.packet(), Some(interface)) | ||
.unwrap() | ||
.unwrap(); | ||
} | ||
for (interface, source_ip) in running_interface_with_ip() { | ||
let mut header = [0u8; common::ICMP_LEN]; | ||
let mut icmp_packet = | ||
MutableEchoRequestPacket::new(&mut header).context(OptionEmptySnafu)?; | ||
common::set_icmp_send_packet(&mut icmp_packet); | ||
|
||
let mut ipv4_header = [0u8; common::IPV4_LEN]; | ||
let mut ipv4_packet = MutableIpv4Packet::new(&mut ipv4_header).context(OptionEmptySnafu)?; | ||
ipv4_packet.set_version(4); | ||
ipv4_packet.set_header_length(5); | ||
ipv4_packet.set_dscp(4); | ||
ipv4_packet.set_ecn(1); | ||
ipv4_packet.set_ttl(64); | ||
ipv4_packet.set_next_level_protocol(IpNextHeaderProtocols::Icmp); | ||
ipv4_packet.set_source(source_ip); | ||
ipv4_packet.set_destination(target_ip); | ||
ipv4_packet.set_total_length(common::IPV4_LEN as u16); | ||
ipv4_packet.set_payload(icmp_packet.packet_mut()); | ||
ipv4_packet.set_checksum(pnet::packet::ipv4::checksum(&ipv4_packet.to_immutable())); | ||
|
||
let mut ethernet_buffer = [0u8; common::ETHERNET_LEN]; | ||
let mut ethernet_packet = | ||
MutableEthernetPacket::new(&mut ethernet_buffer).context(OptionEmptySnafu)?; | ||
|
||
ethernet_packet.set_destination(MacAddr::broadcast()); | ||
ethernet_packet.set_source(interface.mac.context(OptionEmptySnafu)?); | ||
ethernet_packet.set_ethertype(EtherTypes::Ipv4); | ||
ethernet_packet.set_payload(ipv4_packet.packet_mut()); | ||
|
||
let (mut sender, _) = match pnet::datalink::channel(&interface, Default::default()) { | ||
Ok(Channel::Ethernet(tx, rx)) => (tx, rx), | ||
Ok(_) => panic!("Unknown channel type"), | ||
Err(e) => panic!("Error happened {}", e), | ||
}; | ||
sender | ||
.send_to(ethernet_packet.packet(), Some(interface)) | ||
.context(OptionEmptySnafu)? | ||
.context(CommonIoSnafu)?; | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.