-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
217 lines (167 loc) · 9.14 KB
/
main.cpp
File metadata and controls
217 lines (167 loc) · 9.14 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <iostream>
#include <winsock2.h>
#include <lmcons.h>
#include <vector>
#include <thread>
#include <string>
void runServer();
char * translateEncryptionKey(std::string encryptionKey);
std::string XOR(std::string data, const char key[]);
std::vector<std::string> keysArray;
std::string encryptionKey;
std::string username;
std::string broadcastAddress = "192.168.1.255";
int main() {
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
// Get username
TCHAR name [ UNLEN + 1 ];
DWORD size = UNLEN + 1;
if (GetUserName( (TCHAR*)name, &size )) {
username = name;
}
time_t now = time(0);
tm* ltm = localtime(&now);
encryptionKey = username + std::to_string(ltm->tm_sec);
std::cout << " ██████╗ ██╗ ██╗███╗ ██╗ █████╗ ███╗ ███╗██╗ ██████╗\n"
" ██╔══██╗╚██╗ ██╔╝████╗ ██║██╔══██╗████╗ ████║██║██╔════╝\n"
" ██║ ██║ ╚████╔╝ ██╔██╗ ██║███████║██╔████╔██║██║██║ \n"
" ██║ ██║ ╚██╔╝ ██║╚██╗██║██╔══██║██║╚██╔╝██║██║██║ \n"
" ██████╔╝ ██║ ██║ ╚████║██║ ██║██║ ╚═╝ ██║██║╚██████╗\n"
" ╚═════╝ ╚═╝ ╚═╝ ╚═══╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═════╝\n"
" \n"
" ██████╗██╗ ██╗ █████╗ ████████╗ \n"
" ██╔════╝██║ ██║██╔══██╗╚══██╔══╝ \n"
" ██║ ███████║███████║ ██║ \n"
" ██║ ██╔══██║██╔══██║ ██║ \n"
" ╚██████╗██║ ██║██║ ██║ ██║ \n"
" ╚═════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ v1\n\n\n" << std::endl;
std::cout << "* Votre mot de passe est '" + encryptionKey + "'\n"
<< "* Les autre utilisateurs doivent faire 'addmdp " + encryptionKey + " pour pouvoir communiquer avec vous.\n"
<< "* Pour pouvoir reçevoir les messages des autres utilisateurs faites 'addmdp leurmotdepasse'\n"
<< "* Vous pouvez changer votre mot de passe avec la commande 'mdp votrenouveaumdp'\n" << std::endl;
std::thread serverThread(runServer);
while (true) {
// Initialize WinSock
WSAData wsaData{};
// Start WSA
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
std::cout << "An error occurred when starting WSA." << std::endl;
return 1;
}
// Create ClientSocket
auto clientSocket = INVALID_SOCKET;
clientSocket = socket(AF_INET, SOCK_DGRAM, 0);
if (clientSocket == INVALID_SOCKET) {
std::cout << "An error occurred when creating client socket." << std::endl;
WSACleanup();
return 1;
}
// Allow for broadcast sending
char broadcast = '1';
if (setsockopt(clientSocket, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast)) < 0) {
std::cout << "An error occurred when changing broadcast settings." << std::endl;
closesocket(clientSocket);
WSACleanup();
}
// Configure for broadcast send on port 888
struct sockaddr_in clientSocketConfig{};
clientSocketConfig.sin_family = AF_INET;
clientSocketConfig.sin_port = htons(888);
clientSocketConfig.sin_addr.s_addr = inet_addr(broadcastAddress.c_str());
std::string userMessage;
std::getline(std::cin, userMessage);
if (userMessage.find("mdp ") == 0) {
encryptionKey = userMessage.substr(4, userMessage.length());
std::cout << "*\n"
<< "* Votre nouveau mot de passe est désormait '"
<< encryptionKey
<< "'\n"
<< "*\n" << std::endl;
} else if (userMessage.find("addmdp ") == 0) {
std::string addKey = userMessage.substr(7, userMessage.length());
keysArray.push_back(addKey);
std::cout << "*\n"
<< "* La clé '"
<< addKey
<< "' a était ajouté\n"
<< "*\n"
<< std::endl;
} else if (userMessage.find("broadcastIp ") == 0) {
broadcastAddress = userMessage.substr(12, userMessage.length());
std::cout << "*\n"
<< "* L'addresse de broadcast est désormait '"
<< broadcastAddress + "'"
<< "*\n"
<< std::endl;
} else {
std::string message = XOR("IDENT=" + encryptionKey + "USER=" + username + "," + userMessage, translateEncryptionKey(encryptionKey));
// Send UDP packet to broadcast
sendto(clientSocket, message.c_str(), strlen(message.c_str()) + 1, 0, (sockaddr *) &clientSocketConfig,
sizeof(clientSocketConfig));
// Cleanup
closesocket(clientSocket);
WSACleanup();
}
}
}
void runServer() {
while (true) {
// Initialize WinSock
WSAData wsaData{};
// Start WSA
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
std::cout << "An error occurred when starting WSA." << std::endl;
}
// Create ServerSocket
auto serverSocket = INVALID_SOCKET;
serverSocket = socket(AF_INET, SOCK_DGRAM, 0);
if (serverSocket == INVALID_SOCKET) {
std::cout << "An error occurred when creating server socket." << std::endl;
WSACleanup();
}
// Allow for broadcast receiving
char broadcast = '1';
if (setsockopt(serverSocket, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast)) < 0) {
std::cout << "An error occurred when changing broadcast settings." << std::endl;
closesocket(serverSocket);
WSACleanup();
}
// Configure for broadcast receive on port 888
struct sockaddr_in serverSocketConfig{};
struct sockaddr_in clientSocketConfig{};
int len = sizeof(struct sockaddr_in);
char recvbuff[65535];
int recvbufflen = 65535;
serverSocketConfig.sin_family = AF_INET;
serverSocketConfig.sin_port = htons(888);
serverSocketConfig.sin_addr.s_addr = INADDR_ANY;
// Bind client socket to listen for broadcasts
if (bind(serverSocket, (sockaddr *) &serverSocketConfig, sizeof(serverSocketConfig)) < 0) {
std::cout << "An error occurred when binding server socket." << std::endl;
closesocket(serverSocket);
WSACleanup();
}
// Receive incoming data from ClientSocket
recvfrom(serverSocket, recvbuff, recvbufflen, 0, (sockaddr *) &clientSocketConfig, &len);
std::string decryptedString;
for (const auto & i : keysArray) {
decryptedString = XOR(recvbuff, translateEncryptionKey(i));
if (decryptedString.substr(0, i.length() + 6) == "IDENT=" + i) {
// Get current time
time_t now = time(0);
tm* ltm = localtime(&now);
std::string time = std::to_string(ltm->tm_hour) + ":" + std::to_string(ltm->tm_min) + ":" + std::to_string(ltm->tm_sec);
// Get sender's username & message cleanly
std::string cleanMessage = decryptedString.substr(decryptedString.find(',') + 1, decryptedString.length());
std::string displayUsername = decryptedString.substr(i.length() + 11, (decryptedString.length() - 11 - i.length()) - (cleanMessage.length() + 1));
std::cout << time + " | " + displayUsername + ": " + cleanMessage << std::endl;
}
}
// Cleanup
closesocket(serverSocket);
WSACleanup();
}
}
char * translateEncryptionKey(std::string encryptionKey) { const int length = encryptionKey.length(); char* char_array = new char[length + 1]; strcpy(char_array, encryptionKey.c_str()); return char_array; }
std::string XOR(std::string data, const char key[]) { std::string xorstring = data; for (int i = 0; i < xorstring.size(); i++) { xorstring[i] = data[i] ^ key[i % (sizeof(key) / sizeof(char))]; } return xorstring; }