From 3d7ce0d49f33dc2713fc82922604e89bc31d63ff Mon Sep 17 00:00:00 2001 From: Mr-Dave Date: Thu, 14 Apr 2022 20:44:24 -0600 Subject: [PATCH] Add webcontrol_base_path Co-authored-by: Gareth Co-authored-by: Mr-Dave --- doc/motionplus_config.html | 19 +++++++++++++++ src/conf.cpp | 29 ++++++++++++++++++++++ src/conf.hpp | 1 + src/webu.cpp | 50 +++++++++++++++++++++++++------------- 4 files changed, 82 insertions(+), 17 deletions(-) diff --git a/doc/motionplus_config.html b/doc/motionplus_config.html index f3496b1e..beff5fbd 100644 --- a/doc/motionplus_config.html +++ b/doc/motionplus_config.html @@ -1245,6 +1245,12 @@ webcontrol_port + +
+ + + webcontrol_base_path +
@@ -1611,6 +1617,7 @@ webcontrol_html + webcontrol_base_path @@ -3632,6 +3639,18 @@ placed in motionplus.conf and not in a camera config file.

+

webcontrol_base_path

+

+
    +
  • Type: String
  • +
  • Valid values: Valid url path
  • +
  • Default:
  • +
+

+ Change the base path for the web UI. This option must be placed in motionplus.conf + and not in a camera config file. +

+

