Archived
2
0

more tweeks

This commit is contained in:
Craig 2020-01-13 23:12:55 +00:00
parent c85abb3b63
commit 5830f92637
15 changed files with 81 additions and 118 deletions

14
.docker/build Executable file
View File

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

View File

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

View File

@ -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"]

1
client/.env.development Normal file
View File

@ -0,0 +1 @@
NEKO_DEV=localhost:3000

View File

@ -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)

View File

@ -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),
}
}

View File

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

View File

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

View File

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

View File

@ -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()

View File

@ -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 {

View File

@ -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)

View File

@ -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() {