181 lines
5.6 KiB
C++
181 lines
5.6 KiB
C++
#pragma once
|
|
#include <map>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <optional>
|
|
#include "Export.h"
|
|
#include "DateTime.h"
|
|
|
|
#pragma warning(disable : 4455)
|
|
|
|
#define G_SERVER_VERSION "2.0.0"
|
|
|
|
constexpr double G_SERVICE_UPDATE_TIMESPAN = 1000;
|
|
|
|
#define G_SERVER_NAME "U.N.S. Server Core/" G_SERVER_VERSION
|
|
|
|
constexpr auto G_SERVICE_NAME = "UNS_HTTP_ServerCore";
|
|
|
|
constexpr auto G_ERROR_PAGE = R"(
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8"/>
|
|
<title>HTTP {:03d}</title>
|
|
</head>
|
|
<body>
|
|
<center>
|
|
<h1>HTTP ERROR {:03d}</h1>
|
|
<hr/>
|
|
<p>{}</p>
|
|
</center>
|
|
</body>
|
|
</html>
|
|
)";
|
|
|
|
// inline constexpr std::string_view G_HTTP_STD_MONTH[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
|
|
|
|
// inline constexpr std::string_view G_HTTP_STD_WEEK[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
|
|
|
|
namespace Json
|
|
{
|
|
class Value;
|
|
}
|
|
|
|
namespace uns
|
|
{
|
|
enum HTTPMethod
|
|
{
|
|
H_GET = 0b0000000001,
|
|
H_PUT = 0b0000000010,
|
|
H_POST = 0b0000000100,
|
|
H_HEAD = 0b0000001000,
|
|
H_TRACE = 0b0000010000,
|
|
H_PATCH = 0b0000100000,
|
|
H_DELETE = 0b0001000000,
|
|
H_OPTIONS = 0b0010000000,
|
|
H_CONNECT = 0b0100000000,
|
|
H_UNKNOWN = 0b1000000000,
|
|
H_ALL_ENABLED = 0b1111111111
|
|
};
|
|
|
|
namespace cors
|
|
{
|
|
inline constexpr std::string_view reqh_o = "Origin";
|
|
inline constexpr std::string_view reqh_acrm = "Access-Control-Request-Method";
|
|
inline constexpr std::string_view reqh_acrh = "Access-Control-Request-Headers";
|
|
|
|
inline constexpr std::string_view resh_acao = "Access-Control-Allow-Origin";
|
|
inline constexpr std::string_view resh_acam = "Access-Control-Allow-Methods";
|
|
inline constexpr std::string_view resh_acah = "Access-Control-Allow-Headers";
|
|
inline constexpr std::string_view resh_acac = "Access-Control-Allow-Credentials";
|
|
inline constexpr std::string_view resh_acma = "Access-Control-Max-Age";
|
|
};
|
|
|
|
inline constexpr auto url_all = R"(/[\s\S]*)";
|
|
|
|
using POSTArgs = std::map<std::string, std::string>;
|
|
|
|
std::string UNSWSC_DLL_EXPORT EncodeErrorPage(int code);
|
|
std::string UNSWSC_DLL_EXPORT EncodeHTTPTime(time_t* time);
|
|
|
|
std::string UNSWSC_DLL_EXPORT btos(bool val);
|
|
bool UNSWSC_DLL_EXPORT stob(const std::string& obj);
|
|
DateTime UNSWSC_DLL_EXPORT stot(const std::string& obj);
|
|
std::string UNSWSC_DLL_EXPORT RestoreURL(const std::string& url);
|
|
void UNSWSC_DLL_EXPORT Stringsplit(const std::string& str, const std::string& splits, std::vector<std::string>& res);
|
|
void UNSWSC_DLL_EXPORT ProcessPOSTArgs(const std::string& args, POSTArgs& args_output);
|
|
|
|
namespace tools
|
|
{
|
|
//基于SHA3-256,无暴力破解防护能力,不建议使用
|
|
std::string UNSWSC_DLL_EXPORT EncryptPassword(const std::string reg_date, const std::string& password);
|
|
//安全的密码加密函数,基于libsodium
|
|
std::string UNSWSC_DLL_EXPORT EncryptPasswordSodium(const std::string& pwd);
|
|
//密码检查函数
|
|
bool UNSWSC_DLL_EXPORT CheckPasswordSodium(const std::string& pwd, const std::string& encrypted);
|
|
|
|
//安全随机数
|
|
std::string UNSWSC_DLL_EXPORT SecureRandomHex(size_t bytes);
|
|
|
|
std::string UNSWSC_DLL_EXPORT ToUpper(const std::string& s);
|
|
std::string UNSWSC_DLL_EXPORT ToLower(const std::string& s);
|
|
|
|
std::string UNSWSC_DLL_EXPORT CalculateFileHashSHA256(const std::string& file);
|
|
|
|
Json::Value UNSWSC_DLL_EXPORT SafeJsonDecode(const std::string& str);
|
|
|
|
template <typename T, typename CharT = char>
|
|
std::optional<T> UNSWSC_DLL_EXPORT SafeStoX(const std::basic_string<CharT>& str, std::size_t* pos = nullptr, int base = 10);
|
|
template <typename T, typename CharT = char>
|
|
std::optional<T> UNSWSC_DLL_EXPORT SafeStoX(std::basic_string_view<CharT> str, std::size_t* pos = nullptr, int base = 10);
|
|
template <typename T, typename CharT>
|
|
inline std::optional<T> SafeStoX(const CharT* str, std::size_t* pos = nullptr, int base = 10)
|
|
{
|
|
#if defined(__cpp_char8_t)
|
|
if constexpr (std::is_same_v<CharT, char8_t>)
|
|
return SafeStoX<T>(std::basic_string<char>(reinterpret_cast<const char*>(str)), pos, base); // 只有 u8"..." (char8_t) 强制转为 char 版本的 SafeStoX 处理
|
|
else
|
|
#endif
|
|
return SafeStoX<T>(std::basic_string<CharT>(str), pos, base); // 普通 "..." (char) 和 L"..." (wchar_t) 保持各自类型,构造对应的 basic_string
|
|
}
|
|
}
|
|
|
|
namespace secure
|
|
{
|
|
/**
|
|
* @brief 基于物理与逻辑边界的路径穿透(目录穿越)安全校验
|
|
* @param safe_path 允许访问的沙盒根目录(绝对或相对路径均可)
|
|
* @param requested_path 客户端传入的、解码后的目标子路径
|
|
* @return true 安全(在沙盒内);false 不安全(企图穿越或路径非法)
|
|
*/
|
|
bool IsSafePath(const std::string& safe_path, const std::string& requested_path);
|
|
}
|
|
};
|
|
|
|
#undef MIN
|
|
|
|
inline constexpr unsigned long long operator""B(unsigned long long n)
|
|
{
|
|
return n;
|
|
}
|
|
|
|
inline constexpr unsigned long long operator""KB(unsigned long long n)
|
|
{
|
|
return (n * 1024);
|
|
}
|
|
|
|
inline constexpr unsigned long long operator""MB(unsigned long long n)
|
|
{
|
|
return (n * 1024 * 1024);
|
|
}
|
|
|
|
inline constexpr unsigned long long operator""GB(unsigned long long n)
|
|
{
|
|
return (n * 1024 * 1024 * 1024);
|
|
}
|
|
|
|
inline constexpr unsigned long long operator""TB(unsigned long long n)
|
|
{
|
|
return (n * 1024 * 1024 * 1024 * 1024);
|
|
}
|
|
|
|
inline constexpr time_t operator""S(unsigned long long t)
|
|
{
|
|
return t;
|
|
}
|
|
|
|
inline constexpr time_t operator""MIN(unsigned long long t)
|
|
{
|
|
return (t * 60);
|
|
}
|
|
|
|
inline constexpr time_t operator""HOUR(unsigned long long t)
|
|
{
|
|
return (t * 60 * 60);
|
|
}
|
|
|
|
inline constexpr time_t operator""DAY(unsigned long long t)
|
|
{
|
|
return (t * 60 * 60 * 24);
|
|
}
|