Логин в ЖЖ через HTTP

Для одной мерзкой цели потребовалось ходить в ЖЖ через HTTP — так удобней парсить некоторые вещи.

Собственно, сниппет под катом.

Пара слов о куках. Мне стало уже лениво писать под них лишние регулярки — но список их следующий:

  • ljuniq
  • ljmastersession
  • ljloggedin
  • BMLschemepref (схема кстати — кому хочется обратно dystopia — просто смените эту куку)
  • langpref
  • ljsession
  • ljdomsess.{username} — у меня, соответственно, ljdomsess.skazo4nik

#!/usr/bin/perl -w
use strict;
use URI::Escape;
use Carp qw[croak];

my $login = q[skazo4nik];
my $password = q[];
my $lj_cookie_path = q[/home/common/lj.cookie];

my @user_agents = (q[MegaBot],q[InsertHereSomeMozilla]);

my $content = getURL ({href=>q[http://www.livejournal.com/login.bml],cookie_file=>$lj_cookie_path});
if ($content->{retcode} == 0 && $content->{http_code} == 200){
my $lj_form_auth = $content->{content} =~ /lj_form_auth['"]\s*value\s*=\s*['"]([^"']+)>/i ? $1 : q[];
my $cha1 = $content->{content} =~ /lj_login_chal['"]\s*value=\s*['"]([^'"]+)/i ? $1 : q[];
my $response = $content->{content} =~ /login_response['"]\s*value\s*=\s*['"]([^'"]*)/i ? $1 : q[];
my $post = q[mode=login&lj_form_auth=].uri_escape ($lj_form_auth).q[&cha1=].uri_escape ($cha1).qq[&response=$response&user=$login&password=$password&remember_me=1];
$content = getURL ({href=>q[http://www.livejournal.com/login.bml],cookie_file=>$lj_cookie_path,post=>$post, referer=>q[http://www.livejournal.com/login.bml]});
}

sub getURL {
my $params = shift;

croak q[HASH needed as param] if ref ($params) ne 'HASH';

croak q[HREF needed in HASH] if (!exists ($params->{href}));

$params->{href} = q[http://].$params->{href} if $params->{href} !~ /^http:\/\//;
$params->{headers} = 0 if !$params->{headers};
$params->{ag} = qq ($user_agents[rand(($#user_agents+1))]) if !exists $params->{ag};
$params->{timeout} = 20 if !exists $params->{timeout};
$params->{content_length} = 600000000 if !exists $params->{content_length};

my $result = 'false';
open my $oldout, «>&STDOUT» or die «Can't dup STDOUT: $!»;

close STDOUT;
open STDOUT, «> /dev/null»;
my $retcode;

my $curl;
{
$curl = new WWW::Curl::Easy;

$curl->setopt (CURLOPT_URL, $params->{href});
$curl->setopt (CURLOPT_INTERFACE, $params->{ip}) if exists $params->{ip};
$curl->setopt (CURLOPT_CONNECTTIMEOUT,$params->{timeout});
$curl->setopt (CURLOPT_TIMEOUT,$params->{timeout});
$curl->setopt (CURLOPT_NOPROGRESS, 0);
$curl->setopt (CURLOPT_PROGRESSFUNCTION, sub {$_[2]>$params->{content_length}?1:0});
$curl->setopt (CURLOPT_USERAGENT,$params->{ag});
$curl->setopt (CURLOPT_POST,1) if $params->{post};
$curl->setopt (CURLOPT_POSTFIELDS,$params->{post}) if $params->{post};
$curl->setopt (CURLOPT_HEADER,$params->{headers});
$curl->setopt (CURLOPT_COOKIE,$params->{cookie_string}) if $params->{cookie_string};
$curl->setopt (CURLOPT_COOKIEJAR, $params->{cookie_file}) if $params->{cookie_file};
$curl->setopt (CURLOPT_COOKIEFILE, $params->{cookie_file}) if $params->{cookie_file};
$curl->setopt (CURLOPT_FOLLOWLOCATION,1);
$curl->setopt (CURLOPT_REFERER,$params->{referer}) if exists $params->{referer};
open (my $tmp_for_curl, ">", \$result);
$curl->setopt (CURLOPT_FILE,$tmp_for_curl);
$retcode = $curl->perform;
}

close STDOUT;
open STDOUT, «>&», $oldout or die «Can't dup \$oldout: $!»;

my $cp = q[];
my $headers_length = $curl->getinfo (CURLINFO_HEADER_SIZE);
my $length = length $result;
if ($params->{headers}){
$length -= $headers_length;
}

$cp = $1 if $result =~ /Content\-Type:\s*[^;]+;\s*charset\s*=\s*([^\s]+)/i;
if ($cp eq ''){
$cp = $1 if $result =~ /;\s*charset\s*=\s*([^"'\s]+)/;
}
my $our_headers = $curl->getinfo (CURLINFO_HEADER_OUT);
return {
content => $result,
length => $length,
retcode => $retcode,
http_code => $curl->getinfo (CURLINFO_HTTP_CODE),
codepage => $cp,
headers_length => $headers_length,
our_headers => $our_headers
};
}

Комментировать

Войдите чтобы оставить комментарий.