lucasheld / uptime-kuma-api

A Python wrapper for the Uptime Kuma Socket.IO API
https://uptime-kuma-api.readthedocs.io
MIT License
272 stars 21 forks source link

Doesn't work behind reverse proxy #18

Closed m4dm4rtig4n closed 1 year ago

m4dm4rtig4n commented 1 year ago

Hello,

I Try to use your librarie behind traefik, but connection are always refuse :/

Traceback (most recent call last):
  File "/home/user/.local/lib/python3.10/site-packages/uptime_kuma_api/api.py", line 550, in connect
    self.sio.connect(f'{url}/socket.io/', wait_timeout=self.wait_timeout)
  File "/home/user/.local/lib/python3.10/site-packages/socketio/client.py", line 338, in connect
    raise exceptions.ConnectionError(exc.args[0]) from None
socketio.exceptions.ConnectionError: Unexpected response from server

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/git/k3s/main.py", line 306, in <module>
    STACK = UptimeKuma().get()
  File "/home/user/git/k3s/models/uptime_kuma.py", line 8, in __init__
    self.api = UptimeKumaApi(url)
  File "/home/user/.local/lib/python3.10/site-packages/uptime_kuma_api/api.py", line 417, in __init__
    self.connect()
  File "/home/user/.local/lib/python3.10/site-packages/uptime_kuma_api/api.py", line 552, in connect
    raise UptimeKumaException("unable to connect")
uptime_kuma_api.exceptions.UptimeKumaException: unable to connect
make: *** [Makefile:141 : retry] Erreur 1

My code :

from uptime_kuma_api import UptimeKumaApi, MonitorType

class UptimeKuma:

    def __init__(self, url="https://myurl.behind.reverseproxy"):
        self.api = UptimeKumaApi(url)
        self.api.login()
mariogarridopt commented 1 year ago

I was able to reproduce your problem. Let me describe the environment so we better debug this:

If you setup a traefik + uptime-kuma environment like this (docker-compose.yml):

version: "3.3"

services:
  traefik:
    image: "traefik:v2.9"
    container_name: "traefik"
    command:
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"

  uptime-kuma:
    image: louislam/uptime-kuma:1
    container_name: uptime-kuma
    volumes:
      - ./uptime-kuma-data:/app/data
    ports:
      - "3001:3001"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.uptimekuma.rule=Host(`uptimekuma.localhost`)"
      - "traefik.http.routers.uptimekuma.entrypoints=web"
      - "traefik.http.services.uptimekuma.loadbalancer.server.port=3001"

Just note that uptime-kuma has 2 ingress ways:

So now I made some tests:

from uptime_kuma_api import UptimeKumaApi, MonitorType

# connect
api = UptimeKumaApi('http://uptimekuma.localhost') # traefik proxy
#api = UptimeKumaApi('http://localhost:3001') # direct connection
api.login('admin', 'hddmg123')

monitor_list = api.get_monitors()
for monitor in monitor_list:
    print(monitor)

api.disconnect()

With this setup, I can connect using a direct connection but not with the proxy connection.

lucasheld commented 1 year ago

@mariogarridopt This works for me. But after the start I had to wait about two minutes until Traefik allowed the connection to uptime kuma. If you open the url in the web browser you see exactly the same behavior.

>>> api = UptimeKumaApi('http://uptimekuma.localhost')
>>> api.info()
{'version': '1.19.6', 'latestVersion': '1.19.5', 'primaryBaseURL': None, 'serverTimezone': 'Europe/Berlin', 'serverTimezoneOffset': '+01:00'}

But this is without SSL. Perhaps the above problem only occurs with encrypted connections.

@m4dm4rtig4n Can you please try to use replace https:// with wss:// in the url (wss://myurl.behind.reverseproxy)? Does Uptime Kuma works in the web browser?

m4dm4rtig4n commented 1 year ago

Can you please try to use replace https:// with wss:// in the url (wss://myurl.behind.reverseproxy)?

Same error :/

Does Uptime Kuma works in the web browser?

Service available on url : image

m4dm4rtig4n commented 1 year ago

I think found error, calls are blocked by my SSO...

m4dm4rtig4n commented 1 year ago

I have create port forwarding to skip my SSO and it's work. Sorry for inconveniant :/