Perl coders

For the generic offtopic chit-chat
Post Reply
harryb382003
Member
Member
Posts: 20
Joined: Mon Jul 03, 2023 4:48 pm

Perl coders

Post by harryb382003 »

Do we have anyone in here that codes in perl?
harryb382003
Member
Member
Posts: 20
Joined: Mon Jul 03, 2023 4:48 pm

Re: Perl coders

Post by harryb382003 »

I have been working on coding something up to work with the WebUI in perl, with limited successes.

Currently I am having an issue that I cannot wrap my head around regarding authentication/method.
I do not understand how/why it is that I am getting a cookie while also getting a method not allowed.

this is the response:

Code: Select all

HTTP/1.1 405 Method Not Allowed
connection: keep-alive
content-length: 18
content-security-policy: default-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; script-src 'self' 'unsafe-inline'; object-src 'none'; form-action 'self'; frame-ancestors 'self';
content-type: text/plain; charset=UTF-8
date: Tue, 12 Sep 2023 14:25:20 GMT
referrer-policy: same-origin
set-cookie: SID=FXrLrSbj/fUkQSslURGk8VsY+kWpEkQe; HttpOnly; path=/; SameSite=Strict
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block

Method Not Allowed
User avatar
Peter
Administrator
Administrator
Posts: 2702
Joined: Wed Jul 07, 2010 6:14 pm

Re: Perl coders

Post by Peter »

harryb382003 wrote: Mon Aug 21, 2023 12:18 am Do we have anyone in here that codes in perl?
You can always use ChatGPT. If you know basic programming, it can help you a lot with any language.

But in this case, you are just requesting the auth "wrong".
https://developer.mozilla.org/en-US/doc ... Status/405

(since there is no code posted here I can't really comment on the code itself.)
harryb382003
Member
Member
Posts: 20
Joined: Mon Jul 03, 2023 4:48 pm

Re: Perl coders

Post by harryb382003 »

I get that I am doing it wrong, I am just confused that it is rewarding me with a cookie while doing so.
harryb382003
Member
Member
Posts: 20
Joined: Mon Jul 03, 2023 4:48 pm

Re: Perl coders

Post by harryb382003 »

Peter, thank you.

Code: Select all

sub qbt_auth {
    my ($api , $do) = @_;
    my $url = $host.=$port.=$api.=$do;
    d "\t$url";
    my @headers = $referrer;
    my $response_body = "$0_response_body";
    open( RESPONSE, "> $response_body" );
    my $easy = Net::Curl::Easy->new();
        $easy->setopt( CURLOPT_VERBOSE, 1 );#, CURLOPT_DEBUGFUNCTION );
        $easy->setopt( CURLOPT_USERAGENT, "MyBrowser_$0" );
        $easy->setopt( CURLOPT_URL, "$url" );
        $easy->setopt( CURLOPT_COOKIEFILE, "$cookie" ); # enable cookie session
        $easy->setopt( CURLOPT_COOKIEJAR, "$cookie" );

        $easy->setopt( CURLOPT_WRITEDATA, *RESPONSE);

        $easy->setopt( CURLOPT_HTTPHEADER, \@headers);
        $easy->setopt( CURLOPT_HEADERDATA, *RESPONSE );

    my $ret_code = $easy->perform();
    # Looking at the results...
    if ($ret_code == 0) {
        say "\nTransfer went ok\n";
        my $response_code = $easy->getinfo(CURLINFO_HTTP_CODE);
        # judge result and next action based on $response_code
        say "Received response: $response_body\n";
    } else {
        # Error code, type of error, error message
        say "An error happened: $ret_code ".
            $easy->strerror($ret_code)." ".$easy->errbuf."\n";
    }
}
harryb382003
Member
Member
Posts: 20
Joined: Mon Jul 03, 2023 4:48 pm

Re: Perl coders

Post by harryb382003 »

I overcame the issue I was having.

I found that using the "--libcurl <filename.c> to be very useful.

Now I am getting the error:
9/15/23 10:50 PM - WebAPI login failure. Reason: invalid credentials, attempt count: 2, IP: 127.0.0.1, username:

using: curl -i --data --header -c /Users/<path>/cookies.txt http://localhost:8080/api/v2/auth/login -u <username> --libcurl login.c
which then prompts me for the password.

I have checked & changed the password multiple times including restarts of qBittorrent to no avail.
(bypassing of localhost authentication has been disabled so I can develop this segment)

For the life of me I do not understand why this is not working.
harryb382003
Member
Member
Posts: 20
Joined: Mon Jul 03, 2023 4:48 pm

Re: Perl coders

Post by harryb382003 »

Whatever the login credentials issue I was having, I managed to get resolved. I cannot recall what the issue was or how I resolved it.

Moving on.....

My method of attack while developing my perl script is to, on the command line, get curl to perform a task. Using --libcurl to create a <function>.c file. I find the data and structure of the C file very useful for writing my perl code for the same task.

Currently, I seem to be having an issue with cookies when attempting to add a torrent.

I have been using the following code to gather "info" from qBt.

Code: Select all

curl -K curl_info
where

Code: Select all

-i
-A "Qbt_curl"
-b "curl_cookies.txt"
header "Referer: http://localhost:8080"
url "http://localhost:8080/api/v2/torrents/info"
libcurl info.c
is the contents of the config file. This works fine. Note the line for cookies.

When using this configuration

Code: Select all

-i
-A "Qbt_curl"
-b "curl_cookies.txt"
header "Referer: http://127.0.0.1:8080"
header "Content-Type: multipart/form-data"
form "name=torrents"
form "[email protected]"
form "savepath=testing"
form "root_folder=false"
form "autoTMM=false"
form "paused=true"
form "firstLastPiecePrio=true"
url "http://127.0.0.1:8080/api/v2/torrents/add"
libcurl add_torrent.c
for adding a torrent, I get the "forbidden" error. If I switch -b "curl_cookies.txt" to -b "SID=<cookie_data>" it behave as it should. The second method is documented in the wiki.

So I guess my question is: does this seem like a bug?

I realize that it is documented, but it just seems so natural to use -b curl_cookies.txt for adding (/api/v2/torrents/add) the torrent because it just worked to get info (/api/v2/torrents/info).
Post Reply