From d02a29f0ff33279268e675aae0856f3f8cf9d939 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Tue, 10 Jan 2023 14:22:47 +0100 Subject: Configurable Websocket für HTTPS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/websocket/Makefile | 55 ++++++++++++++++++++++++++++ plugins/websocket/websocket.cpp | 80 +++++++++++++++++++++++++++++++++++++++++ plugins/websocket/websocket.h | 29 +++++++++++++++ 3 files changed, 164 insertions(+) create mode 100644 plugins/websocket/Makefile create mode 100644 plugins/websocket/websocket.cpp create mode 100644 plugins/websocket/websocket.h (limited to 'plugins/websocket') diff --git a/plugins/websocket/Makefile b/plugins/websocket/Makefile new file mode 100644 index 0000000..4e841a8 --- /dev/null +++ b/plugins/websocket/Makefile @@ -0,0 +1,55 @@ +include ../../common.mk + +PROJECTNAME=websocket + +CXXFLAGS+= -fvisibility=hidden -fPIC + +CXXFLAGS+= -I../.. + +LDLIBS=\ +-lreichwein \ +-lboost_context \ +-lboost_coroutine \ +-lboost_program_options \ +-lboost_system \ +-lboost_thread \ +-lboost_filesystem \ +-lboost_regex \ +-lpthread \ +-lssl -lcrypto \ +-ldl + +PROGSRC=\ + websocket.cpp + +SRC=$(PROGSRC) + +all: $(PROJECTNAME).so + +$(PROJECTNAME).so: $(SRC:.cpp=.o) + $(CXX) $(CXXFLAGS) $^ -shared $(LIBS) -o $@ + +%.d: %.cpp + $(CXX) $(CXXFLAGS) -MM -MP -MF $@ -c $< + +%.o: %.cpp %.d + $(CXX) $(CXXFLAGS) -c $< -o $@ + +# dependencies + +ADD_DEP=Makefile + +install: + mkdir -p $(DESTDIR)/usr/lib/webserver/plugins + cp $(PROJECTNAME).so $(DESTDIR)/usr/lib/webserver/plugins + +# misc --------------------------------------------------- + +debs: $(DISTROS) + +clean: + -rm -f *.o *.so *.d + +.PHONY: clean install all + +-include $(wildcard $(SRC:.cpp=.d)) diff --git a/plugins/websocket/websocket.cpp b/plugins/websocket/websocket.cpp new file mode 100644 index 0000000..884f691 --- /dev/null +++ b/plugins/websocket/websocket.cpp @@ -0,0 +1,80 @@ +#include "websocket.h" + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +using namespace std::string_literals; +namespace bp = boost::process; +namespace fs = std::filesystem; + +namespace { + + // Used to return errors by generating response page and HTTP status code + std::string HttpStatus(std::string status, std::string message, std::function& SetResponseHeader) + { + SetResponseHeader("status", status); + SetResponseHeader("content_type", "text/html"); + return status + " " + message; + } + +} // anonymous namespace + +std::string websocket_plugin::name() +{ + return "websocket"; +} + +websocket_plugin::websocket_plugin() +{ + //std::cout << "Plugin constructor" << std::endl; +} + +websocket_plugin::~websocket_plugin() +{ + //std::cout << "Plugin destructor" << std::endl; +} + +std::string websocket_plugin::generate_page( + std::function& GetServerParam, + std::function& GetRequestParam, // request including body (POST...) + std::function& SetResponseHeader // to be added to result string +) +{ + try { + // Request path must not contain "..". + std::string rel_target{GetRequestParam("rel_target")}; + size_t query_pos{rel_target.find("?")}; + if (query_pos != rel_target.npos) + rel_target = rel_target.substr(0, query_pos); + + std::string target{GetRequestParam("target")}; + if (rel_target.find("..") != std::string::npos) { + return HttpStatus("400", "Illegal request: "s + target, SetResponseHeader); + } + + try { + return "Dummy"; + } catch (const std::exception& ex) { + return HttpStatus("500", "Internal Server Error: "s + ex.what(), SetResponseHeader); + } + + } catch (const std::exception& ex) { + return HttpStatus("500", "Unknown Error: "s + ex.what(), SetResponseHeader); + } +} + +bool websocket_plugin::has_own_authentication() +{ + return false; +} + diff --git a/plugins/websocket/websocket.h b/plugins/websocket/websocket.h new file mode 100644 index 0000000..27218da --- /dev/null +++ b/plugins/websocket/websocket.h @@ -0,0 +1,29 @@ +#pragma once + +#include "../../plugin_interface.h" + +#include + +#include +#include +#include + +class websocket_plugin: public webserver_plugin_interface +{ + +public: + websocket_plugin(); + ~websocket_plugin(); + + std::string name() override; + std::string generate_page( + std::function& GetServerParam, + std::function& GetRequestParam, // request including body (POST...) + std::function& SetResponseHeader // to be added to result string + ) override; + + bool has_own_authentication() override; +}; + +extern "C" BOOST_SYMBOL_EXPORT websocket_plugin webserver_plugin; +websocket_plugin webserver_plugin; -- cgit v1.2.3