diff --git a/Dockerfile.dev b/Dockerfile.dev index d0f2e87..45e6073 100644 --- a/Dockerfile.dev +++ b/Dockerfile.dev @@ -19,5 +19,6 @@ RUN apk del build-deps EXPOSE 5000 ENV PYTHONUNBUFFERED 1 +ENV TOKEN="123abc!'?" CMD flask --app ping_engine --debug run --host=0.0.0.0 \ No newline at end of file diff --git a/ping_engine.py b/ping_engine.py index 9ec0b72..19d728f 100644 --- a/ping_engine.py +++ b/ping_engine.py @@ -11,51 +11,75 @@ 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 + return greet + link + token -@app.route('/ping/') +@app.route('/ping//') def ping(name): - response = os.system("ping -c 1 " + name) now = str(datetime.datetime.now()) - if response == 0: - instruc = ' up' + 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 = ' down' + instruc = ' wrong token' + ip_addr = request.remote_addr + out = '%s - Unauthorized from IP: %s' %(now, ip_addr) + print (out) return now + instruc -@app.route('/http/') +@app.route('/http//') def http(name): - address = 'http://%s' %name + apiToken = request.args.get('token') now = str(datetime.datetime.now()) - 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) + 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 now + status_string -@app.route('/https/') +@app.route('/https//') def https(name): - 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) + 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 now + status_string if __name__ == '__main__':