Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic SSL Support #3

Merged
merged 4 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config.gahoot
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions config/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions config/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand Down Expand Up @@ -46,4 +46,4 @@ export interface GameState<T> {
// Sends a properly formatted message over ws
export function SendMessage(ws: WebSocket, action: string, body: any) {
ws.send(action + " " + JSON.stringify(body))
}
}
6 changes: 5 additions & 1 deletion frontend/src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 3 additions & 1 deletion frontend/templates/host.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<script>
window.pin = {{.Pin}};
window.title = {{.Title}};

window.ws_proto = {{.WebsocketProto}};
</script>
<script type="module" src="/static/js/host.js"></script>
</head>
Expand Down Expand Up @@ -83,4 +85,4 @@
</div>
</body>

</html>
</html>
4 changes: 3 additions & 1 deletion frontend/templates/play.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<script>
window.uid = {{.UID}};
window.pin = {{.Pin}};

window.ws_proto = {{.WebsocketProto}};
</script>
<script type="module" src="/static/js/play.js"></script>
</head>
Expand Down Expand Up @@ -54,4 +56,4 @@

</body>

</html>
</html>
2 changes: 1 addition & 1 deletion game/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"log"
"math/rand"
"net/http"
"os"
"os/signal"
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 8 additions & 6 deletions play.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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)
Expand Down
Loading