
$ ./configure --with-http_perl_module package session; use strict; use Digest::MD5 qw(md5_hex); my $secret_key = '__TOP_SECRET__KEEP_IT_IN_BANK__'; # , my $cookie_name = 'SID'; # my $rand_len = 16; # my $hex_length = $rand_len * 2; my $hex_mask = "H".$hex_length; my $digest_length = 32; # hexstr - 32 . # sub hash { # data - , ng - nginx . my ($data,$ng) = @_; # ip return md5_hex($data."_".$secret_key."_".$ng->header_in("User-Agent")."_".$ng->remote_addr); } # . MAN, , /dev/random . # nginx. # . open(my $rand, '<', "/dev/random"); sub gen { # ng - nginx my $ng = shift; # # 32 (hexstr) # 32 (hexstr) (. sub hash) if ($ng->header_in("Cookie")=~/$cookie_name=(\w{$hex_length})(\w{$digest_length});?/) { if ($2 eq hash($1, $ng)) { return "$1$2"; } } # read($rand, my $data, $rand_len); # hexstr my $h = unpack($hex_mask, $data); # (. sub hash) my $id = $h.hash($h, $ng); # $ng->header_out("Set-Cookie","$cookie_name=$id;"); # nginx return $id; } 1; __END__ http {
...
perl_modules conf / perl; # directory where our module is stored
perl_require session.pm; # module file
perl_set $ sid session :: gen; # variable in which the identifier will be saved
...
server {
..
location ~ * \. php $ {
root html / www;
fastcgi_pass http: // backend_upstreams;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $ document_root / $ fastcgi_script_name;
include fastcgi_params;
fastcgi_param SID $ sid; # pass the identifier via FastCGI to the backend
}
location ~ * \. tst $ {
fastcgi_pass unix: / tmp / cpp_server;
include fastcgi_params;
fastcgi_param SID $ sid; # pass the identifier by FastCGI to the backend
}
}
...
}
#!/usr/bin/perl use strict; use Benchmark; use Digest::MD5 qw(md5_hex); my $secret_key = '__TOP_SECRET__KEEP_IT_IN_BANK__'; my $cookie_name = 'SID'; my $rand_length = 16; my $hex_mask = "H".($rand_length * 2); open(my $rand, '<', "/dev/random"); sub hash { my ($data) = @_; my $hash = md5_hex($data."_".$secret_key."_Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.63 Safari/535.7_127.0.0.1"); return $hash; } sub gen { read($rand, my $data, $rand_length); my $h = unpack($hex_mask,$data); my $id = $h.hash($h); my $ng = "$cookie_name=$id;"; return $ng; } my $t0 = new Benchmark; for (my $i =0; $i < 1000000;++$i) { gen(); } my $t1 = new Benchmark; my $td = timediff($t1, $t0); print "Total:".timestr($td)."\n"; <? session_id($_SERVER['SID']); session_name('SID'); // PHPSESSID, SID session_start(); ?> Source: https://habr.com/ru/post/135777/
All Articles