Possibly working tanks

This commit is contained in:
Neale Pickett 2010-10-26 13:02:26 -06:00
parent b563456fda
commit 9c472de526
14 changed files with 367 additions and 6 deletions

View File

@ -1,5 +1,5 @@
Things That Need Fixin'
=======================
* puzzles.cgi really needs to cross out points your team has claimed.
* Make an OpenWRT router image for a FitPC
* Do something about all the symbolic links in the build tree
* Make arc4 global somehow

View File

@ -49,3 +49,6 @@ echo "$1" > $base/teams/names/$hash
echo "$color" > $base/teams/colors/$hash
echo "Registered with hash: $hash"
# Create encrypted listing of teams
ls $base/teams/names | KEY="Too much cheese." | /opt/mcp/bin/arc4 > $www/teams.txt

View File

@ -1,7 +1,7 @@
CFLAGS = -Wall -Werror
TARGETS = in.tokend tokencli claim.cgi
TARGETS += puzzler.cgi puzzles.cgi
TARGETS += pointscli mktoken
TARGETS += pointscli mktoken arc4
all: build
@ -11,6 +11,7 @@ in.tokend: in.tokend.o arc4.o common.o
tokencli: tokencli.o arc4.o
pointscli: pointscli.o common.o
mktoken: mktoken.o common.o
arc4: arc4-main.o arc4.o
puzzles.cgi: puzzles.cgi.o common.o
claim.cgi: claim.cgi.o common.o

View File

@ -0,0 +1 @@
../../tokens/src/arc4-main.c

View File

@ -0,0 +1 @@
U'0絤騺q<1B>p

19
packages/mktokendkey Executable file
View File

