Skip to content

Commit 958bace

Browse files
committed
Merge remote-tracking branch 'lj/main'
2 parents b2d8f1e + fd27789 commit 958bace

19 files changed

+301
-248
lines changed

.gitmodules

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[submodule "drogon"]
2+
path = drogon
3+
url = https://github.com/drogonframework/drogon
4+
[submodule "jwt-cpp"]
5+
path = jwt-cpp
6+
url = https://github.com/Thalhammer/jwt-cpp

CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
2424
set(CMAKE_CXX_EXTENSIONS OFF)
2525

2626

27-
add_executable(${PROJECT_NAME} main.cc mysql.cc user_controller.cc jwt_controller.cc msg_controller.cc)
27+
add_executable(${PROJECT_NAME} main.cc mysql.cc user_controller.cc jwt_controller.cc msg_controller.cc file_controller.cc)
2828

2929

3030
# 链接MySQL Connector/C++库
@@ -52,7 +52,7 @@ target_link_libraries(${PROJECT_NAME} PRIVATE -L${MYSQL_CONNECTOR_CPP_ROOT}/lib6
5252
find_package(OpenSSL REQUIRED)
5353

5454
# 添加 OpenSSL 库到可执行文件链接参数
55-
target_link_libraries(flypen PRIVATE OpenSSL::Crypto)
55+
target_link_libraries(${PROJECT_NAME} PRIVATE OpenSSL::Crypto)
5656

5757
#link_directories(/home/lgy/c++kc/mysql-connector/lib64)
5858
#
@@ -95,4 +95,4 @@ target_sources(${PROJECT_NAME}
9595

9696
# ##############################################################################
9797

98-
add_subdirectory(test)
98+
#add_subdirectory(test)

drogon

Submodule drogon added at 6cb8ac6

file_controller.cc

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include "file_controller.h"
2+
#include <iostream>
3+
#include <stdexcept>
4+
#include <stdio.h>
5+
#include <string>
6+
#include <json/json.h>
7+
8+
9+
std::string shell_commons(const char* cmd) {
10+
char buffer[128];
11+
std::string result = "";
12+
FILE* pipe = popen(cmd, "r");
13+
if (!pipe) throw std::runtime_error("popen() failed!");
14+
try {
15+
while (fgets(buffer, sizeof buffer, pipe) != NULL) {
16+
result += buffer;
17+
}
18+
} catch (...) {
19+
pclose(pipe);
20+
throw;
21+
}
22+
pclose(pipe);
23+
return result;
24+
}
25+
26+
void genTree(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
27+
{
28+
char *pathvar;
29+
pathvar = getenv("PWD");
30+
std::string result = shell_commons(("cd "+std::string(pathvar)+"/.. " + "&&"+"tree -J root" ).c_str()) ;
31+
auto res = HttpResponse::newHttpResponse();
32+
res->addHeader("Access-Control-Allow-Origin", "*");
33+
res ->setBody(result);
34+
callback(res);
35+
}
36+
void catFile(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
37+
{
38+
char *pathvar;
39+
pathvar = getenv("PWD");
40+
std::string path = req->getParameter("path");
41+
std::string result = shell_commons(("cat "+std::string(pathvar)+"/../root/"+path).c_str()) ;
42+
auto res = HttpResponse::newHttpResponse();
43+
res->addHeader("Access-Control-Allow-Origin", "*");
44+
res ->setBody(result);
45+
callback(res);
46+
}
47+
void saveFile(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
48+
{
49+
auto body = req->getBody();
50+
Json::Value req_json;
51+
Json::Reader reader;
52+
std::string bodyStr(body);
53+
if (!reader.parse(bodyStr, req_json)) {
54+
std::cout<<"parse failed"<<std::endl;
55+
callback(HttpResponse::newHttpResponse());
56+
return;
57+
}
58+
char *pathvar;
59+
pathvar = getenv("PWD");
60+
std::string filename = req_json["filename"].asString();
61+
std::string content = req_json["content"].asString();
62+
std::string result = shell_commons(("echo '"+content+"'>"+std::string(pathvar)+"/../root/"+filename).c_str()) ;
63+
auto res = HttpResponse::newHttpResponse();
64+
res->addHeader("Access-Control-Allow-Origin", "*");
65+
res ->setBody("success");
66+
callback(res);
67+
}

file_controller.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef _FILE_CONTROLLER_H_
2+
#define _FILE_CONTROLLER_H_
3+
#include <string>
4+
#include <drogon/drogon.h>
5+
using namespace drogon;
6+
std::string shell_commons(const char* cmd);
7+
void genTree(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback);
8+
void catFile(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback);
9+
void saveFile(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback);
10+
//std::string
11+
#endif

jwt-cpp

Submodule jwt-cpp added at ce1f9df

jwt_controller.cc

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
#include "jwt_controller.h"
21
#include <iostream>
2+
#include <drogon/drogon.h>
33
#include <jwt-cpp/jwt.h>
44
#include <cstring>
55
#include <chrono>
6+
#include "jwt_controller.h"
7+
68
using namespace jwt;
9+
710
std::string jwtGen(const Json::Value& req_json)
811
{
912
auto now = std::chrono::system_clock::now();
@@ -16,11 +19,6 @@ std::string jwtGen(const Json::Value& req_json)
1619
.sign(jwt::algorithm::hs256{"secret"});
1720
return std::string(token);
1821
}
19-
// std::string jwtDecode(const std::string& token)
20-
// {
21-
// auto decoded_token = jwt::decode(token);
22-
// return decoded_token.get_payload_claim("name").as_string();
23-
// }
2422

2523
std::string jwtDecrypt(const std::string& token)
2624
{
@@ -35,4 +33,21 @@ std::string jwtDecrypt(const std::string& token)
3533
std::cout<<"Failed to decrypt JWT: " + std::string(e.what())<<std::endl;
3634
throw std::runtime_error("Failed to decrypt JWT");
3735
}
36+
}
37+
38+
bool jwtVerify(const drogon::HttpRequestPtr &req){
39+
std::string authHeader = req->getHeader("Authorization");
40+
if (authHeader.substr(0, 7) == "Bearer ") {
41+
std::string bearerToken = authHeader.substr(7);
42+
try {
43+
std::string sender = jwtDecrypt(bearerToken);
44+
return true;
45+
} catch (const std::exception &e) {
46+
std::cout << "Wrong token" << std::endl;
47+
return false;
48+
}
49+
} else {
50+
std::cout << "No Authorization" << std::endl;
51+
return false;
52+
}
3853
}

jwt_controller.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
#ifndef _JWT_CONTROLLER_H_
22
#define _JWT_CONTROLLER_H_
3+
4+
#include <drogon/drogon.h>
35
#include <json/json.h>
46
#include <cstring>
7+
58
std::string jwtGen(const Json::Value& req_json);
69
std::string jwtDecrypt(const std::string& token);
10+
bool jwtVerify(const drogon::HttpRequestPtr &req);
11+
712
#endif

main.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <drogon/drogon.h>
33
#include "msg_controller.h"
44
#include "user_controller.h"
5+
#include "file_controller.h"
56
#include <drogon/WebSocketController.h>
67
using namespace drogon;
78
std::unordered_map<std::string, WebSocketConnectionPtr> clientTable;
@@ -29,6 +30,19 @@ int main() {
2930
drogon::app().registerHandler("/api/info", [](const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
3031
info(req, std::move(callback));
3132
});
33+
34+
drogon::app().registerHandler("/api/file/tree", [](const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
35+
genTree(req, std::move(callback));
36+
});
37+
drogon::app().registerHandler("/api/file/cat", [](const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
38+
catFile(req, std::move(callback));
39+
});
40+
drogon::app().registerHandler("/api/file/save", [](const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
41+
saveFile(req, std::move(callback));
42+
});
43+
drogon::app().registerHandler("/api/avatar", [](const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
44+
avatar(req, std::move(callback));
45+
});
3246
drogon::app().run();
3347
return 0;
3448
}

0 commit comments

Comments
 (0)