Skip to content

Commit

Permalink
Merge pull request #633 from Pheromir/dev
Browse files Browse the repository at this point in the history
Add nHentai-Plugin to bypass CF JS-Challenge
  • Loading branch information
Difegue authored May 26, 2022
2 parents 1e1f9c5 + 1220308 commit 38989bb
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 14 deletions.
83 changes: 83 additions & 0 deletions lib/LANraragi/Plugin/Login/nHentai.pm
Original file line number Diff line number Diff line change
@@ -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;
28 changes: 14 additions & 14 deletions lib/LANraragi/Plugin/Metadata/nHentai.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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.
<br>Supports reading the ID from files formatted as \"{Id} Title\" and if not, tries to search for a matching gallery.
<br><i class='fa fa-exclamation-circle'></i> This plugin will use the source: tag of the archive if it exists.",
Expand All @@ -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
Expand All @@ -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?
Expand All @@ -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} );

Expand All @@ -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();

Expand All @@ -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 );
Expand All @@ -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();

Expand All @@ -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) {

Expand All @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 38989bb

Please sign in to comment.