diff --git a/action_map.yml b/action_map.yml index d3eeda41..0340df1c 100644 --- a/action_map.yml +++ b/action_map.yml @@ -345,6 +345,15 @@ app: full: --value help: Value to set + ### app_checkport() + checkport: + action_help: Check availability of a local port + api: GET /app/checkport + arguments: + port: + help: Port to check + pattern: '^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$' + ### app_checkurl() checkurl: action_help: Check availability of a web path @@ -507,6 +516,7 @@ firewall: arguments: port: help: Port to open + pattern: '^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$' protocol: help: Protocol associated with port choices: diff --git a/yunohost_app.py b/yunohost_app.py index 164d4bad..a78bb9c4 100644 --- a/yunohost_app.py +++ b/yunohost_app.py @@ -31,6 +31,7 @@ import stat import yaml import time import re +import socket from yunohost import YunoHostError, YunoHostLDAP, win_msg, random_password, is_true, validate from yunohost_domain import domain_list, domain_add from yunohost_user import user_info @@ -500,6 +501,22 @@ def app_setting(app, key, value=None): yaml.safe_dump(app_settings, f, default_flow_style=False) +def app_checkport(port): + """ + + """ + try: + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.settimeout(1) + s.connect(("localhost", int(port))) + s.close() + except socket.error: + win_msg(_("Port available: ")+ str(port)) + else: + raise YunoHostError(22, _("Port not available")) + + + def app_checkurl(url, app=None): """