initial commit
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
build
|
||||
cmake-build-debug
|
||||
@@ -0,0 +1,11 @@
|
||||
cmake_minimum_required (VERSION 2.6)
|
||||
project(openrouter)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
include_directories(./src/libs/)
|
||||
|
||||
add_executable(
|
||||
openrouter
|
||||
src/openrouter.cpp
|
||||
src/security/cors.cpp
|
||||
)
|
||||
Executable
+77
@@ -0,0 +1,77 @@
|
||||
#ifndef C_COLORS_H
|
||||
#define C_COLORS_H
|
||||
|
||||
// reset
|
||||
#define RESET "\033[0m"
|
||||
|
||||
// regular colrs
|
||||
#define GREEN "\033[0;32m" // GREEN
|
||||
#define BLUE "\033[0;34m" // BLUE
|
||||
#define YELLOW "\033[1;33m" // YELLOW
|
||||
#define RED "\033[0;31m" // RED
|
||||
#define BLACK "\033[0;30m" // BLACK
|
||||
#define PURPLE "\033[0;35m" // PURPLE
|
||||
#define CYAN "\033[0;36m" // CYAN
|
||||
#define WHITE "\033[0;37m" // WHITE
|
||||
|
||||
// bold
|
||||
#define BBLACK "\033[1;30m" // BLACK
|
||||
#define BRED "\033[1;31m" // RED
|
||||
#define BGREEN "\033[1;32m" // GREEN
|
||||
#define BYELLOW "\033[1;33m" // YELLOW
|
||||
#define BBLUE "\033[1;34m" // BLUE
|
||||
#define BPURPLE "\033[1;35m" // PURPLE
|
||||
#define BCYAN "\033[1;36m" // CYAN
|
||||
#define BWHITE "\033[1;37m" // WHITE
|
||||
|
||||
// Underline
|
||||
#define UBLACK "\033[4;30m" // BLACK
|
||||
#define URED "\033[4;31m" // RED
|
||||
#define UGREEN "\033[4;32m" // GREEN
|
||||
#define UYELLOW "\033[4;33m" // YELLOW
|
||||
#define UBLUE "\033[4;34m" // BLUE
|
||||
#define UPURPLE "\033[4;35m" // PURPLE
|
||||
#define UCYAN "\033[4;36m" // CYAN
|
||||
#define UWHITE "\033[4;37m" // WHITE
|
||||
|
||||
// Background
|
||||
#define ON_BLACK "\033[40m" // BLACK
|
||||
#define ON_RED "\033[41m" // RED
|
||||
#define ON_GREEN "\033[42m" // GREEN
|
||||
#define ON_YELLOW "\033[43m" // YELLOW
|
||||
#define ON_BLUE "\033[44m" // BLUE
|
||||
#define ON_PURPLE "\033[45m" // PURPLE
|
||||
#define ON_CYAN "\033[46m" // CYAN
|
||||
#define ON_WHITE "\033[47m" // WHITE
|
||||
|
||||
// High Intensity
|
||||
#define IBLACK "\033[0;90m" // BLACK
|
||||
#define IRED "\033[0;91m" // RED
|
||||
#define IGREEN "\033[0;92m" // GREEN
|
||||
#define IYELLOW "\033[0;93m" // YELLOW
|
||||
#define IBLUE "\033[0;94m" // BLUE
|
||||
#define IPURPLE "\033[0;95m" // PURPLE
|
||||
#define ICYAN "\033[0;96m" // CYAN
|
||||
#define IWHITE "\033[0;97m" // WHITE
|
||||
|
||||
// Bold High Intensity
|
||||
#define BIBLACK "\033[1;90m" // BLACK
|
||||
#define BIRED "\033[1;91m" // RED
|
||||
#define BIGREEN "\033[1;92m" // GREEN
|
||||
#define BIYELLOW "\033[1;93m" // YELLOW
|
||||
#define BIBLUE "\033[1;94m" // BLUE
|
||||
#define BIPURPLE "\033[1;95m" // PURPLE
|
||||
#define BICYAN "\033[1;96m" // CYAN
|
||||
#define BIWHITE "\033[1;97m" // WHITE
|
||||
|
||||
// High Intensity backgrounds
|
||||
#define ON_IBLACK "\033[0;100m" // BLACK
|
||||
#define ON_IRED "\033[0;101m" // RED
|
||||
#define ON_IGREEN "\033[0;102m" // GREEN
|
||||
#define ON_IYELLOW "\033[0;103m" // YELLOW
|
||||
#define ON_IBLUE "\033[0;104m" // BLUE
|
||||
#define ON_IPURPLE "\033[0;105m" // PURPLE
|
||||
#define ON_ICYAN "\033[0;106m" // CYAN
|
||||
#define ON_IWHITE "\033[0;107m" // WHITE
|
||||
|
||||
#endif // C_COLORS_H
|
||||
Executable
+9788
File diff suppressed because it is too large
Load Diff
Executable
+24766
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,22 @@
|
||||
#include <iostream>
|
||||
#include <httplib.h>
|
||||
#include <colors.h>
|
||||
|
||||
#define CORS_ENABLE
|
||||
#include "security/cors.hpp"
|
||||
|
||||
int main() {
|
||||
httplib::Server srv;
|
||||
|
||||
srv.Options("/(.*)", set_cors_headers);
|
||||
|
||||
srv.set_mount_point("/" , "../src/web");
|
||||
srv.set_mount_point("/js", "../src/web/js");
|
||||
srv.set_mount_point("/css", "../src/web/css");
|
||||
|
||||
std::cout << GREEN << "Starting" << RESET << " server on [ " << YELLOW
|
||||
<< "localhost" << RESET << ":" << CYAN << "43243" << RESET << " ]" << std::endl;
|
||||
|
||||
srv.listen("localhost", 43244);
|
||||
return 0;
|
||||
}
|
||||
Executable
+9
@@ -0,0 +1,9 @@
|
||||
#include "cors.hpp"
|
||||
#include <colors.h>
|
||||
|
||||
void set_cors_headers(const httplib::Request &request, httplib::Response &response) {
|
||||
std::cout << GREEN << request.path << " " << std::endl;
|
||||
response.set_header("Access-Control-Allow-Origin", "*");
|
||||
response.set_header("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
|
||||
response.set_header("Access-Control-Allow-Headers", "Authorization,X-Custom,Content-Type");
|
||||
}
|
||||
Executable
+13
@@ -0,0 +1,13 @@
|
||||
#ifndef CORS_HPP
|
||||
#define CORS_HPP
|
||||
|
||||
#include <httplib.h>
|
||||
|
||||
#ifdef CORS_ENABLE
|
||||
|
||||
// function to set cors headers
|
||||
void set_cors_headers(const httplib::Request& request, httplib::Response& response);
|
||||
|
||||
#endif // CORS_ENABLE
|
||||
|
||||
#endif // CORS_HPP
|
||||
@@ -0,0 +1,107 @@
|
||||
.form {
|
||||
width: 100vw;
|
||||
height: 100vh;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
pointer-events: none;
|
||||
padding: 10px;
|
||||
transform: scale(0.5);
|
||||
opacity: 0;
|
||||
transition: 0.3s;
|
||||
position: absolute;
|
||||
margin: auto;
|
||||
}
|
||||
|
||||
.form.active {
|
||||
pointer-events: all;
|
||||
opacity: 1;
|
||||
transform: scale(1);
|
||||
}
|
||||
|
||||
.formbody {
|
||||
width: 50%;
|
||||
height: 50%;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 10px;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background-color: hsl(from black h s 20%);
|
||||
opacity: 0.7;
|
||||
border-radius: 20px;
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
.formheader {
|
||||
width: 100%;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.forminputbody {
|
||||
width: 50%;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.forminputlabel {
|
||||
text-align: center;
|
||||
font-style: normal;
|
||||
font-size: 20px;
|
||||
margin: 5px 0;
|
||||
}
|
||||
|
||||
.forminput {
|
||||
padding: 10px;
|
||||
border-radius: 10px;
|
||||
border: 2px solid;
|
||||
transition: 0.2s;
|
||||
background-color: inherit;
|
||||
}
|
||||
|
||||
.forminput:focus {
|
||||
border: 5px solid;
|
||||
}
|
||||
|
||||
.formbutton {
|
||||
padding: 10px;
|
||||
border-radius: 10px;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
transition: 0.3s;
|
||||
}
|
||||
|
||||
.formbutton:hover {
|
||||
opacity: 0.6;
|
||||
}
|
||||
|
||||
@media screen and (max-width: 800px) {
|
||||
.forminputbody {
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
|
||||
@media screen and (max-width: 800px) {
|
||||
.formbody {
|
||||
height: 60%;
|
||||
}
|
||||
}
|
||||
|
||||
@media screen and (max-width: 500px) {
|
||||
.formbody {
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
|
||||
@media screen and (max-width: 300px) {
|
||||
.formbody {
|
||||
height: 60%;
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
|
||||
@media screen and (max-width: 500px) {
|
||||
.formheader {
|
||||
font-size: 20px;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
:root {
|
||||
color-scheme: light dark;
|
||||
/* --font-color: light-dark(black, white); */
|
||||
}
|
||||
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
width: 100vw;
|
||||
height: 100vh;
|
||||
/* background-color: light-dark(white, black); */
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="stylesheet" href="css/index.css">
|
||||
<link rel="stylesheet" href="css/form.css">
|
||||
<title>Openrouter Admin</title>
|
||||
</head>
|
||||
<body>
|
||||
<div class="form loginform">
|
||||
<form class="formbody loginformbody">
|
||||
<h1 class="formheader">OpenRouter Login</h1>
|
||||
<div class="forminputbody">
|
||||
<label for="loginusernameinput" class="forminputlabel">Username</label>
|
||||
<input type="text" required id="loginusernameinput" class="forminput">
|
||||
</div>
|
||||
<div class="forminputbody">
|
||||
<label for="loginpasswordinput" class="forminputlabel">Username</label>
|
||||
<input type="password" required id="loginpasswordinput" class="forminput">
|
||||
</div>
|
||||
<div class="forminputbody">
|
||||
<button class="formbutton">Login</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<button class="exitbutton">exit</button>
|
||||
|
||||
<script src="js/routes.js"></script>
|
||||
<script src="js/login.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,57 @@
|
||||
const loginform = document.querySelector('.loginform')
|
||||
const loginformbody = document.querySelector('.loginformbody')
|
||||
const loginusername = document.querySelector("#loginusernameinput")
|
||||
const loginpassword = document.querySelector("#loginpasswordinput")
|
||||
const exitbutton = document.querySelector('.exitbutton')
|
||||
|
||||
console.log(localStorage.getItem("token"))
|
||||
|
||||
if (localStorage.getItem("token") === null) {
|
||||
loginform.classList.add('active')
|
||||
// loginform.style.background = "initial"
|
||||
}
|
||||
|
||||
loginformbody.addEventListener('submit', (e) => {
|
||||
e.preventDefault()
|
||||
login(loginusername.value, loginpassword.value)
|
||||
})
|
||||
|
||||
const login = async (username, password) => {
|
||||
const response = await fetch(routes.login(), {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
// 'Access-Control-Allow-Origin': '*',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
// mode: 'no-cors',
|
||||
body: `{"username":"${username}","password":"${password}"}`
|
||||
}).then(data => data.json()).then(jsondata => jsondata).catch((error) => {
|
||||
if (error.message === "Unexpected end of JSON input") {
|
||||
console.log("error")
|
||||
// notification(`Ошибка на сервере: Создайте файл users.json и запишите в него первого пользователя`, "error")
|
||||
} else {
|
||||
console.log("error")
|
||||
// notification(`Ошибка на сервере: ${error}`, "error")
|
||||
}
|
||||
})
|
||||
if (response.status === "403") {
|
||||
console.log("Неправильный логин или пароль")
|
||||
// notification("Неправильный логин или пароль", "error")
|
||||
}
|
||||
if (response.token) {
|
||||
// notification("Успешно", "success")
|
||||
loginform.classList.remove("active")
|
||||
localStorage.setItem("token", response.token)
|
||||
await get_user_data()
|
||||
loginforminputusername.value = ""
|
||||
loginforminputpassword.value = ""
|
||||
}
|
||||
}
|
||||
|
||||
exitbutton.addEventListener('click', () => {
|
||||
localStorage.clear()
|
||||
loginform.classList.add('active')
|
||||
loginusername.value = ""
|
||||
loginpassword = ""
|
||||
})
|
||||
@@ -0,0 +1,13 @@
|
||||
const host = 'http://localhost:43243'
|
||||
const prefix = 'api'
|
||||
|
||||
const routes = {
|
||||
login: () => [host, prefix, 'token'].join('/'),
|
||||
// user: () => [host, prefix, 'users', 'user'].join('/'),
|
||||
// all_users: () => [host, prefix, 'users', 'all'].join('/'),
|
||||
// access: () => [host, prefix, 'access'].join('/'),
|
||||
// adduser: () => [host, prefix, 'users', 'add'].join('/'),
|
||||
// deluser: () => [host, prefix, 'users', 'del'].join('/'),
|
||||
// change_password_url: () => [host, prefix, 'users', 'user', 'password', 'change'].join('/'),
|
||||
// change_user: () => [host, prefix, 'users', 'user', 'change'].join('/')
|
||||
}
|
||||
Reference in New Issue
Block a user