5 #include <sys/socket.h>
6 #include <netinet/in.h>
7 #include <netinet/tcp.h>
10 #include "../Includes/Log.hpp"
11 #include "../Includes/Client.hpp"
14 Log::SetDebugMode(false);
15 Log::SetVerboseMode(true);
17 std::filesystem::create_directories("/var/log/link/");
18 Log::SetSaveLogs(true);
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!");
26 int ServerSocket
= socket(AF_INET
, SOCK_STREAM
, 0);
27 if (ServerSocket
== 0) {
28 Log::Error("\033[31mCould not create socket");
32 if (setsockopt(ServerSocket
, SOL_SOCKET
, SO_REUSEADDR
, &opt
, sizeof(opt
)) < 0) {
33 Log::Error("\033[31mThis port is still in use!");
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!");
45 if (listen(ServerSocket
, 50) < 0) {
46 Log::Error("\033[31mCould not listen to port 80!");
49 Log::Info("\033[36mListening on port \033[32m80");
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
)));
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
);