webcontrol_parms

    diff --git a/src/conf.cpp b/src/conf.cpp index bf7cbfab..d46aefc1 100644 --- a/src/conf.cpp +++ b/src/conf.cpp @@ -151,6 +151,7 @@ struct ctx_parm config_parms[] = { {"video_pipe_motion", PARM_TYP_STRING, PARM_CAT_12, WEBUI_LEVEL_LIMITED }, {"webcontrol_port", PARM_TYP_INT, PARM_CAT_13, WEBUI_LEVEL_ADVANCED }, + {"webcontrol_base_path", PARM_TYP_STRING, PARM_CAT_13, WEBUI_LEVEL_ADVANCED }, {"webcontrol_ipv6", PARM_TYP_BOOL, PARM_CAT_13, WEBUI_LEVEL_ADVANCED }, {"webcontrol_localhost", PARM_TYP_BOOL, PARM_CAT_13, WEBUI_LEVEL_ADVANCED }, {"webcontrol_parms", PARM_TYP_LIST, PARM_CAT_13, WEBUI_LEVEL_NEVER}, @@ -2112,6 +2113,33 @@ static void conf_edit_webcontrol_port(struct ctx_cam *cam, std::string &parm, en MOTION_LOG(DBG, TYPE_ALL, NO_ERRNO,"%s:%s","webcontrol_port",_("webcontrol_port")); } +static void conf_edit_webcontrol_base_path(struct ctx_cam *cam, std::string &parm, enum PARM_ACT pact) +{ + if (pact == PARM_ACT_DFLT) { + cam->conf->webcontrol_base_path = ""; + } else if (pact == PARM_ACT_SET) { + if (parm == "/") { + MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO + , _("Invalid webcontrol_base_path: Use blank instead of single / ")); + cam->conf->webcontrol_base_path = ""; + } else if (parm.length() >= 1) { + if (parm.substr(0, 1) != "/") { + MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO + , _("Invalid webcontrol_base_path: Must start with a / ")); + cam->conf->webcontrol_base_path = "/" + parm; + } else { + cam->conf->webcontrol_base_path = parm; + } + } else { + cam->conf->webcontrol_base_path = parm; + } + } else if (pact == PARM_ACT_GET) { + parm = cam->conf->webcontrol_base_path; + } + return; + MOTION_LOG(DBG, TYPE_ALL, NO_ERRNO,"%s:%s","webcontrol_base_path",_("webcontrol_base_path")); +} + static void conf_edit_webcontrol_ipv6(struct ctx_cam *cam, std::string &parm, enum PARM_ACT pact) { if (pact == PARM_ACT_DFLT) { @@ -2991,6 +3019,7 @@ static void conf_edit_cat13(struct ctx_cam *cam, std::string parm_nm , std::string &parm_val, enum PARM_ACT pact) { if (parm_nm == "webcontrol_port") { conf_edit_webcontrol_port(cam, parm_val, pact); + } else if (parm_nm == "webcontrol_base_path") { conf_edit_webcontrol_base_path(cam, parm_val, pact); } else if (parm_nm == "webcontrol_ipv6") { conf_edit_webcontrol_ipv6(cam, parm_val, pact); } else if (parm_nm == "webcontrol_localhost") { conf_edit_webcontrol_localhost(cam, parm_val, pact); } else if (parm_nm == "webcontrol_parms") { conf_edit_webcontrol_parms(cam, parm_val, pact); diff --git a/src/conf.hpp b/src/conf.hpp index bdd643e4..04e12d2b 100644 --- a/src/conf.hpp +++ b/src/conf.hpp @@ -141,6 +141,7 @@ /* Webcontrol configuration parameters */ int webcontrol_port; + std::string webcontrol_base_path; bool webcontrol_ipv6; bool webcontrol_localhost; int webcontrol_parms; diff --git a/src/webu.cpp b/src/webu.cpp index 8280f0b3..cf2a4100 100644 --- a/src/webu.cpp +++ b/src/webu.cpp @@ -204,7 +204,7 @@ static void webu_parms_edit(struct ctx_webui *webui) static int webu_parseurl(struct ctx_webui *webui) { char *tmpurl; - size_t pos_slash1, pos_slash2; + size_t pos_slash1, pos_slash2, baselen; /* Example: /camid/cmd1/cmd2/cmd3 */ webui->uri_camid = ""; @@ -212,14 +212,6 @@ static int webu_parseurl(struct ctx_webui *webui) webui->uri_cmd2 = ""; webui->uri_cmd3 = ""; - if (webui->url.length() == 0) { - return -1; - } - - if (webui->url == "/favicon.ico") { - return -1; - } - MOTION_LOG(DBG, TYPE_STREAM, NO_ERRNO, _("Sent url: %s"),webui->url.c_str()); tmpurl = (char*)mymalloc(webui->url.length()+1); @@ -232,20 +224,39 @@ static int webu_parseurl(struct ctx_webui *webui) MOTION_LOG(DBG, TYPE_STREAM, NO_ERRNO, _("Decoded url: %s"),webui->url.c_str()); - if (webui->url.length() == 1) { + baselen = webui->motapp->cam_list[0]->conf->webcontrol_base_path.length(); + + if (webui->url.length() < baselen) { + return -1; + } + + if (webui->url.substr(baselen) == "/favicon.ico") { + return -1; + } + + if (webui->url.substr(0, baselen) != + webui->motapp->cam_list[0]->conf->webcontrol_base_path) { + return -1; + } + + if (webui->url == "/") { return 0; } - /* Remove any trailing slash */ + /* Remove any trailing slash to keep parms clean */ if (webui->url.substr(webui->url.length()-1,1) == "/") { webui->url = webui->url.substr(0, webui->url.length()-1); } - pos_slash1 = webui->url.find("/", 1); + if (webui->url.length() == baselen) { + return 0; + } + + pos_slash1 = webui->url.find("/", baselen+1); if (pos_slash1 != std::string::npos) { - webui->uri_camid = webui->url.substr(1, pos_slash1 - 1); + webui->uri_camid = webui->url.substr(baselen+1, pos_slash1-baselen- 1); } else { - webui->uri_camid = webui->url.substr(1); + webui->uri_camid = webui->url.substr(baselen+1); return 0; } @@ -276,7 +287,7 @@ static int webu_parseurl(struct ctx_webui *webui) if (pos_slash2 != std::string::npos) { webui->uri_cmd2 = webui->url.substr(pos_slash1, pos_slash2 - pos_slash1); } else { - webui->uri_cmd1 = webui->url.substr(pos_slash1); + webui->uri_cmd2 = webui->url.substr(pos_slash1); return 0; } @@ -336,9 +347,12 @@ static void webu_hostname(struct ctx_webui *webui) hdr = MHD_lookup_connection_value(webui->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_HOST); if (hdr == NULL) { - webui->hostfull = "//localhost:" + std::to_string(webui->motapp->cam_list[0]->conf->webcontrol_port); + webui->hostfull = "//localhost:" + + std::to_string(webui->motapp->cam_list[0]->conf->webcontrol_port) + + webui->motapp->cam_list[0]->conf->webcontrol_base_path; } else { - webui->hostfull = "//" + std::string(hdr); + webui->hostfull = "//" + std::string(hdr) + + webui->motapp->cam_list[0]->conf->webcontrol_base_path; } MOTION_LOG(DBG,TYPE_ALL, NO_ERRNO, _("Full Host: %s"), webui->hostfull.c_str()); @@ -994,6 +1008,8 @@ static void *webu_mhd_init(void *cls, const char *uri, struct MHD_Connection *co if (retcd != 0) { webui->uri_camid = ""; webui->uri_cmd1 = ""; + webui->uri_cmd2 = ""; + webui->uri_cmd3 = ""; webui->url = ""; }