server.ino 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. Web Server
  3. This is the main entry point of the WebStick bare metal
  4. web server. If you have exception rules that shall not
  5. be handled by the main router, you can do them here.
  6. */
  7. //Check if a user is authenticated / logged in
  8. bool IsUserAuthed(AsyncWebServerRequest *request) {
  9. if (request->hasHeader("Cookie")) {
  10. //User cookie from browser
  11. String authCookie = GetCookieValueByKey(request, "web-auth");
  12. if (authCookie == "") {
  13. return false;
  14. }
  15. //Check if it is user login (no state keeping)
  16. bool isUserLogin = DBKeyExists("sess", authCookie);
  17. if (isUserLogin){
  18. //User login
  19. return true;
  20. }
  21. //Check if it is admin login (state keeping)
  22. if (authSession == "") {
  23. //Server side has no resumable login session
  24. return false;
  25. }
  26. bool isAdminLogin = authCookie.equals(authSession);
  27. if (isAdminLogin) {
  28. //Admin login
  29. return true;
  30. }
  31. return false;
  32. } else {
  33. Serial.println("Cookie Missing");
  34. return false;
  35. }
  36. }
  37. //Check if a user is authenticated and is Admin
  38. bool IsAdmin(AsyncWebServerRequest *request) {
  39. if (request->hasHeader("Cookie")) {
  40. //User cookie from browser
  41. String authCookie = GetCookieValueByKey(request, "web-auth");
  42. if (authCookie == "") {
  43. return false;
  44. }
  45. //Match it to the server side value in kvdb
  46. if (authSession == "") {
  47. //Server side has no resumable login session
  48. return false;
  49. }
  50. if (authCookie.equals(authSession)) {
  51. return true;
  52. }
  53. return false;
  54. } else {
  55. return false;
  56. }
  57. }
  58. //Reply the request by a directory list
  59. void HandleDirRender(AsyncWebServerRequest *r, String dirName, String dirToList) {
  60. AsyncResponseStream *response = r->beginResponseStream("text/html");
  61. //Serve directory entries
  62. File directory = SD.open(dirToList);
  63. // Check if the directory is open
  64. if (!directory) {
  65. SendErrorResp(r, "unable to open directory");
  66. return;
  67. }
  68. response->print("<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>Content of " + dirName + "</title></head><body style=\"margin: 3em;font-family: Arial;\">");
  69. response->print("<h3>Content of " + dirName + "</h3><div style=\"width: 100%;border-bottom: 1px solid #d9d9d9;\"></div><ul>");
  70. // List the contents of the directory
  71. while (true) {
  72. File entry = directory.openNextFile();
  73. if (!entry) {
  74. // No more files
  75. break;
  76. }
  77. // Print the file name
  78. response->print("<li><a href=\"./" + String(entry.name()) + "\">");
  79. response->print(entry.name());
  80. response->print(" (" + humanReadableSize(entry.size()) + ")</a></li>");
  81. Serial.println(entry.name());
  82. entry.close();
  83. }
  84. // Close the directory
  85. directory.close();
  86. response->print("</ul><div style=\"width: 100%;border-bottom: 1px solid #d9d9d9;\"></div><br><a href=\"../\">Back</a>");
  87. response->print("<br><br><body></html>");
  88. r->send(response);
  89. }
  90. void initWebServer() {
  91. /*
  92. Other handles here, like this
  93. server.on("/logout", HTTP_GET, [](AsyncWebServerRequest *request){
  94. request->send(401);
  95. });
  96. */
  97. /*
  98. server.on("/test", HTTP_GET, [](AsyncWebServerRequest * request) {
  99. getSDCardUsedSpace();
  100. request->send(200);
  101. });
  102. */
  103. /* Authentication Functions */
  104. server.on("/api/auth/chk", HTTP_GET, HandleCheckAuth);
  105. server.on("/api/auth/login", HTTP_POST, HandleLogin);
  106. server.on("/api/auth/logout", HTTP_GET, HandleLogout);
  107. /* User System Functions */
  108. server.on("/api/user/info", HTTP_GET, HandleGetUserinfo);
  109. server.on("/api/user/new", HTTP_POST, HandleNewUser);
  110. server.on("/api/user/chpw", HTTP_POST, HandleUserChangePassword);
  111. server.on("/api/user/del", HTTP_POST, HandleRemoveUser);
  112. server.on("/api/user/list", HTTP_GET, HandleUserList);
  113. /* File System Functions */
  114. server.on("/api/fs/list", HTTP_GET, HandleListDir);
  115. server.on("/api/fs/del", HTTP_POST, HandleFileDel);
  116. server.on("/api/fs/move", HTTP_POST, HandleFileRename);
  117. server.on("/api/fs/download", HTTP_GET, HandleFileDownload);
  118. server.on("/api/fs/newFolder", HTTP_POST, HandleNewFolder);
  119. server.on("/api/fs/disk", HTTP_GET, HandleLoadSpaceInfo);
  120. server.on("/api/fs/properties", HTTP_GET, HandleFileProp);
  121. server.on("/api/fs/search", HTTP_GET, HandleFileSearch);
  122. /* File Share Functions */
  123. server.on("/api/share/new", HTTP_POST, HandleCreateShare);
  124. server.on("/api/share/del", HTTP_POST, HandleRemoveShare);
  125. server.on("/api/share/list", HTTP_GET, HandleShareList);
  126. server.on("/api/share/clean", HTTP_GET, HandleShareListCleaning);
  127. server.on("/share", HTTP_GET, HandleShareAccess);
  128. /* Preference */
  129. server.on("/api/pref/set", HTTP_GET, HandleSetPref);
  130. server.on("/api/pref/get", HTTP_GET, HandleLoadPref);
  131. /* Others */
  132. server.on("/api/info/wifi", HTTP_GET, HandleWiFiInfo); //Show WiFi Information
  133. server.on("/api/wol", HTTP_GET, HandleWakeOnLan); //Handle WoL request
  134. //File upload handler. see upload.ino
  135. server.onFileUpload(handleFileUpload);
  136. //Not found handler
  137. server.onNotFound([](AsyncWebServerRequest *request) {
  138. //Generally it will not arrive here as NOT FOUND is also handled in the main router.
  139. //See router.ino for implementation details.
  140. prettyPrintRequest(request);
  141. request->send(404, "text/plain", "Not Found");
  142. });
  143. //Main Router, see router.ino
  144. server.addHandler(new MainRouter());
  145. server.begin();
  146. }