From 42430a231f19dfb11015d1d6b59700e2f1166f1c Mon Sep 17 00:00:00 2001 From: RyosukeDTomita Date: Fri, 30 Aug 2024 16:04:35 +0900 Subject: [PATCH] refactor Scan.py --- src/my_portscanner/scan_tools/ConnectScan.py | 2 -- src/my_portscanner/scan_tools/Scan.py | 29 +++++++++++--------- src/my_portscanner/scan_tools/SynScan.py | 2 -- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/my_portscanner/scan_tools/ConnectScan.py b/src/my_portscanner/scan_tools/ConnectScan.py index 7930f26..42bfc6d 100644 --- a/src/my_portscanner/scan_tools/ConnectScan.py +++ b/src/my_portscanner/scan_tools/ConnectScan.py @@ -1,11 +1,9 @@ # coding: utf-8 import socket import asyncio -from dataclasses import dataclass from .Scan import Scan -@dataclass class ConnectScan(Scan): def run(self) -> list[dict]: """_summary_ diff --git a/src/my_portscanner/scan_tools/Scan.py b/src/my_portscanner/scan_tools/Scan.py index 98ab878..c324b4a 100644 --- a/src/my_portscanner/scan_tools/Scan.py +++ b/src/my_portscanner/scan_tools/Scan.py @@ -1,27 +1,30 @@ # coding: utf-8 -from dataclasses import dataclass import asyncio -from abc import abstractmethod +from abc import ABC, abstractmethod -@dataclass -class Scan: +class Scan(ABC): + # 初期化 target_ip: str target_port_list: list[int] max_rtt_timeout: int max_parallelism: int + scan_result: list[dict] - def __init__(self, *args, **kwargs): + def __init__(self, target_ip: str, target_port_list: list[int], max_rtt_timeout: int, max_parallelism: int): """_summary_ - - 継承専用のクラスのため,直接インスタンス化できないようにする。 - Raises: - TypeError: _description_ + constructor + Args: + target_ip str: target ip address + port_list list[int]: list of target port numbers + max_rtt_timeout int: max rtt timeout + max_parallelism int: max parallelism """ - if type(self) is Scan: - raise TypeError( - "Scan is an abstract class and cannot be instantiated directly" - ) + self.target_ip = target_ip + self.target_port_list = target_port_list + self.max_rtt_timeout = max_rtt_timeout + self.max_parallelism = max_parallelism + self.scan_result = [] def __str__(self) -> str: """_summary_ diff --git a/src/my_portscanner/scan_tools/SynScan.py b/src/my_portscanner/scan_tools/SynScan.py index 6112ab0..93d8153 100644 --- a/src/my_portscanner/scan_tools/SynScan.py +++ b/src/my_portscanner/scan_tools/SynScan.py @@ -1,12 +1,10 @@ # coding: utf-8 from scapy.all import IP, TCP, sr1, conf -from dataclasses import dataclass import asyncio import sys from .Scan import Scan -@dataclass class SynScan(Scan): def run(self) -> list[dict]: """