Use HTTP for IP host in getOriginFromHost()

This commit is contained in:
Yang Luo
2023-04-17 00:55:40 +08:00
parent 0d12972e92
commit 714f69be7b

View File

@ -18,6 +18,7 @@ import (
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"fmt" "fmt"
"net"
"strings" "strings"
"github.com/casdoor/casdoor/conf" "github.com/casdoor/casdoor/conf"
@ -43,6 +44,26 @@ type OidcDiscovery struct {
EndSessionEndpoint string `json:"end_session_endpoint"` EndSessionEndpoint string `json:"end_session_endpoint"`
} }
func isIpAddress(host string) bool {
// Attempt to split the host and port, ignoring the error
hostWithoutPort, _, err := net.SplitHostPort(host)
if err != nil {
// If an error occurs, it might be because there's no port
// In that case, use the original host string
hostWithoutPort = host
}
// Attempt to parse the host as an IP address (both IPv4 and IPv6)
ip := net.ParseIP(hostWithoutPort)
if ip != nil {
// The host is an IP address
return true
}
// The host is not an IP address
return false
}
func getOriginFromHost(host string) (string, string) { func getOriginFromHost(host string) (string, string) {
origin := conf.GetConfigString("origin") origin := conf.GetConfigString("origin")
if origin != "" { if origin != "" {
@ -52,6 +73,8 @@ func getOriginFromHost(host string) (string, string) {
protocol := "https://" protocol := "https://"
if strings.HasPrefix(host, "localhost") { if strings.HasPrefix(host, "localhost") {
protocol = "http://" protocol = "http://"
} else if isIpAddress(host) {
protocol = "http://"
} }
if host == "localhost:8000" { if host == "localhost:8000" {