Rustlang Project: Port Sniffer CLI
Key Takeaways
The video demonstrates building a Port sniffer command line interface using Rustlang and its standard library, showcasing concepts such as command-line tool creation, argument parsing, and multi-threaded programming.
Full Transcript
hi guys welcome to programming and rust my name is tensor today we're going to be building a project and our project is going to be a porch sniffer so this will be a tool that we write in rust and we can access it via the command line it will also be multi-threaded and will have multiple different flags that we can pass into it to create our new project we want to call cargo new and then the name of our project in this case I'm just gonna call it IP sniffer we want it to be a binary so we're gonna pass the bin flag first we want to consider the type of syntax that we want to use here so these are what valid inputs for our command-line tool should look like the first one will be a flag that will present us with a help screen the second one here will allow the user to set how many threads they want this process to use and the final one will be just calling the tool and then buying it on an IP address and this will use the default set number of threads that we specify inside of our code here and it will scan this IP address automatically we want to bring in the standard library environmental namespace and this will allow us to pull our arguments out of our command-line arguments will be a args variable that is a vector of strings we can do this simply just by calling environments args not correct and this will then take all the arguments that we passed to this program and put them inside a vector of strings so we can see what this looks like by iterating through it and also by just printing it with the debug flag we can call our program by simply calling cargo run and then if we want to pass arguments to it we put double dashes here and this tells cargo that we're not putting the arguments towards cargo itself but rather we want to put it towards the executable and then say we pass in - H and you'll see here that the first element inside of our vector will be the path to the executable and then the second element will be our flag here if we call it with more arguments you can see here it's the same syntax we can assign our program name to a variable called program we can just pull it out of our argument vector by calling the zero index and we can clone it so we could do something like this but it's a little bit too verbose so let's set up a struct for our flag threads and IP address so that we can create a struct to hold all of this data properly right so I've created a struct here called arguments inside of it we have a field called Flags which will take a string then we have our IP address field which will take an IP address now this is an enum it basically can either be version 4 or version 6 for those of you who don't know much about IP addresses there are two different patterns that we can follow there's IP version 4 and IP version 6 and we want to make sure that it's valid before we actually try to run anything on top of it then finally our threads number will be au 16 now that we have a struct let's create an implementation block so that we can create a method that will allow us to instantiate this try the method that we want to create here is called new this will take in our arguments or a reference to our vector of string and then it will return a result that will have our arguments truck inside of the ok portion of it or it will have a static slice of string inside of the error portion of it so the reason why we're using this static reference to a slice of string is so that we can send back the errors to the main function and then have it handle those errors so first we want to run checks on our args list we want to see what the length is like so you can see here we're checking to see if the length is less than 2 because remember when we run our argument list the lowest amount that we can have our to so the help flag or if we just throw an IP address in so it'll have that program name and then the IP address or the program name and then the help flag our largest will be if we have the program name the thread flag the thread number and then the IP address below two lengths here we're just gonna pass back an error and it says not enough arguments above 4 then we're gonna pass back an error that says too many arguments because we're bringing in our arguments as strings we want to bring in the standard library STR from stir trait and this will allow us to convert our string to our IP address type first we want to create a variable that looks at the first index of our vector then we're going to use an if let binding 2d struck our IP address from string which returns a result and if we get back and ok then we use this IP address inside of here we can then explicitly say return our argument struct and then we'll put flag in here as an empty string and then we'll send back IP address which is this here as IP address and then we'll put our default thread number which is 4 if we don't get an okay from trying to convert F into a IP address that means that there's no IP address there which means that either a we have one of our Flags so either - H or - J or B we have some gobbly goo we want to assign a variable flag to args one clone then we want to check what's inside a flag by checking to see if it contains - H or if it contains - hell if it contains one of these we want to check to see if the argument length is exactly equal to - if we're calling help we do not want to have other arguments as well behind hell we just want to show this message here that we're printing out or it just says usage and it tells the user what J does and it tells us the user what H or helped us so this says usage desu J - select how many threads you want and then we have a return character and a newline - H or help to show this help message so we're actually going to return an error with help inside of it so that we can do some error handling outside of this function then we want to check to see if flag contains that's H or - help this case is basically saying okay well if the args dot rank wasn't - but it also contains - H or - help and we just want to return that there are too many arguments the only case that this will happen is if the user put in this H or - help and then put in something else after it so our final if is just going to check to see if our flag contains desu J and if it does then we're just going to match on turning our args index 3 into an IP address and we're going to bind it to this IP address variable here so then we'll just unwrap our ok value here otherwise if we get an error here we don't care about the actual air inside of it will return our own error that says not a valid IP address must be an ipv4 and ipv6 we want to deal with our threads variable so because we're getting strings again we want to be able to change those strings into au 16 we're going to use this farce method which will allow us to pass in u16 and it will then allow us to convert the string into you sixteen all right so our purse method here will return a result as well and all we really want to do is unwrap the value from inside of the okay and then bind it to threads if we get an error then we just pass back failed to parse thread number then at the bottom of this else if statement we just want to return an okay with our arguments struct inside of it we've bound this two threads so threads will be a parse to you sixteen brag will be the string that we've been matching on up here and then IP address will be the IP address that we matched on right here you'll see that we're still getting an error here because we have one more case but we need to write an else statement here and in this else statement all we really need to do is just return error invalid syntax if all of these checks fail then we're just simply throwing in gobbly goop that our program shouldn't be able to parse but so we can just create a variable called arguments and we call arguments pass in a reference to our args up here and then we want to call unwrap or else on it and this takes a closure inside this closure we just want to check to see if the error contains help otherwise we're just going to handle the other errors in a standard way before we go any further we need to bring in another namespace here we're bringing in the namespace standard library process and this will allow us to manage the way that our program shuts down if our error contains help we want to call process exit which will actually quit out of our program here and we want to pass a 0 so it doesn't pin it and then for our else statement we want to use this eprint Ln macro which is error print newline so it'll say the program name and then problem parsing the arguments and then it will display the error all these errors that we wrote up here aside from this help one will then display after this and then we will exit the process like we did up here let's bring in a few more namespaces so we can do some multi-threaded programming you need to bring in standard library sync MPSC sender and channel and then we need to bring in standard library and thread we're going to then bind our arguments dot thread to a variable here called number thread so then we want to instantiate a channel here so we're going to do the tuple that gets returned from it with our transmitter and receiver then we want to iterate from zero to the number of threads that we have and in this iteration we're going to take T X and we're going to bind it to another T X variable so T X clone this way each of our threads has its own transmitter we want to spawn our thread with a move closure and inside of this move closure we're gonna call a scan function that we're going to create and we're going to pass it the TX that we've created the I so the number of the thread then our IP address and then our number of threads that was passed into our arguments before we go further we also want to create a constant so this is the max port that we can actually sniff 65535 right so now let's create our scan function so this will take in our sender with a u 16 inside of it our start port so this is the eye that we sent in remember that we were iterating from 0 to our thread number the reason we're doing this is so that for instance our first thread will look at port number 1 our second thread will look at port number 2 and so on and so forth this will allow our scan function to actually scale based on the amount of threads that we pass into and we have our address here which will be our IP address and finally we have our number 3 the number of threads that we're currently using in the program so we want to take our start port here and add 1 to it that way we're not looking at port 0 then we want to create a loop here then we want to jump up here and we want to bring in another thing from the net name space so that we can actually connect to our IP address the thing we want to bring in here is called TCP stream this will allow us to create a TCP stream as we're looping through this we want to match on TCP stream connect then we're going to pass in our address which is our IP address and then the port number that we're currently scanning we want to match and see what our result is so if we get an okay we don't really care what's inside of it we just care that it's an ok because that signifies to us that the port is open then we just want to print a little period here so this will actually appear in the command line just a little dot it will just send back feedback to the user that it is working by sending back a dot every single time it finds a port that's open and we want to go back up to the top here and bring back more library this will be standard i/o input/output we want to bring in self and write and of course this is important so that we can actually use io inside of our thread here so to do that we want to call io STD out and we want to call flush and then we want to unwrap it this allows us to as it says here constructs a handle to the standard output of the current process and by flushing it allows us to send all these print statements to what is essentially a mutex of shared data then we want to call TX dot send and we want to pass in our port here and this will then send back to our RX that we created down here the port number that was open but then if we get an error back then we just want to return back an empty expression here finally we want to check to see if our max port - our current port is less than the number of threads if we get to a point where we have 0 here and it's less than equal to the current threads then we want to break out of the loop so it doesn't just keep going we want to iterate our port by the number of thread for instance say we have 50 threads the first thread will iterate up to 51 this next thread will iterate up to 52 etc etc etc that way again this function will be able to scale I forgot to mention that we're creating an address variable here that corresponds their arguments IP address and then we're passing it into scan that way we don't have to worry about moved values here alright so outside of our closure here we want to create a mutable out vector and this will be an empty vector we want to drop our TX from this scope that way the TX is only in the other threads it's not in the main thread and we want to get our output from our receiver here so we want to iterate through our receiver and push that into our out vector we want to follow this with a println statement so that we have a line between all of our dots and our output and then we want to sort our out vector then we want to iterate through our out and we want to print out each port and that is open and that's actually our completed application let's take a look at it and see it in action now so if we call cargo run you can see here that we get an error and it says here we get our program name here IP sniffer exe problem parsing arguments not enough arguments if we put - - and then put - H we should get our help menu here so now let's actually run this application on an IP address so you can just run it on your router IP address like I'm about to do so I'm going to pass in J here and then I want to pass in say a thousand threads and then I want to then call 192 168 1 and 1 we're getting these dots so each of these dots signifies that it's found an open port now it kicks back all the ports that are open so I have 2380 234 443 etc etc etc and it goes all the way up here anyway this code will be up on github so that you guys can take a look at it alright guys so that's it for this tutorial I hope you enjoyed it if you did feel free to like and subscribe if you have any questions or comments feel free to leave them in the comment box below and if you disliked it then download it as much as you like have a good night
Original Description
In this video, we build a Port sniffer command line interface using only the standard library of Rust. This program uses many concepts that we looked at in the intro to rust series and shows off a few new ones.
Source Code (v2): https://github.com/tensor-programming/Rust_Port_Sniffer
Source Code (v1): https://github.com/tensor-programming/Rust_Port_Sniffer/tree/v1
Support the Channel and Join Patreon:
Patreon: https://www.patreon.com/tensor_programming
Dontate:
ETH: 0x03247265dd5242605bD2FA3c40fb3b70d9e3D685
Cardano: addr1q9auccwrr9ws8qdyv45f4qwsx76pfmld4zapks89sakq94ay0xmle73y0r8ruwd0zslls4eglf98lghru7ywv56cedysk7ftjt
Check out our Twitter: https://twitter.com/TensorProgram
Check out our Facebook: https://www.facebook.com/Tensor-Programming-1197847143611799/
Check out our Steemit: https://steemit.com/@tensor
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tensor Programming · Tensor Programming · 56 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
▶
57
58
59
60
NodeJs, Text editors and IDEs
Tensor Programming
Vanilla JS todo App
Tensor Programming
Elm Tutorial part 1
Tensor Programming
Elm Lang Tutorial, Part 2
Tensor Programming
Elm Tutorial Part 3
Tensor Programming
Elm Tutorial Part 4 -- Analog Clock App
Tensor Programming
Elm Tutorial part 5 -- Snake Game
Tensor Programming
Elm Tutorial part 6 -- Calculator
Tensor Programming
Go Tutorial part 1 -- Hello World and Static File Server
Tensor Programming
Go Tutorial part 2 -- Web Crawler
Tensor Programming
Go Tutorial Part 3 (Web App part 1)
Tensor Programming
Go tutorial Part 4 (Web tutorial part 2) - Using templates
Tensor Programming
Go tutorial part 5 (web app part 3)
Tensor Programming
Go tutorial part 6 (webapp part 4)
Tensor Programming
Go tutorial part 7 (web app part 5)
Tensor Programming
Go tutorial part 8 (Web app part 6)
Tensor Programming
Go tutorial Part 9 (web tutorial part 7)
Tensor Programming
Go tutorial Part 10 (web app part 8)
Tensor Programming
Go tutorial Part 11 (Web app Part 9)
Tensor Programming
Go Tutorial Part 12 (Web app Part 10)
Tensor Programming
Go Tutorial Part 13 (Web app Part 11)
Tensor Programming
Looking at Elm 0.18
Tensor Programming
Go tutorial Part 14 (Web tutorial part 12)
Tensor Programming
Go tutorial Part 15 (Web tutorial part 13)
Tensor Programming
Go tutorial part 16 (web app part 14)
Tensor Programming
Elm Tutorial Part 7 (SPA part 1)
Tensor Programming
Elm Tutorial Part 8 (SPA Part 2)
Tensor Programming
Electron Elm Tutorial
Tensor Programming
Go tutorial part 17 (web app part 15)
Tensor Programming
Up and Coming Programming Languages and Technologies for 2017
Tensor Programming
elixir tutorial part 1
Tensor Programming
elixir tutorial part 2
Tensor Programming
Elixir tutorial Part 3 (GenServer and Supervisor)
Tensor Programming
Elixir Tutorial Part 4 (GenStage)
Tensor Programming
Elixir Tutorial Part 5 (Plug and Cowboy)
Tensor Programming
Phoenix Framework Tutorial Part 1 (elixir part 6)
Tensor Programming
Phoenix Framework Tutorial Part 2 (elixir part 7)
Tensor Programming
Phoenix Framework Tutorial Part 3 (elixir part 8)
Tensor Programming
A Intro to Clojure and Clojure Syntax
Tensor Programming
An Update about the channel
Tensor Programming
Intro to Rustlang (Setup and Primitives)
Tensor Programming
Intro to Rustlang (Strings, Tuples, Arrays, Slices and Pretty Printing)
Tensor Programming
Intro to Rustlang (Ownership and Borrowing)
Tensor Programming
Intro to Rustlang (Structs, Methods, Functions, Related Functions and the Display/Debug Traits)
Tensor Programming
Intro to Rustlang (Control Flow, Conditionals and Pattern Matching)
Tensor Programming
Intro to RustLang (Enums and Options)
Tensor Programming
Intro to Rustlang (Vectors, HashMaps, Casting, If-Let, While-Let, and the Result Enum)
Tensor Programming
Rustlang Project: Snake Game
Tensor Programming
Intro to Rustlang (Traits and Generic Types)
Tensor Programming
Intro to Rust-lang (Closures, the Box Pointer and Iterators)
Tensor Programming
Intro to Rust-lang (Modules and Lifetimes)
Tensor Programming
Intro to Rust-lang (Macros and Metaprogramming)
Tensor Programming
Intro to Rust-lang (Error Handling)
Tensor Programming
Intro to Rust-lang (Concurrency, Threads, Channels, Mutex and Arc)
Tensor Programming
Intro to Rust-lang (Tests, Attributes, Configuration and Conditional compilation)
Tensor Programming
Rustlang Project: Port Sniffer CLI
Tensor Programming
Rustlang Project: Chat Application
Tensor Programming
Rustlang Project: CLI Toy Blockchain
Tensor Programming
Intro to Rust-lang (Setting up a Development Environment)
Tensor Programming
Intro to Rust-lang (Building a Web API with Iron)
Tensor Programming
More on: AI Tools for PMs
View skill →Related Reads
📰
📰
📰
📰
The Red Flag We Can’t Ignore, Even When the Founder Is Likeable
Medium · Startup
The Unicorn Founder and the Guy Who Never Raised a Dime
Medium · Startup
Why Building Something Meaningful Often Feels Lonely (And Why It’s Not a Bad Sign)
Medium · Startup
How to Design Pricing
Medium · Startup
🎓
Tutor Explanation
DeepCamp AI