-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple.pl
executable file
·332 lines (238 loc) · 8.18 KB
/
simple.pl
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/perl
use warnings;
use strict;
use AnyEvent;
use AnyEvent::Util;
use IO::Socket::INET;
use MIME::Base64;
use Term::ANSIColor qw(:constants);
$AnyEvent::Util::MAX_FORKS = 31;
my %global_user_db = ();
my $server = IO::Socket::INET->new(
'Proto' => 'tcp',
'LocalAddr' => 'localhost',
'LocalPort' => 0x4014,
'Listen' => 3,
'ReuseAddr' => 1
) or die $!;
my $cv = AnyEvent->condvar;
my $handle_client = AnyEvent->io(
fh => \*{$server},
poll => 'r',
cb => sub {
$cv->begin();
fork_call \&handle_connections, $server->accept, \&handle_token;
}
);
sub welcome_msg($) {
my $client = shift;
print $client BRIGHT_BLUE."Welcome to the 'Notes collection' service!\n",
"Possible actions now are : auth, reg, help\n",
RESET."\$ ";
}
sub parse_user_credentials($) {
my $response = shift;
my ($plain_token_data, $plain_user_name) = unpack "H32H*", decode_base64($response);
my ($user_token, $user_name) = ("", "");
map $user_token.= chr hex, (map $_, $plain_token_data =~ /(..)/g);
map $user_name .= chr (hex ^ 0xBB), reverse (map $_, $plain_user_name =~ /(..)/g);
$user_name =~ s/[^|\w ]//g;
($user_name, $user_token);
}
sub get_user_info($) {
my $client = shift;
print $client BRIGHT_BLUE."--> Now send me your token\n".RESET."# ";
my $user_token = <$client>;
if (not defined $user_token) {
return (undef, undef);
}
if (scalar(split //, $user_token) < 0x14) {
return (undef, undef);
}
chomp $user_token;
parse_user_credentials($user_token);
}
sub check_user_id($) {
my $client = shift;
my ($user_name, $token_id) = get_user_info($client);
if (not defined $user_name or not defined $token_id) {
return undef;
}
if (not exists $global_user_db{$user_name}) {
return undef;
}
if ($global_user_db{$user_name} ne $token_id) {
return undef;
}
return $user_name;
}
sub get_user_name($) {
my $client = shift;
print $client BRIGHT_BLUE."--> Enter your name (5 or more chars)\n".RESET."# ";
my $user_name = <$client>;
return undef if not defined $user_name;
chomp $user_name;
$user_name =~ s/[^|\w ]//g;
return undef if length $user_name < 5;
($user_name) = $user_name =~ /^(.{5,15}).*/;
return $user_name;
}
sub generate_user_id($) {
my $user_name = shift;
my @time_values = unpack "(H2)*", time;
srand int(hex $time_values[6] * hex $time_values[7]);
my $user_id = "";
map $user_id.=chr(int(rand(255)) ^ ord((split //, $user_name)[$_ % length $user_name])), (0 .. 15);
$user_id;
}
sub create_new_user($) {
my $client = shift;
my $user_name = get_user_name($client);
if (not defined $user_name) {
print $client RED."--> Wrong user name\n".RESET;
return (undef, undef, undef);
}
if (exists $global_user_db{$user_name}) {
print $client RED."--> User already exist\n".RESET;
return (undef, undef, undef);
}
my $user_id = generate_user_id($user_name);
my $user_token = $user_id.join "", map chr(hex ^ 0xBB), reverse (unpack("H*", $user_name) =~ /(..)/g);
return ($user_id, $user_name, $user_token);
}
sub handle_unauth_cmd($$$) {
my ($client, $cmd, $state) = @_;
if ($cmd =~ /^auth$/) {
my $user_name = check_user_id($client);
if (not defined $user_name) {
print $client RED."--> Wrong token\n".RESET;
return undef;
}
print $client BRIGHT_BLUE."Hello $user_name\n".RESET;
$$state{auth} = 1;
$$state{name} = $user_name;
return $user_name;
}
if ($cmd =~ /^reg$/) {
my ($user_id, $user_name, $user_token) = create_new_user($client);
if (not defined $user_name or not defined $user_id or not defined $user_token) {
print $client RED."--> New account was not created\n".RESET;
return undef;
}
$$state{auth} = 1;
$$state{name} = $user_name;
$$state{token_id} = $user_id;
print $client BRIGHT_BLUE."--> Your token is ".encode_base64($user_token).RESET;
return $user_name;
}
if ($cmd =~ /^h[elp]*$/) {
print $client BRIGHT_BLUE."Currently you are not authenticated thus\n",
"you've access only to the following options :\n",
"=> auth - authenticate yourself\n",
"=> reg - create new user\n",
"=> help - show this message\n".RESET;
return undef;
}
print $client RED."Unknown command\n".RESET;
}
sub get_file_content($) {
my $file_name = shift;
local $/ = undef;
open my $fh, "$file_name" or return undef;
my $file_content = <$fh>;
close $fh;
if (not defined $file_content) {
return undef;
}
if (length $file_content == 0) {
return undef;
}
$file_content;
}
sub get_user_note($) {
my $client = shift;
print $client BRIGHT_BLUE, "Enter your note :\n".RESET."# ";
my $user_note = <$client>;
if (not defined $user_note) {
return undef;
}
chomp $user_note;
$user_note =~ s/[^\w ?!'".,-_]//g;
if (length $user_note == 0) {
return undef;
}
$user_note;
}
sub write_note_into_file($$) {
my ($file_name, $note) = @_;
open my $fh, ">> $file_name" or return undef;
print $fh $note."\n";
close $fh;
}
sub handle_auth_cmd($$$) {
my ($client, $cmd, $state) = @_;
if ($cmd =~ /^read$/) {
my $file_content = get_file_content($$state{name});
if (not defined $file_content) {
print $client BRIGHT_BLUE."You don't have any notes\n".RESET;
return undef;
}
print $client BRIGHT_BLUE."Your notes :\n".$file_content.RESET;
return undef;
}
if ($cmd =~ /^add$/) {
my $note = get_user_note($client);
if (not defined $note) {
print $client RED."Something is wrong with your note\n".RESET;
return undef;
}
write_note_into_file($$state{name}, $note);
print $client BRIGHT_BLUE."Your note : '".join "", ($note =~ /^(.{1,8}).*$/),
length $note > 8 ? "..." : "", "' has been saved\n".RESET;
return undef;
}
if ($cmd =~ /^h[elp]*$/) {
print $client BRIGHT_BLUE."Hey, $$state{name}! here is your help message :\n",
"=> read - read all my notes\n",
"=> users - get userlist\n",
"=> help - show this message\n",
"=> add - add yet another note\n".RESET;
return undef;
}
if ($cmd =~ /^users$/) {
print $client BRIGHT_BLUE, scalar(keys %global_user_db) > 0
? "Registered users : \n".join "\n", (keys %global_user_db)
: "No one is registered", "\n".RESET;
return undef;
}
print $client RED."Unknown command\n".RESET;
}
sub handle_connections($) {
my $client = shift;
print "new connection\n";
welcome_msg($client);
my %user_credentials = (auth => 0, name => undef, token_id => undef);
while (my $client_response = <$client>) {
next if $client_response eq "\n";
if ($user_credentials{'auth'} == 0) {
handle_unauth_cmd($client, $client_response, \%user_credentials);
next;
}
if ($user_credentials{'auth'} == 1) {
handle_auth_cmd($client, $client_response, \%user_credentials);
next;
}
} continue {
print $client $user_credentials{auth} == 1 ? $user_credentials{name}." " : "","\$ ";
}
$cv->end();
($user_credentials{name}, $user_credentials{token_id});
}
sub handle_token($$) {
my ($user_name, $token_id) = @_;
print "connection was closed\n";
if (defined $user_name and defined $token_id) {
$global_user_db{$user_name} = $token_id if not exists $global_user_db{$user_name};
}
}
$cv->recv();