Skip to content

Commit

Permalink
Merge pull request #251 from redpwn/feat/better-config
Browse files Browse the repository at this point in the history
Better config
  • Loading branch information
chen-robert committed May 15, 2020
2 parents 1fcb12c + cff7cd8 commit 264296c
Show file tree
Hide file tree
Showing 23 changed files with 59 additions and 89 deletions.
10 changes: 6 additions & 4 deletions client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
<html lang="en">
<head>
<meta charset="utf-8">
<title><%= htmlWebpackPlugin.options.view.ctfName %></title>
<title>{{ ctfName }}</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<link rel="apple-touch-icon" href="./assets/icons/apple-touch-icon.png">

<meta property="og:title" content="<%= htmlWebpackPlugin.options.view.ctfName %>">
<meta property="og:description" content="<%= htmlWebpackPlugin.options.view.meta.description %>">
<meta property="og:image" content="<%= htmlWebpackPlugin.options.view.meta.imageUrl %>">
<meta property="og:title" content="{{ ctfName }}">
<meta property="og:description" content="{{ meta.description }}">
<meta property="og:image" content="{{ meta.imageUrl }}">
<meta property="og:type" content="website" />

<meta name="rctf-config" content="{{ config }}" />

<link href="https://fonts.googleapis.com/css?family=Nunito+Sans:200,300,400,600,700" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet">
<% preact.headEnd %>
Expand Down
2 changes: 1 addition & 1 deletion client/src/api/util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { route } from 'preact-router'
import config from '../../../config/client'
import config from '../config'

export const relog = () => {
localStorage.removeItem('token')
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/ctftime-additional.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Form from '../components/form'
import config from '../../../config/client'
import config from '../config'
import withStyles from '../components/jss'
import { register } from '../api/auth'
import UserCircle from '../icons/user-circle.svg'
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/profile/memberscard.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import withStyles from '../jss'
import { getMembers, addMember, removeMember } from '../../api/members'
import { updateAccount } from '../../api/profile'
import { useState, useCallback, useEffect } from 'preact/hooks'
import config from '../../../../config/client'
import config from '../../config'

import Form from '../form'
import { useToast } from '../toast'
Expand Down
3 changes: 3 additions & 0 deletions client/src/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const config = JSON.parse(document.head.querySelector('meta[name="rctf-config"]').content)

export default config
2 changes: 1 addition & 1 deletion client/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState, useCallback } from 'preact/hooks'
import Router from 'preact-router'
import config from '../../config/client'
import config from './config'

import 'cirrus-ui'
import withStyles from './components/jss'
Expand Down
2 changes: 1 addition & 1 deletion client/src/routes/admin/challs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState, useEffect } from 'preact/hooks'

import config from '../../../../config/client'
import config from '../../config'
import withStyles from '../../components/jss'
import Problem from '../../components/admin/problem'

Expand Down
2 changes: 1 addition & 1 deletion client/src/routes/challs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback, useState, useEffect, useMemo } from 'preact/hooks'

import config from '../../../config/client'
import config from '../config'
import withStyles from '../components/jss'
import Problem from '../components/problem'

Expand Down
2 changes: 1 addition & 1 deletion client/src/routes/error.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component } from 'preact'
import config from '../../../config/client'
import config from '../config'
import 'linkstate/polyfill'
import withStyles from '../components/jss'

Expand Down
2 changes: 1 addition & 1 deletion client/src/routes/home.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component } from 'preact'
import config from '../../../config/client'
import config from '../config'
import 'linkstate/polyfill'
import withStyles from '../components/jss'

Expand Down
2 changes: 1 addition & 1 deletion client/src/routes/login.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component } from 'preact'
import Form from '../components/form'
import config from '../../../config/client'
import config from '../config'
import 'linkstate/polyfill'
import withStyles from '../components/jss'

Expand Down
2 changes: 1 addition & 1 deletion client/src/routes/profile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState, useCallback, useEffect } from 'preact/hooks'
import { memo } from 'preact/compat'
import config from '../../../config/client'
import config from '../config'
import withStyles from '../components/jss'

import { privateProfile, publicProfile, deleteAccount, updateAccount, updateEmail, deleteEmail } from '../api/profile'
Expand Down
2 changes: 1 addition & 1 deletion client/src/routes/registration.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component } from 'preact'
import Form from '../components/form'
import config from '../../../config/client'
import config from '../config'
import 'linkstate/polyfill'
import withStyles from '../components/jss'

Expand Down
2 changes: 1 addition & 1 deletion client/src/routes/scoreboard.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState, useEffect, useMemo, useCallback, useRef } from 'preact/hooks'
import config from '../../../config/client'
import config from '../config'
import withStyles from '../components/jss'
import Pagination from '../components/pagination'
import Graph from '../components/graph'
Expand Down
2 changes: 1 addition & 1 deletion client/src/routes/sponsors.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component } from 'preact'
import config from '../../../config/client'
import config from '../config'
import 'linkstate/polyfill'
import withStyles from '../components/jss'

Expand Down
2 changes: 1 addition & 1 deletion client/src/routes/verify.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Error from './error'
import config from '../../../config/client'
import config from '../config'
import 'linkstate/polyfill'

import TokenPreview from '../components/tokenPreview'
Expand Down
2 changes: 1 addition & 1 deletion client/src/util/ctftime.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import config from '../../../config/client'
import config from '../config'

const openPopup = ({ url, title, w, h }) => {
const systemZoom = window.innerWidth / window.screen.availWidth
Expand Down
19 changes: 7 additions & 12 deletions config/client.js.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const ret = {
const shared = require('./shared')

module.exports = {
...shared,
apiEndpoint: '/api/v1',
staticEndpoint: '/static',
ctfTitle: ' | yourCTF',
ctfName: 'yourCTF',
ctfTitle: ' | ' + shared.ctfName,
meta: {
description: 'A description of your CTF',
imageUrl: ''
Expand All @@ -19,12 +20,6 @@ const ret = {
description: 'Brief description',
small: true
}
]
],
ctftimeClientId: 0
}

const shared = require('./shared')
Object.entries(shared).forEach(([key, val]) => {
if (ret[key] === undefined) ret[key] = val
})

module.exports = ret
11 changes: 3 additions & 8 deletions config/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const ret = {
const config = {
...require('./shared'),
challengeProvider: {
name: 'challenges/rdeploy-blob',
options: {
Expand All @@ -18,7 +19,6 @@ const ret = {
verifyEmail: false,
removeDownloadHashes: true,
tokenKey: process.env.RCTF_TOKEN_KEY,
ctfName: process.env.RCTF_NAME,
origin: process.env.RCTF_ORIGIN,
databaseUrl: process.env.RCTF_DATABASE_URL,
redisUrl: process.env.RCTF_REDIS_URL,
Expand All @@ -37,9 +37,4 @@ const ret = {
endTime: Date.now() + 24 * 60 * 60 * 1000
}

const shared = require('./shared')
Object.entries(shared).forEach(([key, val]) => {
if (ret[key] === undefined) ret[key] = val
})

module.exports = ret
module.exports = config
3 changes: 2 additions & 1 deletion config/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ module.exports = {
College: 1,
Other: 2
},
defaultDivision: 2
defaultDivision: 2,
ctfName: process.env.RCTF_NAME
}
10 changes: 0 additions & 10 deletions preact.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require('dotenv').config()
const appCfg = require('./config/client')

const glob = require('glob')

Expand Down Expand Up @@ -48,15 +47,6 @@ export default (config, env, helpers) => {
}
})

const HtmlWebpackPluginWrapper = helpers.getPluginsByName(config, 'HtmlWebpackPlugin')[0]
if (HtmlWebpackPluginWrapper !== undefined) {
const HtmlWebpackPlugin = HtmlWebpackPluginWrapper.plugin
HtmlWebpackPlugin.options.view = {
ctfName: appCfg.ctfName,
meta: appCfg.meta
}
}

const SizePluginWrapper = helpers.getPluginsByName(config, 'SizePlugin')[0]
if (SizePluginWrapper !== undefined) {
SizePluginWrapper.plugin.options = {
Expand Down
17 changes: 8 additions & 9 deletions server/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require('path')
const express = require('express')
const helmet = require('helmet')
const { enableCORS, serveDownloads } = require('./util')
const { enableCORS, serveIndex } = require('./util')
const uploadProvider = require('./uploads/index')

require('./leaderboard').startUpdater()
Expand Down Expand Up @@ -40,14 +40,13 @@ app.use(express.raw({
app.use('/api/v1', require('./api'))

const staticPath = path.join(__dirname, '../build')

const indexRoute = serveIndex(path.join(staticPath, 'index.html'))

// Override index.html in express.static
app.get('/', indexRoute)
app.get('/index.html', indexRoute)
app.use(express.static(staticPath, { extensions: ['html'] }))
app.use(serveDownloads('/static/files'))
app.use((req, res, next) => {
if (req.method !== 'GET') {
next()
return
}
res.sendFile(path.join(staticPath, 'index.html'))
})
app.use(indexRoute)

module.exports = app
45 changes: 15 additions & 30 deletions server/util/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const config = require('../../config/server')
const path = require('path')
const clientConfig = require('../../config/client')
const fs = require('fs')
const contentDisposition = require('content-disposition')
const mustache = require('mustache')
const { responses } = require('../responses')
const normalize = require('./normalize')

Expand Down Expand Up @@ -34,37 +34,22 @@ module.exports = {
next()
}
},
serveDownloads: root => {
if (!root.startsWith('/')) root = '/' + root
if (!root.endsWith('/')) root = root + '/'
serveIndex: indexPath => {
const indexTemplate = fs.readFileSync(indexPath).toString()

return (req, res, next) => {
if (req.method !== 'GET') return next()

if (req.path.startsWith(root)) {
const filename = req.path.substring(root.length)

const filepath = path.join(config.rDeployDirectory, config.rDeployFiles, filename)

if (filepath.startsWith(path.join(config.rDeployDirectory, config.rDeployFiles))) {
fs.access(filepath, fs.constants.R_OK, err => {
if (err) return next()
const rendered = mustache.render(indexTemplate, {
config: JSON.stringify(clientConfig),
ctfName: clientConfig.ctfName,
meta: clientConfig.meta
})

const cleanName = normalize.normalizeDownload(filename)

return res.sendFile(filename, {
root: path.join(config.rDeployDirectory, config.rDeployFiles),
headers: {
'Content-Disposition': contentDisposition(cleanName)
}
})
})

return
}
// Something sketchy is happening...
return (req, res, next) => {
if (req.method !== 'GET') {
next()
return
}
return next()
res.setHeader('Content-Type', 'text/html; charset=UTF-8')
res.send(rendered)
}
}
}

0 comments on commit 264296c

Please sign in to comment.