Browse Source

added token

master
Domagoj Zecevic 3 years ago
parent
commit
4fd288cafb
  1. 1
      Dockerfile.dev
  2. 84
      ping_engine.py

1
Dockerfile.dev

@ -19,5 +19,6 @@ RUN apk del build-deps
EXPOSE 5000 EXPOSE 5000
ENV PYTHONUNBUFFERED 1 ENV PYTHONUNBUFFERED 1
ENV TOKEN="123abc!'?"
CMD flask --app ping_engine --debug run --host=0.0.0.0 CMD flask --app ping_engine --debug run --host=0.0.0.0

84
ping_engine.py

@ -11,51 +11,75 @@ app = Flask(__name__)
from flask import Flask from flask import Flask
app = Flask(__name__) app = Flask(__name__)
token = os.environ['TOKEN']
@app.route('/') @app.route('/')
def hello(): def hello():
greet = '<h1>You should not be here!</h1>' greet = '<h1>You should not be here!</h1>'
link = '<p><a href="https://www.google.com">Click me!</a></p>' link = '<p><a href="https://www.google.com">Click me!</a></p>'
return greet + link return greet + link + token
@app.route('/ping/<name>') @app.route('/ping/<name>/')
def ping(name): def ping(name):
response = os.system("ping -c 1 " + name)
now = str(datetime.datetime.now()) now = str(datetime.datetime.now())
if response == 0: apiToken = request.args.get('token')
instruc = ' up' if apiToken == token:
response = os.system("ping -c 1 " + name)
if response == 0:
instruc = ' up'
else:
instruc = ' down'
else: else:
instruc = ' down' instruc = ' wrong token'
ip_addr = request.remote_addr
out = '%s - Unauthorized from IP: %s' %(now, ip_addr)
print (out)
return now + instruc return now + instruc
@app.route('/http/<name>') @app.route('/http/<name>/')
def http(name): def http(name):
address = 'http://%s' %name apiToken = request.args.get('token')
now = str(datetime.datetime.now()) now = str(datetime.datetime.now())
try: if apiToken == token:
r = requests.get(address) address = 'http://%s' %name
status = r.status_code try:
status_string = ' %s' %str(status) r = requests.get(address)
except requests.exceptions.Timeout: status = r.status_code
status_string = " Timeout" status_string = ' %s' %str(status)
except requests.exceptions.TooManyRedirects: except requests.exceptions.Timeout:
status_string = " TooManyRedirects" status_string = " Timeout"
except requests.exceptions.RequestException as e: except requests.exceptions.TooManyRedirects:
status_string = ' %s' %str(e) 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 now + status_string return now + status_string
@app.route('/https/<name>') @app.route('/https/<name>/')
def https(name): def https(name):
address = 'https://%s' %name apiToken = request.args.get('token')
try: now = str(datetime.datetime.now())
r = requests.get(address) if apiToken == token:
status = r.status_code address = 'https://%s' %name
status_string = ' %s' %str(status) try:
except requests.exceptions.Timeout: r = requests.get(address)
status_string = " Timeout" status = r.status_code
except requests.exceptions.TooManyRedirects: status_string = ' %s' %str(status)
status_string = " TooManyRedirects" except requests.exceptions.Timeout:
except requests.exceptions.RequestException as e: status_string = " Timeout"
status_string = ' %s' %str(e) 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 now + status_string return now + status_string
if __name__ == '__main__': if __name__ == '__main__':

Loading…
Cancel
Save