Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

实现登录以及密码验证。

  • Loading branch information...
commit 7c7fd111bb3787730156080b266f9789206237a6 1 parent 9ae7d7e
Geng Shuang authored
Showing with 103 additions and 14 deletions.
  1. +12 −3 editor.pl
  2. +91 −11 listener.pl
15 editor.pl
View
@@ -18,6 +18,7 @@
my $acunt = '';
my $paswd = '';
+my $paspt = '';
my $commd = 'HELP';
my $input = '';
until ($input =~ m{^q$}i) {
@@ -27,11 +28,15 @@
my $s_cmd = $1;
my $msg = decode_base64($2);
- unless ($s_cmd eq 'EROR') {
+ if ($s_cmd ne 'EROR') {
$commd = $s_cmd;
}
else {
$commd = 'HELP';
+ if ($s_cmd eq 'PWOK') {
+ $paswd = $paspt;
+ }
+ $paspt = '';
}
print $msg;
@@ -40,14 +45,18 @@
chomp $input;
$acunt = $input if $commd eq 'ACNT';
- $paswd = $input if $commd eq 'PSWD';
+ if ($commd eq 'PSWD') {
+ $paspt = encode_base64($input);
+ chomp $paspt;
+ }
}
}
$socket -> close() or die "Close Socket failed.$@";
sub talk {
- $socket -> send("$acunt:".encode_base64($paswd).":$commd:".encode_base64($input)."\n", 0);
+# print "SENDING ==> [$acunt:".$paswd.":$commd:".encode_base64($input)."]\n";
+ $socket -> send("$acunt:".$paswd.":$commd:".encode_base64($input)."\n", 0);
$socket -> autoflush(1);
my $sel = IO::Select -> new($socket);
102 listener.pl
View
@@ -8,7 +8,8 @@
my $USERFILE = 'account.txt';
my $HELPLIST = <<END;
\t(a) Creat or modify account
-\t(s) Creat or modify password
+\t(s) Set or change password
+\t(p) Print your imformation
\t(h) Show this help list
\t(q) Quit
?>
@@ -33,23 +34,59 @@ END
my $account = $1;
my $passwrd = $2;
my $command = $3;
- my $message = decode_base64($4);
+ my $message = $4;
if ($command =~ /^(?:HELP)$/) {
- print $new_sock 'HELP:'.encode_base64($HELPLIST) if $message =~ /^h?$/;
- print $new_sock 'ACNT:'.encode_base64('Input your email account > ') if $message =~ /^a$/;
+ $message = decode_base64($message);
+ if ($message =~ /^h?$/) {
+ print $new_sock 'HELP:'.encode_base64($HELPLIST);
+ }
+ elsif ($message =~ /^a$/) {
+ print $new_sock 'ACNT:'.encode_base64('Input your email account > ');
+ }
+ elsif ($message =~ /^s$/) {
+ print $new_sock 'PSWD:'.encode_base64('Input your email password > ');
+ }
+ elsif ($message =~ /^p$/) {
+ print $new_sock 'HELP:'.encode_base64(get_account_information($account, $passwrd)."?> ");
+ }
+ elsif (!$account) {
+ print $new_sock 'HELP:'.encode_base64("Use 'a' to login frist.\n?> ");
+ }
+ else {
+ print $new_sock 'HELP:'.encode_base64("Use 'h' for help.\n?> ");
+ }
}
elsif ($command =~ /^(?:ACNT)$/) {
+ $message = decode_base64($message);
if (get_account($message) eq 'NEW') {
- print $new_sock 'HELP:'.encode_base64("Creat account $message\n?> ");
+ print $new_sock 'HELP:'.encode_base64("Creat account $message, password [neusoft]\n?> ");
}
else {
- print $new_sock 'PSWD:'.encode_base64("Enter password for $message\n?> ");
+ print $new_sock 'HELP:'.encode_base64("Login as $message\n?> ");
}
}
elsif ($command =~ /^(?:PSWD)$/) {
- if (check_password($account, $passwrd) eq 'NEW') {
- print $new_sock 'HELP:'.encode_base64("Creat account $message\n?> ");
+ if ($message =~ /^\s*$/) {
+ print $new_sock 'HELP:'.encode_base64("Not allow empty password!\n?> ");
+ }
+ else {
+ my $chk = check_password($account, $passwrd);
+ if ($passwrd =~ /^\s*$/) {print "new pass\n";
+ if (check_password($account, $message) eq 'LGIN') {
+ print $new_sock 'PWOK:'.encode_base64("Password OK.\n?> ");
+ }
+ else {
+ print $new_sock 'HELP:'.encode_base64("Wrong account or password!\n?> ");
+ }
+ }
+ elsif ($chk eq 'LGIN') {
+ change_password($account, $message);
+ print $new_sock 'HELP:'.encode_base64("Password changed.\n?> ");
+ }
+ elsif ($chk eq 'ILLE') {
+ print $new_sock 'HELP:'.encode_base64("ILLE Wrong account or password!\n?> ");
+ }
}
}
}
@@ -71,7 +108,7 @@ sub get_account {
unless (grep(/^$account/, @user_list)) {
my $save_str = $account;
- `echo $save_str >> $USERFILE`;
+ `echo "$save_str:bmV1c29mdA==:" >> $USERFILE`;
return 'NEW';
}
@@ -88,8 +125,51 @@ sub check_password {
my @user_list = <$UF>;
close $UF;
- unless (grep(/^$account:$password/, @user_list)) {
- foreach my $line (@user_list) {
+ if (my @ttt = grep(/^$account:$password:/, @user_list)) {
+ return 'LGIN';
+ }
+ else {
+ return 'ILLE';
+ }
+}
+
+sub change_password {
+ my $account = shift;
+ chomp $account;
+ my $password = shift;
+ chomp $password;
+
+ open my $UF, '< '.$USERFILE;
+ my @user_list = <$UF>;
+ close $UF;
+
+ foreach my $line (@user_list) {
+ if ($line =~ /^$account:/) {
+ $line =~ s/^(?:$account:?)[^:]*/$password/;
}
}
+
+ open my $OH, '> '.$USERFILE;
+ print $OH join '', @user_list;
+ close $OH;
+}
+
+sub get_account_information {
+ my $account = shift;
+ chomp $account;
+ my $password = shift;
+ chomp $password;
+
+ if (check_password($account, $password) eq 'LGIN') {
+ open my $UF, '< '.$USERFILE;
+ my @user_list = <$UF>;
+ close $UF;
+
+ my @line = grep(/^$account:/, @user_list);
+ my $ifm = shift @line;
+ return $ifm;
+ }
+ else {
+ return "Wrong account or password!\n";
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.