From 3d53652b2dce55e2d11d788d4981463f4f6181f8 Mon Sep 17 00:00:00 2001 From: misyaguziya <53165965+misyaguziya@users.noreply.github.com> Date: Sat, 22 Mar 2025 14:37:05 +0900 Subject: [PATCH] [Update] Controller: Validate and handle IP address setting in setOscIpAddress method. [Update] Utils: Implement isValidIpAddress function to check IP address validity. --- src-python/controller.py | 27 +++++++++++++++++++++++---- src-python/utils.py | 10 +++++++++- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src-python/controller.py b/src-python/controller.py index f0356287..98b84844 100644 --- a/src-python/controller.py +++ b/src-python/controller.py @@ -6,7 +6,7 @@ import re from device_manager import device_manager from config import config from model import model -from utils import removeLog, printLog, errorLogging, isConnectedNetwork +from utils import removeLog, printLog, errorLogging, isConnectedNetwork, isValidIpAddress class Controller: def __init__(self) -> None: @@ -1085,9 +1085,28 @@ class Controller: @staticmethod def setOscIpAddress(data, *args, **kwargs) -> dict: - config.OSC_IP_ADDRESS = data - model.setOscIpAddress(config.OSC_IP_ADDRESS) - return {"status":200, "result":config.OSC_IP_ADDRESS} + if isValidIpAddress(data) is False: + return { + "status":400, + "result":{ + "message":"Invalid IP address", + "data": config.OSC_IP_ADDRESS + } + } + else: + try: + model.setOscIpAddress(data) + config.OSC_IP_ADDRESS = data + return {"status":200, "result":config.OSC_IP_ADDRESS} + except Exception: + model.setOscIpAddress(config.OSC_IP_ADDRESS) + return { + "status":400, + "result":{ + "message":"Cannot set IP address", + "data": config.OSC_IP_ADDRESS + } + } @staticmethod def getOscPort(*args, **kwargs) -> dict: diff --git a/src-python/utils.py b/src-python/utils.py index e31a1f46..de538dae 100644 --- a/src-python/utils.py +++ b/src-python/utils.py @@ -7,14 +7,22 @@ from logging.handlers import RotatingFileHandler from ctranslate2 import get_supported_compute_types import requests +import ipaddress -def isConnectedNetwork(url="http://www.google.com", timeout=3): +def isConnectedNetwork(url="http://www.google.com", timeout=3) -> bool: try: response = requests.get(url, timeout=timeout) return response.status_code == 200 except requests.RequestException: return False +def isValidIpAddress(ip_address: str) -> bool: + try: + ipaddress.ip_address(ip_address) + return True + except ValueError: + return False + def getBestComputeType(device, device_index) -> str: compute_types = get_supported_compute_types(device, device_index) compute_types = set(compute_types)