Created
August 17, 2024 16:48
-
-
Save anir0y/1444602852d8daebfb2d04b1aea444fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import os | |
import socket | |
import requests | |
from flask import Flask, request | |
app = Flask(__name__) | |
# Get absolute path for the log file | |
log_file_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'requests.log') | |
# Print the log file path for debugging | |
print(f"Log file path: {log_file_path}") | |
# Check if the directory is writable | |
if os.access(os.path.dirname(log_file_path), os.W_OK): | |
print("Directory is writable") | |
else: | |
print("Directory is NOT writable") | |
# Set up logging with a backup handler to the console in case of issues | |
try: | |
logging.basicConfig(filename=log_file_path, level=logging.INFO, | |
format='%(asctime)s - %(message)s') | |
logging.info("Logging initialized.") | |
print("Logging initialized successfully.") | |
except Exception as e: | |
print(f"Failed to initialize logging: {e}") | |
# Function to get the private and public IPs | |
def get_private_ip(): | |
try: | |
hostname = socket.gethostname() | |
private_ip = socket.gethostbyname(hostname) | |
return private_ip | |
except Exception as e: | |
logging.error(f"Error retrieving private IP: {e}") | |
return "Unknown" | |
def get_public_ip(): | |
try: | |
public_ip = requests.get('https://api64.ipify.org').text | |
return public_ip | |
except Exception as e: | |
logging.error(f"Error retrieving public IP: {e}") | |
return "Unknown" | |
@app.route('/', defaults={'path': ''}, methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH']) | |
@app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH']) | |
def catch_all(path): | |
log_data = [] | |
log_data.append(f"Received {request.method} request for /{path}") | |
log_data.append("Headers:") | |
for header, value in request.headers.items(): | |
log_data.append(f"{header}: {value}") | |
log_data.append("Body:") | |
log_data.append(request.get_data(as_text=True)) | |
# Log to file | |
logging.info("\n".join(log_data)) | |
# Optionally, print the log data to console as well | |
print("\n".join(log_data)) | |
print("=={EOR}==") | |
return "Server:OK\r\n" | |
if __name__ == "__main__": | |
try: | |
private_ip = get_private_ip() | |
public_ip = get_public_ip() | |
# Log IPs as requested | |
logging.info(f"[+] Private IP: {private_ip}") | |
logging.info(f"[+] Public IP: {public_ip}") | |
# Print IPs as requested | |
print(f"[+] Private IP: {private_ip}") | |
print(f"[+] Public IP: {public_ip}") | |
app.run(host='0.0.0.0', port=80) | |
except Exception as e: | |
logging.error(f"Error starting Flask app: {e}") | |
print(f"Error starting Flask app: {e}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment