23 #include "webview_thread.h"
24 #include "static_processor.h"
25 #include "blackboard_processor.h"
26 #include "startpage_processor.h"
27 #include "plugins_processor.h"
28 #include "service_browse_handler.h"
29 #include "header_generator.h"
30 #include "footer_generator.h"
31 #include "user_verifier.h"
33 #include <core/version.h>
34 #include <core/exceptions/system.h>
35 #include <utils/system/file.h>
36 #include <utils/system/hostinfo.h>
37 #include <webview/request_dispatcher.h>
38 #include <webview/page_reply.h>
39 #include <webview/server.h>
40 #include <webview/url_manager.h>
41 #include <webview/nav_manager.h>
45 using namespace fawkes;
72 WebviewThread::~WebviewThread()
81 bool __cfg_use_ssl =
false;
87 __cfg_ssl_create =
false;
95 if (__cfg_ssl_key[0] !=
'/')
96 __cfg_ssl_key = std::string(CONFDIR
"/") + __cfg_ssl_key;
98 if (__cfg_ssl_cert[0] !=
'/')
99 __cfg_ssl_cert = std::string(CONFDIR
"/") + __cfg_ssl_cert;
102 __cfg_ssl_cert.c_str());
104 if (! File::exists(__cfg_ssl_key.c_str())) {
105 if (File::exists(__cfg_ssl_cert.c_str())) {
106 throw Exception(
"Key file %s does not exist, but certificate file %s "
107 "does", __cfg_ssl_key.c_str(), __cfg_ssl_cert.c_str());
108 }
else if (__cfg_ssl_create) {
109 ssl_create(__cfg_ssl_key.c_str(), __cfg_ssl_cert.c_str());
111 throw Exception(
"Key file %s does not exist", __cfg_ssl_key.c_str());
113 }
else if (! File::exists(__cfg_ssl_cert.c_str())) {
114 throw Exception(
"Certificate file %s does not exist, but key file %s "
115 "does", __cfg_ssl_key.c_str(), __cfg_ssl_cert.c_str());
119 bool __cfg_use_basic_auth =
false;
121 __cfg_use_basic_auth =
config->
get_bool(
"/webview/use_basic_auth");
123 __cfg_basic_auth_realm =
"Fawkes Webview";
125 __cfg_basic_auth_realm =
config->
get_bool(
"/webview/basic_auth_realm");
129 __cache_logger.
clear();
132 "_http._tcp", __cfg_port);
133 __webview_service->
add_txt(
"fawkesver=%u.%u.%u",
134 FAWKES_VERSION_MAJOR, FAWKES_VERSION_MINOR,
135 FAWKES_VERSION_MICRO);
142 __header_gen, __footer_gen);
147 __webserver =
new WebServer(__cfg_port, __dispatcher, __cfg_ssl_key.c_str(),
148 __cfg_ssl_cert.c_str(),
logger);
153 if (__cfg_use_basic_auth) {
159 delete __webview_service;
160 delete __service_browse_handler;
180 logger->
log_info(
"WebviewThread",
"Listening for HTTP connections on port %u", __cfg_port);
203 delete __webview_service;
204 delete __service_browse_handler;
207 delete __static_processor;
208 delete __blackboard_processor;
209 delete __startpage_processor;
210 delete __plugins_processor;
225 WebviewThread::ssl_create(
const char *ssl_key_file,
const char *ssl_cert_file)
228 "This may take a while...");
232 if (asprintf(&cmd,
"openssl req -new -x509 -batch -nodes -days 365 "
233 "-subj \"/C=XX/L=World/O=Fawkes/CN=%s.local\" "
234 "-out \"%s\" -keyout \"%s\" >/dev/null 2>&1",
235 h.
short_name(), ssl_cert_file, ssl_key_file) == -1)
240 int status = system(cmd);
243 if (WEXITSTATUS(status) != 0) {
244 throw Exception(
"Failed to auto-generate key/certificate pair");
WebNavManager * webview_nav_manager
Webview navigation manager.
Thread aspect that allows to provide a logger to Fawkes.
void setup_basic_auth(const char *realm, WebUserVerifier *verifier)
Setup basic authentication.
virtual void unwatch_service(const char *service_type, ServiceBrowseHandler *h)=0
Remove browse handler for specific service.
PluginManager * plugin_manager
This is the member used to access the PluginManager.
Encapsulation of the libmicrohttpd webserver.
const char * short_name()
Get short hostname (up to first dot).
virtual void init()
Initialize the thread.
virtual void finalize()
Finalize the thread.
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
void clear()
Clear messages.
WebviewThread()
Constructor.
ServicePublisher * service_publisher
Service publisher to publish services on the network.
virtual void unpublish_service(NetworkService *service)=0
Revoke service publication.
virtual void publish_service(NetworkService *service)=0
Publish service.
static const char * STATIC_URL_PREFIX
Prefix for the WebStaticRequestProcessor.
virtual bool get_bool(const char *path)=0
Get value from configuration which is of type bool.
void process()
Process requests.
Thread class encapsulation of pthreads.
void set_prepfin_conc_loop(bool concurrent=true)
Set concurrent execution of prepare_finalize() and loop().
Static file web processor.
Logger * logger
This is the Logger member used to access the logger.
Base class for exceptions in Fawkes.
WebUrlManager * webview_url_manager
Webview request processor manager.
void unregister_baseurl(const char *url_prefix)
Remove a request processor.
BlackBoard web request processor.
void add_txt(const char *format,...)
Add a TXT record.
NetworkNameResolver * nnresolver
Network name resolver to lookup IP addresses of hostnames and vice versa.
Webview user verification.
static const char * BLACKBOARD_URL_PREFIX
Prefix for the WebBlackBoardRequestProcessor.
void add_nav_entry(std::string baseurl, std::string name)
Add a navigation entry.
const char * name() const
Get name of thread.
Representation of a service announced or found via service discovery (i.e.
ServiceBrowser * service_browser
Service browser to browse services on the network.
static const char * PLUGINS_URL_PREFIX
Prefix for the WebPluginsRequestProcessor.
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
Browse handler to detect other Webview instances on the network.
void remove_nav_entry(std::string baseurl)
Remove a navigation entry.
virtual unsigned int get_uint(const char *path)=0
Get value from configuration which is of type unsigned int.
virtual void loop()
Code to execute in the thread.
Configuration * config
This is the Configuration member used to access the configuration.
virtual void watch_service(const char *service_type, ServiceBrowseHandler *h)=0
Add browse handler for specific service.
void register_baseurl(const char *url_prefix, WebRequestProcessor *processor)
Add a request processor.
System ran out of memory and desired operation could not be fulfilled.
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
Plugins web request processor.