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