export_server.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*************************************************************************/
  2. /* export_server.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef JAVASCRIPT_EXPORT_SERVER_H
  31. #define JAVASCRIPT_EXPORT_SERVER_H
  32. #include "core/io/image_loader.h"
  33. #include "core/io/stream_peer_ssl.h"
  34. #include "core/io/tcp_server.h"
  35. #include "core/io/zip_io.h"
  36. #include "editor/editor_export.h"
  37. #include "editor/editor_node.h"
  38. class EditorHTTPServer : public RefCounted {
  39. private:
  40. Ref<TCPServer> server;
  41. Map<String, String> mimes;
  42. Ref<StreamPeerTCP> tcp;
  43. Ref<StreamPeerSSL> ssl;
  44. Ref<StreamPeer> peer;
  45. Ref<CryptoKey> key;
  46. Ref<X509Certificate> cert;
  47. bool use_ssl = false;
  48. uint64_t time = 0;
  49. uint8_t req_buf[4096];
  50. int req_pos = 0;
  51. void _clear_client() {
  52. peer = Ref<StreamPeer>();
  53. ssl = Ref<StreamPeerSSL>();
  54. tcp = Ref<StreamPeerTCP>();
  55. memset(req_buf, 0, sizeof(req_buf));
  56. time = 0;
  57. req_pos = 0;
  58. }
  59. void _set_internal_certs(Ref<Crypto> p_crypto) {
  60. const String cache_path = EditorPaths::get_singleton()->get_cache_dir();
  61. const String key_path = cache_path.plus_file("html5_server.key");
  62. const String crt_path = cache_path.plus_file("html5_server.crt");
  63. bool regen = !FileAccess::exists(key_path) || !FileAccess::exists(crt_path);
  64. if (!regen) {
  65. key = Ref<CryptoKey>(CryptoKey::create());
  66. cert = Ref<X509Certificate>(X509Certificate::create());
  67. if (key->load(key_path) != OK || cert->load(crt_path) != OK) {
  68. regen = true;
  69. }
  70. }
  71. if (regen) {
  72. key = p_crypto->generate_rsa(2048);
  73. key->save(key_path);
  74. cert = p_crypto->generate_self_signed_certificate(key, "CN=godot-debug.local,O=A Game Dev,C=XXA", "20140101000000", "20340101000000");
  75. cert->save(crt_path);
  76. }
  77. }
  78. public:
  79. EditorHTTPServer() {
  80. mimes["html"] = "text/html";
  81. mimes["js"] = "application/javascript";
  82. mimes["json"] = "application/json";
  83. mimes["pck"] = "application/octet-stream";
  84. mimes["png"] = "image/png";
  85. mimes["svg"] = "image/svg";
  86. mimes["wasm"] = "application/wasm";
  87. server.instantiate();
  88. stop();
  89. }
  90. void stop() {
  91. server->stop();
  92. _clear_client();
  93. }
  94. Error listen(int p_port, IPAddress p_address, bool p_use_ssl, String p_ssl_key, String p_ssl_cert) {
  95. use_ssl = p_use_ssl;
  96. if (use_ssl) {
  97. Ref<Crypto> crypto = Crypto::create();
  98. if (crypto.is_null()) {
  99. return ERR_UNAVAILABLE;
  100. }
  101. if (!p_ssl_key.is_empty() && !p_ssl_cert.is_empty()) {
  102. key = Ref<CryptoKey>(CryptoKey::create());
  103. Error err = key->load(p_ssl_key);
  104. ERR_FAIL_COND_V(err != OK, err);
  105. cert = Ref<X509Certificate>(X509Certificate::create());
  106. err = cert->load(p_ssl_cert);
  107. ERR_FAIL_COND_V(err != OK, err);
  108. } else {
  109. _set_internal_certs(crypto);
  110. }
  111. }
  112. return server->listen(p_port, p_address);
  113. }
  114. bool is_listening() const {
  115. return server->is_listening();
  116. }
  117. void _send_response() {
  118. Vector<String> psa = String((char *)req_buf).split("\r\n");
  119. int len = psa.size();
  120. ERR_FAIL_COND_MSG(len < 4, "Not enough response headers, got: " + itos(len) + ", expected >= 4.");
  121. Vector<String> req = psa[0].split(" ", false);
  122. ERR_FAIL_COND_MSG(req.size() < 2, "Invalid protocol or status code.");
  123. // Wrong protocol
  124. ERR_FAIL_COND_MSG(req[0] != "GET" || req[2] != "HTTP/1.1", "Invalid method or HTTP version.");
  125. const int query_index = req[1].find_char('?');
  126. const String path = (query_index == -1) ? req[1] : req[1].substr(0, query_index);
  127. const String req_file = path.get_file();
  128. const String req_ext = path.get_extension();
  129. const String cache_path = EditorPaths::get_singleton()->get_cache_dir().plus_file("web");
  130. const String filepath = cache_path.plus_file(req_file);
  131. if (!mimes.has(req_ext) || !FileAccess::exists(filepath)) {
  132. String s = "HTTP/1.1 404 Not Found\r\n";
  133. s += "Connection: Close\r\n";
  134. s += "\r\n";
  135. CharString cs = s.utf8();
  136. peer->put_data((const uint8_t *)cs.get_data(), cs.size() - 1);
  137. return;
  138. }
  139. const String ctype = mimes[req_ext];
  140. FileAccess *f = FileAccess::open(filepath, FileAccess::READ);
  141. ERR_FAIL_COND(!f);
  142. String s = "HTTP/1.1 200 OK\r\n";
  143. s += "Connection: Close\r\n";
  144. s += "Content-Type: " + ctype + "\r\n";
  145. s += "Access-Control-Allow-Origin: *\r\n";
  146. s += "Cross-Origin-Opener-Policy: same-origin\r\n";
  147. s += "Cross-Origin-Embedder-Policy: require-corp\r\n";
  148. s += "Cache-Control: no-store, max-age=0\r\n";
  149. s += "\r\n";
  150. CharString cs = s.utf8();
  151. Error err = peer->put_data((const uint8_t *)cs.get_data(), cs.size() - 1);
  152. if (err != OK) {
  153. memdelete(f);
  154. ERR_FAIL();
  155. }
  156. while (true) {
  157. uint8_t bytes[4096];
  158. uint64_t read = f->get_buffer(bytes, 4096);
  159. if (read == 0) {
  160. break;
  161. }
  162. err = peer->put_data(bytes, read);
  163. if (err != OK) {
  164. memdelete(f);
  165. ERR_FAIL();
  166. }
  167. }
  168. memdelete(f);
  169. }
  170. void poll() {
  171. if (!server->is_listening()) {
  172. return;
  173. }
  174. if (tcp.is_null()) {
  175. if (!server->is_connection_available()) {
  176. return;
  177. }
  178. tcp = server->take_connection();
  179. peer = tcp;
  180. time = OS::get_singleton()->get_ticks_usec();
  181. }
  182. if (OS::get_singleton()->get_ticks_usec() - time > 1000000) {
  183. _clear_client();
  184. return;
  185. }
  186. if (tcp->get_status() != StreamPeerTCP::STATUS_CONNECTED) {
  187. return;
  188. }
  189. if (use_ssl) {
  190. if (ssl.is_null()) {
  191. ssl = Ref<StreamPeerSSL>(StreamPeerSSL::create());
  192. peer = ssl;
  193. ssl->set_blocking_handshake_enabled(false);
  194. if (ssl->accept_stream(tcp, key, cert) != OK) {
  195. _clear_client();
  196. return;
  197. }
  198. }
  199. ssl->poll();
  200. if (ssl->get_status() == StreamPeerSSL::STATUS_HANDSHAKING) {
  201. // Still handshaking, keep waiting.
  202. return;
  203. }
  204. if (ssl->get_status() != StreamPeerSSL::STATUS_CONNECTED) {
  205. _clear_client();
  206. return;
  207. }
  208. }
  209. while (true) {
  210. char *r = (char *)req_buf;
  211. int l = req_pos - 1;
  212. if (l > 3 && r[l] == '\n' && r[l - 1] == '\r' && r[l - 2] == '\n' && r[l - 3] == '\r') {
  213. _send_response();
  214. _clear_client();
  215. return;
  216. }
  217. int read = 0;
  218. ERR_FAIL_COND(req_pos >= 4096);
  219. Error err = peer->get_partial_data(&req_buf[req_pos], 1, read);
  220. if (err != OK) {
  221. // Got an error
  222. _clear_client();
  223. return;
  224. } else if (read != 1) {
  225. // Busy, wait next poll
  226. return;
  227. }
  228. req_pos += read;
  229. }
  230. }
  231. };
  232. #endif