Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #144 from vania-pooh/master
Browse files Browse the repository at this point in the history
Added ability to proxy video (fixes #142)
  • Loading branch information
aandryashin authored Dec 19, 2017
2 parents 27d5f5c + 5548e91 commit bfe3ea0
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
33 changes: 31 additions & 2 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
routePath string = "/wd/hub/session"
proxyPath string = routePath + "/"
vncPath string = "/vnc/"
videoPath string = "/video/"
head int = len(proxyPath)
md5SumLength int = 32
tail int = head + md5SumLength
Expand Down Expand Up @@ -477,12 +478,11 @@ func vnc(wsconn *websocket.Conn) {
defer wsconn.Close()
confLock.RLock()
defer confLock.RUnlock()
sessionId := strings.Split(wsconn.Request().URL.Path, "/")[2]
head := len(vncPath)
tail := head + md5SumLength
path := wsconn.Request().URL.Path
if len(path) < tail {
log.Printf("[INVALID_VNC_REQUEST_URL] [%s]\n", wsconn.Request().URL.Path)
log.Printf("[INVALID_VNC_REQUEST_URL] [%s]\n", path)
return
}
sum := path[head:tail]
Expand All @@ -499,6 +499,7 @@ func vnc(wsconn *websocket.Conn) {
port = vncInfo.Port
path = vncInfo.Path
}
sessionId := strings.Split(wsconn.Request().URL.Path, "/")[2]
switch scheme {
case vncScheme:
proxyVNC(wsconn, sessionId, host, port)
Expand Down Expand Up @@ -548,6 +549,33 @@ func proxyConn(wsconn *websocket.Conn, conn net.Conn, err error, sessionId strin
log.Printf("[VNC_CLIENT_DISCONNECTED] [%s] [%s]\n", sessionId, address)
}

func video(w http.ResponseWriter, r *http.Request) {
confLock.RLock()
defer confLock.RUnlock()

head := len(videoPath)
tail := head + md5SumLength
path := r.URL.Path
if len(path) < tail {
log.Printf("[INVALID_VIDEO_REQUEST_URL] [%s]\n", path)
reply(w, errMsg("invalid video request URL"), http.StatusNotFound)
return
}
sum := path[head:tail]
sessionId := path[tail:]
h, ok := routes[sum]
if ok {
(&httputil.ReverseProxy{Director: func(r *http.Request) {
r.URL.Scheme = "http"
r.URL.Host = h.net()
r.URL.Path = fmt.Sprintf("/video/%s.mp4", sessionId)
}}).ServeHTTP(w, r)
} else {
log.Printf("[UNKNOWN_VIDEO_HOST] [%s]\n", sum)
reply(w, errMsg("unknown video host"), http.StatusNotFound)
}
}

func mux() http.Handler {
mux := http.NewServeMux()
authenticator := auth.NewBasicAuthenticator(
Expand All @@ -560,5 +588,6 @@ func mux() http.Handler {
mux.HandleFunc(routePath, withCloseNotifier(WithSuitableAuthentication(authenticator, postOnly(route))))
mux.Handle(proxyPath, &httputil.ReverseProxy{Director: proxy})
mux.Handle(vncPath, websocket.Handler(vnc))
mux.HandleFunc(videoPath, WithSuitableAuthentication(authenticator, video))
return mux
}
48 changes: 48 additions & 0 deletions proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,54 @@ func TestProxyScreenWebSocketsProtocol(t *testing.T) {

}

func TestProxyVideoFileWithoutAuth(t *testing.T) {
rsp, err := http.Get(gridrouter("/video/123.mp4"))

AssertThat(t, err, Is{nil})
AssertThat(t, rsp, Code{http.StatusUnauthorized})
}

func TestProxyVideoFile(t *testing.T) {

mux := http.NewServeMux()
mux.HandleFunc("/video/123.mp4", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})

fileServer := httptest.NewServer(mux)
defer fileServer.Close()

host, port := hostportnum(fileServer.URL)
node := Host{Name: host, Port: port, Count: 1}

test.Lock()
defer test.Unlock()

browsers := Browsers{Browsers: []Browser{
{Name: "browser", DefaultVersion: "1.0", Versions: []Version{
{Number: "1.0", Regions: []Region{
{Hosts: Hosts{
node,
}},
}},
}}}}
updateQuota(user, browsers)

sessionId := node.sum() + "123"

rsp, err := doBasicHTTPRequest(http.MethodGet, gridrouter(fmt.Sprintf("/video/%s", sessionId)), nil)
AssertThat(t, err, Is{nil})
AssertThat(t, rsp, Code{http.StatusOK})

rsp, err = doBasicHTTPRequest(http.MethodGet, gridrouter("/video/missing-file"), nil)
AssertThat(t, err, Is{nil})
AssertThat(t, rsp, Code{http.StatusNotFound})

rsp, err = doBasicHTTPRequest(http.MethodGet, gridrouter("/video/f7fd94f75c79c36e547c091632da440f_missing-file"), nil)
AssertThat(t, err, Is{nil})
AssertThat(t, rsp, Code{http.StatusNotFound})
}

func TestCreateSessionGet(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, gridrouter("/wd/hub/session"), nil)
req.SetBasicAuth("test", "test")
Expand Down

0 comments on commit bfe3ea0

Please sign in to comment.