PortListController.cpp 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // Created by xcbosa on 2023/1/30.
  3. //
  4. #include "../processor/processor.h"
  5. #include "../utils/utils.h"
  6. #include "../webuiconf.h"
  7. #include "../processor/templates/framework7/Framework7Document.hpp"
  8. #include "../user.hpp"
  9. #include "../frp.h"
  10. using namespace std;
  11. using namespace xc::processor;
  12. using namespace xc::processor::templates;
  13. using namespace xc::processor::templates::framework7;
  14. using namespace xc::utils;
  15. using namespace configor;
  16. namespace xc::controller {
  17. double divide(double a, double b, double zeroIf) {
  18. if (b == 0) return zeroIf;
  19. return a / b;
  20. }
  21. ResponseData *PortListController(RequestData request) {
  22. string username = user::getTokenUserName(request.getCookie("Token"));
  23. auto profiles = frp::listUserAvailableProfiles(username);
  24. int totalPortCnt(0), usedPortCnt(0);
  25. for (auto profile : profiles) {
  26. totalPortCnt += profile.getAllowPortCount();
  27. usedPortCnt += profile.ports.size();
  28. }
  29. double guagePercent = 1 - usedPortCnt / (double)totalPortCnt;
  30. if (totalPortCnt == 0) {
  31. guagePercent = 0;
  32. }
  33. return new TemplateResponseData({
  34. Framework7Document({
  35. a("退出登陆").classAdd("link").onclick("window.location='/quitLogin'"),
  36. a("添加端口").classAdd("link").onclick("window.location='/createPort'")
  37. }, {
  38. BlockTitleView("总览"),
  39. BlockView({
  40. h1("端口总览").style("text-align", "center"),
  41. GuageView(to_string(usedPortCnt) + " / " + to_string(totalPortCnt) + "端口", "已开通", guagePercent, 300)
  42. }),
  43. Foreach(profiles, [] (frp::ProfileInfo profile) {
  44. return View("", {
  45. BlockTitleView(""), center({
  46. templates::div({
  47. templates::div({
  48. templates::div({
  49. templates::div({
  50. profile.getServerAddr(),
  51. VerticalSpacer(10),
  52. small("您可用范围: " + to_string(profile.getAllowPortLow()) + " - " + to_string(profile.getAllowPortLow() + profile.getAllowPortCount() - 1)).style("opacity", "0.7"),
  53. GuageView(to_string(profile.ports.size()) + "/" + to_string(profile.getAllowPortCount()) + "端口",
  54. "可在此服务器创建", divide(profile.getAllowPortCount() - profile.ports.size(), profile.getAllowPortCount(), 0), 200)
  55. }).classAdd("card-header text-color-white display-block"),
  56. Link("关闭").href("#").classAdd("link card-close card-opened-fade-in color-white").prop("style", "position: absolute; right: 15px; top: 15px")
  57. }).classAdd("bg-class-red").style("height", "300px"),
  58. templates::div({
  59. templates::div({
  60. templates::div({
  61. templates::div({
  62. templates::div({
  63. "This is a simple card with pla"
  64. }).classAdd("card-content card-content-padding")
  65. }).classAdd("card")
  66. }).classAdd("page-content")
  67. }).classAdd("page")
  68. }).classAdd("card-content-padding")
  69. }).classAdd("card-content")
  70. }).classAdd("card card-expandable")})
  71. });
  72. })
  73. }, {
  74. a("2023 © Frp-WebUI by XCBOSA").classAdd("link").onclick("window.open('https://github.com/XCBOSA/frp-webui-500k.git')")
  75. })
  76. });
  77. }
  78. ContentGeneratorDefineWithNameS("PortListController", false, PortListController(request))
  79. ResponseData *CreatePortController(RequestData request) {
  80. string username = user::getTokenUserName(request.getCookie("Token"));
  81. if (!username.empty()) {
  82. }
  83. }
  84. ContentGeneratorDefineWithNameS("/createPort", request.getURLPath() == "/createPort", CreatePortController(request))
  85. }