server.ino 5.3 KB

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