From 0c54f31fd99598539549c1d0bda2303727e26f56 Mon Sep 17 00:00:00 2001 From: chris Date: Sat, 15 Aug 2020 09:30:33 -0400 Subject: [PATCH] Allow user to set server address --- README.md | 2 +- cmd/q3/app/server/server.go | 11 +++-------- internal/quake/server/server.go | 17 ++++++++++++++++- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 2859b55..de825cc 100644 --- a/README.md +++ b/README.md @@ -119,7 +119,7 @@ The content server hosts a small upload app to allow uploading `pk3` or `zip` fi The easiest way to develop quake-kube is building the binary locally with `make` and running it directly. This only requires that you have the `ioq3ded` binary in your path: ```shell -$ bin/q3 -c config.yaml --assets-dir $HOME/.q3a --agree-eula +$ bin/q3 server -c config.yaml --assets-dir $HOME/.q3a --agree-eula ``` ## Credits diff --git a/cmd/q3/app/server/server.go b/cmd/q3/app/server/server.go index 2557631..4e96a9b 100644 --- a/cmd/q3/app/server/server.go +++ b/cmd/q3/app/server/server.go @@ -40,13 +40,7 @@ func NewCommand() *cobra.Command { } opts.ClientAddr = fmt.Sprintf("%s:8080", hostIPv4) } - if opts.ServerAddr == "" { - hostIPv4, err := netutil.DetectHostIPv4() - if err != nil { - return err - } - opts.ServerAddr = fmt.Sprintf("%s:27960", hostIPv4) - } + ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -72,6 +66,7 @@ func NewCommand() *cobra.Command { Dir: opts.AssetsDir, WatchInterval: opts.WatchInterval, ConfigFile: opts.ConfigFile, + Addr: opts.ServerAddr, } if err := s.Start(ctx); err != nil { panic(err) @@ -100,7 +95,7 @@ func NewCommand() *cobra.Command { cmd.Flags().BoolVar(&opts.AcceptEula, "agree-eula", false, "agree to the Quake 3 demo EULA") cmd.Flags().StringVar(&opts.AssetsDir, "assets-dir", "assets", "location for game files") cmd.Flags().StringVar(&opts.ClientAddr, "client-addr", "", "client address :") - cmd.Flags().StringVar(&opts.ServerAddr, "server-addr", "", "dedicated server :") + cmd.Flags().StringVar(&opts.ServerAddr, "server-addr", "0.0.0.0:27960", "dedicated server :") cmd.Flags().DurationVar(&opts.WatchInterval, "watch-interval", 15*time.Second, "dedicated server :") return cmd } diff --git a/internal/quake/server/server.go b/internal/quake/server/server.go index cd67122..69f7e19 100644 --- a/internal/quake/server/server.go +++ b/internal/quake/server/server.go @@ -4,6 +4,7 @@ import ( "context" "io/ioutil" "log" + "net" "os" "path/filepath" "time" @@ -17,10 +18,24 @@ type Server struct { Dir string WatchInterval time.Duration ConfigFile string + Addr string } func (s *Server) Start(ctx context.Context) error { - cmd := exec.CommandContext(ctx, "ioq3ded", "+set", "dedicated", "1", "+exec", "server.cfg") + if s.Addr == "" { + s.Addr = "0.0.0.0:27960" + } + host, port, err := net.SplitHostPort(s.Addr) + if err != nil { + return err + } + args := []string{ + "+set", "dedicated", "1", + "+set", "net_ip", host, + "+set", "net_port", port, + "+exec", "server.cfg", + } + cmd := exec.CommandContext(ctx, "ioq3ded", args...) cmd.Dir = s.Dir cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr