From dd8e14478cf7448d900048a30ed9e93e7a206950 Mon Sep 17 00:00:00 2001 From: deflax Date: Fri, 26 Jan 2018 01:33:52 +0200 Subject: [PATCH] get pub ips from api, place them in cache --- frankenrouter.py | 76 +++++++++++++++--------------------------------- interfaces | 1 + slavefw.sh | 4 ++- updateipcache.py | 26 +++++++++++++++++ 4 files changed, 53 insertions(+), 54 deletions(-) create mode 120000 interfaces create mode 100644 updateipcache.py diff --git a/frankenrouter.py b/frankenrouter.py index fee178f..36dd0a0 100644 --- a/frankenrouter.py +++ b/frankenrouter.py @@ -5,10 +5,9 @@ import requests import json import datetime import os +import sys -dburl = 'https://www.datapoint.bg/vmanager/slavetables/1' clientiface = 'ens19' - workscriptpath = '/root/fr-workscripts/' ### @@ -278,58 +277,29 @@ dhcpd -4 -cf /root/fr-vlanconf/v{0}.dhconf -lf /root/fr-vlanconf/v{0}.dhlease -p return data def setpubips(): - db_result = requests.get(dburl, headers={"content-type": "application/json"}, timeout=30 ) - proxjson = db_result.json() - for key, value in proxjson['addresses'].items(): - pass - #data += '#ip: ' + value['ipv4'] + ' mac:' + value['mac'] + '\n' - #data += 'iptables -P FORWARD DROP\n' - #data += 'iptables -P FORWARD -j ACCEPT -i ' - - data = """ -ip link del vtap107 -ip link add vtap107 link $INET_IFACE type macvlan -ip addr add 87.120.110.42/24 dev vtap107 -ip link set dev vtap107 up -$IPT -t nat -A PREROUTING -d 87.120.110.42 -j DNAT --to-destination 10.0.107.10 -$IPT -t nat -A POSTROUTING -s 10.0.107.10 -j SNAT --to-source 87.120.110.42 - -ip link del vtap120 -ip link add vtap120 link $INET_IFACE type macvlan -ip addr add 87.120.110.43/24 dev vtap120 -ip link set dev vtap120 up -$IPT -t nat -A PREROUTING -d 87.120.110.43 -j DNAT --to-destination 10.0.120.10 -$IPT -t nat -A POSTROUTING -s 10.0.120.10 -j SNAT --to-source 87.120.110.43 - -ip link del vtap121 -ip link add vtap121 link $INET_IFACE type macvlan -ip addr add 87.120.110.44/24 dev vtap121 -ip link set dev vtap121 up -$IPT -t nat -A PREROUTING -d 87.120.110.44 -j DNAT --to-destination 10.0.121.10 -$IPT -t nat -A POSTROUTING -s 10.0.121.10 -j SNAT --to-source 87.120.110.44 - -ip link del vtap122 -ip link add vtap122 link $INET_IFACE type macvlan -ip addr add 87.120.110.41/24 dev vtap122 -ip link set dev vtap122 up -$IPT -t nat -A PREROUTING -d 87.120.110.41 -j DNAT --to-destination 10.0.122.10 -$IPT -t nat -A POSTROUTING -s 10.0.122.10 -j SNAT --to-source 87.120.110.41 - -ip link del vtap140 -ip link add vtap140 link $INET_IFACE type macvlan -ip addr add 87.120.110.40/24 dev vtap140 -ip link set dev vtap140 up -$IPT -t nat -A PREROUTING -d 87.120.110.40 -j DNAT --to-destination 10.0.140.10 -$IPT -t nat -A POSTROUTING -s 10.0.140.10 -j SNAT --to-source 87.120.110.40 - -""" + rr = open('/root/pubip.cache', 'r').read() + cache = json.loads(rr) + data = '' + for ip, vlan in cache.items(): + print('ip: ' + ip) + print('vlan: ' + vlan) + print(' ') + data += """ +ip link del vtap{1} +ip link add vtap{1} link $INET_IFACE type macvlan +ip addr add {0}/24 dev vtap{1} +ip link set dev vtap{1} up +$IPT -t nat -A PREROUTING -d {0} -j DNAT --to-destination 10.0.{1}.10 +$IPT -t nat -A POSTROUTING -s 10.0.{1}.10 -j SNAT --to-source {0} +""".format(ip, vlan) return data -def routerinit(): - bashexec('fwfconfig', initfw()) - bashexec('vlfconfig', setvlans(clientiface)) - bashexec('ipfconfig', setpubips()) - if __name__ == "__main__": - routerinit() + if sys.argv[1] == 'init': + bashexec('fwfconfig', initfw()) + bashexec('vlfconfig', setvlans(clientiface)) + + if sys.argv[1] == 'apply': + print(setpubips()) + #bashexec('ipfconfig', setpubips()) diff --git a/interfaces b/interfaces new file mode 120000 index 0000000..1ed334e --- /dev/null +++ b/interfaces @@ -0,0 +1 @@ +/etc/network/interfaces \ No newline at end of file diff --git a/slavefw.sh b/slavefw.sh index b1d2aa1..78659b4 100755 --- a/slavefw.sh +++ b/slavefw.sh @@ -11,4 +11,6 @@ ip addr add $TRANSPORT_IP dev $PUBIF sleep 5 ip route add default via $TRANSPORT_GW -python3 /root/frankenrouter/frankenrouter.py +python3 /root/frankenrouter/frankenrouter.py init +python3 /root/frankenrouter/frankenrouter.py apply + diff --git a/updateipcache.py b/updateipcache.py new file mode 100644 index 0000000..b97756d --- /dev/null +++ b/updateipcache.py @@ -0,0 +1,26 @@ +# downloads and update the ip cache. + +import requests +import json +import sys + +slave_name = 'lexx' + +api_url = 'https://www.datapoint.bg/vmanager/slavetables' + +### + +try: + data = {"passphrase": "batkataisthebest1", "slavename": str(slave_name)} + apireq = requests.post(api_url, headers={'Content-Type': 'application/json'}, data=json.dumps(data), timeout=30) + result = apireq.json() +except: + sys.exit() + +if result['status'] == 'ok': + del result['status'] + wr = open('/root/pubip.cache', 'w') + wr.write(json.dumps(result)) + wr.close() + print('public ip cache updated') +