From 60a7974b6d7051ee2b4930a22e93e423f48ef4ab Mon Sep 17 00:00:00 2001 From: tttttangTH Date: Tue, 13 Oct 2020 09:02:06 -0400 Subject: [PATCH] replace otbr-web --- .github/workflows/build.yml | 2 +- .travis/check-docker | 9 +- CMakeLists.txt | 11 - etc/docker/docker_entrypoint.sh | 3 +- etc/rest/nginx.conf | 135 +++ script/_nginx_server_install | 50 + script/_otbr | 12 +- script/bootstrap | 6 +- script/console | 5 +- script/server | 2 - script/setup | 2 + script/test | 7 +- src/CMakeLists.txt | 5 - src/agent/main.cpp | 2 +- src/rest/CMakeLists.txt | 1 + src/rest/connection.cpp | 27 +- src/rest/connection.hpp | 4 +- .../web-service => rest}/frontend/index.html | 20 +- .../frontend/join.dialog.html | 0 .../frontend/res/css/styles.css | 1 - .../frontend/res/img/android-desktop.png | Bin .../frontend/res/img/borderrouter.png | Bin .../frontend/res/img/favicon.png | Bin .../frontend/res/img/icon-info.png | Bin .../frontend/res/img/ios-desktop.png | Bin .../frontend/res/img/openthread_logo.png | Bin .../frontend/res/js/app.js | 561 +++++++---- src/rest/json.cpp | 294 +++++- src/rest/json.hpp | 93 +- src/rest/parser.cpp | 5 +- src/rest/parser.hpp | 8 +- src/rest/request.cpp | 4 +- src/rest/request.hpp | 6 +- src/rest/resource.cpp | 875 ++++++++++++++++-- src/rest/resource.hpp | 87 +- src/rest/response.cpp | 8 +- src/rest/response.hpp | 5 +- src/rest/rest_web_server.cpp | 7 +- src/rest/rest_web_server.hpp | 9 +- src/rest/types.hpp | 83 +- src/web/CMakeLists.txt | 83 -- src/web/main.cpp | 147 --- src/web/otbr-web.init.in | 102 -- src/web/otbr-web.service.in | 15 - src/web/web-service/ot_client.cpp | 258 ------ src/web/web-service/ot_client.hpp | 129 --- src/web/web-service/web_server.cpp | 374 -------- src/web/web-service/web_server.hpp | 135 --- src/web/web-service/wpan_service.cpp | 449 --------- src/web/web-service/wpan_service.hpp | 214 ----- tests/rest/test-rest-server | 37 + tests/rest/test_rest.py | 258 ++++-- tests/scripts/check-scan-build | 1 - tests/scripts/check-scripts | 1 - tests/scripts/meshcop | 35 +- third_party/CMakeLists.txt | 2 +- third_party/angular-material/CMakeLists.txt | 4 +- third_party/angular/CMakeLists.txt | 2 +- third_party/cJSON/CMakeLists.txt | 2 - third_party/d3js/CMakeLists.txt | 2 +- third_party/mdl/CMakeLists.txt | 4 +- 61 files changed, 2213 insertions(+), 2390 deletions(-) create mode 100644 etc/rest/nginx.conf create mode 100644 script/_nginx_server_install rename src/{web/web-service => rest}/frontend/index.html (96%) rename src/{web/web-service => rest}/frontend/join.dialog.html (100%) rename src/{web/web-service => rest}/frontend/res/css/styles.css (99%) rename src/{web/web-service => rest}/frontend/res/img/android-desktop.png (100%) rename src/{web/web-service => rest}/frontend/res/img/borderrouter.png (100%) rename src/{web/web-service => rest}/frontend/res/img/favicon.png (100%) rename src/{web/web-service => rest}/frontend/res/img/icon-info.png (100%) rename src/{web/web-service => rest}/frontend/res/img/ios-desktop.png (100%) rename src/{web/web-service => rest}/frontend/res/img/openthread_logo.png (100%) rename src/{web/web-service => rest}/frontend/res/js/app.js (61%) delete mode 100644 src/web/CMakeLists.txt delete mode 100644 src/web/main.cpp delete mode 100755 src/web/otbr-web.init.in delete mode 100644 src/web/otbr-web.service.in delete mode 100644 src/web/web-service/ot_client.cpp delete mode 100644 src/web/web-service/ot_client.hpp delete mode 100644 src/web/web-service/web_server.cpp delete mode 100644 src/web/web-service/web_server.hpp delete mode 100644 src/web/web-service/wpan_service.cpp delete mode 100644 src/web/web-service/wpan_service.hpp diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a8c90bd74dd..2a8591d7f1e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -90,7 +90,7 @@ jobs: runs-on: ubuntu-18.04 strategy: matrix: - rest: ["rest", ""] + rest: ["rest off", ""] env: BUILD_TARGET: check OTBR_REST: ${{ matrix.rest }} diff --git a/.travis/check-docker b/.travis/check-docker index b2140b8faed..7cd830f82d8 100755 --- a/.travis/check-docker +++ b/.travis/check-docker @@ -72,11 +72,11 @@ main() readonly OTBR_DOCKER_PID=$(docker run --rm -dit \ --sysctl "net.ipv6.conf.all.disable_ipv6=0 net.ipv4.conf.all.forwarding=1 net.ipv6.conf.all.forwarding=1" \ - --privileged -p 8080:80 --dns=127.0.0.1 --volume "$DOCKER_PTY":/dev/ttyUSB0 otbr) + --privileged -p 8081:8081 --dns=127.0.0.1 --volume "$DOCKER_PTY":/dev/ttyUSB0 otbr) sleep 10 - sudo lsof -i :8080 + sudo lsof -i :8081 - local -r OTBR_WEB_URL="http://127.0.0.1:8080" + local -r OTBR_REST_URL="http://127.0.0.1:8081" local -r OT_MASTER_KEY=00112233445566778899aabbccddeeff local -r OT_XPANID=0011223344556677 local -r OT_PANID=0xface @@ -84,8 +84,7 @@ main() local -r OT_CHANNEL=12 local -r OT_NETWORK_NAME=OpenThreadDocker - curl "${OTBR_WEB_URL}"/index.html | grep 'What is OpenThread' - curl --header "Content-Type: application/json" --request POST --data "{\"masterKey\":\"${OT_MASTER_KEY}\",\"prefix\":\"fd11:22::\",\"defaultRoute\":true,\"extPanId\":\"${OT_XPANID}\",\"panId\":\"${OT_PANID}\",\"passphrase\":\"${OT_AGENT_PASSPHRASE}\",\"channel\":${OT_CHANNEL},\"networkName\":\"${OT_NETWORK_NAME}\"}" "${OTBR_WEB_URL}"/form_network | grep "success" + curl --header "Content-Type: application/json" --request POST --data "{\"masterKey\":\"${OT_MASTER_KEY}\",\"prefix\":\"fd11:22::\",\"defaultRoute\":true,\"extPanId\":\"${OT_XPANID}\",\"panId\":\"${OT_PANID}\",\"passphrase\":\"${OT_AGENT_PASSPHRASE}\",\"channel\":${OT_CHANNEL},\"networkName\":\"${OT_NETWORK_NAME}\"}" "${OTBR_REST_URL}"/v1/networks | grep "${OT_PANID}" } main "$@" diff --git a/CMakeLists.txt b/CMakeLists.txt index 48ffe9ec2c9..3e327542ada 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,7 +36,6 @@ option(OTBR_BACKBONE_ROUTER "Build Backbone Router" OFF) option(OTBR_DBUS "Build DBus support" OFF) option(OTBR_OPENWRT "Build OpenWrt support" OFF) option(OTBR_UNSECURE_JOIN "Enable unsecure joining" OFF) -option(OTBR_WEB "Build Web GUI" OFF) option(OTBR_REST "Build Rest Server" OFF) @@ -115,16 +114,6 @@ if(OTBR_REST) ) endif() -if(OTBR_WEB) - pkg_check_modules(JSONCPP jsoncpp REQUIRED) - set(Boost_USE_STATIC_LIBS ON) - set(Boost_USE_MULTITHREADED ON) - set(Boost_USE_STATIC_RUNTIME OFF) - find_package(Boost REQUIRED - COMPONENTS filesystem system) - set(OTBR_WEB_DATADIR ${CMAKE_INSTALL_FULL_DATADIR}/otbr-web) -endif() - if(OTBR_OPENWRT) target_compile_definitions(otbr-config INTERFACE OTBR_ENABLE_OPENWRT=1 diff --git a/etc/docker/docker_entrypoint.sh b/etc/docker/docker_entrypoint.sh index 1ac169db256..396161ff6ba 100755 --- a/etc/docker/docker_entrypoint.sh +++ b/etc/docker/docker_entrypoint.sh @@ -88,8 +88,7 @@ NAT64_PREFIX=${NAT64_PREFIX/\//\\\/} sed -i "s/^prefix.*$/prefix $NAT64_PREFIX/" /etc/tayga.conf sed -i "s/dns64.*$/dns64 $NAT64_PREFIX {};/" /etc/bind/named.conf.options -echo "OTBR_AGENT_OPTS=\"-I $TUN_INTERFACE_NAME $BACKBONE_INTERFACE_ARG -d7 $RADIO_URL\"" >/etc/default/otbr-agent -echo "OTBR_WEB_OPTS=\"-I $TUN_INTERFACE_NAME -d7 -p 80\"" >/etc/default/otbr-web +echo "OTBR_AGENT_OPTS=\"-I $TUN_INTERFACE_NAME -d7 $RADIO_URL\"" >/etc/default/otbr-agent /app/script/server diff --git a/etc/rest/nginx.conf b/etc/rest/nginx.conf new file mode 100644 index 00000000000..438361ac717 --- /dev/null +++ b/etc/rest/nginx.conf @@ -0,0 +1,135 @@ +# +# Copyright (c) 2020, The OpenThread Authors. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# 3. Neither the name of the copyright holder nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# + +user www-data; +worker_processes auto; +pid /run/nginx.pid; +include /etc/nginx/modules-enabled/*.conf; + +events { + worker_connections 768; + # multi_accept on; +} + +http { + + ## + # Basic Settings + ## + server { + listen 80; + server_name 0.0.0.0; + + #access_log /var/log/nginx/localhost.access.log; + + location / { + root to be set; + index index.html ; + } + location /favicon.ico + { + root to be set/res/img; + } + location /v1/ { + proxy_pass http://0.0.0.0:8081; + proxy_set_header Host $proxy_host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } + + + } + + sendfile on; + tcp_nopush on; + tcp_nodelay on; + keepalive_timeout 65; + types_hash_max_size 2048; + # server_tokens off; + # server_names_hash_bucket_size 64; + # server_name_in_redirect off; + + include /etc/nginx/mime.types; + default_type application/octet-stream; + + ## + # SSL Settings + ## + + ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE + ssl_prefer_server_ciphers on; + + ## + # Logging Settings + ## + + access_log /var/log/nginx/access.log; + error_log /var/log/nginx/error.log; + + ## + ## + # Gzip Settings + ## + + gzip on; + + # gzip_vary on; + # gzip_proxied any; + # gzip_comp_level 6; + # gzip_buffers 16 8k; + # gzip_http_version 1.1; + # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; + + ## + # Virtual Host Configs + ## + + include /etc/nginx/conf.d/*.conf; + include /etc/nginx/sites-enabled/*; +} + + +#mail { +# # See sample authentication script at: +# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript +# +# # auth_http localhost/auth.php; +# # pop3_capabilities "TOP" "USER"; +# # imap_capabilities "IMAP4rev1" "UIDPLUS"; # +# server { +# listen localhost:110; +# protocol pop3; +# proxy on; +# } +# +# server { +# listen localhost:143; +# protocol imap; +# proxy on; +# } +#} diff --git a/script/_nginx_server_install b/script/_nginx_server_install new file mode 100644 index 00000000000..eaf850f4721 --- /dev/null +++ b/script/_nginx_server_install @@ -0,0 +1,50 @@ +#!/bin/bash +# +# Copyright (c) 2020, The OpenThread Authors. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# 3. Neither the name of the copyright holder nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# + +nginx_server_install() +{ + if command -v nginx; then + + top_dir="$(pwd)" + + nginx_conf_file=$(nginx -V 2>&1 | grep -o "\-\-conf-path=\(.*conf\)" | cut -d "=" -f2) + + sudo rm "${nginx_conf_file}" + + sudo cp "${top_dir}"/etc/rest/nginx.conf "${nginx_conf_file}" + + sudo sed -i.bak "s:to be set:${top_dir}/src/rest/frontend:" "${nginx_conf_file}" + + else + + echo >&2 ' *** WARNING: nginx not found, please install nginx manually and run setup again' + + fi + +} diff --git a/script/_otbr b/script/_otbr index 4392276e2c8..cd9c58f93bf 100644 --- a/script/_otbr +++ b/script/_otbr @@ -35,12 +35,12 @@ readonly REFERENCE_DEVICE=${REFERENCE_DEVICE:-0} otbr_uninstall() { if have systemctl; then - sudo systemctl stop otbr-web otbr-agent || true - sudo systemctl disable otbr-web otbr-agent || true - ! sudo systemctl is-enabled otbr-web + sudo systemctl stop otbr-agent || true + sudo systemctl disable otbr-agent || true + ! sudo systemctl is-enabled ! sudo systemctl is-enabled otbr-agent fi - sudo killall otbr-web otbr-agent || true + sudo killall otbr-agent || true ( if cd "${OTBR_TOP_BUILDDIR}"; then @@ -72,7 +72,6 @@ otbr_install() "-DBUILD_TESTING=OFF" "-DCMAKE_INSTALL_PREFIX=/usr" "-DOTBR_DBUS=ON" - "-DOTBR_WEB=ON" "-DOTBR_REST=ON" "${otbr_options[@]}" ) @@ -92,9 +91,8 @@ otbr_install() if have systemctl; then sudo systemctl reload dbus sudo systemctl daemon-reload - sudo systemctl enable otbr-web otbr-agent || true + sudo systemctl enable otbr-agent || true sudo systemctl is-enabled otbr-agent || die 'Failed to enable otbr-agent!' - sudo systemctl is-enabled otbr-web || die 'Failed to enable otbr-web!' else echo >&2 ' *** WARNING: systemctl not found. otbr cannot start on boot.' fi diff --git a/script/bootstrap b/script/bootstrap index 1a6e917590f..7a75af84c6f 100755 --- a/script/bootstrap +++ b/script/bootstrap @@ -86,6 +86,9 @@ install_packages_apt() # libjsoncpp sudo apt-get install --no-install-recommends -y libjsoncpp-dev + + # nginx + sudo apt-get install --no-install-recommends -y nginx } install_packages_opkg() @@ -109,11 +112,12 @@ install_packages_rpm() sudo $PM install -y tayga iptables sudo $PM install -y jsoncpp-devel sudo $PM install -y wget + sudo $PM install -y nginx } install_packages_brew() { - brew install boost cmake cpputest dbus jsoncpp ninja + brew install cmake cpputest dbus ninja nginx } install_packages_source() diff --git a/script/console b/script/console index 232e51aecd0..24b79731993 100755 --- a/script/console +++ b/script/console @@ -41,7 +41,7 @@ readonly RADIO_URL="${RADIO_URL:-spinel+hdlc+uart:///dev/ttyUSB0}" killall_services() { echo 'Closing services...' - sudo killall otbr-agent otbr-web || true + sudo killall otbr-agent || true } on_exit() @@ -54,7 +54,7 @@ main() { . "$BEFORE_HOOK" if have systemctl; then - sudo systemctl stop otbr-web otbr-agent || true + sudo systemctl stop otbr-agent || true fi killall_services @@ -62,7 +62,6 @@ main() ipforward_enable sudo sh -s < 0 || (received == -1 && (err == EAGAIN || err == EWOULDBLOCK)), error = OTBR_ERROR_REST); exit: + if (error != OTBR_ERROR_NONE) { if (received < 0) { - mResource->ErrorHandler(mResponse, HttpStatusCode::kStatusInternalServerError); + mResource->ErrorHandler(mResponse, HttpStatusCode::kStatusInternalServerError, "System call read error "); Write(); } else { - mResource->ErrorHandler(mResponse, HttpStatusCode::kStatusRequestTimeout); + mResource->ErrorHandler(mResponse, HttpStatusCode::kStatusRequestTimeout, + "Read timeout or sender stop sending"); Write(); } } @@ -250,7 +245,7 @@ void Connection::Handle(void) if (error != OTBR_ERROR_NONE) { - mResource->ErrorHandler(mResponse, HttpStatusCode::kStatusInternalServerError); + mResource->ErrorHandler(mResponse, HttpStatusCode::kStatusInternalServerError, "system call shutdown error"); Write(); } } @@ -269,7 +264,7 @@ void Connection::ProcessWaitCallback(void) { if (duration >= kCallbackTimeout) { - mResource->ErrorHandler(mResponse, HttpStatusCode::kStatusInternalServerError); + mResource->ErrorHandler(mResponse, HttpStatusCode::kStatusInternalServerError, "Callback handler timeout"); Write(); } } @@ -307,7 +302,6 @@ void Connection::Write(void) mWriteContent = mResponse.Serialize(); } - // Check we do have something to write. VerifyOrExit(mWriteContent.size() > 0, error = OTBR_ERROR_REST); sendLength = write(mFd, mWriteContent.c_str(), mWriteContent.size()); @@ -328,7 +322,6 @@ void Connection::Write(void) { if (errno == EINTR) { - // Try again Write(); } else @@ -350,5 +343,5 @@ bool Connection::IsComplete() const return mState == ConnectionState::kComplete; } -} // namespace rest +} // namespace Rest } // namespace otbr diff --git a/src/rest/connection.hpp b/src/rest/connection.hpp index 249f9d52ce3..7d8e2ceaa97 100644 --- a/src/rest/connection.hpp +++ b/src/rest/connection.hpp @@ -43,7 +43,7 @@ using std::chrono::steady_clock; namespace otbr { -namespace rest { +namespace Rest { /** * This class implements a Connection class of each socket connection. @@ -132,7 +132,7 @@ class Connection std::string mWriteContent; }; -} // namespace rest +} // namespace Rest } // namespace otbr #endif // OTBR_REST_CONNECTION_HPP_ diff --git a/src/web/web-service/frontend/index.html b/src/rest/frontend/index.html similarity index 96% rename from src/web/web-service/frontend/index.html rename to src/rest/frontend/index.html index 88291dbbdff..1ca911d2cbf 100644 --- a/src/web/web-service/frontend/index.html +++ b/src/rest/frontend/index.html @@ -49,7 +49,7 @@ - +