cargo fmt. new format serialize notfound packet

This commit is contained in:
serxoz 2022-12-07 10:21:35 +01:00
parent 76e73b4058
commit 90156e3837
4 changed files with 66 additions and 71 deletions

View File

@ -5,13 +5,13 @@
*/
pub mod sip;
use crate::sip::not_found::*;
use crate::sip::options::*;
use std::error::Error;
use std::net::SocketAddr;
use std::{env, io};
use tokio::net::UdpSocket;
use tokio::time::{sleep, Duration};
use crate::sip::options::*;
use crate::sip::not_found::*;
struct Server {
socket: UdpSocket,
@ -40,7 +40,7 @@ impl Server {
match msg {
msg if msg.contains("OPTIONS") => {
// esperar - tarpit!
sleep(Duration::from_secs(5)).await;
sleep(Duration::from_secs(1)).await;
let options = Options::parse(&msg);
// respondemos con un 404 not found
@ -61,9 +61,14 @@ impl Server {
content_length: Some(0)
};
let amt = socket.send_to(&not_found.serialize(), &peer).await?;
println!("Sent {}/{} bytes to {}", amt, &not_found.serialize().len(), peer);
},
_ => println!("Packet don't known, yet...")
println!(
"Sent {}/{} bytes to {}",
amt,
&not_found.serialize().len(),
peer
);
}
_ => println!("Packet don't known, yet..."),
}
}

View File

@ -1,2 +1,2 @@
pub mod options;
pub mod not_found;
pub mod options;

View File

