From 5830f92637fab86186306bea83ec70eb6bc2eca0 Mon Sep 17 00:00:00 2001 From: Craig Date: Mon, 13 Jan 2020 23:12:55 +0000 Subject: [PATCH] more tweeks --- .docker/build | 14 ++++++ .docker/build.sh | 12 ----- .docker/{test.sh => test} | 0 .docker/{x11vnc.sh => x11vnc} | 0 Dockerfile | 13 +++--- client/.env.development | 1 + client/src/App.vue | 6 +-- server/internal/http/api.go | 14 ------ server/internal/http/handler/handler.go | 55 ----------------------- server/internal/http/handler/ping.go | 10 ----- server/internal/http/handler/websocket.go | 9 ---- server/internal/preflight/config.go | 6 +-- server/internal/preflight/logs.go | 4 +- server/internal/webrtc/websocket.go | 6 ++- server/neko.go | 49 +++++++++++++++++++- 15 files changed, 81 insertions(+), 118 deletions(-) create mode 100755 .docker/build delete mode 100755 .docker/build.sh rename .docker/{test.sh => test} (100%) rename .docker/{x11vnc.sh => x11vnc} (100%) create mode 100644 client/.env.development delete mode 100644 server/internal/http/api.go delete mode 100644 server/internal/http/handler/handler.go delete mode 100644 server/internal/http/handler/ping.go delete mode 100644 server/internal/http/handler/websocket.go diff --git a/.docker/build b/.docker/build new file mode 100755 index 00000000..a1bddda8 --- /dev/null +++ b/.docker/build @@ -0,0 +1,14 @@ +#!/bin/bash + +cd ../server +go get && make build + +cd ../client +npm install && npm run build + +cd ../ +sudo docker build -f Dockerfile -t neko . + +# sudo docker run -p 8080:8080 --shm-size=2gb neko:latest +# sudo docker run --network host --shm-size=2gb neko:latest +# sudo docker run --network host --shm-size=2gb -it neko:latest /bin/bash \ No newline at end of file diff --git a/.docker/build.sh b/.docker/build.sh deleted file mode 100755 index 2de139ca..00000000 --- a/.docker/build.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash - -cd ../server -go get && make build - -cd ../client -npm install && npm run build - -cd ../ -sudo docker build -f Dockerfile -t neko . - -# sudo docker run -p 8080:8080 --shm-size=2gb neko:latest \ No newline at end of file diff --git a/.docker/test.sh b/.docker/test similarity index 100% rename from .docker/test.sh rename to .docker/test diff --git a/.docker/x11vnc.sh b/.docker/x11vnc similarity index 100% rename from .docker/x11vnc.sh rename to .docker/x11vnc diff --git a/Dockerfile b/Dockerfile index 43dcf8f7..30311313 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,7 +9,7 @@ ENV DEBIAN_FRONTEND=noninteractive # Install dependencies ttf-freefont RUN apt-get update \ - && apt-get -y install curl supervisor openbox dbus-x11 xvfb pulseaudio firefox-esr \ + && apt-get -y install curl apt-utils supervisor openbox dbus-x11 xvfb pulseaudio firefox-esr \ && apt-get -y install gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-pulseaudio libxcb-xkb-dev libxkbcommon-x11-dev \ # # Create a non-root user @@ -29,10 +29,13 @@ RUN apt-get update \ && curl -o /usr/lib/firefox-esr/distribution/extensions/uBlock0@raymondhill.net.xpi https://addons.mozilla.org/firefox/downloads/latest/ublock-origin/addon-607454-latest.xpi \ # # Setup Pulse Audio - mkdir -p /home/$USERNAME/.config/pulse/ \ + && mkdir -p /home/$USERNAME/.config/pulse/ \ && echo "default-server=unix:/tmp/pulseaudio.socket" > /home/$USERNAME/.config/pulse/client.conf \ && chown -R $USERNAME:$USERNAME /home/$USERNAME \ # + # Workaround for an X11 problem. See: http://blog.tigerteufel.de/?p=476 + && mkdir /tmp/.X11-unix && chmod 1777 /tmp/.X11-unix && chown $USERNAME /tmp/.X11-unix/ \ + # # Make directories for neko && mkdir -p /etc/neko /var/www \ # @@ -62,9 +65,7 @@ ENV NEKO_DISPLAY=0 ENV NEKO_WIDTH=1280 ENV NEKO_HEIGHT=720 ENV NEKO_PASSWORD=neko -ENV NEKO_BIND=0.0.0.0:8080 -ENV NEKO_KEY= -ENV NEKO_CERT= +ENV NEKO_BIND=:8080 # Nayn ENV NEKO_URL=https://www.youtube.com/embed/QH2-TGUlwu4 @@ -74,5 +75,5 @@ ENV NEKO_URL=https://www.youtube.com/embed/QH2-TGUlwu4 COPY .docker/entrypoint.sh /entrypoint.sh # -# Run entrypoint +# Run Service CMD ["/bin/bash", "/entrypoint.sh"] \ No newline at end of file diff --git a/client/.env.development b/client/.env.development new file mode 100644 index 00000000..4e3b4138 --- /dev/null +++ b/client/.env.development @@ -0,0 +1 @@ +NEKO_DEV=localhost:3000 diff --git a/client/src/App.vue b/client/src/App.vue index dfcf29cb..e8fe0719 100644 --- a/client/src/App.vue +++ b/client/src/App.vue @@ -470,12 +470,10 @@ } connect() { - /* this.ws = new WebSocket( - `${/https/gi.test(location.protocol) ? 'wss' : 'ws'}://${location.host}/ws?password=${this.password}`, + process.env.NODE_ENV === 'development' ? `ws://${process.env.NEKO_DEV}/ws?password=${this.password}` : `${/https/gi.test(location.protocol) ? 'wss' : 'ws'}://${location.host}/ws?password=${this.password}` , ) - */ - this.ws = new WebSocket(`ws://localhost:3000/ws?password=${this.password}`) + this.ws.onmessage = this.onMessage.bind(this) this.ws.onerror = event => console.error((event as ErrorEvent).error) this.ws.onclose = event => this.onClose.bind(this) diff --git a/server/internal/http/api.go b/server/internal/http/api.go deleted file mode 100644 index d0f98465..00000000 --- a/server/internal/http/api.go +++ /dev/null @@ -1,14 +0,0 @@ -package http - -import ( - "net/http" - - "n.eko.moe/neko/internal/http/handler" -) - -func New(bind, password, static string) *http.Server { - return &http.Server{ - Addr: bind, - Handler: handler.New(password, static), - } -} diff --git a/server/internal/http/handler/handler.go b/server/internal/http/handler/handler.go deleted file mode 100644 index 458f6a9d..00000000 --- a/server/internal/http/handler/handler.go +++ /dev/null @@ -1,55 +0,0 @@ -package handler - -import ( - "fmt" - "net/http" - "os" - - "n.eko.moe/neko/internal/http/middleware" - "n.eko.moe/neko/internal/http/endpoint" - "n.eko.moe/neko/internal/webrtc" - - "github.com/go-chi/chi" -) - -type Handler struct { - router *chi.Mux - manager *webrtc.WebRTCManager -} - -func New(password, static string) *chi.Mux { - router := chi.NewRouter() - manager, err := webrtc.NewManager(password) - if err != nil { - panic(err) - } - - handler := &Handler{ - router: router, - manager: manager, - } - - router.Use(middleware.Recoverer) // Recover from panics without crashing server - // router.Use(middleware.Logger) // Log API request calls - - router.Get("/ping", endpoint.Handle(handler.Ping)) - router.Get("/ws", endpoint.Handle(handler.WebSocket)) - - fs := http.FileServer(http.Dir(static)) - router.Get("/*", func(w http.ResponseWriter, r *http.Request) { - if _, err := os.Stat(static + r.RequestURI); os.IsNotExist(err) { - http.StripPrefix(r.RequestURI, fs).ServeHTTP(w, r) - } else { - fs.ServeHTTP(w, r) - } - }) - - router.NotFound(endpoint.Handle(func(w http.ResponseWriter, r *http.Request) error { - return &endpoint.HandlerError{ - Status: http.StatusNotFound, - Message: fmt.Sprintf("Endpoint '%s' is not avalible", r.RequestURI), - } - })) - - return router -} diff --git a/server/internal/http/handler/ping.go b/server/internal/http/handler/ping.go deleted file mode 100644 index b2e20189..00000000 --- a/server/internal/http/handler/ping.go +++ /dev/null @@ -1,10 +0,0 @@ -package handler - -import "net/http" - -func (h *Handler) Ping(w http.ResponseWriter, r *http.Request) error { - w.Header().Set("Content-Type", "text/plain") - w.WriteHeader(http.StatusOK) - w.Write([]byte(".")) - return nil -} diff --git a/server/internal/http/handler/websocket.go b/server/internal/http/handler/websocket.go deleted file mode 100644 index 9087a410..00000000 --- a/server/internal/http/handler/websocket.go +++ /dev/null @@ -1,9 +0,0 @@ -package handler - -import ( - "net/http" -) - -func (h *Handler) WebSocket(w http.ResponseWriter, r *http.Request) error { - return h.manager.Upgrade(w, r) -} diff --git a/server/internal/preflight/config.go b/server/internal/preflight/config.go index e5426d60..b4006452 100644 --- a/server/internal/preflight/config.go +++ b/server/internal/preflight/config.go @@ -8,7 +8,7 @@ import ( ) func Config(name string) { - config := viper.GetString("neko.config") + config := viper.GetString("config") if config != "" { viper.SetConfigFile(config) // Use config file from the flag. @@ -35,8 +35,8 @@ func Config(name string) { file := viper.ConfigFileUsed() logger := log.With(). - Bool("debug", viper.GetBool("neko.debug")). - Str("logging", viper.GetString("neko.logs")). + Bool("debug", viper.GetBool("debug")). + Str("logging", viper.GetString("logs")). Str("config", file). Logger() diff --git a/server/internal/preflight/logs.go b/server/internal/preflight/logs.go index e921ad33..518f07ec 100644 --- a/server/internal/preflight/logs.go +++ b/server/internal/preflight/logs.go @@ -18,13 +18,13 @@ func Logs(name string) { zerolog.TimeFieldFormat = "" zerolog.SetGlobalLevel(zerolog.InfoLevel) - if viper.GetBool("neko.debug") { + if viper.GetBool("debug") { zerolog.SetGlobalLevel(zerolog.DebugLevel) } console := zerolog.ConsoleWriter{Out: os.Stdout} - if !viper.GetBool("neko.logs") { + if !viper.GetBool("logs") { log.Logger = log.Output(console) } else { diff --git a/server/internal/webrtc/websocket.go b/server/internal/webrtc/websocket.go index 7677715f..4671f55d 100644 --- a/server/internal/webrtc/websocket.go +++ b/server/internal/webrtc/websocket.go @@ -18,10 +18,14 @@ const ( ) func (manager *WebRTCManager) Upgrade(w http.ResponseWriter, r *http.Request) error { + manager.logger. + Info(). + Msg("Attempting to upgrade ws") + socket, err := manager.upgrader.Upgrade(w, r, nil) if err != nil { manager.logger.Error().Err(err).Msg("Failed to upgrade websocket!") - return nil + return err } sessionID, ok := manager.authenticate(r) diff --git a/server/neko.go b/server/neko.go index a870f019..4f13fcc4 100644 --- a/server/neko.go +++ b/server/neko.go @@ -9,9 +9,12 @@ import ( "runtime" "n.eko.moe/neko/internal/config" + "n.eko.moe/neko/internal/http/endpoint" + "n.eko.moe/neko/internal/http/middleware" "n.eko.moe/neko/internal/structs" - api "n.eko.moe/neko/internal/http" + "n.eko.moe/neko/internal/webrtc" + "github.com/go-chi/chi" "github.com/rs/zerolog" "github.com/rs/zerolog/log" "github.com/spf13/cobra" @@ -68,7 +71,49 @@ func (neko *Neko) Preflight() { } func (neko *Neko) Start() { - server := api.New(neko.Serve.Bind, neko.Serve.Password, neko.Serve.Static) + router := chi.NewRouter() + + manager, err := webrtc.NewManager(neko.Serve.Password) + if err != nil { + neko.Logger.Panic().Err(err).Msg("Can not start webrtc manager") + } + + router.Use(middleware.Recoverer) // Recover from panics without crashing server + router.Use(middleware.RequestID) // Create a request ID for each request + router.Use(middleware.Logger) // Log API request calls + + router.Get("/ping", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/plain") + w.WriteHeader(http.StatusOK) + w.Write([]byte(".")) + }) + + router.Get("/ws", func(w http.ResponseWriter, r *http.Request) { + if err := manager.Upgrade(w, r); err != nil { + neko.Logger.Error().Err(err).Msg("session.destroy has failed") + } + }) + + fs := http.FileServer(http.Dir(neko.Serve.Static)) + router.Get("/*", func(w http.ResponseWriter, r *http.Request) { + if _, err := os.Stat(neko.Serve.Static + r.RequestURI); os.IsNotExist(err) { + http.StripPrefix(r.RequestURI, fs).ServeHTTP(w, r) + } else { + fs.ServeHTTP(w, r) + } + }) + + router.NotFound(endpoint.Handle(func(w http.ResponseWriter, r *http.Request) error { + return &endpoint.HandlerError{ + Status: http.StatusNotFound, + Message: fmt.Sprintf("Endpoint '%s' is not avalible", r.RequestURI), + } + })) + + server := &http.Server{ + Addr: neko.Serve.Bind, + Handler: router, + } if neko.Serve.Cert != "" && neko.Serve.Key != "" { go func() {