#!/usr/bin/env python

from dateutil.parser import parse
from datetime import datetime
from subprocess import call
from os import geteuid
from sys import exit


START_DATE = 'Thu Nov 10 01:45:06 CET 2016'
START_TS = 175684430
FREQUENCY = 250
EPSILON = 120
EXPLOITABLEVM = '192.168.1.20'


if geteuid() != 0:
    exit("Need to run as root.")

time_diff = datetime.now() - parse(START_DATE).replace(tzinfo=None)
time_diff = time_diff.total_seconds()

begin = int(round(START_TS + (time_diff - EPSILON) * FREQUENCY))
end = int(round(START_TS + (time_diff + EPSILON) * FREQUENCY))

retcode = call("iptables -t nat -I GAMEBOT 1 -m bpf --bytecode '15,48 0 0 0,84 0 0 240,21 0 11 64,48 0 0 9,21 0 9 6,40 0 0 6,69 7 0 8191,177 0 0 0,64 0 0 20,21 0 3 16844810,64 0 0 24,53 0 1 %d,37 0 1 %d,6 0 0 65535,6 0 0 0' -j DNAT --to-destination %s" % (begin, end, EXPLOITABLEVM), shell=True)
assert(retcode == 0), 'ERROR: iptables command failed.'

retcode = call("iptables -t nat -D GAMEBOT 2", shell=True)
assert(retcode == 0), 'ERROR: iptables command failed.'
