mirror of
https://github.com/m1k1o/neko.git
synced 2026-11-03 00:11:48 -06:00
3191dbb058
- Deleted the legacy WebSocket client implementation (`wstoclient.go`) and its test file (`wstoclient_test.go`). - Removed legacy handling from the HTTP manager, including legacy proxy setup and routing. - Cleaned up session management by removing legacy host checks and related functions. - Eliminated the legacy WebRTC handler and its associated logic. - Updated session interface to remove legacy methods. - Removed legacy API documentation and configuration options from migration guide and help files.
191 lines
6.7 KiB
Go
191 lines
6.7 KiB
Go
package config
|
|
|
|
import (
|
|
"path"
|
|
"slices"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type Server struct {
|
|
Cert string
|
|
Key string
|
|
Bind string
|
|
Proxy bool
|
|
Static string
|
|
PathPrefix string
|
|
PProf bool
|
|
Metrics bool
|
|
CORS []string
|
|
}
|
|
|
|
func (Server) Init(cmd *cobra.Command) error {
|
|
cmd.PersistentFlags().String("server.bind", "127.0.0.1:8080", "address/port/socket to serve neko")
|
|
if err := viper.BindPFlag("server.bind", cmd.PersistentFlags().Lookup("server.bind")); err != nil {
|
|
return err
|
|
}
|
|
|
|
cmd.PersistentFlags().String("server.cert", "", "path to the SSL cert used to secure the neko server")
|
|
if err := viper.BindPFlag("server.cert", cmd.PersistentFlags().Lookup("server.cert")); err != nil {
|
|
return err
|
|
}
|
|
|
|
cmd.PersistentFlags().String("server.key", "", "path to the SSL key used to secure the neko server")
|
|
if err := viper.BindPFlag("server.key", cmd.PersistentFlags().Lookup("server.key")); err != nil {
|
|
return err
|
|
}
|
|
|
|
cmd.PersistentFlags().Bool("server.proxy", false, "trust reverse proxy headers")
|
|
if err := viper.BindPFlag("server.proxy", cmd.PersistentFlags().Lookup("server.proxy")); err != nil {
|
|
return err
|
|
}
|
|
|
|
cmd.PersistentFlags().String("server.static", "", "path to neko client files to serve")
|
|
if err := viper.BindPFlag("server.static", cmd.PersistentFlags().Lookup("server.static")); err != nil {
|
|
return err
|
|
}
|
|
|
|
cmd.PersistentFlags().String("server.path_prefix", "/", "path prefix for HTTP requests")
|
|
if err := viper.BindPFlag("server.path_prefix", cmd.PersistentFlags().Lookup("server.path_prefix")); err != nil {
|
|
return err
|
|
}
|
|
|
|
cmd.PersistentFlags().Bool("server.pprof", false, "enable pprof endpoint available at /debug/pprof")
|
|
if err := viper.BindPFlag("server.pprof", cmd.PersistentFlags().Lookup("server.pprof")); err != nil {
|
|
return err
|
|
}
|
|
|
|
cmd.PersistentFlags().Bool("server.metrics", true, "enable prometheus metrics available at /metrics")
|
|
if err := viper.BindPFlag("server.metrics", cmd.PersistentFlags().Lookup("server.metrics")); err != nil {
|
|
return err
|
|
}
|
|
|
|
cmd.PersistentFlags().StringSlice("server.cors", []string{}, "list of allowed origins for CORS, if empty CORS is disabled, if '*' is present all origins are allowed")
|
|
if err := viper.BindPFlag("server.cors", cmd.PersistentFlags().Lookup("server.cors")); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (Server) InitV2(cmd *cobra.Command) error {
|
|
cmd.PersistentFlags().String("bind", "", "V2: address/port/socket to serve neko")
|
|
if err := viper.BindPFlag("bind", cmd.PersistentFlags().Lookup("bind")); err != nil {
|
|
return err
|
|
}
|
|
|
|
cmd.PersistentFlags().String("cert", "", "V2: path to the SSL cert used to secure the neko server")
|
|
if err := viper.BindPFlag("cert", cmd.PersistentFlags().Lookup("cert")); err != nil {
|
|
return err
|
|
}
|
|
|
|
cmd.PersistentFlags().String("key", "", "V2: path to the SSL key used to secure the neko server")
|
|
if err := viper.BindPFlag("key", cmd.PersistentFlags().Lookup("key")); err != nil {
|
|
return err
|
|
}
|
|
|
|
cmd.PersistentFlags().Bool("proxy", false, "V2: enable reverse proxy mode")
|
|
if err := viper.BindPFlag("proxy", cmd.PersistentFlags().Lookup("proxy")); err != nil {
|
|
return err
|
|
}
|
|
|
|
cmd.PersistentFlags().String("static", "", "V2: path to neko client files to serve")
|
|
if err := viper.BindPFlag("static", cmd.PersistentFlags().Lookup("static")); err != nil {
|
|
return err
|
|
}
|
|
|
|
cmd.PersistentFlags().String("path_prefix", "", "V2: path prefix for HTTP requests")
|
|
if err := viper.BindPFlag("path_prefix", cmd.PersistentFlags().Lookup("path_prefix")); err != nil {
|
|
return err
|
|
}
|
|
|
|
cmd.PersistentFlags().StringSlice("cors", []string{}, "V2: list of allowed origins for CORS")
|
|
if err := viper.BindPFlag("cors", cmd.PersistentFlags().Lookup("cors")); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *Server) Set() {
|
|
s.Cert = viper.GetString("server.cert")
|
|
s.Key = viper.GetString("server.key")
|
|
s.Bind = viper.GetString("server.bind")
|
|
s.Proxy = viper.GetBool("server.proxy")
|
|
s.Static = viper.GetString("server.static")
|
|
s.PathPrefix = path.Join("/", path.Clean(viper.GetString("server.path_prefix")))
|
|
s.PProf = viper.GetBool("server.pprof")
|
|
s.Metrics = viper.GetBool("server.metrics")
|
|
|
|
s.CORS = viper.GetStringSlice("server.cors")
|
|
if slices.Contains(s.CORS, "*") {
|
|
s.CORS = []string{"*"}
|
|
log.Warn().Msg("CORS is enabled for all origins, this is not recommended for production environments")
|
|
}
|
|
}
|
|
|
|
func (s *Server) SetV2() {
|
|
deprecatedV2ConfigUsed := false
|
|
|
|
if viper.IsSet("cert") {
|
|
s.Cert = viper.GetString("cert")
|
|
log.Warn().Msg("you are using v2 configuration 'NEKO_CERT' which is deprecated, please use 'NEKO_SERVER_CERT' instead")
|
|
deprecatedV2ConfigUsed = true
|
|
}
|
|
if viper.IsSet("key") {
|
|
s.Key = viper.GetString("key")
|
|
log.Warn().Msg("you are using v2 configuration 'NEKO_KEY' which is deprecated, please use 'NEKO_SERVER_KEY' instead")
|
|
deprecatedV2ConfigUsed = true
|
|
}
|
|
if viper.IsSet("bind") {
|
|
s.Bind = viper.GetString("bind")
|
|
log.Warn().Msg("you are using v2 configuration 'NEKO_BIND' which is deprecated, please use 'NEKO_SERVER_BIND' instead")
|
|
deprecatedV2ConfigUsed = true
|
|
}
|
|
if viper.IsSet("proxy") {
|
|
s.Proxy = viper.GetBool("proxy")
|
|
log.Warn().Msg("you are using v2 configuration 'NEKO_PROXY' which is deprecated, please use 'NEKO_SERVER_PROXY' instead")
|
|
deprecatedV2ConfigUsed = true
|
|
}
|
|
if viper.IsSet("static") {
|
|
s.Static = viper.GetString("static")
|
|
log.Warn().Msg("you are using v2 configuration 'NEKO_STATIC' which is deprecated, please use 'NEKO_SERVER_STATIC' instead")
|
|
deprecatedV2ConfigUsed = true
|
|
}
|
|
if viper.IsSet("path_prefix") {
|
|
s.PathPrefix = path.Join("/", path.Clean(viper.GetString("path_prefix")))
|
|
log.Warn().Msg("you are using v2 configuration 'NEKO_PATH_PREFIX' which is deprecated, please use 'NEKO_SERVER_PATH_PREFIX' instead")
|
|
deprecatedV2ConfigUsed = true
|
|
}
|
|
if viper.IsSet("cors") {
|
|
s.CORS = viper.GetStringSlice("cors")
|
|
if slices.Contains(s.CORS, "*") {
|
|
s.CORS = []string{"*"}
|
|
log.Warn().Msg("CORS is enabled for all origins, this is not recommended for production environments")
|
|
}
|
|
log.Warn().Msg("you are using v2 configuration 'NEKO_CORS' which is deprecated, please use 'NEKO_SERVER_CORS' instead")
|
|
deprecatedV2ConfigUsed = true
|
|
}
|
|
|
|
if deprecatedV2ConfigUsed {
|
|
log.Warn().Msg("deprecated V2 configuration is in use; migrate to V3 configuration, see https://neko.m1k1o.net/docs/v3/migration-from-v2")
|
|
}
|
|
}
|
|
|
|
func (s *Server) HasCors() bool {
|
|
return len(s.CORS) > 0
|
|
}
|
|
|
|
func (s *Server) AllowOrigin(origin string) bool {
|
|
// if CORS is not configured, or a wildcard is present,
|
|
// allow WebSocket connections from any origin
|
|
if len(s.CORS) == 0 || s.CORS[0] == "*" {
|
|
return true
|
|
}
|
|
|
|
// allow only origins in the list
|
|
return slices.Contains(s.CORS, origin)
|
|
}
|