xcbosa-itx 2 жил өмнө
parent
commit
c17d9cd096
4 өөрчлөгдсөн 61 нэмэгдсэн , 11 устгасан
  1. BIN
      .DS_Store
  2. 59 0
      main.cpp
  3. 1 1
      utils/FileReader.cpp
  4. 1 10
      utils/FileResponseData.cpp

BIN
.DS_Store


+ 59 - 0
main.cpp

@@ -31,6 +31,65 @@ const static strcmd assign("assign", "assign <UserName> <Profile>", "使用户
     cout << "成功将配置文件 " << args[1] << " 绑定到用户 " << args[0] << endl;
 });
 
+int call_shell(string cmd) {
+    cout << "$ " << cmd << endl;
+    ::system(cmd.c_str());
+}
+
+const static strcmd install("install", "install", "安装", 0, [] (auto args) {
+    uid_t uid = getuid();
+    if (setuid(0)) {
+        cerr << "安装失败,无法获取root权限。" << endl;
+        return;
+    }
+
+    char selfPath[1024];
+    int len;
+    if ((len = readlink("/proc/self/exe", selfPath, sizeof(selfPath))) <= 0) {
+        cerr << "安装失败,无法获取自身位置。" << endl;
+        return;
+    }
+    selfPath[len] = 0;
+    string programPath(selfPath);
+
+    replace_all(programPath, " ", "\\ ");
+    string cpCmd = "cp " + programPath + " /usr/local/bin/fpw";
+    if (call_shell(cpCmd)) {
+        cerr << "安装失败,无法将自身拷贝到 /usr/local/bin/fpw。" << endl;
+        return;
+    }
+
+    cout << "$ vi /usr/lib/systemd/system/fpw.service" << endl;
+
+    INIFile systemd("/usr/lib/systemd/system/fpw.service");
+    systemd.getMust("Unit")->set("Description", "FRPCWebUI Daemon");
+    systemd.getMust("Unit")->set("After", "network.target");
+    systemd.getMust("Service")->set("Type", "Simple");
+    systemd.getMust("Service")->set("User", "root");
+    systemd.getMust("Service")->set("Restart", "on-failure");
+    systemd.getMust("Service")->set("RestartSec", "1s");
+    systemd.getMust("Service")->set("ExecStart", "fpw");
+    systemd.getMust("Service")->set("LimitNOFILE", "1048576");
+    systemd.getMust("Install")->set("WantedBy", "multi-user.target");
+    systemd.save();
+
+    if (call_shell("systemctl daemon-reload")) {
+        cerr << "安装失败,无法调用systemctl。" << endl;
+        return;
+    }
+    if (call_shell("systemctl enable fpw")) {
+        cerr << "安装失败,无法调用systemctl。" << endl;
+        return;
+    }
+    if (call_shell("systemctl start fpw")) {
+        cerr << "安装失败,无法调用systemctl。" << endl;
+        return;
+    }
+
+    setuid(uid);
+    cout << "安装成功。" << endl;
+});
+
 int main(int argc, char **argv) {
     std::cout << "FRPC WebUI HelloWorld!" << std::endl;
     CommandLineWorker cmdLine;

+ 1 - 1
utils/FileReader.cpp

@@ -12,7 +12,7 @@ namespace xc::utils {
         ifstream fin(filePath);
         if (fin.fail()) {
             cerr << "[FileIOError]: Read Error " << ::strerror(errno) << endl;
-            return "404";
+            return "";
         }
         stringstream buffer;
         buffer << fin.rdbuf();

+ 1 - 10
utils/FileResponseData.cpp

@@ -3,6 +3,7 @@
 //
 
 #include "FileResponseData.h"
+#include "strop.h"
 
 using namespace std;
 
@@ -33,16 +34,6 @@ namespace xc {
             this->setBody(contentsOfTextFile(filePath));
         }
 
-        string& replace_all(string& src, const string& old_value, const string& new_value) {
-            for (string::size_type pos(0); pos != string::npos; pos += new_value.length()) {
-                if ((pos = src.find(old_value, pos)) != string::npos) {
-                    src.replace(pos, old_value.length(), new_value);
-                }
-                else break;
-            }
-            return src;
-        }
-
         void FileResponseData::setFilePath(string filePath, vector<Replacement> replacements) {
             this->filePath = filePath;
             string str = contentsOfTextFile(filePath);