mirror of
https://git.vectorsigma.ru/public/photoview.git
synced 2026-07-29 03:58:59 +00:00
* Split long functions and optimize code * Addressing comments --------- Co-authored-by: Konstantin Koval
48 lines
1000 B
Go
48 lines
1000 B
Go
package server
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"github.com/gorilla/websocket"
|
|
"github.com/photoview/photoview/api/utils"
|
|
)
|
|
|
|
func WebsocketUpgrader(devMode bool) websocket.Upgrader {
|
|
return websocket.Upgrader{
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
if devMode {
|
|
return true
|
|
} else {
|
|
uiEndpoint := utils.UiEndpointUrl()
|
|
if uiEndpoint == nil {
|
|
return true
|
|
}
|
|
|
|
if r.Header.Get("origin") == "" {
|
|
return true
|
|
}
|
|
|
|
originURL, err := url.Parse(r.Header.Get("origin"))
|
|
if err != nil {
|
|
log.Printf("Could not parse origin header of websocket request: %s", err)
|
|
return false
|
|
}
|
|
|
|
return isUIOnSameHost(uiEndpoint, originURL)
|
|
}
|
|
},
|
|
}
|
|
}
|
|
|
|
func isUIOnSameHost(uiEndpoint *url.URL, originURL *url.URL) bool {
|
|
if uiEndpoint.Host == originURL.Host {
|
|
return true
|
|
} else {
|
|
log.Printf("Not allowing websocket request from %s because it doesn't match PHOTOVIEW_UI_ENDPOINT %s",
|
|
originURL.Host, uiEndpoint.Host)
|
|
return false
|
|
}
|
|
}
|