Added
Link Here
|
0 |
- |
1 |
--- service/OneService.cpp.orig 2023-09-14 19:09:26 UTC |
|
|
2 |
+++ service/OneService.cpp |
3 |
@@ -795,6 +795,7 @@ class OneServiceImpl : public OneService (public) |
4 |
bool _allowTcpFallbackRelay; |
5 |
bool _forceTcpRelay; |
6 |
bool _allowSecondaryPort; |
7 |
+ bool _enableWebServer; |
8 |
|
9 |
unsigned int _primaryPort; |
10 |
unsigned int _secondaryPort; |
11 |
@@ -1557,6 +1558,7 @@ class OneServiceImpl : public OneService (public) |
12 |
|
13 |
std::vector<std::string> noAuthEndpoints { "/sso", "/health" }; |
14 |
|
15 |
+ |
16 |
auto setContent = [=] (const httplib::Request &req, httplib::Response &res, std::string content) { |
17 |
if (req.has_param("jsonp")) { |
18 |
if (content.length() > 0) { |
19 |
@@ -1573,8 +1575,98 @@ class OneServiceImpl : public OneService (public) |
20 |
} |
21 |
}; |
22 |
|
23 |
+ // |
24 |
+ // static file server for app ui' |
25 |
+ // |
26 |
+ if (_enableWebServer) { |
27 |
+ static std::string appUiPath = "/app"; |
28 |
+ static char appUiDir[16384]; |
29 |
+ sprintf(appUiDir,"%s%s",_homePath.c_str(),appUiPath.c_str()); |
30 |
|
31 |
- auto authCheck = [=] (const httplib::Request &req, httplib::Response &res) { |
32 |
+ auto ret = _controlPlane.set_mount_point(appUiPath, appUiDir); |
33 |
+ _controlPlaneV6.set_mount_point(appUiPath, appUiDir); |
34 |
+ if (!ret) { |
35 |
+ fprintf(stderr, "Mounting app directory failed. Creating it. Path: %s - Dir: %s\n", appUiPath.c_str(), appUiDir); |
36 |
+ if (!OSUtils::mkdir(appUiDir)) { |
37 |
+ fprintf(stderr, "Could not create app directory either. Path: %s - Dir: %s\n", appUiPath.c_str(), appUiDir); |
38 |
+ } else { |
39 |
+ ret = _controlPlane.set_mount_point(appUiPath, appUiDir); |
40 |
+ _controlPlaneV6.set_mount_point(appUiPath, appUiDir); |
41 |
+ if (!ret) { |
42 |
+ fprintf(stderr, "Really could not create and mount directory. Path: %s - Dir: %s\nWeb apps won't work.\n", appUiPath.c_str(), appUiDir); |
43 |
+ } |
44 |
+ } |
45 |
+ } |
46 |
+ |
47 |
+ if (ret) { |
48 |
+ // fallback to /index.html for paths that don't exist for SPAs |
49 |
+ auto indexFallbackGet = [](const httplib::Request &req, httplib::Response &res) { |
50 |
+ // fprintf(stderr, "fallback \n"); |
51 |
+ |
52 |
+ auto match = req.matches[1]; |
53 |
+ if (match.matched) { |
54 |
+ |
55 |
+ // fallback |
56 |
+ char indexHtmlPath[16384]; |
57 |
+ sprintf(indexHtmlPath,"%s/%s/%s", appUiDir, match.str().c_str(), "index.html"); |
58 |
+ // fprintf(stderr, "fallback path %s\n", indexHtmlPath); |
59 |
+ |
60 |
+ std::string indexHtml; |
61 |
+ |
62 |
+ if (!OSUtils::readFile(indexHtmlPath, indexHtml)) { |
63 |
+ res.status = 500; |
64 |
+ return; |
65 |
+ } |
66 |
+ |
67 |
+ res.set_content(indexHtml.c_str(), "text/html"); |
68 |
+ } else { |
69 |
+ res.status = 500; |
70 |
+ return; |
71 |
+ } |
72 |
+ }; |
73 |
+ |
74 |
+ auto slashRedirect = [](const httplib::Request &req, httplib::Response &res) { |
75 |
+ // fprintf(stderr, "redirect \n"); |
76 |
+ |
77 |
+ // add .html |
78 |
+ std::string htmlFile; |
79 |
+ char htmlPath[16384]; |
80 |
+ sprintf(htmlPath,"%s%s%s", appUiDir, (req.path).substr(appUiPath.length()).c_str(), ".html"); |
81 |
+ // fprintf(stderr, "path: %s\n", htmlPath); |
82 |
+ if (OSUtils::readFile(htmlPath, htmlFile)) { |
83 |
+ res.set_content(htmlFile.c_str(), "text/html"); |
84 |
+ return; |
85 |
+ } else { |
86 |
+ res.status = 301; |
87 |
+ res.set_header("location", req.path + "/"); |
88 |
+ } |
89 |
+ }; |
90 |
+ |
91 |
+ // auto missingAssetGet = [&, setContent](const httplib::Request &req, httplib::Response &res) { |
92 |
+ // fprintf(stderr, "missing \n"); |
93 |
+ // res.status = 404; |
94 |
+ // std::string html = "oops"; |
95 |
+ // res.set_content(html, "text/plain"); |
96 |
+ // res.set_header("Content-Type", "text/plain"); |
97 |
+ // return; |
98 |
+ // }; |
99 |
+ |
100 |
+ // auto fix no trailing slash by adding .html or redirecting to path/ |
101 |
+ _controlPlane.Get(appUiPath + R"((/[\w|-]+)+$)", slashRedirect); |
102 |
+ _controlPlaneV6.Get(appUiPath + R"((/[\w|-]+)+$)", slashRedirect); |
103 |
+ |
104 |
+ // // 404 missing assets for *.ext paths |
105 |
+ // s.Get(appUiPath + R"(/\.\w+$)", missingAssetGet); |
106 |
+ // sv6.Get(appUiPath + R"(/\.\w+$)", missingAssetGet); |
107 |
+ |
108 |
+ // fallback to index.html for unknown paths/files |
109 |
+ _controlPlane.Get(appUiPath + R"((/[\w|-]+)(/[\w|-]+)*/$)", indexFallbackGet); |
110 |
+ _controlPlaneV6.Get(appUiPath + R"((/[\w|-]+)(/[\w|-]+)*/$)", indexFallbackGet); |
111 |
+ |
112 |
+ } |
113 |
+ } |
114 |
+ |
115 |
+ auto authCheck = [=] (const httplib::Request &req, httplib::Response &res) { |
116 |
if (req.path == "/metrics") { |
117 |
|
118 |
if (req.has_header("x-zt1-auth")) { |
119 |
@@ -1624,6 +1716,11 @@ class OneServiceImpl : public OneService (public) |
120 |
isAuth = true; |
121 |
} |
122 |
|
123 |
+ // Web Apps base path |
124 |
+ if (req.path.rfind("/app", 0) == 0) { //starts with /app |
125 |
+ isAuth = true; |
126 |
+ } |
127 |
+ |
128 |
if (!isAuth) { |
129 |
// check auth token |
130 |
if (req.has_header("x-zt1-auth")) { |
131 |
@@ -2429,6 +2526,7 @@ class OneServiceImpl : public OneService (public) |
132 |
// bondingPolicy cannot be used with allowTcpFallbackRelay |
133 |
_allowTcpFallbackRelay = OSUtils::jsonBool(settings["allowTcpFallbackRelay"],true); |
134 |
_forceTcpRelay = OSUtils::jsonBool(settings["forceTcpRelay"],false); |
135 |
+ _enableWebServer = (OSUtils::jsonBool(settings["enableWebServer"],false)); |
136 |
|
137 |
#ifdef ZT_TCP_FALLBACK_RELAY |
138 |
_fallbackRelayAddress = InetAddress(OSUtils::jsonString(settings["tcpFallbackRelay"], ZT_TCP_FALLBACK_RELAY).c_str()); |