From 935070560511def3abe5dbe747435a953660355c Mon Sep 17 00:00:00 2001 From: Ethan Marshall Date: Sat, 11 Nov 2023 18:22:10 +0000 Subject: [PATCH 1/4] Fix SendResults action warnings --- game/action.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/game/action.go b/game/action.go index e9a0171..0824127 100644 --- a/game/action.go +++ b/game/action.go @@ -346,7 +346,7 @@ func (a Answer) Perform(game *Game) { type SendResults struct{} -func (s SendResults) Perform(game *Game) { +func (s SendResults) Perform(_ *Game) { } // EndGame shuts down the game runner, thereby terminating the current From 604a73873f3bdd260b72a6a2b6422233adf43d3e Mon Sep 17 00:00:00 2001 From: Ethan Marshall Date: Sat, 11 Nov 2023 18:22:24 +0000 Subject: [PATCH 2/4] Remove deprecated rand.Seed --- main.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/main.go b/main.go index def2fbe..95dc590 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,6 @@ import ( "context" "errors" "log" - "math/rand" "net/http" "os" "os/signal" @@ -54,10 +53,6 @@ func main() { log.Fatal(err) } - // Seed random - // MUST be done before game coordinator - rand.Seed(time.Now().UnixMilli()) - // Init configs vd = validator.New() Config, err = config.New(PathConfig, vd) From cadf56f1adbb7263c4c5929ee65d6d6d3a4522d8 Mon Sep 17 00:00:00 2001 From: Ethan Marshall Date: Sat, 11 Nov 2023 18:36:38 +0000 Subject: [PATCH 3/4] Implement secure websockets support Added a config flag which flags up if an SSL tunnel is to be used (probably configured separately). If so, the client will use "wss://" instead of "ws://" in the connection such that secure connections will not fail. --- config/conf.go | 9 +++++++++ config/parse.go | 3 +++ frontend/src/common.ts | 6 +++--- frontend/src/global.d.ts | 6 +++++- frontend/templates/host.gohtml | 4 +++- frontend/templates/play.gohtml | 4 +++- play.go | 14 ++++++++------ 7 files changed, 34 insertions(+), 12 deletions(-) diff --git a/config/conf.go b/config/conf.go index f447f36..8037145 100644 --- a/config/conf.go +++ b/config/conf.go @@ -19,6 +19,7 @@ type Config struct { ListenAddr string `validate:"ip_addr|hostname"` ListenPort uint64 `validate:"gte=1,lte=65535"` TrustedProxies []string `validate:"dive,ip_addr"` + HasSSL bool QuizPath string `validate:"dir"` @@ -31,6 +32,14 @@ func (c Config) FullAddr() string { return fmt.Sprintf("%s:%d", c.ListenAddr, c.ListenPort) } +func (c Config) WSProto() string { + if c.HasSSL { + return "wss" + } + + return "ws" +} + func New(path string, validator *validator.Validate) (Config, error) { c := Config{} err := parse(&c, path) diff --git a/config/parse.go b/config/parse.go index 57c24ff..b982f8d 100644 --- a/config/parse.go +++ b/config/parse.go @@ -105,6 +105,9 @@ func parse(c *Config, path string) error { case "game_timeout": i, e := strconv.ParseInt(trail, 10, 32) c.GameTimeout, err = time.Second*time.Duration(i), e + case "ssl": + lc := strings.ToLower(trail) + c.HasSSL = (lc == "true" || lc == "yes") default: err = fmt.Errorf("unknown key: %q", key) } diff --git a/frontend/src/common.ts b/frontend/src/common.ts index bfe7943..aa0d102 100644 --- a/frontend/src/common.ts +++ b/frontend/src/common.ts @@ -6,8 +6,8 @@ */ // Endpoint locations -export const PlayEndpoint = "ws://" + location.host + "/api/play/" -export const HostEndpoint = "ws://" + location.host + "/api/host/" +export const PlayEndpoint = ws_proto + "://" + location.host + "/api/play/" +export const HostEndpoint = ws_proto + "://" + location.host + "/api/host/" // Common icon resource paths export const iconpath: string = "/static/assets/" @@ -46,4 +46,4 @@ export interface GameState { // Sends a properly formatted message over ws export function SendMessage(ws: WebSocket, action: string, body: any) { ws.send(action + " " + JSON.stringify(body)) -} +} \ No newline at end of file diff --git a/frontend/src/global.d.ts b/frontend/src/global.d.ts index ef3e9e8..1824e21 100644 --- a/frontend/src/global.d.ts +++ b/frontend/src/global.d.ts @@ -7,4 +7,8 @@ declare global { var uid: number var pin: number var title: string -} + + // Websocket protocol definition. + // Set by server to support both SSL and non-SSL servers. + var ws_proto: string +} \ No newline at end of file diff --git a/frontend/templates/host.gohtml b/frontend/templates/host.gohtml index 8ea5258..bcde8ed 100644 --- a/frontend/templates/host.gohtml +++ b/frontend/templates/host.gohtml @@ -9,6 +9,8 @@ @@ -83,4 +85,4 @@ - + \ No newline at end of file diff --git a/frontend/templates/play.gohtml b/frontend/templates/play.gohtml index abeff88..b47c09d 100644 --- a/frontend/templates/play.gohtml +++ b/frontend/templates/play.gohtml @@ -9,6 +9,8 @@ @@ -54,4 +56,4 @@ - + \ No newline at end of file diff --git a/play.go b/play.go index cc2c4c7..e94ad39 100644 --- a/play.go +++ b/play.go @@ -16,9 +16,10 @@ import ( // UI. func handleHost(c *gin.Context) { dat := struct { - Title string - Pin uint32 - }{} + Title string + Pin uint32 + WebsocketProto string + }{WebsocketProto: Config.WSProto()} spin := c.Param("pin") if spin == "" { @@ -56,9 +57,10 @@ func handleHost(c *gin.Context) { func handleGame(c *gin.Context) { dat := struct { // NOTE: Must be uint32, as game.GamePin is formatted as a JS string - Pin uint32 - UID int - }{} + Pin uint32 + UID int + WebsocketProto string + }{WebsocketProto: Config.WSProto()} id, pin := c.Param("pin"), game.Pin(0) uid, intuid := c.Query("plr"), int(0) From 194171c5d5eb1a45cbcfc5e5cdf7c6058b86186d Mon Sep 17 00:00:00 2001 From: Ethan Marshall Date: Sat, 11 Nov 2023 18:42:16 +0000 Subject: [PATCH 4/4] Add `ssl` key to sample config file --- config.gahoot | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config.gahoot b/config.gahoot index 2d58ec5..2da673d 100644 --- a/config.gahoot +++ b/config.gahoot @@ -23,10 +23,11 @@ addr: 0.0.0.0 port: 8080 proxies: [] +ssl: false // Directory for quiz archive files. Must be present at startup. // Can be relative (to cwd at startup), or absolute. quiz_dir: quizzes // Gameplay settings -game_timeout: 2700 +game_timeout: 2700 \ No newline at end of file