Giter Site home page Giter Site logo

port-scanner's People

Contributors

ssilatel avatar eonraider avatar

Watchers

 avatar

port-scanner's Issues

Assignment

1. Move the main context of the main.py file into the App class

You can now move the cli_args and port_scanner objects into App as instance variables so there's nothing left under main other than a single call to App().scan()

2. Implement observers for screen and file output on the PortScanner class

Check the observer pattern: https://refactoring.guru/design-patterns/observer

This will be pretty much the end of this project, and its most complex step since it's the first time you use this pattern. Implement a CLI option that takes a path to a file in which scan results will be saved in parallel to the screen output. You need two observers to be instantiated, one for each method.

Assignment

1. Read about the Command design pattern

https://refactoring.guru/design-patterns/command

Everything from line 128 until the end can be moved into a command class at this point, so the PortScanner does nothing but scan and present results. Can you figure out a way?

2. Move classes into separate modules

Have you noticed how long the file is getting and the number of classes in it? We should try to separate classes with different responsibilities into separate files and directories and then import them as they are needed. Give it a go.

Assignment

1. Add a "--timeout" option to the CLI parser

Now that you've managed to set a timeout to the socket, the only thing left to do is get rid of the hard coded value you left there (which is 3) and replace it with a user-supplied value. Set a default time like 3.0 on the CLI parser itself so the user doesn't need to pass something to the flag every time.

2. Cleanup PortScanner.scan_ports

Now that you have the port_states dictionary you don't need the open_ports lost anymore. It's become redundant. If anyone needs a list containing the ports that are open they only need to write a comprehension expression such as [port for port,state in self.port_states.items() if state == "Open"]. So this way you can safely delete that attribute. It all comes to responsibilities again... The scanner scans and reports results - the interpretation of results belongs to the caller.

Along this same line of thought you could also eliminate those two for loops in the end of the scan_ports method entirely. They are outputting results, which is again a violation of responsibility. The scanner should not have to deal with that. You should then delete those loops and yield an object representing a scan result. What should you use? A good approach would be a dataclass, but in case you're not familiar with yet you can just stick to a tuple like (port, port_state) for now. By yielding the result instead of returning it you'll be able to print results as they become available, instead of having to wait until the end of the scan operation to give something to the user.

Once you have this figured out you'll need to implement a loop under main that will use the results yielded by scan_ports and then print them.

3. Read about dataclass and Enum

An elegant solution can be achieved by combining a dataclass and Enum so we can return specific results instead of relying on a tuple or dictionary for this. The importance of this solution will become clear once you consider that this scanner we're making right now is just ONE type of scan operation... We can still go forward and implement SYN, NULL, ACK and XMAS scans just like nmap does. Relying on a dictionary for each of those will be a nightmare.

Go ahead and read about these two concepts so I can show you a solution I came up with that uses them.

https://realpython.com/python-enum/
https://realpython.com/python-data-classes/

Assignment

1. Add a timeout to the PortScanner

The code is working as intended right now, but as soon as you try to scan a port that doesn't respond the process simply hangs there and does nothing. We need to add a timeout to the socket connection so it continues the scanning even after a port gives no response. The official documentation will lead you to the solution: https://docs.python.org/3/library/socket.html

2. Offer the opportunity to check on all results, not only open ports

What if one needs a list with all results, regardless of port state? As it is now all you offer is a list of open ports. Figure out a way to return a dictionary with a structure similar to this:
{
21: "Closed | Timeout",
80: "Open",
443: "Closed | ConnectionRefused"
}
Compĺeting this assignment will take us closer to a goal I had in mind since the beginning: Extracting from the PortScanner class all responsibility to show output. You'll see what I mean very soon.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.