| 1 | + | #!/bin/python3 |
| 2 | + | # |
| 3 | + | # Author by Panagiotis Chartas (t3l3machus) |
| 4 | + | # https://github.com/t3l3machus |
| 5 | + | |
| 6 | + | from http.server import HTTPServer, BaseHTTPRequestHandler |
| 7 | + | import ssl, sys, base64, re, os, argparse |
| 8 | + | from warnings import filterwarnings |
| 9 | + | from datetime import date, datetime |
| 10 | + | from urllib.parse import unquote, urlparse |
| 11 | + | from threading import Thread |
| 12 | + | from io import StringIO |
| 13 | + | from time import sleep |
| 14 | + | from subprocess import check_output |
| 15 | + | |
| 16 | + | filterwarnings("ignore", category = DeprecationWarning) |
| 17 | + | |
| 18 | + | ''' Colors ''' |
| 19 | + | MAIN = '\033[38;5;50m' |
| 20 | + | GREEN = '\033[38;5;82m' |
| 21 | + | BLUE = '\033[0;38;5;12m' |
| 22 | + | LPURPLE = '\033[0;38;5;201m' |
| 23 | + | ORANGE = '\033[0;38;5;214m' |
| 24 | + | ORANGEB = '\033[1;38;5;214m' |
| 25 | + | PURPLE = '\033[0;38;5;141m' |
| 26 | + | B_PURPLE = '\033[45m' |
| 27 | + | YELLOW="\033[0;38;5;11m" |
| 28 | + | RED = '\033[1;31m' |
| 29 | + | B_RED = '\033[41m' |
| 30 | + | END = '\033[0m' |
| 31 | + | B_END = '\033[49m' |
| 32 | + | BOLD = '\033[1m' |
| 33 | + | ULINE = '\033[4m' |
| 34 | + | |
| 35 | + | |
| 36 | + | ''' MSG Prefixes ''' |
| 37 | + | INFO = f'[{MAIN}Info{END}]' |
| 38 | + | WARN = f'[{ORANGE}Warning{END}]' |
| 39 | + | DBG = f'[{ORANGE}Debug{END}]' |
| 40 | + | IMPORTANT = f'[{ORANGE}Important{END}]' |
| 41 | + | FAILED = f'[{RED}Fail{END}]' |
| 42 | + | |
| 43 | + | parser = argparse.ArgumentParser() |
| 44 | + | parser.add_argument("-c", "--cert", action="store", help = "Your certificate.") |
| 45 | + | parser.add_argument("-k", "--key", action="store", help = "The private key for your certificate. ") |
| 46 | + | parser.add_argument("-p", "--port", action="store", help = "Server port.", type = int) |
| 47 | + | parser.add_argument("-f", "--file", action="store", help = "File to be served via GET.") |
| 48 | + | |
| 49 | + | args = parser.parse_args() |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | def print_green(msg): |
| 54 | + | print(f'{GREEN}{msg}{END}') |
| 55 | + | |
| 56 | + | |
| 57 | + | def debug(msg): |
| 58 | + | print(f'{DBG} {msg}{END}') |
| 59 | + | |
| 60 | + | |
| 61 | + | def chill(): |
| 62 | + | pass |
| 63 | + | |
| 64 | + | |
| 65 | + | # -------------- HTTPS Server -------------- # |
| 66 | + | class Https_Server(BaseHTTPRequestHandler): |
| 67 | + | |
| 68 | + | valid_endpoints = { |
| 69 | + | 'GET' : 'x3Rty7', |
| 70 | + | 'POST' : 'aWq8tY' |
| 71 | + | } |
| 72 | + | |
| 73 | + | |
| 74 | + | def do_GET(self): |
| 75 | + | |
| 76 | + | try: |
| 77 | + | if self.path == f'/{self.valid_endpoints["GET"]}': |
| 78 | + | self.server_version = "Apache/2.4.1" |
| 79 | + | self.sys_version = "" |
| 80 | + | self.send_response(200) |
| 81 | + | self.send_header('Content-type', 'text/javascript; charset=UTF-8') |
| 82 | + | self.send_header('Access-Control-Allow-Origin', '*') |
| 83 | + | self.end_headers() |
| 84 | + | |
| 85 | + | try: |
| 86 | + | print(f'GET {self.path} :\nFrom: {self.client_address[0]}\nCookies: {self.headers["Cookies"]}') |
| 87 | + | except: |
| 88 | + | print(f'GET {self.path} :\nFrom: {self.client_address[0]}\nCookies: ') |
| 89 | + | |
| 90 | + | if args.file: |
| 91 | + | f = open(args.file, 'r') |
| 92 | + | content = f.read() |
| 93 | + | f.close() |
| 94 | + | |
| 95 | + | else: |
| 96 | + | content = "Move on mate." |
| 97 | + | |
| 98 | + | self.wfile.write(bytes(content, "utf-8")) |
| 99 | + | |
| 100 | + | except: |
| 101 | + | pass |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | def do_POST(self): |
| 107 | + | |
| 108 | + | try: |
| 109 | + | if self.path == f'/{self.valid_endpoints["POST"]}': |
| 110 | + | self.server_version = "Apache/2.4.1" |
| 111 | + | self.sys_version = "" |
| 112 | + | self.send_response(200) |
| 113 | + | self.send_header('Access-Control-Allow-Origin', '*') |
| 114 | + | self.send_header('Content-Type', 'text/plain') |
| 115 | + | self.end_headers() |
| 116 | + | self.wfile.write(b'OK') |
| 117 | + | real_action = self.headers["Action"] |
| 118 | + | content_len = int(self.headers.get('Content-Length')) |
| 119 | + | form_attrs = {'Action':self.headers.get("X-form-action"), 'Method':self.headers.get("X-form-method"), 'Enctype':self.headers.get("X-form-enctype"), 'Encoding':self.headers.get("X-form-encoding")} |
| 120 | + | post_data = self.rfile.read(content_len) |
| 121 | + | |
| 122 | + | except: |
| 123 | + | pass |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | def do_OPTIONS(self): |
| 128 | + | |
| 129 | + | self.server_version = "Apache/2.4.1" |
| 130 | + | self.sys_version = "" |
| 131 | + | self.send_response(200) |
| 132 | + | self.send_header('Access-Control-Allow-Origin', self.headers["Origin"]) |
| 133 | + | self.send_header('Vary', "Origin") |
| 134 | + | self.send_header('Access-Control-Allow-Credentials', 'true') |
| 135 | + | #self.send_header('Access-Control-Allow-Headers', 'X-form-responseHeaders') |
| 136 | + | self.end_headers() |
| 137 | + | self.wfile.write(b'OK') |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | def log_message(self, format, *args): |
| 142 | + | return |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | def main(): |
| 147 | + | |
| 148 | + | try: |
| 149 | + | server_port = args.port if args.port else 443 |
| 150 | + | |
| 151 | + | try: |
| 152 | + | httpd = HTTPServer(('0.0.0.0', server_port), Https_Server) |
| 153 | + | |
| 154 | + | except OSError: |
| 155 | + | exit(f'\n[{FAILED}] Port {server_port} seems to already be in use.{END}\n') |
| 156 | + | |
| 157 | + | protocol = 'http' |
| 158 | + | |
| 159 | + | if args.cert and args.key: |
| 160 | + | |
| 161 | + | try: |
| 162 | + | httpd.socket = ssl.wrap_socket ( |
| 163 | + | httpd.socket, |
| 164 | + | keyfile = args.key, |
| 165 | + | certfile = args.cert, |
| 166 | + | server_side = True, |
| 167 | + | ssl_version=ssl.PROTOCOL_TLS |
| 168 | + | ) |
| 169 | + | |
| 170 | + | protocol = 'https' |
| 171 | + | |
| 172 | + | except Exception as e: |
| 173 | + | debug(f'Failed to establish SSL: {e}') |
| 174 | + | exit(1) |
| 175 | + | |
| 176 | + | |
| 177 | + | server = Thread(target = httpd.serve_forever, args = ()) |
| 178 | + | server.daemon = True |
| 179 | + | server.start() |
| 180 | + | print(f'[{INFO}] {protocol} server is up and running!') |
| 181 | + | |
| 182 | + | try: |
| 183 | + | server_public_ip = check_output("curl --connect-timeout 3.14 -s ifconfig.me", shell = True).decode(sys.stdout.encoding) |
| 184 | + | |
| 185 | + | except: |
| 186 | + | server_public_ip = '127.0.0.1' |
| 187 | + | pass |
| 188 | + | |
| 189 | + | for key,val in Https_Server.valid_endpoints.items(): |
| 190 | + | print(f'{INFO} {key} endpoint: {protocol}://{server_public_ip}:{server_port}/{val}') |
| 191 | + | |
| 192 | + | while True: |
| 193 | + | sleep(5) |
| 194 | + | |
| 195 | + | except KeyboardInterupt: |
| 196 | + | exit(0) |
| 197 | + | |
| 198 | + | except Exception as e: |
| 199 | + | debug(f'Something went wrong: {e}') |
| 200 | + | exit(1) |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | if __name__ == '__main__': |
| 205 | + | main() |
| 206 | + | |