#!/usr/bin/env python

import requests, sys, time
import paho.mqtt.client as mqtt


# set connection to broker
mqttBroker ="mqtt.33co.de"
client = mqtt.Client (client_id="", clean_session=True)
client.tls_set()
client.connect(mqttBroker, port=8883)

# get ip location
#print(sys.argv[1])
res = requests.get('http://ipwho.is/'+ sys.argv[1]).json()
# print(res)
print(res['ip'])
print(res['latitude'])
print(res['longitude'])

servername = "satelite"

client.publish("Attacker/"+ servername+"/location", "{\"iconColor\":\"#0434F2\",\"time\":\""+str(time.ctime())+"\",\"lat\":"+str(res['latitude'])+",\"lon\":"+str(res['longitude'])+",\"name\":\""+str(res['ip'])+"\"}")

client.disconnect()

# in case of problem check SELinux
# many t=runs to release ports, access
# audit2allow -w -a
# audit2allow  -a
# audit2allow  -a -M fail2ban_t
# semodule -i fail2ban_t.pp

# or just run setsebool 0


# to get the location  curl http://ipwho.is/197.26.19.254
# curl http://ipwho.is/197.26.19.254 |  jq -r '.latitude,  .longitude'
# guide to create custom action
# https://webcp.io/custom-fail2ban-action/
