|
| 1 | +from flask import Flask, make_response, abort, jsonify |
| 2 | +import os |
| 3 | +import requests |
| 4 | +import hashlib |
| 5 | +from cryptography.hazmat.primitives import serialization, hashes |
| 6 | +from cryptography.hazmat.primitives.asymmetric import padding |
| 7 | +from flask_caching import Cache |
| 8 | + |
| 9 | +# Configuration |
| 10 | +USE_GITHUB = os.environ.get('USE_GITHUB', '0') == '1' |
| 11 | +GITHUB_RAW_BASE_URL = "https://raw.githubusercontent.com/huntbase-io/scout-content/main" |
| 12 | +ALLOWED_OS_DIRS = ["windows", "linux", "darwin"] |
| 13 | + |
| 14 | +# Local directories (used when USE_GITHUB=0) |
| 15 | +SCRIPTS_DIR = os.path.join(os.getcwd(), 'scripts') |
| 16 | +BIN_DIR = os.path.join(os.getcwd(), 'bin') |
| 17 | + |
| 18 | +app = Flask(__name__) |
| 19 | + |
| 20 | +# Configure caching (mainly for GitHub fetching) |
| 21 | +app.config['CACHE_TYPE'] = 'SimpleCache' # for production consider Redis or Memcached |
| 22 | +app.config['CACHE_DEFAULT_TIMEOUT'] = 300 |
| 23 | +cache = Cache(app) |
| 24 | + |
| 25 | +# Load the private key |
| 26 | +with open('private_key.pem', 'rb') as f: |
| 27 | + private_key = serialization.load_pem_private_key( |
| 28 | + f.read(), |
| 29 | + password=None, |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +# ---------- Helper Functions ---------- |
| 34 | +@cache.memoize(timeout=300) |
| 35 | +def fetch_from_github(path): |
| 36 | + """ |
| 37 | + Fetch content from GitHub raw URL. Returns content or None if not found. |
| 38 | + """ |
| 39 | + url = f"{GITHUB_RAW_BASE_URL}/{path}" |
| 40 | + response = requests.get(url) |
| 41 | + if response.status_code == 200: |
| 42 | + return response.content |
| 43 | + return None |
| 44 | + |
| 45 | + |
| 46 | +def get_local_file_content(root_dir, filename): |
| 47 | + """ |
| 48 | + Get content from a local file. Returns content or None if not found. |
| 49 | + """ |
| 50 | + file_path = os.path.join(root_dir, filename) |
| 51 | + if not os.path.isfile(file_path): |
| 52 | + return None |
| 53 | + with open(file_path, 'rb') as f: |
| 54 | + return f.read() |
| 55 | + |
| 56 | + |
| 57 | +def get_script_content(os_dir, filename): |
| 58 | + """ |
| 59 | + Get script content either from GitHub or locally, depending on USE_GITHUB. |
| 60 | + If GitHub fails or USE_GITHUB=0, fallback to local if desired. |
| 61 | + """ |
| 62 | + # Validate OS directory if provided |
| 63 | + if os_dir and os_dir not in ALLOWED_OS_DIRS: |
| 64 | + return None |
| 65 | + |
| 66 | + # Construct path |
| 67 | + if os_dir: |
| 68 | + path = f"scripts/{os_dir}/{filename}" |
| 69 | + local_path = os.path.join(SCRIPTS_DIR, os_dir, filename) |
| 70 | + else: |
| 71 | + path = f"scripts/{filename}" |
| 72 | + local_path = os.path.join(SCRIPTS_DIR, filename) |
| 73 | + |
| 74 | + content = None |
| 75 | + if USE_GITHUB: |
| 76 | + # Try to fetch from GitHub |
| 77 | + content = fetch_from_github(path) |
| 78 | + # If not found on GitHub, we could optionally fallback to local: |
| 79 | + # if content is None: |
| 80 | + # content = get_local_file_content(os.path.join(SCRIPTS_DIR, os_dir) if os_dir else SCRIPTS_DIR, filename) |
| 81 | + else: |
| 82 | + # Local only |
| 83 | + content = get_local_file_content(os.path.join(SCRIPTS_DIR, os_dir) if os_dir else SCRIPTS_DIR, filename) |
| 84 | + |
| 85 | + return content |
| 86 | + |
| 87 | + |
| 88 | +def get_bin_content(filename): |
| 89 | + """ |
| 90 | + Get binary content either from GitHub or locally. |
| 91 | + """ |
| 92 | + path = f"bin/{filename}" |
| 93 | + if USE_GITHUB: |
| 94 | + content = fetch_from_github(path) |
| 95 | + # If desired, fallback to local if GitHub not found: |
| 96 | + # if content is None: |
| 97 | + # content = get_local_file_content(BIN_DIR, filename) |
| 98 | + else: |
| 99 | + content = get_local_file_content(BIN_DIR, filename) |
| 100 | + return content |
| 101 | + |
| 102 | + |
| 103 | +def sign_content(content): |
| 104 | + """ |
| 105 | + Sign the given content using the private key. |
| 106 | + """ |
| 107 | + signature = private_key.sign( |
| 108 | + content, |
| 109 | + padding.PKCS1v15(), |
| 110 | + hashes.SHA256() |
| 111 | + ) |
| 112 | + return signature.hex() |
| 113 | + |
| 114 | +def compute_hash(content): |
| 115 | + """ |
| 116 | + Compute SHA256 hash of the content. |
| 117 | + """ |
| 118 | + hasher = hashlib.sha256() |
| 119 | + hasher.update(content) |
| 120 | + return hasher.hexdigest() |
| 121 | + |
| 122 | + |
| 123 | +# ---------- Routes ---------- |
| 124 | + |
| 125 | +@app.route('/bin/<path:filename>', methods=['GET']) |
| 126 | +def serve_bin(filename): |
| 127 | + content = get_bin_content(filename) |
| 128 | + if content is None: |
| 129 | + abort(404, description="Binary not found") |
| 130 | + |
| 131 | + signature_hex = sign_content(content) |
| 132 | + response = make_response(content) |
| 133 | + response.headers['Content-Type'] = 'application/octet-stream' |
| 134 | + response.headers['X-Signature'] = signature_hex |
| 135 | + return response |
| 136 | + |
| 137 | + |
| 138 | +@app.route('/scripts/<path:filename>', methods=['GET']) |
| 139 | +def serve_script_no_os(filename): |
| 140 | + # This route is for scripts without OS directory specification |
| 141 | + content = get_script_content(None, filename) |
| 142 | + if content is None: |
| 143 | + abort(404, description="Script not found") |
| 144 | + |
| 145 | + signature_hex = sign_content(content) |
| 146 | + response = make_response(content) |
| 147 | + response.headers['Content-Type'] = 'application/octet-stream' |
| 148 | + response.headers['X-Signature'] = signature_hex |
| 149 | + return response |
| 150 | + |
| 151 | + |
| 152 | +@app.route('/scripts/<os_dir>/<path:filename>', methods=['GET']) |
| 153 | +def serve_script(os_dir, filename): |
| 154 | + # Validate OS directory and fetch the script |
| 155 | + content = get_script_content(os_dir, filename) |
| 156 | + if content is None: |
| 157 | + abort(404, description="Script not found") |
| 158 | + |
| 159 | + signature_hex = sign_content(content) |
| 160 | + response = make_response(content) |
| 161 | + response.headers['Content-Type'] = 'application/octet-stream' |
| 162 | + response.headers['X-Signature'] = signature_hex |
| 163 | + return response |
| 164 | + |
| 165 | + |
| 166 | +@app.route('/scripts/hash/<path:filename>', methods=['GET']) |
| 167 | +def get_script_hash_no_os(filename): |
| 168 | + content = get_script_content(None, filename) |
| 169 | + if content is None: |
| 170 | + abort(404, description="Script not found") |
| 171 | + |
| 172 | + script_hash = compute_hash(content) |
| 173 | + return jsonify({"script_hash": script_hash}) |
| 174 | + |
| 175 | + |
| 176 | +@app.route('/scripts/hash/<os_dir>/<path:filename>', methods=['GET']) |
| 177 | +def get_script_hash(os_dir, filename): |
| 178 | + content = get_script_content(os_dir, filename) |
| 179 | + if content is None: |
| 180 | + abort(404, description="Script not found") |
| 181 | + |
| 182 | + script_hash = compute_hash(content) |
| 183 | + return jsonify({"script_hash": script_hash}) |
| 184 | + |
| 185 | + |
| 186 | +if __name__ == '__main__': |
| 187 | + # Run the app |
| 188 | + # Set the USE_GITHUB=1 environment variable before running if you want GitHub fetching |
| 189 | + # Example: USE_GITHUB=1 python unified_app.py |
| 190 | + app.run(host='127.0.0.1', port=5000) |
0 commit comments