-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
243 lines (201 loc) · 8.25 KB
/
main.py
File metadata and controls
243 lines (201 loc) · 8.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env python3
import asyncio
import aiohttp
import uuid
import json
import os
import datetime
import yaml
import sys
from aiohttp import web
from pathlib import Path
import signal
import threading
from flask import Flask, render_template, jsonify
class HTTPProxyLogger:
def __init__(self, config):
self.listen_port = config['server']['listen_port']
self.forward_port = config['server']['forward_port']
self.host = config['server']['host']
self.log_dir = Path(config['logging']['log_dir'])
self.log_dir.mkdir(exist_ok=True)
self.master_log = self.log_dir / "requests.log"
async def proxy_handler(self, request):
request_id = str(uuid.uuid4())
timestamp = datetime.datetime.now().isoformat()
# Extract request data
request_data = {
"id": request_id,
"timestamp": timestamp,
"method": request.method,
"path": request.path,
"query_string": str(request.query_string),
"headers": dict(request.headers),
"body": await request.text() if request.can_read_body else None
}
# Forward the request
async with aiohttp.ClientSession() as session:
forward_url = f"http://{self.host}:{self.forward_port}{request.path}"
if request.query_string:
forward_url += f"?{request.query_string}"
try:
async with session.request(
method=request.method,
url=forward_url,
headers={k: v for k, v in request.headers.items() if k.lower() != 'host'},
data=await request.read()
) as response:
response_data = {
"status": response.status,
"headers": dict(response.headers),
"body": await response.text()
}
# Log to individual file
request_file = self.log_dir / f"{request_id}.log"
with open(request_file, 'w') as f:
json.dump({
"request": request_data,
"response": response_data
}, f, indent=2)
# Log to master file with response code
with open(self.master_log, 'a') as f:
f.write(f"{request_data['method']} {request_data['path']} {request_id} {response_data['status']}\n")
# Return response to client
return web.Response(
status=response.status,
headers={k: v for k, v in response.headers.items() if k.lower() not in ['content-encoding', 'transfer-encoding', 'content-length']},
body=response_data["body"]
)
except Exception as e:
error_data = {
"error": str(e),
"timestamp": timestamp
}
# Log error
request_file = self.log_dir / f"{request_id}.log"
with open(request_file, 'w') as f:
json.dump({
"request": request_data,
"error": error_data
}, f, indent=2)
with open(self.master_log, 'a') as f:
f.write(f"{request_data['method']} {request_data['path']} {request_id} 502 ERROR\n")
return web.Response(status=502, text=f"Proxy Error: {str(e)}")
async def start(self):
app = web.Application()
app.router.add_route('*', '/{path:.*}', self.proxy_handler)
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, self.host, self.listen_port)
print(f"HTTP Proxy listening on {self.host}:{self.listen_port}")
print(f"Forwarding requests to {self.host}:{self.forward_port}")
print(f"Logging to directory: {self.log_dir}")
await site.start()
# Keep the server running
while True:
await asyncio.sleep(3600)
def load_config(config_path):
try:
with open(config_path, 'r') as file:
return yaml.safe_load(file)
except FileNotFoundError:
print(f"Error: Configuration file not found at {config_path}")
sys.exit(1)
except yaml.YAMLError as e:
print(f"Error parsing YAML configuration: {e}")
sys.exit(1)
except Exception as e:
print(f"Unexpected error loading configuration: {e}")
sys.exit(1)
# Flask UI setup
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/requests')
def get_requests():
config_path = os.environ.get("PROXY_CONFIG", "config.yaml")
config = load_config(config_path)
log_dir = Path(config['logging']['log_dir'])
master_log = log_dir / "requests.log"
requests_list = []
if master_log.exists():
with open(master_log, 'r') as f:
for line in f:
parts = line.strip().split()
if len(parts) >= 4:
method = parts[0]
path = parts[1]
request_id = parts[2]
status_code = parts[3]
has_error = len(parts) > 4 and parts[4] == "ERROR"
requests_list.insert(0, {
"method": method,
"path": path,
"id": request_id,
"status_code": status_code,
"status": "ERROR" if has_error else "OK"
})
return jsonify(requests_list)
@app.route('/request/<request_id>')
def get_request_details(request_id):
config_path = os.environ.get("PROXY_CONFIG", "config.yaml")
config = load_config(config_path)
log_dir = Path(config['logging']['log_dir'])
request_file = log_dir / f"{request_id}.log"
if request_file.exists():
with open(request_file, 'r') as f:
try:
data = json.load(f)
return jsonify(data)
except json.JSONDecodeError:
return jsonify({"error": "Invalid JSON in log file"}), 500
else:
return jsonify({"error": "Request not found"}), 404
@app.route('/clean', methods=['POST'])
def clean_logs():
config_path = os.environ.get("PROXY_CONFIG", "config.yaml")
config = load_config(config_path)
log_dir = Path(config['logging']['log_dir'])
master_log = log_dir / "requests.log"
if master_log.exists():
open(master_log, 'w').close()
for file in log_dir.glob("*.log"):
if file.name != "requests.log":
try:
file.unlink()
except Exception:
pass
return jsonify({"success": True})
def start_ui_server(config):
if not config.get('ui', {}).get('enabled', False):
return
templates_dir = Path(__file__).parent / "templates"
templates_dir.mkdir(exist_ok=True)
ui_port = config['ui']['port']
print(f"Starting UI server on port {ui_port}")
threading.Thread(target=lambda: app.run(host='0.0.0.0', port=ui_port, debug=False, use_reloader=False)).start()
def main():
config_path = os.environ.get("PROXY_CONFIG", "config.yaml")
config = load_config(config_path)
# Start UI server if enabled
if config.get('ui', {}).get('enabled', False):
start_ui_server(config)
print(f"UI server started on port {config['ui']['port']}")
# Start proxy server
proxy = HTTPProxyLogger(config)
loop = asyncio.get_event_loop()
# Handle graceful shutdown
def signal_handler():
print("\nShutting down...")
loop.stop()
for sig in (signal.SIGINT, signal.SIGTERM):
loop.add_signal_handler(sig, signal_handler)
try:
loop.run_until_complete(proxy.start())
except KeyboardInterrupt:
print("\nProxy stopped")
finally:
loop.close()
if __name__ == "__main__":
main()