Skip to content

Commit

Permalink
Add iperf3
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ketchel committed Nov 26, 2024
1 parent 4380da1 commit 7c605b6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
19 changes: 17 additions & 2 deletions wlanpi_core/api/api_v1/endpoints/utils_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ async def execute_ping(request: utils.PingRequest):


@router.post("/iperf2/client", response_model=utils.Iperf2Result)
async def execute_iperf(request: utils.IperfRequest):
async def execute_iperf(request: utils.Iperf2ClientRequest):
"""
Runs iperf against a target and returns the results
Runs iperf2 against a target and returns the results
"""

try:
Expand All @@ -160,6 +160,21 @@ async def execute_iperf(request: utils.IperfRequest):
return Response(content=f"Internal Server Error: {ex}", status_code=500)


@router.post("/iperf3/client", response_model=None)
async def execute_iperf3_client(request: utils.Iperf3ClientRequest):
"""
Runs iperf3 against a target and returns the results
"""

try:
return await utils_service.run_iperf3_client(**request.__dict__)
except ValidationError as ve:
return Response(content=ve.error_msg, status_code=ve.status_code)
except Exception as ex:
log.error(ex, exc_info=ex)
return Response(content=f"Internal Server Error: {ex}", status_code=500)


@router.post("/traceroute", response_model=TracerouteResponse)
async def execute_traceroute(request: utils.TracerouteRequest):
"""
Expand Down
3 changes: 2 additions & 1 deletion wlanpi_core/schemas/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
DigRequest,
DigResponse,
Iperf2Result,
IperfRequest,
Iperf2ClientRequest,
Iperf3ClientRequest,
PingRequest,
PingResponse,
PingResult,
Expand Down
14 changes: 13 additions & 1 deletion wlanpi_core/schemas/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,19 @@ class PingResult(
responses: list[PingResponse] = Field()


class IperfRequest(BaseModel):
class Iperf3ClientRequest(BaseModel):
host: str = Field(examples=["192.168.1.1"])
port: int = Field(examples=[5001], default=5001)
time: int = Field(examples=[10], default=10)
udp: bool = Field(default=False)
reverse: bool = Field(default=False)
interface: Optional[str] = Field(examples=["wlan0"], default=None)


# No Iperf3Result yet as it hasn't been fully modeled and I (MDK) don't know what all potential output forms are in JSON mode.


class Iperf2ClientRequest(BaseModel):
host: str = Field(examples=["192.168.1.1"])
port: int = Field(examples=[5001], default=5001)
time: int = Field(examples=[10], default=10)
Expand Down
23 changes: 21 additions & 2 deletions wlanpi_core/services/utils_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,27 @@ async def run_iperf2_client(
}


async def run_iperf3_client(host: str, time: int = 10, bind_host: Optional[str] = None):
command = ["iperf3", "--json", "-t", str(time), "-c", host]
async def run_iperf3_client(
host: str,
port: int = 5001,
time: int = 10,
reverse: bool = False,
bind: Optional[str] = None,
interface: Optional[str] = None,
udp=False,
):
command = ["iperf3", "--json", "-t", str(time), "-c", host, "-p", str(port)]

if reverse:
command.append("-R")

if bind:
command.extend(["-B", bind])
elif interface:
command.extend(["-B", get_ip_address(interface)])
if udp:
command.append("-u")

res = await run_command_async(command)
return res.output_from_json()

Expand Down

0 comments on commit 7c605b6

Please sign in to comment.