from flask import Flask,request,jsonify
import os
import requests
import datetime
import json
import OpenSSL
import ssl
import pytz
app = Flask(__name__)
from flask import Flask
app = Flask(__name__)
token = os.environ['TOKEN']
@app.route('/')
def hello():
greet = '
You should not be here!
'
link = 'Click me!
'
return greet + link + token
@app.route('/ping//')
def ping(name):
now = str(datetime.datetime.now())
apiToken = request.args.get('token')
if apiToken == token:
response = os.system("ping -c 1 " + name)
if response == 0:
instruc = 'up'
else:
instruc = 'down'
else:
instruc = ' wrong token'
ip_addr = request.remote_addr
out = '%s - Unauthorized from IP: %s' %(now, ip_addr)
print (out)
return jsonify(
time = now,
status = instruc)
@app.route('/http//')
def http(name):
apiToken = request.args.get('token')
now = str(datetime.datetime.now())
if apiToken == token:
address = 'http://%s' %name
try:
r = requests.get(address)
status = r.status_code
status_string = '%s' %str(status)
except requests.exceptions.Timeout:
status_string = "Timeout"
except requests.exceptions.TooManyRedirects:
status_string = "TooManyRedirects"
except requests.exceptions.RequestException as e:
status_string = '%s' %str(e)
else:
status_string = 'wrong token'
ip_addr = request.remote_addr
out = '%s - Unauthorized from IP: %s' %(now, ip_addr)
print (out)
return jsonify (
time = now,
status = status_string)
@app.route('/https//')
def https(name):
apiToken = request.args.get('token')
now = str(datetime.datetime.now())
if apiToken == token:
address = 'https://%s' %name
try:
r = requests.get(address)
status = r.status_code
status_string = '%s' %str(status)
except requests.exceptions.Timeout:
status_string = "Timeout"
except requests.exceptions.TooManyRedirects:
status_string = "TooManyRedirects"
except requests.exceptions.RequestException as e:
status_string = '%s' %str(e)
else:
status_string = ' wrong token'
ip_addr = request.remote_addr
out = '%s - Unauthorized from IP: %s' %(now, ip_addr)
print (out)
return jsonify (
time = now,
status = status_string)
@app.route('/api/', methods=['POST'])
def api():
apiToken = request.args.get('token')
now = str(datetime.datetime.now())
if apiToken == token:
request_data = request.get_json()
address = request_data['address']
if address[0 : 4] == 'http':
r = requests.get(address)
try:
json.loads(r.content)
status_string=r.content
except ValueError as e:
status_string="not json"
else:
status_string = ' wrong token'
ip_addr = request.remote_addr
out = '%s - Unauthorized from IP: %s' %(now, ip_addr)
print (out)
return status_string
# return jsonify (
# time = now,
# status = status_string)
@app.route('/certdate//')
def cert(name):
apiToken = request.args.get('token')
now = str(datetime.datetime.now())
now_date=datetime.datetime.now()
if apiToken == token:
cert=ssl.get_server_certificate((name, 443))
x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)
bytes=x509.get_notAfter()
timestamp_str = str(bytes.decode('utf-8'))
date_string = str(datetime.datetime.strptime(timestamp_str, '%Y%m%d%H%M%S%z'))
date = datetime.datetime.strptime(timestamp_str, '%Y%m%d%H%M%S%z')
if date.replace(tzinfo=pytz.utc) > now_date.replace(tzinfo=pytz.utc):
status_string = "OK"
else:
status_string = "Expired"
else:
status_string = ' wrong token'
ip_addr = request.remote_addr
out = '%s - Unauthorized from IP: %s' %(now, ip_addr)
print (out)
return jsonify (
time = now,
date_valid = date_string,
status = status_string)
if __name__ == '__main__':
app.run(debug=True)