Skip to content

Commit

Permalink
legacy: implement files.
Browse files Browse the repository at this point in the history
  • Loading branch information
m1k1o committed Sep 6, 2024
1 parent 47153d3 commit 94717bd
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 35 deletions.
72 changes: 54 additions & 18 deletions server/internal/http/legacy/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"io"
"net/http"
"strconv"
"net/url"
"time"

"m1k1o/neko/internal/api"
Expand Down Expand Up @@ -89,7 +89,7 @@ func (h *LegacyHandler) Route(r types.Router) {
defer s.destroy()

// dial to the remote backend
connBackend, _, err := DefaultDialer.Dial("ws://"+h.serverAddr+"/api/ws?token="+s.token, nil)
connBackend, _, err := DefaultDialer.Dial("ws://"+h.serverAddr+"/api/ws?token="+url.QueryEscape(s.token), nil)
if err != nil {
h.logger.Error().Err(err).Msg("couldn't dial to the remote backend")

Expand Down Expand Up @@ -282,37 +282,73 @@ func (h *LegacyHandler) Route(r types.Router) {
return utils.HttpUnauthorized().Msg("bad authorization")
}

quality, err := strconv.Atoi(r.URL.Query().Get("quality"))
quality := r.URL.Query().Get("quality")

// get the screenshot
body, headers, err := s.req(http.MethodGet, "/api/room/screen/shot.jpg?quality="+url.QueryEscape(quality), nil, nil)
if err != nil {
quality = 90
return utils.HttpInternalServerError().WithInternalErr(err)
}

w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Content-Type", "image/jpeg")
// copy headers
w.Header().Set("Content-Length", headers.Get("Content-Length"))
w.Header().Set("Content-Type", headers.Get("Content-Type"))

// get the screenshot
body, err := s.req(http.MethodGet, "/api/room/screen/shot.jpg?quality="+strconv.Itoa(quality), nil)
// copy the body to the response writer
_, err = io.Copy(w, body)
return err
})

// allow downloading and uploading files
r.Get("/file", func(w http.ResponseWriter, r *http.Request) error {
s := newSession(h.logger, h.serverAddr)

// create a new session
username := r.URL.Query().Get("usr")
password := r.URL.Query().Get("pwd")
err := s.create(username, password)
if err != nil {
return utils.HttpForbidden(err.Error())
}
defer s.destroy()

filename := r.URL.Query().Get("filename")

body, headers, err := s.req(http.MethodGet, "/api/filetransfer?filename="+url.QueryEscape(filename), r.Header, nil)
if err != nil {
return utils.HttpInternalServerError().WithInternalErr(err)
}

// copy headers
w.Header().Set("Content-Length", headers.Get("Content-Length"))
w.Header().Set("Content-Type", headers.Get("Content-Type"))

// copy the body to the response writer
_, err = io.Copy(w, body)
return err
})

/*
// allow downloading and uploading files
if webSocketHandler.FileTransferEnabled() {
r.Get("/file", func(w http.ResponseWriter, r *http.Request) error {
return nil
})
r.Post("/file", func(w http.ResponseWriter, r *http.Request) error {
s := newSession(h.logger, h.serverAddr)

r.Post("/file", func(w http.ResponseWriter, r *http.Request) error {
return nil
})
// create a new session
username := r.URL.Query().Get("usr")
password := r.URL.Query().Get("pwd")
err := s.create(username, password)
if err != nil {
return utils.HttpForbidden(err.Error())
}
*/
defer s.destroy()

body, _, err := s.req(http.MethodPost, "/api/filetransfer", r.Header, r.Body)
if err != nil {
return utils.HttpInternalServerError().WithInternalErr(err)
}

// copy the body to the response writer
_, err = io.Copy(w, body)
return err
})

r.Get("/health", func(w http.ResponseWriter, r *http.Request) error {
_, err := w.Write([]byte("true"))
Expand Down
40 changes: 23 additions & 17 deletions server/internal/http/legacy/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,23 @@ func newSession(logger zerolog.Logger, serverAddr string) *session {
}
}

func (s *session) req(method, path string, request any) (io.ReadCloser, error) {
body, err := json.Marshal(request)
func (s *session) req(method, path string, headers http.Header, request io.Reader) (io.ReadCloser, http.Header, error) {
req, err := http.NewRequest(method, "http://"+s.serverAddr+path, request)
if err != nil {
return nil, err
return nil, nil, err
}

req, err := http.NewRequest(method, "http://"+s.serverAddr+path, bytes.NewReader(body))
if err != nil {
return nil, err
for k, v := range headers {
req.Header[k] = v
}

req.Header.Set("Content-Type", "application/json")

if s.token != "" {
req.Header.Set("Authorization", "Bearer "+s.token)
}

res, err := s.client.Do(req)
if err != nil {
return nil, err
return nil, nil, err
}

if res.StatusCode < 200 || res.StatusCode >= 300 {
Expand All @@ -88,32 +85,41 @@ func (s *session) req(method, path string, request any) (io.ReadCloser, error) {
Message string `json:"message"`
}
if err := json.Unmarshal(body, &apiErr); err == nil {
return nil, fmt.Errorf("%w: %s", ErrBackendRespone, apiErr.Message)
return nil, nil, fmt.Errorf("%w: %s", ErrBackendRespone, apiErr.Message)
}
// return raw body if failed to unmarshal
return nil, fmt.Errorf("unexpected status code: %d, body: %s", res.StatusCode, strings.TrimSpace(string(body)))
return nil, nil, fmt.Errorf("unexpected status code: %d, body: %s", res.StatusCode, strings.TrimSpace(string(body)))
}

return res.Body, nil
return res.Body, res.Header, nil
}

func (s *session) apiReq(method, path string, request, response any) error {
body, err := s.req(method, path, request)
reqBody, err := json.Marshal(request)
if err != nil {
return err
}

headers := http.Header{
"Content-Type": []string{"application/json"},
}

resBody, _, err := s.req(method, path, headers, bytes.NewReader(reqBody))
if err != nil {
return err
}
defer body.Close()
defer resBody.Close()

if body == nil {
if resBody == nil {
return nil
}

if response == nil {
io.Copy(io.Discard, body)
io.Copy(io.Discard, resBody)
return nil
}

return json.NewDecoder(body).Decode(response)
return json.NewDecoder(resBody).Decode(response)
}

// send message to client (in old format)
Expand Down

0 comments on commit 94717bd

Please sign in to comment.