Skip to content

Commit

Permalink
🚧 improve output in asynchronous scanning
Browse files Browse the repository at this point in the history
change default value of `with_output` to False
  • Loading branch information
zrquan committed Aug 4, 2024
1 parent 3f0c99c commit cfb5784
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 32 deletions.
5 changes: 3 additions & 2 deletions nmass/masscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __post_init__(self):
def run(
self,
timeout: float | None = None,
with_output: bool = True,
with_output: bool = False,
) -> NmapRun | None:
"""Run masscan command
Expand All @@ -44,7 +44,8 @@ def run(
async def arun(
self,
timeout: float | None = None,
with_output: bool = True,
# FIXME: 异步执行 masscan 时,没有输出进度和倒计时那一行
with_output: bool = False,
) -> NmapRun | None:
try:
return await self._arun_command(timeout, with_output)
Expand Down
4 changes: 2 additions & 2 deletions nmass/nmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __post_init__(self):
def run(
self,
timeout: float | None = None,
with_output: bool = True,
with_output: bool = False,
) -> NmapRun | None:
"""Run nmap command
Expand All @@ -43,7 +43,7 @@ def run(
async def arun(
self,
timeout: float | None = None,
with_output: bool = True,
with_output: bool = False,
) -> NmapRun | None:
try:
return await self._arun_command(timeout, with_output)
Expand Down
49 changes: 21 additions & 28 deletions nmass/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import subprocess
import tempfile
import time
from abc import abstractmethod
from dataclasses import dataclass, field
from typing import Self
Expand All @@ -17,18 +18,10 @@ class Scanner:
_args: list[str] = field(default_factory=lambda: [], init=False)

@abstractmethod
def run(
self,
timeout: float | None = None,
with_output: bool = True,
) -> NmapRun | None:
def run(self, timeout: float | None, with_output: bool) -> NmapRun | None:
raise NotImplementedError()

def _run_command(
self,
timeout: float | None = None,
with_output: bool = True,
) -> NmapRun | None:
def _run_command(self, timeout: float | None, with_output: bool) -> NmapRun | None:
with tempfile.NamedTemporaryFile() as xml_out:
cmd = [self.bin_path, "-oX", xml_out.name, *self._args]
try:
Expand All @@ -51,39 +44,39 @@ def _run_command(
return None

@abstractmethod
async def arun(
self,
timeout: float | None = None,
with_output: bool = True,
) -> NmapRun | None:
async def arun(self, timeout: float | None, with_output: bool) -> NmapRun | None:
raise NotImplementedError()

async def _arun_command(
self,
timeout: float | None = None,
with_output: bool = True,
) -> NmapRun | None:
async def _arun_command(self, timeout: float | None, with_output: bool) -> NmapRun | None:
async with atempfile.NamedTemporaryFile() as xml_out:
proc = await asyncio.create_subprocess_exec(
self.bin_path,
*["-oX", xml_out.name, *self._args],
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT,
)

if with_output:
start_time = time.time()
killed = False
async for line in proc.stdout:
print(line.decode().rstrip())
if killed:
continue
if timeout and time.time() - start_time > timeout:
proc.kill()
killed = True
if killed:
raise asyncio.TimeoutError()

try:
stdout, stderr = await asyncio.wait_for(
proc.communicate(), timeout=timeout
)
# TODO: 运行中实时打印输出
if with_output:
print(stdout.decode())
await asyncio.wait_for(proc.wait(), timeout=timeout)
except asyncio.TimeoutError:
raise
except Exception as why:
logging.exception(why)
else:
if proc.returncode != 0:
logging.error(stderr.decode())
raise subprocess.CalledProcessError(returncode=proc.returncode)
else:
return NmapRun.from_xml(await xml_out.read())
Expand Down

0 comments on commit cfb5784

Please sign in to comment.