-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.cpp
More file actions
216 lines (182 loc) · 7.03 KB
/
client.cpp
File metadata and controls
216 lines (182 loc) · 7.03 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
#include <iostream>
#include <string>
#include <thread>
#include <cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#include <atomic>
using namespace std;
// Simple XOR encryption/decryption (must match server)
namespace Encryption
{
const string KEY = "OpticomSecureKey2025"; // Shared key (must match server)
string encrypt(const string &plaintext)
{
string result = plaintext;
for (size_t i = 0; i < result.length(); ++i)
{
result[i] ^= KEY[i % KEY.length()];
}
return result;
}
string decrypt(const string &ciphertext)
{
return encrypt(ciphertext); // XOR is symmetric
}
}
static const char* CLR_RESET = "\033[0m";
static const char* CLR_INFO = "\033[36m"; // cyan
static const char* CLR_WARN = "\033[33m"; // yellow
static const char* CLR_ERR = "\033[31m"; // red
static const char* CLR_PM = "\033[35m"; // magenta
static const char* CLR_ME = "\033[32m"; // green
static atomic<bool> g_running(true);
static void printBanner() {
cout << CLR_INFO;
cout << "\n================================================\n";
cout << " \n";
cout << " OPTICOM CLIENT \n";
cout << " This Chat is end to end encrypted \n";
cout << " \n";
cout << "================================================\n";
cout << CLR_RESET;
cout << CLR_WARN << "Commands: " << CLR_RESET
<< "/list /rooms /join /pm /block /help /clear /quit\n\n";
}
static void printPrompt(const string& prompt) {
cout << CLR_ME << prompt << CLR_RESET << flush;
}
void receiveMessages(int sock, const string promptLabel) {
char buffer[1024];
while (true) {
memset(buffer, 0, sizeof(buffer));
ssize_t bytes = recv(sock, buffer, sizeof(buffer) - 1, 0);
if (bytes <= 0) {
cout << "\r\033[K" << CLR_ERR << "╔═══════════════════════════════╗\n";
cout << "║ Disconnected from server ║\n";
cout << "╚═══════════════════════════════╝" << CLR_RESET << endl;
close(sock);
g_running = false;
return;
}
// Decrypt message
string encrypted(buffer, bytes);
string line = Encryption::decrypt(encrypted);
// Remove trailing newlines/carriage returns from the decrypted message
while (!line.empty() && (line.back() == '\n' || line.back() == '\r')) {
line.pop_back();
}
// Clear current line and move to new line before printing message
cout << "\r\033[K";
// Enhanced color heuristics with better formatting
if (line.find("[SERVER]") != string::npos) {
cout << CLR_INFO << "┃ " << line << CLR_RESET << endl;
} else if (line.find("[PM from ") != string::npos) {
cout << CLR_PM << "✉ " << line << CLR_RESET << endl;
} else if (line.find("joined") != string::npos) {
cout << CLR_INFO << "→ " << line << CLR_RESET << endl;
} else if (line.find("left") != string::npos) {
cout << CLR_WARN << "← " << line << CLR_RESET << endl;
} else if (line.find("Rate limit") != string::npos || line.find("blocked") != string::npos) {
cout << CLR_ERR << "⚠ " << line << CLR_RESET << endl;
} else if (line.find("Blocked user") != string::npos || line.find("Unblocked") != string::npos) {
cout << CLR_WARN << "🚫 " << line << CLR_RESET << endl;
} else if (line.find("pinned") != string::npos) {
cout << CLR_INFO << "📌 " << line << CLR_RESET << endl;
} else if (line.find("Usage:") != string::npos || line.find("Commands:") != string::npos) {
cout << CLR_WARN << line << CLR_RESET << endl;
} else {
cout << line << endl;
}
// Repaint prompt
printPrompt(promptLabel);
}
}
int main(int argc, char* argv[]) {
signal(SIGPIPE, SIG_IGN);
if (argc != 2 && argc != 3) {
cerr << "Usage: ./client [server_ip] <port>" << endl;
cerr << "Example (local): ./client 8080" << endl;
cerr << "Example (remote): ./client 192.168.1.105 8080" << endl;
return 1;
}
string serverIp = "127.0.0.1";
int port;
try {
if (argc == 2) {
port = stoi(argv[1]);
} else {
serverIp = argv[1];
port = stoi(argv[2]);
}
if (port < 1 || port > 65535) {
cerr << "Error: Port must be between 1 and 65535" << endl;
return 1;
}
} catch (const exception &e) {
cerr << "Error: Invalid port number" << endl;
return 1;
}
printBanner();
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
cerr << "Failed to create socket." << endl;
return 1;
}
struct sockaddr_in serverAddr{};
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(port);
inet_pton(AF_INET, serverIp.c_str(), &serverAddr.sin_addr);
if (connect(sock, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) < 0) {
cerr << "Connection failed to " << serverIp << ":" << port << endl;
close(sock);
return 1;
}
string username;
cout << "Enter your username: ";
getline(cin, username);
if (username.empty()) username = "Anonymous";
// Limit username length to prevent buffer issues (server expects max 63 bytes)
if (username.length() > 63) {
username = username.substr(0, 63);
cout << "Warning: Username truncated to 63 characters" << endl;
}
send(sock, username.c_str(), username.size(), 0);
cout << "Connected to server (" << serverIp << ":" << port << ") as " << username << endl;
cout << "Type messages below. Use /quit to disconnect & /list to see people who are online." << endl;
string promptLabel = username + " > ";
thread receiver(receiveMessages, sock, promptLabel);
receiver.detach();
string message;
while (true) {
printPrompt(promptLabel);
if (!getline(cin, message)) {
cout << endl;
break;
}
if (message == "/quit") {
cout << "Disconnecting..." << endl;
close(sock);
return 0;
}
if (message == "/clear") {
cout << "\033[2J\033[H";
printBanner();
continue;
}
if (message.empty()) continue;
// Encrypt message before sending
string encrypted = Encryption::encrypt(message);
ssize_t sent = send(sock, encrypted.c_str(), encrypted.size(), 0);
if (sent < 0) {
cerr << "Send failed." << endl;
close(sock);
return 1;
}
if (!g_running.load()) break;
}
return 0;
}