From a2193fd0960a7b6075de9ce4c8e71f40e7dc7a24 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sun, 26 May 2019 22:33:28 +0200 Subject: [PATCH] Fix /register --- README.md | 14 ++++++++----- server.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 62 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 06021ca..9d6a163 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,21 @@ ## Welcome to the ungleich-game, a geek game engine! ungleich-game is supposed to be an easy-to-use, easy-to-play and -easy-to-extend game framework for geeks. The project name is -ungleich-game, as it has its roots at ungleich - the project name -might change later. +easy-to-extend game framework for geeks. -## How to play +It features registration, challenges with dependencies and an easy +point system. + +The project name is ungleich-game, as it has its roots at ungleich - +the project name might change later. + +## How to play (in general) * Select a game server * Register * List challenges - have fun! -### How to play on Nico's notebook +### How to play (for instance on Nico's notebook) 1. Register diff --git a/server.py b/server.py index 4124a43..23dac6c 100644 --- a/server.py +++ b/server.py @@ -21,6 +21,12 @@ def get_random_ip(network): return addr +def require_args(*args): + parser = reqparse.RequestParser() + for arg in args: + parser.add_argument(arg, required=True) + return parser.parse_args() + class Challenge(object): """ A sample challenge -- inherit this and overwrite accordingly """ @@ -32,12 +38,6 @@ class Challenge(object): def __init__(self, etcdclient): self.client = etcdclient - def require_args(self, *args): - parser = reqparse.RequestParser() - for arg in args: - parser.add_argument(arg, required=True) - return parser.parse_args() - def game(self): if request.method == 'GET': return self.describe() @@ -74,7 +74,39 @@ and to setup services listening on these IPv6 addresses. Submit your network with the "network" parameter. """ def solve(self): - args = self.require_args("user", "network") + args = require_args("user", "network") + network = args['network'] + user = args['user'] + + try: + net = ipaddress.IPv6Network(network) + except Exception as e: + return Response(status=400, response="Cannot register network {}: {}".format(network, e)) + + if not net.prefixlen == 64: + return Response(status=400, response="{} mask is not /64 - please use a /64 network".format(net)) + + # Save network + self.set_user_key(user, "network", network) + self.save_points(user) + + return "Network {} registered, have fun with the next challenge!".format(network) + +class IPv6Address(Challenge): + points = 20 + requires = [ "network" ] + + description = """ +You have setup your network, great! +Now it is time to show that you are really controlling your network! + +""" + + def describe(self): + pass + + def solve(self): + args = require_args("user", "network") network = args['network'] user = args['user'] @@ -100,6 +132,7 @@ class Game(object): self.app.add_url_rule('/', 'index', self.index) self.app.add_url_rule('/points', 'points', self.points) + self.app.add_url_rule('/register', 'register', self.register, methods=['POST']) # etcd paths are below here self.etcbase = etcbase @@ -186,6 +219,19 @@ https://code.ungleich.ch/nico/ungleich-game return "\n".join(res) + def register(self): + args = require_args("user") + path = "{}/{}/registered_at".format(self.userbase, args['user']) + value = str(datetime.datetime.now()) + + cur = self.read_etcd(path) + + if cur: + value = cur.value + else: + self.client.write(path, value) + + return "Registered at: {}\n".format(value) # def get_ip_address(): # args = self.require_args("network", "user")