Skip to content

Instantly share code, notes, and snippets.

@mindc
Created August 21, 2017 07:35
Show Gist options
  • Save mindc/d0d5398939f6730a8561c001ca5c232b to your computer and use it in GitHub Desktop.
Save mindc/d0d5398939f6730a8561c001ca5c232b to your computer and use it in GitHub Desktop.
LINE instant messenger protocol in Perl
#!/usr/bin/perl
package customHttpClient;
use base qw(Thrift::HttpClient);
sub setCustomHeader {
my $self = shift;
push @{$self->{'customHeaders'}},[@_];
}
sub flush
{
my $self = shift;
my $ua = LWP::UserAgent->new('timeout' => 1);
$ua->default_header('Accept' => 'application/x-thrift');
$ua->default_header('Content-Type' => 'application/x-thrift');
$ua->cookie_jar({}); # hash to remember cookies between redirects
my $out = $self->{out};
$out->setpos(0); # rewind
my $buf = join('', <$out>);
my $request = new HTTP::Request(POST => $self->{url}, undef, $buf);
foreach ( @{$self->{'customHeaders'}} ) {
$request->header(@$_);
}
my $response = $ua->request($request);
my $content_ref = $response->content_ref;
my $in = IO::String->new($content_ref);
binmode($in);
$self->{in} = $in;
$in->setpos(0); # rewind
# reset write buffer
$out = IO::String->new;
binmode($out);
$self->{out} = $out;
}
package main;
use strict;
use warnings;
use Data::Dumper;
use Thrift::XS;
use TalkService;
# TalkService module, generated from https://github.com/cslinmiso/LINE-instant-messenger-protocol/blob/master/line.thrift
# thrift -r --gen perl line.thrift
use Try::Tiny;
use constant LINE_DOMAIN => "http://125.209.252.15";
use constant LINE_HTTP_URL => LINE_DOMAIN . "/api/v4/TalkService.do";
my $ip = "127.0.0.1";
my $version = "4.0.2.366";
my $com_name = "LineJ";
my $id = '<email>';
my $password = '<password>';
my $os_version = "6.2.9200-8-x64";
my $user_agent = sprintf("DESKTOP:WINDOWS:%s(%s)",$os_version,$version);
my $app = sprintf("DESKTOPWIN\t%s\tWINDOWS\t%s",$version, $os_version);
my $transport = new customHttpClient(LINE_HTTP_URL);
$transport->setCustomHeader('User-Agent' => $user_agent);
$transport->setCustomHeader('X-Line-Application' => $app);
my $protocol = new Thrift::XS::CompactProtocol($transport);
my $client = new TalkServiceClient($protocol);
try {
my $msg = $client->loginWithIdentityCredentialForCertificate(
IdentityProvider::LINE, $id, $password, 0, $ip, $com_name, ""
);
$transport->setCustomHeader('X-Line-Access' => $msg->{authToken});
print Dumper $client->getProfile();
} catch {
print STDERR Dumper $_;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment