]> git.feebdaed.xyz Git - linuxyz.git/commitdiff
ncat rust tested
authorseantywork <seantywork@gmail.com>
Mon, 28 Apr 2025 00:28:09 +0000 (09:28 +0900)
committerseantywork <seantywork@gmail.com>
Mon, 28 Apr 2025 00:28:09 +0000 (09:28 +0900)
0xetc/0xrs/ncat/src/main.rs
0xetc/0xrs/ncat/src/ncat/ncat.rs
0xetc/0xrs/ncat/test.sh

index 686d0a423cb0047a45b43d10d8adf97add8fe496..3a7e4d732c2a05861706950c5e25dd97fcb6b8ed 100644 (file)
@@ -1,7 +1,7 @@
 mod ncat;
 
 use std::{
-    env, ops::Deref, process::{self, ExitCode}, thread
+    env, ops::Deref, process::{self, ExitCode}, thread, sync::Arc
 };
 
 use tokio;
@@ -13,8 +13,6 @@ async fn main() -> process::ExitCode {
     
     let args: Vec<String> = env::args().collect();
 
-    let mut ncat_opts = NCAT::NcatOptions::new();
-
     if args.len() < 2 {
 
         println!("too few arguments");
@@ -54,7 +52,22 @@ async fn main() -> process::ExitCode {
 
         Ok(mut arcno) => {
 
-            ncat_opts = arcno.as_ref().clone();
+            let mut ncat_opts = arcno.clone();
+
+            match NCAT::runner(ncat_opts.clone()) {
+
+                Ok(()) => {
+
+                    return process::ExitCode::from(0u8);
+
+                }
+
+                Err(e) => {
+
+                    println!("error: {}", e);
+                }
+            }
+
 
         }
 
@@ -64,11 +77,5 @@ async fn main() -> process::ExitCode {
         }
     }
 
-    tokio::spawn(NCAT::runner(ncat_opts.clone()));
-
-    loop {
-
-    }
-
     return process::ExitCode::from(0u8);
 }
index 36d357aa1dc12991d49dab873a8ad01b9bda256a..910e4a149d998fe479f618cae81afe3b1970ef57 100644 (file)
@@ -104,7 +104,7 @@ pub fn parse_args(args: &Vec<String>) -> Result<Arc<NcatOptions>, String>{
     
 }
 
-pub async fn runner(mut ncat_opts: NcatOptions) -> Result<(), String> {
+pub fn runner(mut ncat_opts: Arc<NcatOptions>) -> Result<(), String> {
 
     let (tx, rx) = mpsc::channel::<NcatOptions>();
 
@@ -136,19 +136,22 @@ pub async fn runner(mut ncat_opts: NcatOptions) -> Result<(), String> {
 
                 ncat_opts_updated = received;
 
+                let result = listen_and_serve(ncat_opts_updated.clone());
+
+                return result;
             }
 
             Err(e) => {
 
-                ncat_opts_updated = ncat_opts.clone();
+                ncat_opts_updated = ncat_opts.as_ref().clone();
+
+                let result = listen_and_serve(ncat_opts_updated.clone());
+            
+                return result;
             }
         };
 
 
-        let result = listen_and_serve(ncat_opts_updated.clone());
-        
-        return result;
-
     }
 
     return Err("unsupported mode".to_string());
@@ -156,7 +159,7 @@ pub async fn runner(mut ncat_opts: NcatOptions) -> Result<(), String> {
 }
 
 
-fn client(mut ncat_opts: NcatOptions, tx: Sender<TcpStream>) -> Result<(), String> {
+fn client(mut ncat_opts: Arc<NcatOptions>, tx: Sender<TcpStream>) -> Result<(), String> {
 
     let mut stream = TcpStream::connect((ncat_opts.host.as_str(), ncat_opts.port.to_string().parse::<u16>().unwrap())).unwrap();
 
@@ -184,7 +187,7 @@ fn client(mut ncat_opts: NcatOptions, tx: Sender<TcpStream>) -> Result<(), Strin
         
         let message = line.unwrap();
         
-        if message == "exit".to_string() {
+        if message.trim() == "exit" {
 
             break;
 
@@ -204,8 +207,6 @@ fn client(mut ncat_opts: NcatOptions, tx: Sender<TcpStream>) -> Result<(), Strin
 
         wbuff_vec.append(&mut message_vec);
 
-        //let wbuff = wbuff_vec.as_slice();
-
         let wsize = io_stream.write(&wbuff_vec).unwrap();
 
         if wsize <= 0 {
@@ -214,6 +215,8 @@ fn client(mut ncat_opts: NcatOptions, tx: Sender<TcpStream>) -> Result<(), Strin
         }
     }
 
+    println!("return");
+
     return Ok(());
 }
 
@@ -236,6 +239,8 @@ fn listen_and_serve(mut ncat_opts: NcatOptions) -> Result<(), String> {
 
                 let mut header = [0u8; 4];
 
+                let mut count = 0u32;
+
                 loop {
             
                     let mut valread = 0;
@@ -261,12 +266,10 @@ fn listen_and_serve(mut ncat_opts: NcatOptions) -> Result<(), String> {
                     }
             
             
-                    let mut datalen = LittleEndian::read_u32(&mut header);
-                    
-                    println!("datalen: {}", datalen);
+                    let mut datalen = BigEndian::read_u32(&mut header);
 
-                    let mut data = Vec::<u8>::with_capacity(datalen as usize);
-            
+                    let mut data = vec![0; datalen as usize];;
+        
                     valread = 0;
             
                     loop {
@@ -289,7 +292,10 @@ fn listen_and_serve(mut ncat_opts: NcatOptions) -> Result<(), String> {
             
                     }
             
+
                     println!("{}", String::from_utf8(data).unwrap());
+            
+                    count += 1;
                 }
             
 
@@ -306,7 +312,7 @@ fn listen_and_serve(mut ncat_opts: NcatOptions) -> Result<(), String> {
 }
 
 
-async fn get_thread(mut ncat_opts: NcatOptions, tx: Sender<NcatOptions>, rx: Receiver<TcpStream>) {
+async fn get_thread(mut ncat_opts: Arc<NcatOptions>, tx: Sender<NcatOptions>, rx: Receiver<TcpStream>) {
 
     if(ncat_opts.mode_client) {
 
index efc8508fa7fe9f0353b28fd53514d31e1487fbf3..d91959eb904fff2bd8d7c8ece730888712714edb 100755 (executable)
@@ -26,7 +26,7 @@ do
 done
 echo "exit" >> test.txt
 
-sudo ip netns exec net1 ./ncat.out -l 192.168.62.6 9999 > /dev/null 2>&1 &
+sudo ip netns exec net1 ./ncat.out -l 192.168.62.6 9999 
 
 sleep 1