Archived
2
0
This repository has been archived on 2024-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
neko-custom/server/neko.go

177 lines
4.1 KiB
Go
Raw Normal View History

2020-01-13 21:05:38 +13:00
package neko
import (
"fmt"
"os"
"os/signal"
"runtime"
"strings"
2020-01-13 21:05:38 +13:00
2022-09-13 08:12:47 +12:00
"m1k1o/neko/internal/capture"
2022-09-13 08:18:18 +12:00
"m1k1o/neko/internal/config"
2022-09-13 08:12:47 +12:00
"m1k1o/neko/internal/desktop"
2021-10-06 09:38:24 +13:00
"m1k1o/neko/internal/http"
"m1k1o/neko/internal/session"
"m1k1o/neko/internal/webrtc"
"m1k1o/neko/internal/websocket"
2020-01-13 21:05:38 +13:00
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
2020-11-03 08:11:27 +13:00
const Header = `&34
2020-01-19 12:30:09 +13:00
_ __ __
/ | / /__ / /______ \ /\
/ |/ / _ \/ //_/ __ \ ) ( ')
/ /| / __/ ,< / /_/ / ( / )
/_/ |_/\___/_/|_|\____/ \(__)|
&1&37 nurdism/m1k1o &33%s %s&0
2020-01-19 12:30:09 +13:00
`
2020-01-13 21:05:38 +13:00
var (
//
2020-01-26 23:43:08 +13:00
buildDate = "dev"
2020-01-13 21:05:38 +13:00
//
2020-01-26 23:43:08 +13:00
gitCommit = "dev"
2020-01-13 21:05:38 +13:00
//
2020-01-26 23:43:08 +13:00
gitBranch = "dev"
//
gitTag = "dev"
2020-01-13 21:05:38 +13:00
)
var Service *Neko
func init() {
Service = &Neko{
2020-01-19 12:30:09 +13:00
Version: &Version{
2020-01-26 23:43:08 +13:00
GitCommit: gitCommit,
GitBranch: gitBranch,
GitTag: gitTag,
2020-01-26 23:43:08 +13:00
BuildDate: buildDate,
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
2020-01-13 21:05:38 +13:00
},
2020-01-19 12:30:09 +13:00
Root: &config.Root{},
Server: &config.Server{},
2022-09-13 08:12:47 +12:00
Capture: &config.Capture{},
Desktop: &config.Desktop{},
2020-01-19 12:30:09 +13:00
WebRTC: &config.WebRTC{},
WebSocket: &config.WebSocket{},
2020-01-13 21:05:38 +13:00
}
}
2020-01-19 12:30:09 +13:00
type Version struct {
2020-01-26 23:43:08 +13:00
GitCommit string
GitBranch string
GitTag string
2020-01-26 23:43:08 +13:00
BuildDate string
GoVersion string
Compiler string
Platform string
2020-01-19 12:30:09 +13:00
}
func (i *Version) String() string {
version := i.GitTag
if version == "" || version == "dev" {
version = i.GitBranch
}
return fmt.Sprintf("%s@%s", version, i.GitCommit)
2020-01-26 23:43:08 +13:00
}
func (i *Version) Details() string {
return "\n" + strings.Join([]string{
fmt.Sprintf("Version %s", i.String()),
2020-01-26 23:43:08 +13:00
fmt.Sprintf("GitCommit %s", i.GitCommit),
fmt.Sprintf("GitBranch %s", i.GitBranch),
fmt.Sprintf("GitTag %s", i.GitTag),
2020-01-26 23:43:08 +13:00
fmt.Sprintf("BuildDate %s", i.BuildDate),
fmt.Sprintf("GoVersion %s", i.GoVersion),
fmt.Sprintf("Compiler %s", i.Compiler),
fmt.Sprintf("Platform %s", i.Platform),
}, "\n") + "\n"
2020-01-19 12:30:09 +13:00
}
2020-01-13 21:05:38 +13:00
type Neko struct {
2020-01-19 12:30:09 +13:00
Version *Version
Root *config.Root
2022-09-13 08:12:47 +12:00
Capture *config.Capture
Desktop *config.Desktop
2020-01-19 12:30:09 +13:00
Server *config.Server
WebRTC *config.WebRTC
WebSocket *config.WebSocket
logger zerolog.Logger
server *http.Server
2020-04-06 10:34:51 +12:00
sessionManager *session.SessionManager
2022-09-13 08:12:47 +12:00
captureManager *capture.CaptureManagerCtx
desktopManager *desktop.DesktopManagerCtx
2020-01-19 12:30:09 +13:00
webRTCManager *webrtc.WebRTCManager
webSocketHandler *websocket.WebSocketHandler
2020-01-13 21:05:38 +13:00
}
func (neko *Neko) Preflight() {
2020-01-19 12:30:09 +13:00
neko.logger = log.With().Str("service", "neko").Logger()
2020-01-13 21:05:38 +13:00
}
func (neko *Neko) Start() {
2022-09-17 22:43:17 +12:00
desktopManager := desktop.New(neko.Desktop)
2022-09-13 08:12:47 +12:00
desktopManager.Start()
2020-04-06 10:34:51 +12:00
2022-09-17 22:43:17 +12:00
captureManager := capture.New(desktopManager, neko.Capture)
2022-09-13 08:12:47 +12:00
captureManager.Start()
2022-09-13 08:12:47 +12:00
sessionManager := session.New(captureManager)
webRTCManager := webrtc.New(sessionManager, captureManager, desktopManager, neko.WebRTC)
2020-01-19 12:30:09 +13:00
webRTCManager.Start()
2020-01-14 21:36:48 +13:00
2022-09-17 22:43:17 +12:00
webSocketHandler := websocket.New(sessionManager, desktopManager, captureManager, webRTCManager, neko.WebSocket)
2020-01-19 12:30:09 +13:00
webSocketHandler.Start()
2020-01-14 12:12:55 +13:00
2022-09-18 04:17:04 +12:00
server := http.New(neko.Server, webSocketHandler, desktopManager)
2020-01-19 12:30:09 +13:00
server.Start()
2020-01-13 21:05:38 +13:00
2020-04-06 10:34:51 +12:00
neko.sessionManager = sessionManager
2022-09-13 08:12:47 +12:00
neko.captureManager = captureManager
neko.desktopManager = desktopManager
2020-01-19 12:30:09 +13:00
neko.webRTCManager = webRTCManager
neko.webSocketHandler = webSocketHandler
neko.server = server
2020-01-13 21:05:38 +13:00
}
func (neko *Neko) Shutdown() {
2021-10-25 05:31:16 +13:00
var err error
2021-10-06 10:10:10 +13:00
2022-09-14 06:33:00 +12:00
err = neko.server.Shutdown()
neko.logger.Err(err).Msg("server shutdown")
2022-09-13 08:12:47 +12:00
2022-09-14 06:33:00 +12:00
err = neko.webSocketHandler.Shutdown()
neko.logger.Err(err).Msg("websocket handler shutdown")
2020-01-19 12:30:09 +13:00
2021-10-25 05:31:16 +13:00
err = neko.webRTCManager.Shutdown()
neko.logger.Err(err).Msg("webrtc manager shutdown")
2020-01-19 12:30:09 +13:00
2022-09-14 06:33:00 +12:00
err = neko.captureManager.Shutdown()
neko.logger.Err(err).Msg("capture manager shutdown")
2021-10-25 05:31:16 +13:00
2022-09-14 06:33:00 +12:00
err = neko.desktopManager.Shutdown()
neko.logger.Err(err).Msg("desktop manager shutdown")
2020-01-13 21:05:38 +13:00
}
func (neko *Neko) ServeCommand(cmd *cobra.Command, args []string) {
2020-01-19 12:30:09 +13:00
neko.logger.Info().Msg("starting neko server")
2020-01-13 21:05:38 +13:00
neko.Start()
2020-01-19 12:30:09 +13:00
neko.logger.Info().Msg("neko ready")
2020-01-13 21:05:38 +13:00
2021-09-12 04:15:12 +12:00
quit := make(chan os.Signal, 1)
2020-01-13 21:05:38 +13:00
signal.Notify(quit, os.Interrupt)
sig := <-quit
2021-09-12 04:15:12 +12:00
neko.logger.Warn().Msgf("received %s, attempting graceful shutdown", sig)
2020-01-13 21:05:38 +13:00
neko.Shutdown()
2020-01-19 12:30:09 +13:00
neko.logger.Info().Msg("shutdown complete")
2020-01-13 21:05:38 +13:00
}