-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
158 lines (140 loc) · 3.48 KB
/
client.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
#include "Utils/dh.hpp"
#include "Utils/rc4.hpp"
#include "Utils/prompt.hpp"
#include "Sockets/Client.hpp"
using namespace std;
string username;
ll privateKey;
ll secretKey;
bool secure;
// GLOBAL CLIENT OBJECT
Client client(PORT, IP_ADDRESS);
// UTILITY FUNCTIONS
string removeUsername(string msg) {
string name;
int i = 0;
while (i < msg.length() && msg[i] != '$') {
name.push_back(msg[i]);
i++;
}
i++;
if (i >= msg.length()) {
// SERVER MESSAGE
string temp = msg;
encrypt(temp, to_string(secretKey));
return temp;
}
string newMsg = msg.substr(i);
if(newMsg != "goodbye" && newMsg != "close") {
cout << MAG << name << NRM << ": ";
}
return newMsg;
}
void getPublicKeys(string s) {
string strG = "", strP = "";
bool genP = false;
for(int i = 0; i < s.size(); i++) {
if(s[i] == ' ') {
genP = true;
}
if(genP) {
strG += s[i];
}
else {
strP += s[i];
}
}
setPrimitiveKeys(stoll(strG), stoll(strP));
}
void exitHandler(int sig) {
client.sendMessage("close");
client.disconnect();
exit(0);
}
// MESSAGE HANDLERS
void *receiver(void *arg) {
while (true) {
string message = client.receiveMessage();
if (secure) {
message = removeUsername(message);
if(message != "goodbye" && message != "close" && message != "handshake" && message != "keys") {
encrypt(message, to_string(secretKey));
}
}
if (message == "close" ) {
cout << "Server closed the connection." << endl;
exit(0);
}
else if (message == "goodbye") {
secure = false;
secretKey = 0;
continue;
}
else if (message == "handshake") {
privateKey = getPrivateKey();
client.sendMessage("key " + to_string(createPublicKey(privateKey)));
continue;
}
else if (message == "keys") {
string B = client.receiveMessage();
secretKey = createSecretKey(stoll(B), privateKey);
secure = true;
continue;
}
cout << message << endl;
}
}
void *sender(void *arg) {
while (true) {
string message;
getline(cin, message);
string user = "[" + username + "]: ";
if (message.size()) {
cout << GOUP << left << CYN << user << NRM << message << endl;
}
if (message == "clear") {
cout << CLR;
continue;
}
if (secure && message != "status" && message != "goodbye" && message != "close") {
encrypt(message, to_string(secretKey));
}
client.sendMessage(message);
// sleep(0.1);
if (message == "close") {
exit(0);
}
}
}
int main() {
srand(time(NULL));
signal(SIGINT, exitHandler);
// CONNECT TO SERVER
client.connectServer();
cout <<GRN<<"Connected to server."<<NRM<<endl;
getPublicKeys(client.receiveMessage());
// RECEIVE A VALID USERNAME: ONE THAT DOES NOT EXIST
int redo = 0;
do {
if(redo == 1) {
cout<<"Username Already Exists!"<<endl;
}
cout << "Enter username: ";
cin >> username;
client.sendMessage(username);
if(redo == 0) {
redo++;
}
} while(client.receiveMessage() == "REDO USERNAME.\n");
// PRINT THE WELCOME MESSAGE AND SHOW INSTRUCTIONS
cout<<GRN<<"Username Accepted"<<NRM<<endl;
cout<<readme(username)<<endl;
// USE SEPARATE THREADS TO RECEIVE AND SEND MESSAGES
pthread_t receive_t, send_t;
pthread_create(&receive_t, NULL, receiver, NULL);
pthread_create(&send_t, NULL, sender, NULL);
pthread_join(receive_t, NULL);
pthread_join(send_t, NULL);
client.disconnect();
return 0;
}