jocxfin / pwgen

Simple self hosted password generator
GNU Affero General Public License v3.0
61 stars 4 forks source link

[BUG] not compatible with traefik reverse proxy && PathPrefix(`/pwgen`) #91

Open E-J-D opened 1 month ago

E-J-D commented 1 month ago

Describe the bug Using pwgen behind a traefik reverse proxy with && PathPrefix(/pwgen) function breaks the ui.

To Reproduce Steps to reproduce the behavior: Use docker compose file

``

services:
  pwgen:
    image: jocxfin/pwgen:latest
    container_name: pwgen
    ports:
      - 5069:5069
    environment:
      - NO_API_CHECK=false
      - PW_LENGTH=16
      - PW_INCLUDE_UPPERCASE=true
      - PW_INCLUDE_DIGITS=true
      - PW_INCLUDE_SPECIAL=true
      - PW_EXCLUDE_HOMOGLYPHS=true
      - PP_WORD_COUNT=4
      - PP_CAPITALIZE=true
      - PP_SEPARATOR_TYPE=dash
      - PP_USER_DEFINED_SEPARATOR='+'
      - PP_MAX_WORD_LENGTH=5
      - PP_INCLUDE_NUMBERS=true
      - PP_INCLUDE_SPECIAL_CHARS=false
      - PP_HIDE_LANG=true
      - MULTI_GEN=true
      - GENERATE_PP=true
      - ROBOTS_ALLOW=false

    labels:
      - "traefik.enable=true" #
      - "traefik.http.routers.pwgen.entrypoints=web,websecure"
      **- "traefik.http.routers.pwgen.rule=Host(`sub.domain.de`) && PathPrefix(`/pwgen`)"**
      - "traefik.http.routers.pwgen.service=pwgen-service"
      - "traefik.http.routers.pwgen.tls.certresolver=letsencrypt"
      - "traefik.http.routers.pwgen.tls=true"
      - "traefik.http.services.pwgen-service.loadbalancer.server.port=5069"

# https://iceburn.medium.com/reverse-proxy-in-traefik-with-subdirectories-eef4261939e
      - "traefik.http.routers.pwgen.middlewares=pwgen-stripprefix"
      - "traefik.http.middlewares.pwgen-stripprefix.stripprefix.prefixes=/pwgen"

# https://community.traefik.io/t/web-page-behind-traefik-2-3-shows-partially/7958/3
      - "traefik.http.routers.pwgen.middlewares=pwgen-redirectregex, pwgen-replacepathregex"
      - "traefik.http.middlewares.pwgen-replacepathregex.replacepathregex.regex=^/pwgen/(.*)"
      - "traefik.http.middlewares.pwgen-replacepathregex.replacepathregex.replacement=/$$1"
      - "traefik.http.middlewares.pwgen-redirectregex.redirectregex.regex=^(.*)/pwgen$$"
      - "traefik.http.middlewares.pwgen-redirectregex.redirectregex.replacement=$$1/pwgen/"

    networks:
      proxy: null
    restart: always

networks:
  proxy:
    external: true
`

The container starts and traefik routes the https://sub.domain.de/pwgen request to the container, but the ui is broken.

Expected behavior Ui should be the same as browsing to the internal container IP e.g. http://10.200.100.1:5069 which works perfectly.

Screenshots grafik

Desktop (please complete the following information):

Would love to use pwgen in my docker enviroment which is handled by traefik as a reverse proxy and handles multiple container as a path instead of subdomains.

jocxfin commented 1 month ago

Hey! Thanks for the report. I haven't used traefik myself, so can't really debug this. Are there any console logs or anything from the browser? First thing that pops into my head is some kind of CORS limitation within traefik. I tried this with cloudflare tunnel and it seems to work just fine. Although there seems to be some kinds of issues with previous prs, so I might have to roll back some changes. I'll let you know. I also don't have much time to develop this, but I'll try my best!

veerendra2 commented 6 days ago

Hi @E-J-D, you need to set BASEA_PATH in environmental variables (For example; BASE_PATH=/pwgen/) and don't have to add stripprefix or redirectregex, because the app is already aware of the base path(BASEA_PATH).

So, you just have to add PathPrefix in traefik labels like below

...
    labels:
     ...
      - traefik.http.routers.pwgen.rule=Host(`${MY_DOMAIN}`) && PathPrefix(`/pwgen`)
    environment:
      BASE_PATH: "/pwgen/"

NOTE: While setting environmental variable BASE_PATH, make sure end with trailing slash(/) i.e. BASE_PATH: "/pwgen/" otherwise it won't generate the password

E-J-D commented 3 days ago

Hi @veerendra2, thanks for your help. You made my day. Adding BASE_PATH=/pwgen/ to the environment did the trick. Thanks! And sorry @jocxfin for creating a ticket, which did not belong to your software.