@ -16,63 +16,52 @@ Content-Length: 0
*/
#[derive(Debug)]
pub struct NotFoundPacket {
pub command: String, // SIP/2.0 404 Not Found
pub via: String, // SIP/2.0/UDP 127.0.1.1:5061;branch=z9hG4bK-487618168;rport
pub call_id: Option<String>, // Call-ID: 447615548427934163033914
pub from: String, // "sipvicious"<sip:100@1.1.1.1>;tag=37663030303030313133633401393537303038303638
pub to: String, // "sipvicious"<sip:100@1.1.1.1>
pub cseq: String, // 1 OPTIONS
pub accept: Option<String>, // application/sdp
pub allow: Option<String>, // OPTIONS, REGISTER, SUBSCRIBE, NOTIFY, PUBLISH, INVITE...
pub supported: Option<String>, // 100rel, timer, replaces, norefersub
pub accept_encoding: Option<String>, // identity
pub accept_language: Option<String>, // en
pub server: Option<String>, // Asterisk PBX 18.9.0
pub content_length: Option<i32> // 0
pub command: String, // SIP/2.0 404 Not Found
pub via: String, // SIP/2.0/UDP 127.0.1.1:5061;branch=z9hG4bK-487618168;rport
pub call_id: Option<String>, // Call-ID: 447615548427934163033914
pub from: String, // "sipvicious"<sip:100@1.1.1.1>;tag=37663030303030313133633401393537303038303638
pub to: String, // "sipvicious"<sip:100@1.1.1.1>
pub cseq: String, // 1 OPTIONS
pub accept: Option<String>, // application/sdp
pub allow: Option<String>, // OPTIONS, REGISTER, SUBSCRIBE, NOTIFY, PUBLISH, INVITE...
pub supported: Option<String>, // 100rel, timer, replaces, norefersub
pub accept_encoding: Option<String>, // identity
pub accept_language: Option<String>, // en
pub server: Option<String>, // Asterisk PBX 18.9.0
pub content_length: Option<i32>, // 0
}
impl NotFoundPacket {
pub fn serialize(&self) -> Vec<u8> {
let mut preout = String::new();
preout.push_str(&self.command);
preout.push_str("\n");
preout.push_str("Via: ");
preout.push_str(&self.via);
preout.push_str("\n");
preout.push_str("Call-ID: ");
preout.push_str(&self.call_id.as_ref().unwrap());
preout.push_str("\n");
preout.push_str("From: ");
preout.push_str(&self.from);
preout.push_str("\n");
preout.push_str("To: ");
preout.push_str(&self.to);
preout.push_str("\n");
preout.push_str("CSeq: ");
preout.push_str(&self.cseq);
preout.push_str("\n");
preout.push_str("Accept: ");
preout.push_str(&self.accept.as_ref().unwrap());
preout.push_str("\n");
preout.push_str("Allow: ");
preout.push_str(&self.allow.as_ref().unwrap());
preout.push_str("\n");
preout.push_str("Supported: ");
preout.push_str(&self.supported.as_ref().unwrap());
preout.push_str("\n");
preout.push_str("Accept-Encoding: ");
preout.push_str(&self.accept_encoding.as_ref().unwrap());
preout.push_str("\n");
preout.push_str("Accept-Language: ");
preout.push_str(&self.accept_language.as_ref().unwrap());
preout.push_str("\n");
preout.push_str("Server: ");
preout.push_str(&self.server.as_ref().unwrap());
preout.push_str("\n");
preout.push_str("Content-Length: ");
preout.push_str(&self.content_length.unwrap().to_string());
preout.push_str("\n");
preout.push_str("\n");
let mut preout = format!(
"{}
Via: {}
Call-ID: {}
From: {}
To: {}
CSeq: {}
Accept: {}
Allow: {}
Supported: {}
Accept-Encoding: {}
Accept-Language: {}
Server: {}
Content-Length: {}",
&self.command,
&self.via,
&self.call_id.as_ref().unwrap(),
&self.from,
&self.to,
&self.cseq,
&self.accept.as_ref().unwrap(),
&self.allow.as_ref().unwrap(),
&self.supported.as_ref().unwrap(),
&self.accept_encoding.as_ref().unwrap(),
&self.accept_language.as_ref().unwrap(),
&self.server.as_ref().unwrap(),
&self.content_length.unwrap().to_string(),
);
preout.push_str("\n\n");
// println!("{}", preout);
preout.as_bytes().to_vec()
}

View File

@ -6,13 +6,13 @@ pub struct Options {
pub via: String, // SIP/2.0/UDP 127.0.1.1:5061;branch=z9hG4bK-487618168;rport
pub max_forwards: i32, // 70
pub to: String, // "sipvicious"<sip:100@1.1.1.1>
pub from: String, // "sipvicious"<sip:100@1.1.1.1>;tag=37663030303030313133633401393537303038303638
pub user_agent: Option<String>, // friendly-scanner
pub call_id: Option<String>, // Call-ID: 447615548427934163033914
pub contact: Option<String>, // sip:100@127.0.1.1:5061
pub cseq: String, // 1 OPTIONS
pub accept: Option<String>, // application/sdp
pub content_length: Option<i32> // 0
pub from: String, // "sipvicious"<sip:100@1.1.1.1>;tag=37663030303030313133633401393537303038303638
pub user_agent: Option<String>, // friendly-scanner
pub call_id: Option<String>, // Call-ID: 447615548427934163033914
pub contact: Option<String>, // sip:100@127.0.1.1:5061
pub cseq: String, // 1 OPTIONS
pub accept: Option<String>, // application/sdp
pub content_length: Option<i32>, // 0
}
impl Options {
@ -28,9 +28,8 @@ impl Options {
contact: Some(String::new()),
cseq: String::new(),
accept: Some(String::new()),
content_length: Some(0,)
content_length: Some(0),
}
}
pub fn parse(msg: &String) -> Options {
@ -42,7 +41,7 @@ impl Options {
let parts = line.split(":").collect::<Vec<&str>>();
let header = parts[0].trim();
let mut content = "".to_owned();
for p in &parts[1..]{
for p in &parts[1..] {
content.push_str(p.trim())
}
match header {
@ -56,7 +55,9 @@ impl Options {
"Contact" => packet.contact = Some(content.to_string()),
"CSeq" => packet.cseq = content.to_string(),
"Accept" => packet.accept = Some(content.to_string()),
"Content-Length" => packet.content_length = Some(content.parse::<i32>().unwrap()),
"Content-Length" => {
packet.content_length = Some(content.parse::<i32>().unwrap())
}
_ => println!("---> {:?} no contemplada!", header),
}
}