@ -0,0 +1,19 @@
#! /bin/sh
if [ $# -lt 1 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo "Usage: $0 CATEGORY"
echo
echo "Creates tokend key for CATEGORY."
exit 1
fi
puz=$1; shift
cat=${1:-$puz}
d=$(dirname $0)
td=$d/mcp/tokend.keys/$cat
echo "Writing new server key to $td"
dd bs=1 count=16 if=/dev/urandom of=$td
hd $td

View File

@ -0,0 +1 @@
U'0絤騺q<1B>p

View File

@ -0,0 +1,6 @@
#! /bin/sh
mkdir -p /var/lib/ctf/tanks
mkdir -p /var/lib/ctf/tanks/www
mkdir -p /var/lib/ctf/tanks/players
exec ./tanksd

View File

@ -0,0 +1,116 @@
#! /bin/sh
d=/var/lib/ctf/tanks
p=$d/players
w=$d/www
mkdir -p $w $p
summary () {
cat <<EOF
<!DOCTYPE html>
<html>
<head>
<title>Tanks</title>
<link rel=\"stylesheet\" href=\"http://10.0.0.2/ctf.css\" type=\"text/css\">
</head>
<body>
<h1>Tanks</h1>
<p>New here? Start with the <a href=\"intro.html\">introduction</a>.</p>
<p>New round every minute.</p>
<h2>Round results</h2>
<ul>
EOF
printed=0
find $w -name "round-*.html" | sort -r | while read fn; do
$b=$(basename $fn)
if [ $printed -lt 20 ]; then
echo "<li><a href=\"$b\">$b</a></li>"
else
rm -f $fn
fi
printed=$(expr $printed + 1)
done
cat <<EOF
</ul>
EOF
cat /opt/tanks/www/nav.html.inc
cat <<EOF
</body>
</html>
EOF
}
while true; do
# Make sure all teams exist
# XXX: pull this out into another daemon
wget -O - http://10.0.0.2/teams.txt | \
KEY="Too much cheese." /opt/tokens/bin/arc4 | \
while read hash; do
mkdir -p $p/$hash
chown ctf $p/$hash
done
# Round number?
if [ -f $d/next-round ]; then
next=$(cat $d/next-round)
else
next=0
fi
expr $next + 1 > $d/next-round
fn=$(printf "%s/round-%04d.html" $d $next)
rfn=$(printf "/tmp/tanks-results-%04d.txt" $next)
tfn=$(printf "/tmp/tanks-token-%04d.txt" $next)
# Run a game
echo "Running round $next"
cat <<EOF >$fn
<!DOCTYPE html>
<html>
<head>
<title>Tanks Round $next</title>
<script type="application/javascript" src="tanks.js"></script>
<link rel="stylesheet" href="http://10.0.0.2/ctf.css" type="text/css">
<script type="application/javascript">
function go() {
start("battlefield",
// Start JSON data
EOF
/opt/tanks/bin/forftanks $d/*/ >>$fn 3>$rfn
cat <<EOF >>$fn
// end JSON data
);
}
window.onload = go;
</script>
</head>
<body>
<h1>Tanks Round $next</h1>
<div id="game_box"><canvas id="battlefield"></canvas></div>
<p><span id="fps">0</span> fps</p>
EOF
/opt/tanks/bin/rank.awk $rfn >>$fn
cat /opt/tanks/www/nav.html.inc
cat <<EOF >>$fn
</body>
</html>
EOF
# Get a token and add it to the redemption log
nc 10.0.0.2 1 -e /opt/tokens/bin/tokencli tanks ./category.key 3> $tfn
k=$(cat $tfn)
/opt/tanks/bin/winners.awk $rfn | while read winner; do
echo "Round $next winner: $winner"
# Squirrel it away just in case
cat $tfn >> $p/$winner/tokens
# XXX: pull this out into another daemon
# XXX: this puts the token in /proc/self/cmdline
wget -O /dev/null "http://10.0.0.2/claim.cgi?t=$winner&k=$k"
done
summary > $d/summary.html.$$
mv $d/summary.html.$$ $d/summary.html
done

View File

@ -16,6 +16,24 @@ tanks-build: tanks-source
$(MAKE) -C $(TANKS_BUILDDIR)/ctanks
tanks-install: tanks-build
mkdir -p $(TANKS_PKGDIR)/bin
cp $(TANKS_BUILDDIR)/ctanks/forftanks $(TANKS_PKGDIR)/bin
cp $(TANKS_BUILDDIR)/ctanks/designer.cgi $(TANKS_PKGDIR)/bin
cp $(TANKS_BUILDDIR)/ctanks/rank.awk $(TANKS_PKGDIR)/bin
cp $(TANKS_BUILDDIR)/ctanks/winners.awk $(TANKS_PKGDIR)/bin
$(call COPYTREE, packages/tanks/www, $(TANKS_PKGDIR)/www/tanks)
cp $(TANKS_BUILDDIR)/ctanks/nav.html.inc $(TANKS_PKGDIR)/www/tanks
cp $(TANKS_BUILDDIR)/ctanks/tanks.js $(TANKS_PKGDIR)/www/tanks
cp $(TANKS_BUILDDIR)/ctanks/forf.html $(TANKS_PKGDIR)/www/tanks
cp $(TANKS_BUILDDIR)/ctanks/intro.html $(TANKS_PKGDIR)/www/tanks
cp $(TANKS_BUILDDIR)/ctanks/figures.js $(TANKS_PKGDIR)/www/tanks
cp $(TANKS_BUILDDIR)/ctanks/procs.html $(TANKS_PKGDIR)/www/tanks
cp $(TANKS_BUILDDIR)/ctanks/designer.js $(TANKS_PKGDIR)/www/tanks
$(call COPYTREE, packages/tanks/service, $(TANKS_PKGDIR)/service)
$(call COPYTREE, $(TANKS_BUILDDIR)/ctanks/examples, $(TANKS_PKGDIR)/examples)
tanks-clean:
rm -f $(TANKS_BUILDDIR)

4
packages/tanks/www/design.cgi Executable file
View File

@ -0,0 +1,4 @@
#! /bin/sh
BASE_PATH=/var/lib/ctf/tanks/players; export BASE_PATH
exec /opt/tanks/bin/design.cgi

View File

@ -0,0 +1,178 @@
<!DOCTYPE html>
<html>
<head>
<title>Tank Designer</title>
<link rel="stylesheet" href="dirtbags.css" type="text/css">
<style type="text/css">
#preview {
float: right;
}
#sensors input {
width: 5em;
}
#program textarea {
width: 100%;
min-height: 20em;
}
td {
text-align: left;
}
</style>
<script type="application/javascript" src="tanks.js"></script>
<script type="application/javascript" src="designer.js"></script>
<script type="application/javascript">
window.onload = design;
</script>
</head>
<body>
<h1>Tank Designer</h1>
<div id="preview"><canvas id="design"></canvas><p id="debug"></p></div>
<form action="designer.cgi" method="post">
<fieldset id="metadata">
<legend>Information</legend>
<table>
<tr>
<td>Team hash:</td>
<td><input name="token"></td>
</tr>
<tr>
<td>Tank name:</td>
<td><input name="name"></td>
</tr>
<tr>
<td>Author:</td>
<td>
<input name="author">
(eg. Joe Cool &lt;joe@cool.cc&gt;)
</td>
</tr>
<tr>
<td>Color:</td>
<td>
<input name="color" type="color" value="#c0c0c0" onchange="update();">
(eg. #c7e148)
</td>
</tr>
</table>
</fieldset>
<fieldset id="sensors">
<legend>Sensors</legend>
<table>
<thead>
<tr>
<td>#</td>
<td>Range</td>
<td>Angle</td>
<td>Width</td>
<td>Turret?</td>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td><input name="s0r" type="number" min="0" max="100" onchange="update();"></td>
<td><input name="s0a" type="number" min="-360" max="360" onchange="update();"></td>
<td><input name="s0w" type="number" min="-360" max="360" onchange="update();"></td>
<td><input name="s0t" type="checkbox" onchange="update();"></td>
</tr>
<tr>
<td>1</td>
<td><input name="s1r" type="number" min="0" max="100" onchange="update();"></td>
<td><input name="s1a" type="number" min="-360" max="360" onchange="update();"></td>
<td><input name="s1w" type="number" min="-360" max="360" onchange="update();"></td>
<td><input name="s1t" type="checkbox" onchange="update();"></td>
</tr>
<tr>
<td>2</td>
<td><input name="s2r" type="number" min="0" max="100" onchange="update();"></td>
<td><input name="s2a" type="number" min="-360" max="360" onchange="update();"></td>
<td><input name="s2w" type="number" min="-360" max="360" onchange="update();"></td>
<td><input name="s2t" type="checkbox" onchange="update();"></td>
</tr>
<tr>
<td>3</td>
<td><input name="s3r" type="number" min="0" max="100" onchange="update();"></td>
<td><input name="s3a" type="number" min="-360" max="360" onchange="update();"></td>
<td><input name="s3w" type="number" min="-360" max="360" onchange="update();"></td>
<td><input name="s3t" type="checkbox" onchange="update();"></td>
</tr>
<tr>
<td>4</td>
<td><input name="s4r" type="number" min="0" max="100" onchange="update();"></td>
<td><input name="s4a" type="number" min="-360" max="360" onchange="update();"></td>
<td><input name="s4w" type="number" min="-360" max="360" onchange="update();"></td>
<td><input name="s4t" type="checkbox" onchange="update();"></td>
</tr>
<tr>
<td>5</td>
<td><input name="s5r" type="number" min="0" max="100" onchange="update();"></td>
<td><input name="s5a" type="number" min="-360" max="360" onchange="update();"></td>
<td><input name="s5w" type="number" min="-360" max="360" onchange="update();"></td>
<td><input name="s5t" type="checkbox" onchange="update();"></td>
</tr>
<tr>
<td>6</td>
<td><input name="s6r" type="number" min="0" max="100" onchange="update();"></td>
<td><input name="s6a" type="number" min="-360" max="360" onchange="update();"></td>
<td><input name="s6w" type="number" min="-360" max="360" onchange="update();"></td>
<td><input name="s6t" type="checkbox" onchange="update();"></td>
</tr>
<tr>
<td>7</td>
<td><input name="s7r" type="number" min="0" max="100" onchange="update();"></td>
<td><input name="s7a" type="number" min="-360" max="360" onchange="update();"></td>
<td><input name="s7w" type="number" min="-360" max="360" onchange="update();"></td>
<td><input name="s7t" type="checkbox" onchange="update();"></td>
</tr>
<tr>
<td>8</td>
<td><input name="s8r" type="number" min="0" max="100" onchange="update();"></td>
<td><input name="s8a" type="number" min="-360" max="360" onchange="update();"></td>
<td><input name="s8w" type="number" min="-360" max="360" onchange="update();"></td>
<td><input name="s8t" type="checkbox" onchange="update();"></td>
</tr>
<tr>
<td>9</td>
<td><input name="s9r" type="number" min="0" max="100" onchange="update();"></td>
<td><input name="s9a" type="number" min="-360" max="360" onchange="update();"></td>
<td><input name="s9w" type="number" min="-360" max="360" onchange="update();"></td>
<td><input name="s9t" type="checkbox" onchange="update();"></td>
</tr>
</tbody>
</table>
</fieldset>
<fieldset id="program">
<legend>Program</legend>
<textarea name="program"></textarea>
</fieldset>
<input type="submit" value="Submit">
</form>
<nav>
<h2>Resources</h2>
<ul>
<li><a href="summary.html">Summary</a></li>
<li><a href="intro.html">Introduction</a></li>
<li><a href="forf.html">Forf manual</a></li>
<li><a href="procs.html">Tanks procedures</a></li>
<li><a href="designer.html">Tanks designer</a></li>
</ul>
</nav>
</body>
</html>

View File

@ -1,4 +1,6 @@
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sysexits.h>
#include "arc4.h"
@ -12,6 +14,7 @@ main(int argc, char *argv[])
{
uint8_t key[256];
size_t keylen = 0;
char *ekey = getenv("KEY");
FILE *f;
if (argc == 2) {
@ -25,13 +28,19 @@ main(int argc, char *argv[])
if (f) {
keylen = fread(key, 1, sizeof(key), f);
fclose(f);
} else if (ekey) {
keylen = strlen(ekey);
if (keylen > sizeof(key)) {
keylen = sizeof(key);
}
memcpy(key, ekey, keylen);
}
if (0 == keylen) {
fprintf(stderr, "Usage: %s [KEYFILE] <PLAINTEXT\n", argv[0]);
fprintf(stderr, "\n");
fprintf(stderr, "You can also pass in the key on fd 3; omit\n");
fprintf(stderr, "KEYFILE in this case.\n");
fprintf(stderr, "You can also pass in the key on fd 3 or in\n");
fprintf(stderr, "$KEY; omit KEYFILE in this case.\n");
return EX_IOERR;
}
arc4_init(&ctx, key, (size_t)keylen);

View File

@ -44,8 +44,12 @@ main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s SERVICE SERVICEKEY 3>TOKENFILE\n", argv[0]);
fprintf(stderr, "\n");
fprintf(stderr, "SERVICEKEY is a filenames.\n");
fprintf(stderr, "SERVICEKEY is a filename.\n");
fprintf(stderr, "Server chatter happens over stdin and stdout.\n");
fprintf(stderr, "Tokens are written to file descriptor 3.\n");
fprintf(stderr, "\n");
fprintf(stderr, "To run with netcat:\n");
fprintf(stderr, " nc server 1 -e tokencli cat cat.key 3> tokenfile\n");
return EX_USAGE;
}