diff --git a/lib/LANraragi/Plugin/Login/nHentai.pm b/lib/LANraragi/Plugin/Login/nHentai.pm new file mode 100644 index 000000000..92b01f85a --- /dev/null +++ b/lib/LANraragi/Plugin/Login/nHentai.pm @@ -0,0 +1,83 @@ +package LANraragi::Plugin::Login::nHentai; + +use strict; +use warnings; +no warnings 'uninitialized'; + +use Mojo::UserAgent; +use LANraragi::Utils::Logging qw(get_logger); + +#Meta-information about your plugin. +sub plugin_info { + + return ( + #Standard metadata + name => "nHentai CF Bypass", + type => "login", + namespace => "nhentaicfbypass", + author => "Pheromir", + version => "0.1", + description => + "Bypasses the Cloudflare Javascript-challenge by re-using cookies from your browser. Both CF cookies and the user-agent must originate from the same webbrowser.", + parameters => [ + { type => "string", desc => "Browser UserAgent string (Can be found at http://useragentstring.com/ for your browser)" }, + { type => "string", desc => "csrftoken cookie for domain nhentai.net" }, + { type => "string", desc => "cf_clearance cookie for domain nhentai.net" } + ] + ); + +} + + +# Mandatory function to be implemented by your login plugin +# Returns a Mojo::UserAgent object only! +sub do_login { + + # Login plugins only receive the parameters entered by the user. + shift; + my ( $useragent, $csrftoken, $cf_clearance ) = @_; + return get_user_agent( $useragent, $csrftoken, $cf_clearance ); +} + +# get_user_agent(useragent, cf cookies) +# Try crafting a Mojo::UserAgent object that can access nHentai. +# Returns the UA object created. +sub get_user_agent { + + my ( $useragent, $csrftoken, $cf_clearance ) = @_; + + my $logger = get_logger( "nHentai Cloudflare Bypass", "plugins" ); + my $ua = Mojo::UserAgent->new; + + if ( $useragent ne "" && $csrftoken ne "" && $cf_clearance ne "") { + $logger->info("Useragent and Cookies provided ($useragent $csrftoken $cf_clearance)!"); + $ua->transactor->name($useragent); + + #Setup the needed cookies + $ua->cookie_jar->add( + Mojo::Cookie::Response->new( + name => 'csrftoken', + value => $csrftoken, + domain => 'nhentai.net', + path => '/' + ) + ); + + $ua->cookie_jar->add( + Mojo::Cookie::Response->new( + name => 'cf_clearance', + value => $cf_clearance, + domain => 'nhentai.net', + path => '/' + ) + ); + + } else { + $logger->info("No cookies provided, returning blank UserAgent."); + } + + return $ua; + +} + +1; \ No newline at end of file diff --git a/lib/LANraragi/Plugin/Metadata/nHentai.pm b/lib/LANraragi/Plugin/Metadata/nHentai.pm index b232c3a82..65f712a57 100644 --- a/lib/LANraragi/Plugin/Metadata/nHentai.pm +++ b/lib/LANraragi/Plugin/Metadata/nHentai.pm @@ -21,8 +21,9 @@ sub plugin_info { name => "nHentai", type => "metadata", namespace => "nhplugin", - author => "Difegue", - version => "1.7.1", + login_from => "nhentaicfbypass", + author => "Difegue and others", + version => "1.7.2", description => "Searches nHentai for tags matching your archive.
Supports reading the ID from files formatted as \"{Id} Title\" and if not, tries to search for a matching gallery.
This plugin will use the source: tag of the archive if it exists.", @@ -40,7 +41,8 @@ sub get_tags { shift; my $lrr_info = shift; # Global info hash my ($savetitle) = @_; # Plugin parameters - + my $ua = $lrr_info->{user_agent}; # UserAgent from login plugin + my $logger = get_plugin_logger(); # Work your magic here - You can create subs below to organize the code better @@ -58,7 +60,7 @@ sub get_tags { } else { #Get Gallery ID by hand if the user didn't specify a URL - $galleryID = get_gallery_id_from_title( $lrr_info->{archive_title} ); + $galleryID = get_gallery_id_from_title( $lrr_info->{archive_title}, $ua ); } # Did we detect a nHentai gallery? @@ -76,7 +78,7 @@ sub get_tags { return ( error => "No matching nHentai Gallery Found!" ); } - my %hashdata = get_tags_from_NH( $galleryID, $savetitle ); + my %hashdata = get_tags_from_NH( $galleryID, $savetitle, $ua ); $logger->info( "Sending the following tags to LRR: " . $hashdata{tags} ); @@ -91,7 +93,7 @@ sub get_tags { #Uses the website's search to find a gallery and returns its content. sub get_gallery_dom_by_title { - my ($title) = @_; + my ($title, $ua) = @_; my $logger = get_plugin_logger(); @@ -101,7 +103,6 @@ sub get_gallery_dom_by_title { my $URL = "https://nhentai.net/search/?q=" . uri_escape_utf8($title); $logger->debug("Using URL $URL to search on nH."); - my $ua = Mojo::UserAgent->new; my $res = $ua->get($URL)->result; $logger->debug( "Got response " . $res->body ); @@ -115,7 +116,7 @@ sub get_gallery_dom_by_title { sub get_gallery_id_from_title { - my ($title) = @_; + my ($title, $ua) = @_; my $logger = get_plugin_logger(); @@ -124,7 +125,7 @@ sub get_gallery_id_from_title { return $1; } - my $dom = get_gallery_dom_by_title($title); + my $dom = get_gallery_dom_by_title($title, $ua); if ($dom) { @@ -146,10 +147,9 @@ sub get_gallery_id_from_title { # retrieves html page from NH sub get_html_from_NH { - my ($gID) = @_; - + my ($gID, $ua) = @_; + my $URL = "https://nhentai.net/g/$gID/"; - my $ua = Mojo::UserAgent->new; my $res = $ua->get($URL)->result; @@ -213,11 +213,11 @@ sub get_title_from_json { sub get_tags_from_NH { - my ( $gID, $savetitle ) = @_; + my ( $gID, $savetitle, $ua ) = @_; my %hashdata = ( tags => "" ); - my $html = get_html_from_NH($gID); + my $html = get_html_from_NH($gID, $ua); my $json = get_json_from_html($html); if ($json) {