Files
photoview/api/server/websocket.go
Kostiantyn 69e595000d Refactoring part 2.11: Long func refactoring in server and utils (#1083)
* Split long functions and optimize code

* Addressing comments

---------

Co-authored-by: Konstantin Koval
2024-10-09 18:20:03 +03:00

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
}
}