Files
filebrowser/backend/internal/web/proxy.go
T
+10 3c02eedca6 Beta/v2.0.0 (#2746)
Co-authored-by: Kevin B. <kurami32_kbm@proton.me>
Co-authored-by: Gabriel Górski <me@glaeqen.eu>
Co-authored-by: Joel Godfrey <82655889+BaccanoMob@users.noreply.github.com>
Co-authored-by: Shane Chiu <71911511+pwn2ooown@users.noreply.github.com>
Co-authored-by: carloda <35763669+carloda@users.noreply.github.com>
Co-authored-by: Taufik Hidayat <taufikdev88@gmail.com>
Co-authored-by: Stephan Paternotte <Stephan-P@users.noreply.github.com>
Co-authored-by: SIDDHU SINGH <siddhu3116@gmail.com>
Co-authored-by: Gaurav Kalal <117557391+Gorupa@users.noreply.github.com>
Co-authored-by: Nebula <40148908+nebula-it@users.noreply.github.com>
Co-authored-by: Juan Soler <juansoler@gmail.com>
Co-authored-by: codacy-production[bot] <61871480+codacy-production[bot]@users.noreply.github.com>
Co-authored-by: Anatolii Prylutskyi <anpryl@gmail.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: Graham Steffaniak <graham@debian-lake.ghome.net>
Co-authored-by: Neil <kylelzzz02@icloud.com>
Co-authored-by: Graham <info@quantumx-apps.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-07 16:24:38 -05:00

90 lines
2.4 KiB
Go

package web
import (
"fmt"
"net/http"
"strings"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/share"
"github.com/gtsteffaniak/filebrowser/backend/pkg/settings"
)
func normalizeHTTPScheme(proto string) string {
p := strings.ToLower(strings.TrimSpace(proto))
switch p {
case "http", "https":
return p
default:
return ""
}
}
func firstForwardedValue(v string) string {
v = strings.TrimSpace(v)
if i := strings.Index(v, ","); i >= 0 {
v = strings.TrimSpace(v[:i])
}
return v
}
// requestScheme returns http or https from TLS and optionally trusted X-Forwarded-Proto.
func requestScheme(r *http.Request) string {
cfg := &settings.Config
if cfg.Http.TrustProxyHeaders {
if proto := normalizeHTTPScheme(r.Header.Get("X-Forwarded-Proto")); proto != "" {
return proto
}
}
if r.TLS != nil {
return "https"
}
return "http"
}
// requestHost returns the client-facing host, honoring trusted X-Forwarded-Host.
func requestHost(r *http.Request) string {
cfg := &settings.Config
if cfg.Http.TrustProxyHeaders {
if h := firstForwardedValue(r.Header.Get("X-Forwarded-Host")); h != "" {
return h
}
}
return r.Host
}
// requestSchemeForPublicURL picks scheme for absolute share URLs (HTTPS default when forwarded host is trusted).
func requestSchemeForPublicURL(r *http.Request) string {
cfg := &settings.Config
if cfg.Http.TrustProxyHeaders {
if firstForwardedValue(r.Header.Get("X-Forwarded-Host")) != "" {
if proto := normalizeHTTPScheme(r.Header.Get("X-Forwarded-Proto")); proto != "" {
return proto
}
return "https"
}
}
return requestScheme(r)
}
func shareURLParams(r *http.Request) (host, scheme string) {
if r == nil {
return "", ""
}
return requestHost(r), requestSchemeForPublicURL(r)
}
// requestPageFullURL builds the canonical page URL from the request (or http.externalUrl when set).
func requestPageFullURL(r *http.Request) string {
if settings.Config.Http.ExternalUrl != "" {
return strings.TrimSuffix(settings.Config.Http.ExternalUrl, "/") + r.URL.Path
}
host, scheme := shareURLParams(r)
return fmt.Sprintf("%s://%s%s", scheme, host, r.URL.Path)
}
// ShareURLFromRequest builds a public share or direct-download URL from the request and server config.
func ShareURLFromRequest(r *http.Request, hash string, isDirectDownload bool, token string) string {
host, scheme := shareURLParams(r)
return share.PublicShareURL(host, scheme, hash, isDirectDownload, token)
}