2022-06-29 16:08:55 +08:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2023-01-19 12:16:42 +08:00
|
|
|
stdpath "path"
|
2022-06-29 16:08:55 +08:00
|
|
|
"strings"
|
2022-08-03 14:26:59 +08:00
|
|
|
|
|
|
|
"github.com/alist-org/alist/v3/internal/conf"
|
2022-06-29 16:08:55 +08:00
|
|
|
)
|
|
|
|
|
2022-08-11 20:32:17 +08:00
|
|
|
func GetApiUrl(r *http.Request) string {
|
2022-09-25 17:57:54 +08:00
|
|
|
api := conf.Conf.SiteURL
|
2023-01-19 12:16:42 +08:00
|
|
|
if strings.HasPrefix(api, "http") {
|
|
|
|
return api
|
2022-09-25 17:57:54 +08:00
|
|
|
}
|
2023-02-21 19:57:50 +08:00
|
|
|
if r != nil {
|
2022-09-25 17:57:54 +08:00
|
|
|
protocol := "http"
|
2023-01-19 12:16:42 +08:00
|
|
|
if r.TLS != nil || r.Header.Get("X-Forwarded-Proto") == "https" {
|
2022-08-11 20:32:17 +08:00
|
|
|
protocol = "https"
|
|
|
|
}
|
2023-01-19 12:16:42 +08:00
|
|
|
host := r.Host
|
|
|
|
if r.Header.Get("X-Forwarded-Host") != "" {
|
|
|
|
host = r.Header.Get("X-Forwarded-Host")
|
|
|
|
}
|
|
|
|
api = fmt.Sprintf("%s://%s", protocol, stdpath.Join(host, api))
|
2022-06-30 22:41:55 +08:00
|
|
|
}
|
2022-08-11 20:32:17 +08:00
|
|
|
strings.TrimSuffix(api, "/")
|
|
|
|
return api
|
2022-06-29 16:08:55 +08:00
|
|
|
}
|