Cursed but it has login and multiple user signon
[link.git] / Source / main.cpp
blobaa673c0560f67bea6b906d318921a909c0a5aff7
1 #include <iostream>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <filesystem>
5 #include <sys/socket.h>
6 #include <netinet/in.h>
7 #include <netinet/tcp.h>
8 #include <arpa/inet.h>
9 #include <unistd.h>
10 #include "../Includes/Log.hpp"
11 #include "../Includes/Client.hpp"
13 int main() {
14 Log::SetDebugMode(false);
15 Log::SetVerboseMode(true);
16 try {
17 std::filesystem::create_directories("/var/log/link/");
18 Log::SetSaveLogs(true);
19 time_t now = time(0);
20 tm *ltm = localtime(&now);
21 Log::SetLogFile("/var/log/link/link_" + std::to_string(ltm->tm_year + 1900) + "-" + std::to_string(ltm->tm_mon + 1) + "-" + std::to_string(ltm->tm_mday) + ".log");
22 } catch (std::filesystem::filesystem_error& e) {
23 Log::Error("\033[31mLogs will not be stored please run as root!");
25 InitializeDatabase();
26 int ServerSocket = socket(AF_INET, SOCK_STREAM, 0);
27 if (ServerSocket == 0) {
28 Log::Error("\033[31mCould not create socket");
29 return 1;
31 int opt = 1;
32 if (setsockopt(ServerSocket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
33 Log::Error("\033[31mThis port is still in use!");
34 return 1;
36 int time = 7200, interval = 60, retry = 3;
37 struct sockaddr_in ServerAddress;
38 ServerAddress.sin_family = AF_INET;
39 ServerAddress.sin_addr.s_addr = INADDR_ANY;
40 ServerAddress.sin_port = htons(3000);
41 if (bind(ServerSocket, (struct sockaddr*)&ServerAddress, sizeof(ServerAddress)) < 0) {
42 Log::Error("\033[31mCould not bind to port 80!");
43 return 1;
45 if (listen(ServerSocket, 50) < 0) {
46 Log::Error("\033[31mCould not listen to port 80!");
47 return 1;
49 Log::Info("\033[36mListening on port \033[32m80");
50 while (true) {
51 char ClientData[1024] = {0};
52 struct sockaddr_in ClientAddress;
53 socklen_t ClientAddressSize = sizeof(ClientAddress);
54 int ClientSocket = accept(ServerSocket, (struct sockaddr*)&ClientAddress, &ClientAddressSize);
55 if (ClientSocket == -1) {
56 Log::Error("\033[31mCould not accept connection from " + std::string(inet_ntoa(ClientAddress.sin_addr)));
57 } else {
58 Client client = Client(ClientSocket, &ClientAddress);
59 pthread_t ClientThread;
60 pthread_create(&ClientThread, NULL, ClientHandler, &client);
61 pthread_join(ClientThread, NULL);
64 shutdown(ServerSocket, SHUT_RDWR);
65 return 0;