2019-05-26 19:19:58 +00:00
|
|
|
#!/usr/bin/env python3
|
2019-04-14 16:57:39 +00:00
|
|
|
|
2019-05-26 19:19:58 +00:00
|
|
|
USERLENGTH = 50
|
2019-04-14 16:57:39 +00:00
|
|
|
|
2019-05-26 19:19:58 +00:00
|
|
|
import ipaddress
|
|
|
|
import random
|
|
|
|
import sys
|
|
|
|
import etcd
|
|
|
|
import json
|
|
|
|
import datetime
|
2019-04-14 16:57:39 +00:00
|
|
|
|
2019-05-26 19:19:58 +00:00
|
|
|
from flask import Flask, abort, request, Response
|
|
|
|
from flask_restful import reqparse
|
2019-04-14 16:57:39 +00:00
|
|
|
|
2019-05-26 19:19:58 +00:00
|
|
|
def get_random_ip(network):
|
|
|
|
net = ipaddress.IPv6Network(network)
|
|
|
|
addr_offset = random.randrange(2**64)
|
|
|
|
addr = net[0] + addr_offset
|
2019-04-14 16:57:39 +00:00
|
|
|
|
2019-05-26 19:19:58 +00:00
|
|
|
return addr
|
2019-05-11 22:18:03 +00:00
|
|
|
|
2019-05-26 20:33:28 +00:00
|
|
|
def require_args(*args):
|
|
|
|
parser = reqparse.RequestParser()
|
|
|
|
for arg in args:
|
|
|
|
parser.add_argument(arg, required=True)
|
|
|
|
return parser.parse_args()
|
|
|
|
|
2019-05-26 21:30:58 +00:00
|
|
|
class etcdWrapper(object):
|
|
|
|
""" Generalises some etcd actions """
|
|
|
|
|
|
|
|
def __init__(self, client, base):
|
|
|
|
self.client = client
|
|
|
|
self.base = base
|
|
|
|
|
|
|
|
def read_key_or_none(self, path, recursive=False):
|
|
|
|
try:
|
|
|
|
data = self.client.read(path, recursive=recursive)
|
|
|
|
except etcd.EtcdKeyNotFound:
|
|
|
|
return None
|
|
|
|
except Exception:
|
|
|
|
abort(Response(status=400, response="Error connecting to etcd"))
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
def get_user_key_or_none(self, user, key):
|
|
|
|
path = "{}/user/{}/{}".format(self.base, user, key)
|
|
|
|
return self.read_key_or_none(path)
|
|
|
|
|
|
|
|
def set_user_key(self, user, key, value):
|
|
|
|
path = "{}/user/{}/{}".format(self.base, user, key)
|
|
|
|
self.client.write(path, value)
|
|
|
|
|
|
|
|
|
2019-05-26 19:19:58 +00:00
|
|
|
class Challenge(object):
|
|
|
|
""" A sample challenge -- inherit this and overwrite accordingly """
|
2019-05-11 22:18:03 +00:00
|
|
|
|
2019-05-26 19:19:58 +00:00
|
|
|
points = 0
|
|
|
|
provides = []
|
|
|
|
requires = []
|
2019-05-26 20:04:59 +00:00
|
|
|
description = None
|
2019-05-26 21:30:58 +00:00
|
|
|
dependencies_provided_by = {}
|
2019-05-11 22:18:03 +00:00
|
|
|
|
2019-05-26 21:30:58 +00:00
|
|
|
def __init__(self, wrapper):
|
|
|
|
self.db = wrapper
|
2019-05-11 22:18:03 +00:00
|
|
|
|
2019-05-26 20:04:59 +00:00
|
|
|
def game(self):
|
|
|
|
if request.method == 'GET':
|
|
|
|
return self.describe()
|
|
|
|
if request.method == 'POST':
|
|
|
|
return self.solve()
|
|
|
|
|
2019-05-26 19:19:58 +00:00
|
|
|
def describe(self):
|
|
|
|
return self.description
|
2019-05-11 22:18:03 +00:00
|
|
|
|
2019-05-26 19:19:58 +00:00
|
|
|
def save_points(self, user):
|
|
|
|
""" should be called when the challenge was solved successfully"""
|
2019-05-11 22:18:03 +00:00
|
|
|
|
2019-05-26 20:04:59 +00:00
|
|
|
key = "points/{}".format(user, type(self).__name__)
|
2019-05-26 21:30:58 +00:00
|
|
|
self.db.set_user_key(user, key, self.points)
|
2019-05-26 19:19:58 +00:00
|
|
|
|
|
|
|
def solve(self):
|
|
|
|
""" Needs to be implemented per challenge """
|
|
|
|
pass
|
|
|
|
|
|
|
|
class RegisterNet(Challenge):
|
|
|
|
points = 10
|
|
|
|
provides = [ "network" ]
|
|
|
|
|
|
|
|
description = """
|
|
|
|
Register a /64 IPv6 network that you fully control.
|
|
|
|
Many other challenges depend on this. You will need to
|
|
|
|
be able to configure IPv6 addresses in this networks
|
|
|
|
and to setup services listening on these IPv6 addresses.
|
|
|
|
|
|
|
|
Submit your network with the "network" parameter.
|
2019-05-11 22:18:03 +00:00
|
|
|
"""
|
2019-05-26 19:19:58 +00:00
|
|
|
def solve(self):
|
2019-05-26 20:33:28 +00:00
|
|
|
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
|
2019-05-26 21:30:58 +00:00
|
|
|
self.db.set_user_key(user, "network", network)
|
2019-05-26 20:33:28 +00:00
|
|
|
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!
|
|
|
|
|
2019-05-26 21:30:58 +00:00
|
|
|
Setup the IPv6 address
|
|
|
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
and POST to this address, when it should be reachable by ping6.
|
2019-05-26 20:33:28 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def describe(self):
|
2019-05-26 21:30:58 +00:00
|
|
|
args = require_args("user")
|
2019-05-26 20:04:59 +00:00
|
|
|
user = args['user']
|
2019-05-26 21:30:58 +00:00
|
|
|
key = "network"
|
2019-05-26 19:19:58 +00:00
|
|
|
|
2019-05-26 21:30:58 +00:00
|
|
|
network = self.db.get_user_key_or_none(user, key)
|
|
|
|
if not network:
|
|
|
|
return Response(status=400, response="""
|
|
|
|
Register a network before trying to be reachable. Possible challenges that
|
|
|
|
provide the network are:
|
2019-05-26 19:19:58 +00:00
|
|
|
|
2019-05-26 21:30:58 +00:00
|
|
|
{}
|
|
|
|
""".format("\n".join(self.dependencies_provided_by['network'])))
|
2019-05-26 19:19:58 +00:00
|
|
|
|
|
|
|
|
2019-05-26 21:30:58 +00:00
|
|
|
key = "address"
|
|
|
|
address = self.db.get_user_key_or_none(user, key)
|
|
|
|
if not address:
|
|
|
|
address = get_random_ip(network.value)
|
|
|
|
self.db.set_user_key(user, key, address)
|
|
|
|
else:
|
|
|
|
address = address.value
|
|
|
|
|
|
|
|
return self.description.format(address)
|
|
|
|
|
|
|
|
def solve(self):
|
|
|
|
args = require_args("user")
|
|
|
|
user = args['user']
|
|
|
|
|
|
|
|
return Response(status=400, response="""
|
|
|
|
Not yet implemented""")
|
2019-05-26 19:19:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Game(object):
|
|
|
|
def __init__(self, name, etcdclient, etcbase="/ungleichgame/v1"):
|
|
|
|
self.client = etcdclient
|
2019-05-26 21:30:58 +00:00
|
|
|
self.etcbase = etcbase
|
|
|
|
|
|
|
|
self.wrapper = etcdWrapper(etcdclient, self.etcbase)
|
|
|
|
|
2019-05-26 19:19:58 +00:00
|
|
|
self.app = Flask(name)
|
|
|
|
|
2019-05-26 20:04:59 +00:00
|
|
|
self.app.add_url_rule('/', 'index', self.index)
|
|
|
|
self.app.add_url_rule('/points', 'points', self.points)
|
2019-05-26 20:33:28 +00:00
|
|
|
self.app.add_url_rule('/register', 'register', self.register, methods=['POST'])
|
2019-05-26 19:19:58 +00:00
|
|
|
|
|
|
|
# etcd paths are below here
|
2019-05-26 21:30:58 +00:00
|
|
|
|
2019-05-26 19:19:58 +00:00
|
|
|
self.userbase = "{}/user".format(self.etcbase)
|
|
|
|
|
|
|
|
# Automate this
|
2019-05-26 21:30:58 +00:00
|
|
|
challenges = [ RegisterNet, IPv6Address ]
|
|
|
|
challenge_instances = []
|
2019-05-26 19:19:58 +00:00
|
|
|
|
2019-05-26 20:04:59 +00:00
|
|
|
self.app.add_url_rule('/challenge', 'list_of_challenges', self.list_of_challenges)
|
|
|
|
self.app.add_url_rule('/challenge/', 'list_of_challenges', self.list_of_challenges)
|
|
|
|
|
2019-05-26 21:30:58 +00:00
|
|
|
self.providers = {}
|
2019-05-26 20:04:59 +00:00
|
|
|
self.challenge_names = []
|
2019-05-26 19:19:58 +00:00
|
|
|
for challenge in challenges:
|
2019-05-26 21:30:58 +00:00
|
|
|
c = challenge(self.wrapper)
|
|
|
|
challenge_instances.append(c)
|
|
|
|
|
2019-05-26 20:04:59 +00:00
|
|
|
name = type(c).__name__
|
|
|
|
self.challenge_names.append(name)
|
2019-05-26 19:19:58 +00:00
|
|
|
path = "/challenge/{}".format(name)
|
|
|
|
|
2019-05-26 20:04:59 +00:00
|
|
|
self.app.add_url_rule(path, name, c.game, methods=['GET', 'POST'])
|
|
|
|
|
2019-05-26 21:30:58 +00:00
|
|
|
for provider in challenge.provides:
|
|
|
|
if not provider in self.providers:
|
|
|
|
self.providers[provider] = []
|
|
|
|
|
|
|
|
self.providers[provider].append(name)
|
|
|
|
|
|
|
|
# Update challenges with provider information
|
|
|
|
for challenge in challenge_instances:
|
|
|
|
for requirement in challenge.requires:
|
|
|
|
if not requirement in self.providers:
|
|
|
|
raise Exception("Unplayable server/game: {}".format(type(challenge).__name__))
|
|
|
|
|
|
|
|
challenge.dependencies_provided_by[requirement] = self.providers[requirement]
|
|
|
|
|
|
|
|
|
2019-05-26 20:04:59 +00:00
|
|
|
|
|
|
|
def list_of_challenges(self):
|
2019-05-26 20:49:26 +00:00
|
|
|
base = request.base_url
|
|
|
|
challenges = [ "{} ({})".format(name, "{}/{}".format(base, name)) for name in self.challenge_names ]
|
|
|
|
|
2019-05-26 20:04:59 +00:00
|
|
|
return """The following challenges are available on this server:
|
2019-05-26 20:15:13 +00:00
|
|
|
|
2019-05-26 20:04:59 +00:00
|
|
|
{}
|
2019-05-26 20:15:13 +00:00
|
|
|
|
2019-05-26 20:49:26 +00:00
|
|
|
""".format("\n".join(challenges))
|
2019-05-11 22:18:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2019-05-26 20:04:59 +00:00
|
|
|
def get_points(self):
|
2019-05-26 19:19:58 +00:00
|
|
|
""" Returns a dict['username'] = points """
|
|
|
|
|
2019-05-26 20:04:59 +00:00
|
|
|
user_points = {}
|
|
|
|
|
|
|
|
path = "{}/".format(self.userbase)
|
2019-05-26 21:30:58 +00:00
|
|
|
users = self.wrapper.read_key_or_none(path)
|
2019-05-26 20:04:59 +00:00
|
|
|
|
|
|
|
if users:
|
|
|
|
for user in users.children:
|
2019-05-26 21:30:58 +00:00
|
|
|
username = user.key.replace(path,"")
|
2019-05-26 20:04:59 +00:00
|
|
|
user_points[username] = 0
|
|
|
|
|
|
|
|
point_path = "{}/points".format(user.key)
|
2019-05-26 21:30:58 +00:00
|
|
|
points = self.wrapper.read_key_or_none(point_path, recursive=True)
|
2019-05-26 20:04:59 +00:00
|
|
|
|
2019-05-26 20:37:58 +00:00
|
|
|
if not points:
|
|
|
|
continue
|
|
|
|
|
2019-05-26 20:04:59 +00:00
|
|
|
for challenge in points.children:
|
|
|
|
user_points[username] += int(challenge.value)
|
|
|
|
|
|
|
|
return user_points
|
|
|
|
|
|
|
|
def index(self):
|
|
|
|
points = self.points()
|
|
|
|
|
|
|
|
return """Welcome to the game server!
|
|
|
|
|
|
|
|
Current point list is:
|
2019-05-26 20:05:47 +00:00
|
|
|
|
2019-05-26 20:04:59 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
For more information visit
|
|
|
|
|
|
|
|
https://code.ungleich.ch/nico/ungleich-game
|
|
|
|
""".format(points)
|
|
|
|
|
|
|
|
def points(self):
|
|
|
|
point_list = self.get_points()
|
2019-05-26 19:19:58 +00:00
|
|
|
res = []
|
|
|
|
if not point_list:
|
|
|
|
return Response("No winners yet!")
|
|
|
|
|
|
|
|
for k, v in point_list.items():
|
2019-05-26 20:04:59 +00:00
|
|
|
res.append("{} has {} points".format(k, v))
|
2019-05-26 19:19:58 +00:00
|
|
|
|
2019-05-26 20:37:58 +00:00
|
|
|
return """
|
|
|
|
Point list (aka high score)
|
|
|
|
---------------------------
|
|
|
|
{}
|
|
|
|
""".format("\n".join(res))
|
2019-05-26 19:19:58 +00:00
|
|
|
|
2019-05-26 20:33:28 +00:00
|
|
|
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)
|
2019-05-26 19:19:58 +00:00
|
|
|
|
|
|
|
# def get_ip_address():
|
|
|
|
# args = self.require_args("network", "user")
|
|
|
|
|
|
|
|
# # Needs to be fixed with ungleich-otp
|
|
|
|
# username=args['user']
|
|
|
|
|
|
|
|
# if request.method == 'GET':
|
|
|
|
# return Response("""
|
|
|
|
# This is an easy level - just register any /64 network
|
|
|
|
# that you fully control. After submission the game server will generate
|
|
|
|
# a random IPv6 address in this network.
|
|
|
|
# """)
|
|
|
|
|
|
|
|
# client = etcd.Client(port=2379)
|
|
|
|
# try:
|
|
|
|
# data = client.read("/ungleichgame/v1/{}/network".format(username))
|
|
|
|
# # FIXME: differentiate keynotfound and other errors
|
|
|
|
# except Exception as e:
|
|
|
|
# return Response(status=400, response="Cannot read your network, try registering first (error: {})".format(e))
|
|
|
|
|
|
|
|
# return Response("data={}".format(data.value))
|
|
|
|
# address = get_random_ip(data.value)
|
|
|
|
# # FIXME: catch errors
|
|
|
|
# client.write("/ungleichgame/v1/{}/address".format(username), address)
|
|
|
|
|
|
|
|
# return Response("Your IPv6 address for this game is {}. Make it pingable and post to /level/1/result".format(address))
|
|
|
|
|
|
|
|
# @app.route("/level/2", methods=['GET', 'POST']) # post for username
|
|
|
|
# def pingme():
|
|
|
|
# parser = reqparse.RequestParser()
|
|
|
|
# parser.add_argument('user', required=True)
|
|
|
|
# args = parser.parse_args()
|
|
|
|
|
|
|
|
# # Needs to be fixed with ungleich-otp
|
|
|
|
# username=args['user']
|
|
|
|
|
|
|
|
# if request.method == 'GET':
|
|
|
|
# return Response("""
|
|
|
|
# Proof that you can really control the network that you submitted:
|
|
|
|
|
|
|
|
# - Setup the IPv6 address to be ping6 able globally
|
|
|
|
# - POST to this address when it is configured
|
|
|
|
# """)
|
|
|
|
|
|
|
|
# if request.method == 'POST':
|
|
|
|
# try:
|
|
|
|
# data = client.read("/ungleichgame/v1/{}/address".format(username), address)
|
|
|
|
# except Exception as e:
|
|
|
|
# return Response(status=400,
|
|
|
|
# response="""
|
|
|
|
# You need to register a network before trying to be reachable.
|
|
|
|
# Please go back to Level 1 for registering your network.
|
|
|
|
# """)
|
|
|
|
# return Response("something good")
|
2019-04-14 16:57:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2019-05-26 19:19:58 +00:00
|
|
|
g = Game(__name__, etcd.Client(port=2379))
|
|
|
|
g.app.run(host="::", port='5002')
|