webuiconf.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // Created by xcbosa on 2023/1/28.
  3. //
  4. #pragma once
  5. #include <vector>
  6. #include <map>
  7. #include <sys/stat.h>
  8. #include "utils/utils.h"
  9. #include "processor/processor.h"
  10. using namespace std;
  11. using namespace xc::utils;
  12. using namespace xc::processor;
  13. using namespace xc::processor::templates;
  14. namespace xc::conf {
  15. const int clientSocketTimeoutSeconds = 3;
  16. const int taskProcessTimeoutSeconds = 1;
  17. const int mtu = 1536;
  18. const bool enableStaticAssetsController = false;
  19. const string natMachineIPPrefix = "10.";
  20. const int natMachineLocalhostPortBegin = 50000;
  21. const int natMachineLocalhostPortSize = 10000;
  22. static queue<int> __natMachineLocalhostPortFreeList;
  23. static int __natMachineLocalhostPortAllocator = natMachineLocalhostPortBegin;
  24. /*分配NAT映射端口*/
  25. static int natMachineLocalhostPortAllocate() {
  26. if (__natMachineLocalhostPortFreeList.empty()) {
  27. int allocated = __natMachineLocalhostPortAllocator++;
  28. assert(allocated < natMachineLocalhostPortBegin + natMachineLocalhostPortSize);
  29. }
  30. int port = __natMachineLocalhostPortFreeList.front();
  31. return port;
  32. }
  33. /*释放NAT映射端口*/
  34. static int natMachineLocalhostPortFree(int port) {
  35. __natMachineLocalhostPortFreeList.push(port);
  36. }
  37. const string title("Frp-WebUI-XCBOSA");
  38. const int allowPortCountPerProfile(10);
  39. const string userPasswordSalt("eDGJ&v,.W0U(66.lVQFsKfWb*bm*M+Lj");
  40. extern string userJWTSecret;
  41. const string rootDir = "/etc/frpcwebui";
  42. const int userTokenExpireSeconds = 60 * 60 * 24;
  43. const vector<string> supportTypes = { "tcp", "udp" };
  44. inline string getDirAndMakesureExists(string dirPath) {
  45. struct stat buffer;
  46. if (stat(dirPath.c_str(), &buffer) == 0) {
  47. assert(!S_ISREG(buffer.st_mode));
  48. return dirPath;
  49. }
  50. mode_t old_mask = umask(0);
  51. int n_ret = mkdir(dirPath.c_str(), S_IRWXU | S_IRGRP | S_IROTH);
  52. umask(old_mask);
  53. if (n_ret != 0) {
  54. ::perror("");
  55. assert(n_ret == 0);
  56. }
  57. return dirPath;
  58. }
  59. inline string getRootDir() {
  60. return getDirAndMakesureExists(rootDir);
  61. }
  62. inline string getUserDataDir() {
  63. return getDirAndMakesureExists(getRootDir() + "/users");
  64. }
  65. inline string getFrpcDir() {
  66. return getDirAndMakesureExists(getRootDir() + "/frpc");
  67. }
  68. const map<string, string> fileExtensionToMimeTypes = {
  69. { ".html", "text/html" },
  70. { ".htm", "text/html" },
  71. { ".js", "text/javascript" },
  72. { ".ts", "text/typescript" },
  73. { ".xcnb", "text/xc-notebook" },
  74. { ".ccdproj", "application/c-code-develop-project" },
  75. { ".png", "image/png" },
  76. { ".jpg", "image/jpeg" },
  77. { ".jpeg", "image/jpeg" },
  78. { ".tiff", "image/tiff" },
  79. { ".css", "text/css" },
  80. { ".less", "text/css" },
  81. { ".scss", "text/css" },
  82. { ".svg", "image/svg+xml" },
  83. { ".ttf", "application/x-font-ttf" },
  84. { ".ttc", "application/x-font-ttf" },
  85. { ".woff", "application/x-font-woff" },
  86. { ".woff2", "application/x-font-woff" },
  87. { "default", "application/octet-stream" },
  88. };
  89. const vector<string> defaultFiles = {
  90. "index.html",
  91. "index.htm"
  92. };
  93. class ErrorView: public View {
  94. public:
  95. ErrorView(int errorCode, string errorMessage): View("") {
  96. ostringstream oss;
  97. oss << "Error ";
  98. oss << errorMessage;
  99. oss << " ";
  100. oss << errorCode;
  101. html html({
  102. head({
  103. meta().charset("UTF-8"),
  104. title(oss.str())
  105. }),
  106. body({
  107. p(oss.str())
  108. .style("text-align", "center")
  109. .pointer(&this->messageView),
  110. p("FRPC-WebUI / XCHttpServer 1.0")
  111. .style("text-align", "center")
  112. })
  113. });
  114. this->inner(html);
  115. }
  116. void setMessage(string text) {
  117. this->messageView->inner(text);
  118. }
  119. private:
  120. View *messageView;
  121. };
  122. const TemplateResponseData errorPage400(400, { ErrorView(400, "请求格式错误,无法解析请求") });
  123. const TemplateResponseData errorPage404(400, { ErrorView(404, "不存在指定的资源") });
  124. const TemplateResponseData errorPage500(400, { ErrorView(500, "服务器内部错误,可能是服务器访问量过大,请稍后重试") });
  125. const TemplateResponseData errorPageTimeout(550, { ErrorView(550, "服务器任务处理已超时,可能服务器访问量过大,请稍后重试") });
  126. }