Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"session_timeout": 0,
//string value of SameSite attribute of the Set-Cookie HTTP respone header
//valid value is either 'Null' (default), 'Lax', 'Strict' or 'None'
"session_same_site" : "Null",
"session_same_site": "Null",
//document_root: Root path of HTTP document, defaut path is ./
"document_root": "./",
//home_page: Set the HTML file of the home page, the default value is "index.html"
Expand Down Expand Up @@ -317,4 +317,4 @@
],
//custom_config: custom configuration for users. This object can be get by the app().getCustomConfig() method.
"custom_config": {}
}
}
12 changes: 6 additions & 6 deletions file_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
#include <iostream>
#include <stdexcept>
#include <string>
std:: string return_status(std::string result,std::string command )
std::string return_status(std::string result, std::string command)
{
if (result != "")
result = "success";
else
result = " error in :"+command;
result = " error in :" + command;
return result;
}
void add_lock(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
Expand Down Expand Up @@ -83,12 +83,12 @@ void commandsCtrl(const HttpRequestPtr &req, std::function<void(const HttpRespon
break;
case cp:
result = shell_commands(("cp -v " + std::string(pathvar) + "/../root/" + params1 + " " + std::string(pathvar) + "/../root/" + params2).c_str());
result = return_status(result,"cp");
result = return_status(result, "cp");

break;
case mv:
result = shell_commands(("mv -v " + std::string(pathvar) + "/../root/" + params1 + " " + std::string(pathvar) + "/../root/" + params2).c_str());
result = return_status(result,"mv");
result = return_status(result, "mv");
break;
case rm:
if (params1.find("..") != std::string::npos)
Expand All @@ -97,11 +97,11 @@ void commandsCtrl(const HttpRequestPtr &req, std::function<void(const HttpRespon
break;
}
result = shell_commands(("rm -rf -v " + std::string(pathvar) + "/../root/" + params1).c_str());
result = return_status(result,"rm");
result = return_status(result, "rm");
break;
case mkdir:
result = shell_commands(("mkdir -v " + std::string(pathvar) + "/../root/" + params1).c_str());
result = return_status(result,"mkdir");
result = return_status(result, "mkdir");
break;
case touch:
if ("" == shell_commands(("ls -l " + std::string(pathvar) + "/../root/" + params1 + " grep ^- ").c_str()))
Expand Down
42 changes: 26 additions & 16 deletions jwt_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,56 @@

using namespace jwt;

std::string jwtGen(const Json::Value& req_json)
std::string jwtGen(const Json::Value &req_json)
{
auto now = std::chrono::system_clock::now();
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
std::string timestamp = std::to_string(ms);
auto token = jwt::create()
.set_type("JWS")
.set_payload_claim("name", jwt::claim(req_json["username"].asString()))
.set_payload_claim("time", jwt::claim(timestamp))
.sign(jwt::algorithm::hs256{"secret"});
.set_type("JWS")
.set_payload_claim("name", jwt::claim(req_json["username"].asString()))
.set_payload_claim("time", jwt::claim(timestamp))
.sign(jwt::algorithm::hs256{"secret"});
return std::string(token);
}

std::string jwtDecrypt(const std::string& token)
std::string jwtDecrypt(const std::string &token)
{
try {
try
{
auto decoded_token = jwt::decode(token);
auto verifier = jwt::verify()
.allow_algorithm(jwt::algorithm::hs256{"secret"})
.with_type("JWS");
.allow_algorithm(jwt::algorithm::hs256{"secret"})
.with_type("JWS");
verifier.verify(decoded_token);
return decoded_token.get_payload_claim("name").as_string();
} catch (const std::exception& e) {
std::cout<<"Failed to decrypt JWT: " + std::string(e.what())<<std::endl;
}
catch (const std::exception &e)
{
std::cout << "Failed to decrypt JWT: " + std::string(e.what()) << std::endl;
throw std::runtime_error("Failed to decrypt JWT");
}
}

bool jwtVerify(const drogon::HttpRequestPtr &req){
bool jwtVerify(const drogon::HttpRequestPtr &req)
{
std::string authHeader = req->getHeader("Authorization");
if (authHeader.substr(0, 7) == "Bearer ") {
if (authHeader.substr(0, 7) == "Bearer ")
{
std::string bearerToken = authHeader.substr(7);
try {
try
{
std::string sender = jwtDecrypt(bearerToken);
return true;
} catch (const std::exception &e) {
}
catch (const std::exception &e)
{
std::cout << "Wrong token" << std::endl;
return false;
}
} else {
}
else
{
std::cout << "No Authorization" << std::endl;
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions jwt_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include <json/json.h>
#include <cstring>

std::string jwtGen(const Json::Value& req_json);
std::string jwtDecrypt(const std::string& token);
std::string jwtGen(const Json::Value &req_json);
std::string jwtDecrypt(const std::string &token);
bool jwtVerify(const drogon::HttpRequestPtr &req);

#endif
2 changes: 1 addition & 1 deletion main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int main()
drogon::app().registerHandler("/api/file/commands", [](const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
{ commandsCtrl(req, std::move(callback)); });
drogon::app().registerHandler("/api/file/lock", [](const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
{add_lock(req, std::move(callback));});
{ add_lock(req, std::move(callback)); });
drogon::app().setUploadPath("./uploads").run();
return 0;
}
36 changes: 20 additions & 16 deletions models/model.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,28 @@
//convert: the value can be changed by a function call before it is stored into database or
//after it is read from database
"convert": {
"enabled": false,
"items":[{
"table": "user",
"column": "password",
"method": {
//after_db_read: name of the method which is called after reading from database, signature: void([const] std::shared_ptr [&])
"after_db_read": "decrypt_password",
//before_db_write: name of the method which is called before writing to database, signature: void([const] std::shared_ptr [&])
"before_db_write": "encrypt_password"
},
"includes": [
"\"file_local_search_path.h\"","<file_in_global_search_path.h>"
]
}]
"enabled": false,
"items": [
{
"table": "user",
"column": "password",
"method": {
//after_db_read: name of the method which is called after reading from database, signature: void([const] std::shared_ptr [&])
"after_db_read": "decrypt_password",
//before_db_write: name of the method which is called before writing to database, signature: void([const] std::shared_ptr [&])
"before_db_write": "encrypt_password"
},
"includes": [
"\"file_local_search_path.h\"",
"<file_in_global_search_path.h>"
]
}
]
},
"relationships": {
"enabled": false,
"items": [{
"items": [
{
"type": "has one",
"original_table_name": "products",
"original_table_alias": "product",
Expand Down Expand Up @@ -101,4 +105,4 @@
// generate_base_only: false by default. Set to true to avoid overwriting custom subclasses.
"generate_base_only": false
}
}
}
107 changes: 53 additions & 54 deletions mysql.cc
Original file line number Diff line number Diff line change
Expand Up @@ -315,68 +315,67 @@ Json::Value get_my_info(std::string me)
sql::Connection *con;
con = driver->connect("tcp://8.130.48.157:3306", "root", "abc.123");
con->setSchema("flypen");
if (!me.empty())
{
std::string sql = "SELECT * FROM users WHERE username = ? LIMIT 1";
sql::PreparedStatement *prepStmt = con->prepareStatement(sql);
prepStmt->setString(1, me);

sql::ResultSet *res = prepStmt->executeQuery();
if (!me.empty())
{
std::string sql = "SELECT * FROM users WHERE username = ? LIMIT 1";
sql::PreparedStatement *prepStmt = con->prepareStatement(sql);
prepStmt->setString(1, me);

if (res->next())
{
Json::Value user;
int avatar = res->getInt("avatar");
std::string friends = res->getString("friends");
std::string req = res->getString("req");
sql::ResultSet *res = prepStmt->executeQuery();

// 使用lambda函数来查询用户信息
auto fetchUserInfo = [&](const std::string &token) -> Json::Value
{
Json::Value info;
info["username"] = token;
std::string sql = "SELECT * FROM users WHERE username = ? LIMIT 1";
sql::PreparedStatement *prepStmt = con->prepareStatement(sql);
prepStmt->setString(1, token);
sql::ResultSet *res = prepStmt->executeQuery();
if (res->next())
{
info["avatar"] = res->getInt("avatar");
}
return info;
};

Json::Value friends_array(Json::arrayValue);
Json::Value req_array(Json::arrayValue);
std::stringstream sf(friends);
std::stringstream sr(req);
std::string token;

// 处理好友列表
while (std::getline(sf, token, ','))
{
Json::Value afriend = fetchUserInfo(token);
friends_array.append(afriend);
}
if (res->next())
{
Json::Value user;
int avatar = res->getInt("avatar");
std::string friends = res->getString("friends");
std::string req = res->getString("req");

// 处理请求列表
while (std::getline(sr, token, ','))
// 使用lambda函数来查询用户信息
auto fetchUserInfo = [&](const std::string &token) -> Json::Value
{
Json::Value info;
info["username"] = token;
std::string sql = "SELECT * FROM users WHERE username = ? LIMIT 1";
sql::PreparedStatement *prepStmt = con->prepareStatement(sql);
prepStmt->setString(1, token);
sql::ResultSet *res = prepStmt->executeQuery();
if (res->next())
{
Json::Value areq = fetchUserInfo(token);
req_array.append(areq);
info["avatar"] = res->getInt("avatar");
}
return info;
};

user["avatar"] = avatar;
user["friends"] = friends_array;
user["req"] = req_array;
Json::Value friends_array(Json::arrayValue);
Json::Value req_array(Json::arrayValue);
std::stringstream sf(friends);
std::stringstream sr(req);
std::string token;

Json::StreamWriterBuilder builder;
std::string userJson = Json::writeString(builder, user);
// 处理好友列表
while (std::getline(sf, token, ','))
{
Json::Value afriend = fetchUserInfo(token);
friends_array.append(afriend);
}

json[me] = user;
// 处理请求列表
while (std::getline(sr, token, ','))
{
Json::Value areq = fetchUserInfo(token);
req_array.append(areq);
}

user["avatar"] = avatar;
user["friends"] = friends_array;
user["req"] = req_array;

Json::StreamWriterBuilder builder;
std::string userJson = Json::writeString(builder, user);

json[me] = user;
}

}
}
catch (sql::SQLException &e)
{
Expand Down Expand Up @@ -431,7 +430,7 @@ bool sql_check(std::string user, std::string passwd)
Json::Value sql_find_my_msg(std::string me, std::string connect_type)

{
//std::cout << "login user: " << me << std::endl;
// std::cout << "login user: " << me << std::endl;
try
{
sql::mysql::MySQL_Driver *driver = sql::mysql::get_mysql_driver_instance();
Expand Down Expand Up @@ -474,7 +473,7 @@ Json::Value sql_find_my_msg(std::string me, std::string connect_type)

while (res->next())
{
//update isread to 1
// update isread to 1
if (connect_type == "new")
{
id = res->getInt("id");
Expand Down
14 changes: 7 additions & 7 deletions mysql.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#ifndef _MYSQL_H_
#ifndef _MYSQL_H_
#define _MYSQL_H_
#include <jdbc/mysql_driver.h>
#include <jdbc/mysql_connection.h>
#include <json/json.h>

void sql_unlocked(std::string DeleteName);
void sql_add(std::string username, std::string passwd, int avatar);
bool sql_check(std::string , std::string passwd="@DEFAULT@");
void sql_addhistory(std::string,std::string,std::string,std::string);
bool sql_check(std::string, std::string passwd = "@DEFAULT@");
void sql_addhistory(std::string, std::string, std::string, std::string);
void sql_addconnect(std::string connectptr);

void sql_addrequest(std::string send,std::string receiver);
void sql_process_request(std::string ,std::string,std::string);
Json::Value sql_find_my_msg(std::string,std::string);
void sql_addrequest(std::string send, std::string receiver);
void sql_process_request(std::string, std::string, std::string);
Json::Value sql_find_my_msg(std::string, std::string);
int lockcheck(std::string filename);
Json::Value get_my_info(std::string);
void sql_delete_operation(std::string,std::string);
void sql_delete_operation(std::string, std::string);
void set_avatar(std::string person, int avatar);
int sql_findexist(std::string receiver);
#endif
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions user_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
#include <drogon/drogon.h>
using namespace drogon;

typedef void (*HandlerFunc)(const Json::Value&, std::string* str, int*);
typedef void (*HandlerFunc)(const Json::Value &, std::string *str, int *);

void Handle(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, HandlerFunc handler);
void registerUser(const Json::Value& req_json, std::string* msg, int* code);
void loginUser(const Json::Value& req_json, std::string* msg, int* code);
void registerUser(const Json::Value &req_json, std::string *msg, int *code);
void loginUser(const Json::Value &req_json, std::string *msg, int *code);
void avatar(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback);
#endif