Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify the use of http, but the client has been trying to connect using https #54

Closed
badgv opened this issue Apr 29, 2021 · 12 comments · Fixed by #56
Closed

Specify the use of http, but the client has been trying to connect using https #54

badgv opened this issue Apr 29, 2021 · 12 comments · Fixed by #56

Comments

@badgv
Copy link

badgv commented Apr 29, 2021

qbt_client = qbittorrentapi.Client(host='http://127.0.0.1:6600/qb/', username='admin', password='adminadmin')

The error message is as follows:
Failed to connect to qBittorrent. Connection Error: ConnectionError(MaxRetryError("HTTPSConnectionPool(host='127.0.0.1', port=6600): Max retries exceeded with url: /qb/api/v2/auth/login (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f0a38170040>: Failed to establish a new connection: [Errno 111] Connection refused'))"))

I found that the error is caused by here:
qbittorrent-api/qbittorrentapi/request.py
369: r = requests_head(base_url.geturl(), allow_redirects=True, verify=False)

My listening address 127.0.0.1:6600 is transferred through a proxy called gost,
Proxy to my intranet address 192.168.1.100:8888.
When allow_redirects=True, it will cause the following error message:
HTTPConnectionPool(host='127.0.0.1', port=8888): Max retries exceeded with url: /qb/ (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f0ca86fa8b0>: Failed to establish a new connection: [Errno 111] Connection refused'))

Because the port is redirected from 6600 to 8888, and the host address remains at 127.0.0.1, the execution of requests.head fails
Then the scheme was redefined as alt_scheme='https'

@badgv
Copy link
Author

badgv commented Apr 29, 2021

Try to fix this problem:
If user specify to use http or https, do not modify the scheme.
#55

@rmartin16
Copy link
Owner

Thanks for bringing this up, @badgv.

I'd like to confirm we have identified the correct problem. I am concerned that qbittorrent/qBittorrent#5693 may be playing a role...but perhaps you've already mitigated that via some mechanism in gost.

At any rate, I believe allow_redirects operates at the HTTP level. If that is truly playing a role here....I'm confused why the proxy is returning HTTP redirects....instead of proxying the requests. Although, gost appears to be a unconventional proxy with an eye towards simplicity.

To help me be sure I understand what is happening between qbittorrent-api and gost.....can you please run the snippets below and post their output back here?

First, can you please confirm exactly what gost is responding to the requests with?

curl -XHEAD -vvv --location http://127.0.0.1:6600/qb/api/v2/auth/login

Additionally, can we take a look at the qbittorrent-api trace?

import logging
logging.basicConfig(level=logging.DEBUG, format='[%(asctime)s] {%(name)s:%(lineno)d} %(levelname)s - %(message)s')
import qbittorrentapi
qbt_client = qbittorrentapi.Client(host='http://127.0.0.1:6600/qb/', username='admin', password='adminadmin', VERBOSE_RESPONSE_LOGGING=True)
qbt_client.auth_log_in()

Thanks again.

@badgv
Copy link
Author

badgv commented Apr 29, 2021

My qb client uses nginx for reverse proxy and uses 192.168.168.3:8888/qb/ for access.
I conducted a test mapping 127.0.0.1:6677 proxy to 192.168.168.3:8888
I use chrome browser to test access http://127.0.0.1:6677/qb/, I can access and log in to qbitorrent webui normally.

curl test:

$ curl -XHEAD -vvv --location http://127.0.0.1:6677/qb/api/v2/auth/login
Warning: Setting custom HTTP method to HEAD with -X/--request may not work the 
Warning: way you want. Consider using -I/--head instead.
*   Trying 127.0.0.1:6677...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 6677 (#0)
> HEAD /qb/api/v2/auth/login HTTP/1.1
> Host: 127.0.0.1:6677
> User-Agent: curl/7.68.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: nginx/1.18.0
< Date: Thu, 29 Apr 2021 15:44:08 GMT
< Content-Type: text/plain; charset=UTF-8
< Content-Length: 6
< Connection: keep-alive
< 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';
< referrer-policy: same-origin
< x-content-type-options: nosniff
< x-xss-protection: 1; mode=block

The API debug log is as follows:

[2021-04-29 23:45:23,007] {qbittorrentapi.request:356} DEBUG - Parsed user URL: ParseResult(scheme='http', netloc='127.0.0.1:6677', path='/qb/', params='', query='', fragment='')
[2021-04-29 23:45:23,018] {qbittorrentapi.request:366} DEBUG - Detecting scheme for URL...
[2021-04-29 23:45:23,024] {urllib3.connectionpool:221} DEBUG - Starting new HTTP connection (1): 127.0.0.1:6677
[2021-04-29 23:45:23,275] {urllib3.connectionpool:428} DEBUG - http://127.0.0.1:6677 "HEAD /qb/ HTTP/1.1" 200 0
[2021-04-29 23:45:23,281] {qbittorrentapi.request:380} DEBUG - Using HTTP scheme
[2021-04-29 23:45:23,284] {qbittorrentapi.request:392} DEBUG - Base URL: http://127.0.0.1:6677/qb/
[2021-04-29 23:45:23,287] {urllib3.connectionpool:221} DEBUG - Starting new HTTP connection (1): 127.0.0.1:6677
[2021-04-29 23:45:23,291] {urllib3.util.retry:438} DEBUG - Incremented Retry for (url='/qb/api/v2/auth/login'): Retry(total=0, connect=0, read=1, redirect=None, status=None)
[2021-04-29 23:45:23,294] {urllib3.connectionpool:744} WARNING - Retrying (Retry(total=0, connect=0, read=1, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f0ca841ca00>: Failed to establish a new connection: [Errno 111] Connection refused')': /qb/api/v2/auth/login
[2021-04-29 23:45:23,295] {urllib3.connectionpool:221} DEBUG - Starting new HTTP connection (2): 127.0.0.1:6677
[2021-04-29 23:45:23,297] {qbittorrentapi.request:251} DEBUG - Retry attempt 1
[2021-04-29 23:45:23,299] {qbittorrentapi.request:356} DEBUG - Parsed user URL: ParseResult(scheme='http', netloc='127.0.0.1:6677', path='/qb/', params='', query='', fragment='')
[2021-04-29 23:45:23,300] {qbittorrentapi.request:366} DEBUG - Detecting scheme for URL...
[2021-04-29 23:45:23,306] {urllib3.connectionpool:221} DEBUG - Starting new HTTP connection (1): 127.0.0.1:6677
[2021-04-29 23:45:23,308] {qbittorrentapi.request:380} DEBUG - Using HTTPS scheme
[2021-04-29 23:45:23,311] {qbittorrentapi.request:383} WARNING - Using 'https' instead of requested 'http' to communicate with qBittorrent
[2021-04-29 23:45:23,312] {qbittorrentapi.request:392} DEBUG - Base URL: https://127.0.0.1:6677/qb/
[2021-04-29 23:45:23,316] {urllib3.connectionpool:957} DEBUG - Starting new HTTPS connection (1): 127.0.0.1:6677
[2021-04-29 23:45:23,320] {urllib3.util.retry:438} DEBUG - Incremented Retry for (url='/qb/api/v2/auth/login'): Retry(total=0, connect=0, read=1, redirect=None, status=None)
[2021-04-29 23:45:23,321] {urllib3.connectionpool:744} WARNING - Retrying (Retry(total=0, connect=0, read=1, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f0ca842cdc0>: Failed to establish a new connection: [Errno 111] Connection refused')': /qb/api/v2/auth/login
[2021-04-29 23:45:23,322] {urllib3.connectionpool:957} DEBUG - Starting new HTTPS connection (2): 127.0.0.1:6677
[2021-04-29 23:45:23,925] {qbittorrentapi.request:251} DEBUG - Retry attempt 2
[2021-04-29 23:45:23,926] {qbittorrentapi.request:356} DEBUG - Parsed user URL: ParseResult(scheme='http', netloc='127.0.0.1:6677', path='/qb/', params='', query='', fragment='')
[2021-04-29 23:45:23,928] {qbittorrentapi.request:366} DEBUG - Detecting scheme for URL...
[2021-04-29 23:45:23,931] {urllib3.connectionpool:221} DEBUG - Starting new HTTP connection (1): 127.0.0.1:6677
[2021-04-29 23:45:23,933] {qbittorrentapi.request:380} DEBUG - Using HTTPS scheme
[2021-04-29 23:45:23,934] {qbittorrentapi.request:383} WARNING - Using 'https' instead of requested 'http' to communicate with qBittorrent
[2021-04-29 23:45:23,935] {qbittorrentapi.request:392} DEBUG - Base URL: https://127.0.0.1:6677/qb/
[2021-04-29 23:45:23,946] {urllib3.connectionpool:957} DEBUG - Starting new HTTPS connection (1): 127.0.0.1:6677
[2021-04-29 23:45:23,954] {urllib3.util.retry:438} DEBUG - Incremented Retry for (url='/qb/api/v2/auth/login'): Retry(total=0, connect=0, read=1, redirect=None, status=None)
[2021-04-29 23:45:23,955] {urllib3.connectionpool:744} WARNING - Retrying (Retry(total=0, connect=0, read=1, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f0ca842cca0>: Failed to establish a new connection: [Errno 111] Connection refused')': /qb/api/v2/auth/login
[2021-04-29 23:45:23,958] {urllib3.connectionpool:957} DEBUG - Starting new HTTPS connection (2): 127.0.0.1:6677
[2021-04-29 23:45:23,959] {qbittorrentapi.request:241} DEBUG - Failed to connect to qBittorrent. Connection Error: ConnectionError(MaxRetryError("HTTPSConnectionPool(host='127.0.0.1', port=6677): Max retries exceeded with url: /qb/api/v2/auth/login (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f0ca842c910>: Failed to establish a new connection: [Errno 111] Connection refused'))"))

It can be seen from the debug log above that after using http:// to access, the scheme was redefined as https due to an error in the requests.head test.
According to the debug information, my judgment may be wrong, but I also suggest that when the user specifies the http or https protocol, the https test should not be performed

[EDIT rmartin] formatting

@badgv
Copy link
Author

badgv commented Apr 29, 2021

I used another proxy tool frp for testing, the api can work normally

[2021-04-30 00:07:26,516] {qbittorrentapi.request:356} DEBUG - Parsed user URL: ParseResult(scheme='http', netloc='127.0.0.1:6600', path='/qb/', params='', query='', fragment='')
[2021-04-30 00:07:26,524] {qbittorrentapi.request:366} DEBUG - Detecting scheme for URL...
[2021-04-30 00:07:26,531] {urllib3.connectionpool:221} DEBUG - Starting new HTTP connection (1): 127.0.0.1:6600
[2021-04-30 00:07:26,749] {urllib3.connectionpool:428} DEBUG - http://127.0.0.1:6600 "HEAD /qb/ HTTP/1.1" 200 0
[2021-04-30 00:07:26,757] {qbittorrentapi.request:380} DEBUG - Using HTTP scheme
[2021-04-30 00:07:26,760] {qbittorrentapi.request:392} DEBUG - Base URL: http://127.0.0.1:6600/qb/
[2021-04-30 00:07:26,764] {urllib3.connectionpool:221} DEBUG - Starting new HTTP connection (1): 127.0.0.1:6600
[2021-04-30 00:07:27,544] {urllib3.connectionpool:428} DEBUG - http://127.0.0.1:6600 "POST /qb/api/v2/auth/login HTTP/1.1" 200 3
[2021-04-30 00:07:27,549] {qbittorrentapi.request:590} DEBUG - Request URL: (POST) http://127.0.0.1:6600/qb/api/v2/auth/login
[2021-04-30 00:07:27,551] {qbittorrentapi.request:591} DEBUG - Request Headers: {'User-Agent': 'python-requests/2.25.1', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Referer': 'http://127.0.0.1:6600/qb/', 'Origin': 'http://127.0.0.1:6600/qb/', 'Content-Length': '31', 'Content-Type': 'application/x-www-form-urlencoded'}
[2021-04-30 00:07:27,552] {qbittorrentapi.request:609} DEBUG - Response status: 200 (OK)
[2021-04-30 00:07:27,554] {qbittorrentapi.request:618} DEBUG - Response text: Ok.
[2021-04-30 00:07:27,557] {qbittorrentapi.auth:105} DEBUG - Login successful for user "badgv"
[2021-04-30 00:07:27,558] {qbittorrentapi.auth:106} DEBUG - SID: VhluxTtPFVlec2QtHeaeHr5J0w3qTQDo

curl test:

curl -XHEAD -vvv --location http://127.0.0.1:6600/qb/api/v2/auth/login
Warning: Setting custom HTTP method to HEAD with -X/--request may not work the 
Warning: way you want. Consider using -I/--head instead.
*   Trying 127.0.0.1:6600...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 6600 (#0)
> HEAD /qb/api/v2/auth/login HTTP/1.1
> Host: 127.0.0.1:6600
> User-Agent: curl/7.68.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: nginx/1.18.0
< Date: Thu, 29 Apr 2021 16:09:26 GMT
< Content-Type: text/plain; charset=UTF-8
< Content-Length: 6
< Connection: keep-alive
< 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';
< referrer-policy: same-origin
< x-content-type-options: nosniff
< x-xss-protection: 1; mode=block

Use gost and frp proxy ports, both can be used normally in chrome browser, but gost proxy port api cannot work normally

@rmartin16
Copy link
Owner

Interesting. Based on the API trace output, python cannot reach the qBittorrent endpoint with HTTP or HTTPS.

When it tried the HTTP connection:

NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f0ca841ca00>: Failed to establish a new connection: [Errno 111] Connection refused')

And then when it tried to connect via HTTPS:

NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f0ca842c910>: Failed to establish a new connection: [Errno 111] Connection refused')

Based on this, even if this client went ahead and used HTTP....I'm not sure the outcome would be any different.

What does this output when you run it?

import logging
logging.basicConfig(level=logging.DEBUG, format='[%(asctime)s] {%(name)s:%(lineno)d} %(levelname)s - %(message)s')
import requests
requests.post('http://127.0.0.1:6600/qb/api/v2/auth/login')

@badgv
Copy link
Author

badgv commented Apr 29, 2021

6600 is frp port:

>>> import logging
>>> logging.basicConfig(level=logging.DEBUG, format='[%(asctime)s] {%(name)s:%(lineno)d} %(levelname)s - %(message)s')
>>> import requests
>>> requests.post('http://127.0.0.1:6600/qb/api/v2/auth/login')
[2021-04-30 00:31:31,664] {urllib3.connectionpool:221} DEBUG - Starting new HTTP connection (1): 127.0.0.1:6600
[2021-04-30 00:31:39,373] {urllib3.connectionpool:428} DEBUG - http://127.0.0.1:6600 "POST /qb/api/v2/auth/login HTTP/1.1" 200 6
<Response [200]>

6677 is gost port:

>>> import logging
>>> logging.basicConfig(level=logging.DEBUG, format='[%(asctime)s] {%(name)s:%(lineno)d} %(levelname)s - %(message)s')
>>> import requests
>>> requests.post('http://127.0.0.1:6677/qb/api/v2/auth/login')
[2021-04-30 00:31:56,131] {urllib3.connectionpool:221} DEBUG - Starting new HTTP connection (1): 127.0.0.1:6677
[2021-04-30 00:31:59,707] {urllib3.connectionpool:428} DEBUG - http://127.0.0.1:6677 "POST /qb/api/v2/auth/login HTTP/1.1" 200 6
<Response [200]>
>>>

Use gost and frp proxy ports, both can be used normally in chrome browser, but gost proxy port api cannot work normally

[EDIT rmartin] formatting

@badgv
Copy link
Author

badgv commented Apr 29, 2021

According to these tests, it seems that gost's port mapping implementation goes wrong? But the browser access is normal, which is very strange

@rmartin16
Copy link
Owner

rmartin16 commented Apr 29, 2021

Weird. Ok.

What if you try requests.head instead of requests.post with those same commands?

@rmartin16
Copy link
Owner

rmartin16 commented Apr 29, 2021

I don't think I had finished my coffee before reading the qbittorrent-api trace this morning.

In the initial attempt to determine the URL, I didn't notice the HEAD request received a 200 back and decided to use HTTP. That said, is this just nginx (or something) returning the 200...?

[2021-04-29 23:45:23,007] {qbittorrentapi.request:356} DEBUG - Parsed user URL: ParseResult(scheme='http', netloc='127.0.0.1:6677', path='/qb/', params='', query='', fragment='')
[2021-04-29 23:45:23,018] {qbittorrentapi.request:366} DEBUG - Detecting scheme for URL...
[2021-04-29 23:45:23,024] {urllib3.connectionpool:221} DEBUG - Starting new HTTP connection (1): 127.0.0.1:6677
[2021-04-29 23:45:23,275] {urllib3.connectionpool:428} DEBUG - http://127.0.0.1:6677 "HEAD /qb/ HTTP/1.1" 200 0
[2021-04-29 23:45:23,281] {qbittorrentapi.request:380} DEBUG - Using HTTP scheme
[2021-04-29 23:45:23,284] {qbittorrentapi.request:392} DEBUG - Base URL: http://127.0.0.1:6677/qb/

Then, the subsequent attempt to log in to qBittorrent hits a connection refused:

[2021-04-29 23:45:23,287] {urllib3.connectionpool:221} DEBUG - Starting new HTTP connection (1): 127.0.0.1:6677
[2021-04-29 23:45:23,291] {urllib3.util.retry:438} DEBUG - Incremented Retry for (url='/qb/api/v2/auth/login'): Retry(total=0, connect=0, read=1, redirect=None, status=None)
[2021-04-29 23:45:23,294] {urllib3.connectionpool:744} WARNING - Retrying (Retry(total=0, connect=0, read=1, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f0ca841ca00>: Failed to establish a new connection: [Errno 111] Connection refused')': /qb/api/v2/auth/login
[2021-04-29 23:45:23,295] {urllib3.connectionpool:221} DEBUG - Starting new HTTP connection (2): 127.0.0.1:6677

When qbittorrent-api hits a comm error, it will re-initialize the entire context to talk to qBittorrent. Therefore, it re-runs the attempt to determine the scheme (in case it changed for some reason or something)....when it says Using HTTPS scheme below, that means HTTP HEAD request errored for some reason but the error isn't captured in the logs (.....id bet its another connection refused):

[2021-04-29 23:45:23,297] {qbittorrentapi.request:251} DEBUG - Retry attempt 1
[2021-04-29 23:45:23,299] {qbittorrentapi.request:356} DEBUG - Parsed user URL: ParseResult(scheme='http', netloc='127.0.0.1:6677', path='/qb/', params='', query='', fragment='')
[2021-04-29 23:45:23,300] {qbittorrentapi.request:366} DEBUG - Detecting scheme for URL...
[2021-04-29 23:45:23,306] {urllib3.connectionpool:221} DEBUG - Starting new HTTP connection (1): 127.0.0.1:6677
[2021-04-29 23:45:23,308] {qbittorrentapi.request:380} DEBUG - Using HTTPS scheme
[2021-04-29 23:45:23,311] {qbittorrentapi.request:383} WARNING - Using 'https' instead of requested 'http' to communicate with qBittorrent
[2021-04-29 23:45:23,312] {qbittorrentapi.request:392} DEBUG - Base URL: https://127.0.0.1:6677/qb/

Then, it re-tries to log in attempt...but this time using HTTPS...but this hits a connection refused as well:

[2021-04-29 23:45:23,316] {urllib3.connectionpool:957} DEBUG - Starting new HTTPS connection (1): 127.0.0.1:6677
[2021-04-29 23:45:23,320] {urllib3.util.retry:438} DEBUG - Incremented Retry for (url='/qb/api/v2/auth/login'): Retry(total=0, connect=0, read=1, redirect=None, status=None)
[2021-04-29 23:45:23,321] {urllib3.connectionpool:744} WARNING - Retrying (Retry(total=0, connect=0, read=1, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f0ca842cdc0>: Failed to establish a new connection: [Errno 111] Connection refused')': /qb/api/v2/auth/login
[2021-04-29 23:45:23,322] {urllib3.connectionpool:957} DEBUG - Starting new HTTPS connection (2): 127.0.0.1:6677

This whole process repeats one more time....and finally errors with connection refused:

[2021-04-29 23:45:23,925] {qbittorrentapi.request:251} DEBUG - Retry attempt 2
[2021-04-29 23:45:23,926] {qbittorrentapi.request:356} DEBUG - Parsed user URL: ParseResult(scheme='http', netloc='127.0.0.1:6677', path='/qb/', params='', query='', fragment='')
[2021-04-29 23:45:23,928] {qbittorrentapi.request:366} DEBUG - Detecting scheme for URL...
[2021-04-29 23:45:23,931] {urllib3.connectionpool:221} DEBUG - Starting new HTTP connection (1): 127.0.0.1:6677
[2021-04-29 23:45:23,933] {qbittorrentapi.request:380} DEBUG - Using HTTPS scheme
[2021-04-29 23:45:23,934] {qbittorrentapi.request:383} WARNING - Using 'https' instead of requested 'http' to communicate with qBittorrent
[2021-04-29 23:45:23,935] {qbittorrentapi.request:392} DEBUG - Base URL: https://127.0.0.1:6677/qb/
[2021-04-29 23:45:23,946] {urllib3.connectionpool:957} DEBUG - Starting new HTTPS connection (1): 127.0.0.1:6677
[2021-04-29 23:45:23,954] {urllib3.util.retry:438} DEBUG - Incremented Retry for (url='/qb/api/v2/auth/login'): Retry(total=0, connect=0, read=1, redirect=None, status=None)
[2021-04-29 23:45:23,955] {urllib3.connectionpool:744} WARNING - Retrying (Retry(total=0, connect=0, read=1, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f0ca842cca0>: Failed to establish a new connection: [Errno 111] Connection refused')': /qb/api/v2/auth/login
[2021-04-29 23:45:23,958] {urllib3.connectionpool:957} DEBUG - Starting new HTTPS connection (2): 127.0.0.1:6677
[2021-04-29 23:45:23,959] {qbittorrentapi.request:241} DEBUG - Failed to connect to qBittorrent. Connection Error: ConnectionError(MaxRetryError("HTTPSConnectionPool(host='127.0.0.1', port=6677): Max retries exceeded with url: /qb/api/v2/auth/login (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f0ca842c910>: Failed to establish a new connection: [Errno 111] Connection refused'))"))

Sooo....sorry for perhaps going a bit in circles earlier....

That said, I am really not sure what's going on.....does your PR actually fix your problem in your environment? (in case it is useful, you can install a python package repo with python -m pip install -e /path/to/directory/with/setup.py)

I think we need to understand which intervening system (e.g. nginx, gost, frp, etc...or even qBittorrent i guess) is returning these connection refused errors. Maybe there is something it doesn't like about the headers Im putting in to the POST requests....idk....

[EDIT rmartin] as for the browser working with WebUI....that at least proves communication is possible and we can get this working....however, the browsers are a lot more robust at dealing with all these esoteric systems and surely have many more failsafes in place to fix problems on the fly...)

@badgv
Copy link
Author

badgv commented Apr 30, 2021

Hello, I have modified the source code many times for testing,
The testing process is as follows:
First, I added 3 print parameters to view the code running process

            base_url = base_url._replace(netloc="%s:%s" % (base_url.netloc, port))

        # detect whether Web API is configured for HTTP or HTTPS
        logger.debug("Detecting scheme for URL...")
        try:
            # skip verification here...if there's a problem, we'll catch it during the actual API call
            r = requests_head(base_url.geturl(), allow_redirects=True, verify=False)
            # if WebUI eventually supports sending a redirect from HTTP to HTTPS then
            # Requests will automatically provide a URL using HTTPS.
            # For instance, the URL returned below will use the HTTPS scheme.
            #  >>> requests.head('http://grc.com', allow_redirects=True).url
            scheme = urlparse(r.url).scheme
            print('scheme-1',scheme)
        except requests_exceptions.RequestException:
            # assume alternative scheme will work...we'll fail later if neither are working
            scheme = alt_scheme
            print('scheme-2',scheme)
        print('scheme-0',scheme)
        # use detected scheme
        logger.debug("Using %s scheme" % scheme.upper())

image

Then I added the merge request code I submitted for testing:

        # detect whether Web API is configured for HTTP or HTTPS
        logger.debug("Detecting scheme for URL...")
        if host.startswith("//"):
            try:
            # skip verification here...if there's a problem, we'll catch it during the actual API call
                r = requests_head(base_url.geturl(), allow_redirects=True, verify=False)
            # if WebUI eventually supports sending a redirect from HTTP to HTTPS then
            # Requests will automatically provide a URL using HTTPS.
            # For instance, the URL returned below will use the HTTPS scheme.
            #  >>> requests.head('http://grc.com', allow_redirects=True).url
                scheme = urlparse(r.url).scheme
                print('scheme-1',scheme)
            except requests_exceptions.RequestException:
            # assume alternative scheme will work...we'll fail later if neither are working
                scheme = alt_scheme
                print('scheme-2',scheme)
        print('scheme-0',scheme)

image

I think we found the problem, and then I tried to modify the test:

        # detect whether Web API is configured for HTTP or HTTPS
        logger.debug("Detecting scheme for URL...")
        if host.startswith("//"):
            try:
            # skip verification here...if there's a problem, we'll catch it during the actual API call
                r = requests_head(base_url.geturl(), allow_redirects=True, verify=False)
            # if WebUI eventually supports sending a redirect from HTTP to HTTPS then
            # Requests will automatically provide a URL using HTTPS.
            # For instance, the URL returned below will use the HTTPS scheme.
            #  >>> requests.head('http://grc.com', allow_redirects=True).url
                scheme = urlparse(r.url).scheme
                print('scheme-1',scheme)
            except requests_exceptions.RequestException:
            # assume alternative scheme will work...we'll fail later if neither are working
                scheme = alt_scheme
                print('scheme-2',scheme)
        else:
            scheme = 'https' if host.startswith('https') else 'http'
        print('scheme-0',scheme)
        # use detected scheme

image

This time the connection was made normally!

@rmartin16
Copy link
Owner

Thank you for that investigation, @badgv.

While bypassing the url scheme determination logic apparently avoids your problems, I am hesitant to make your requested changes without better understanding why connection refused is being returned. I mean, something along the way to qBittorrent is actively rejecting the connection attempt....the logs for these intermediate systems probably have further details.

Additionally, though, it would likely introduce problems for other users if I stopped doing the scheme determination altogether....since they may be depending on it without even knowing.

All that said (and without knowing the true root cause of your problems), I am open to considering yet another flag to qbittorrentapi.Client() to skip the scheme determination logic.

Something like FORCE_HOST_SCHEME maybe:

qbt_client = qbittorrentapi.Client(host='http://127.0.0.1:6600/qb/', username='admin', password='adminadmin', FORCE_HOST_SCHEME=True)

@rmartin16
Copy link
Owner

all right....if you'd like to give this update a go:

python -m pip install git+https://github.com/rmartin16/qbittorrent-api.git@force-user-scheme#egg=qbittorrent-api

Then, you can bypass scheme detection with FORCE_SCHEME_FROM_HOST:

qbt_client = qbittorrentapi.Client(host='http://127.0.0.1:6600/qb/', username='admin', password='adminadmin', FORCE_SCHEME_FROM_HOST=True)

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants