Projects STRLCPY Synergy-httpx Commits bdcef304
🤬
Revision indexing in progress... (symbol navigation in revisions will be accurate after indexed)
  • ■ ■ ■ ■ ■ ■
    synergy_httpx.py
    skipped 4 lines
    5 5   
    6 6  from http.server import HTTPServer, BaseHTTPRequestHandler
    7 7  import ssl, sys, base64, re, os, argparse
     8 +import netifaces as ni
    8 9  from warnings import filterwarnings
    9 10  from datetime import date, datetime
    10 11  from urllib.parse import unquote, urlparse
    skipped 39 lines
    50 51  GET_REQ = f'[{BLUE}GET-request{END}]'
    51 52  META = '[\001\033[38;5;93m\002M\001\033[38;5;129m\002e\001\033[38;5;165m\002t\001\033[38;5;201m\002a\001\033[0m\002]'
    52 53   
     54 +# Check interface
     55 +def get_ip_from_iface(iface):
     56 + try:
     57 + # Check if valid interface
     58 + ip = ni.ifaddresses(iface)[ni.AF_INET][0]['addr']
     59 + return ip
    53 60  
     61 + except:
     62 + return False
     63 + 
     64 + 
     65 +def debug(msg):
     66 + print(f'{DBG} {msg}{END}')
     67 + 
     68 + 
     69 +# Parse arguments
    54 70  parser = argparse.ArgumentParser()
    55 71  parser.add_argument("-c", "--cert", action="store", help = "Your certificate.")
    56 72  parser.add_argument("-k", "--key", action="store", help = "The private key for your certificate. ")
    57 73  parser.add_argument("-p", "--port", action="store", help = "Server port.", type = int)
    58 74  parser.add_argument("-q", "--quiet", action="store_true", help = "Do not print the banner on startup.")
     75 +parser.add_argument("-i", "--interface", action="store", help = "Supply an interface to adjust the server URLs automatically to the corresponding IP address. If omitted, your public IP will be used instead.", type = str)
    59 76   
    60 77  args = parser.parse_args()
    61 78   
    skipped 67 lines
    129 146   
    130 147  def print_green(msg):
    131 148   print(f'{GREEN}{msg}{END}')
    132  - 
    133  - 
    134  -def debug(msg):
    135  - print(f'{DBG} {msg}{END}')
    136 149   
    137 150   
    138 151  def failure(msg):
    skipped 79 lines
    218 231   def get_endpoints():
    219 232   endpoints = list(Synergy_Httpx.user_defined_endpoints['GET'].keys())\
    220 233   + list(Synergy_Httpx.user_defined_endpoints['POST'].keys())
    221  - return endpoints.extend([Synergy_Httpx.basic_endpoints['GET'], Synergy_Httpx.basic_endpoints['POST']])
     234 + endpoints.extend([Synergy_Httpx.basic_endpoints['GET'], Synergy_Httpx.basic_endpoints['POST']])
     235 + return endpoints
    222 236   
    223 237   
    224 238   @staticmethod
    skipped 411 lines
    636 650   
    637 651   print_banner() if not args.quiet else chill()
    638 652   
     653 + if args.interface:
     654 + server_public_ip = get_ip_from_iface(args.interface)
     655 + 
     656 + if not server_public_ip:
     657 + debug('Invalid interface.')
     658 + exit(1)
     659 + # try:
     660 + server_port = args.port if args.port else 8080
     661 + 
    639 662   try:
    640  - server_port = args.port if args.port else 8080
     663 + httpd = HTTPServer(('0.0.0.0', server_port), Synergy_Httpx)
    641 664   
    642  - try:
    643  - httpd = HTTPServer(('0.0.0.0', server_port), Synergy_Httpx)
     665 + except OSError:
     666 + exit(f'\n{FAILED} Port {server_port} seems to already be in use.{END}\n')
    644 667  
    645  - except OSError:
    646  - exit(f'\n{FAILED} Port {server_port} seems to already be in use.{END}\n')
     668 + protocol = 'http'
    647 669  
    648  - protocol = 'http'
     670 + if args.cert and args.key:
    649 671  
    650  - if args.cert and args.key:
     672 + try:
     673 + httpd.socket = ssl.wrap_socket (
     674 + httpd.socket,
     675 + keyfile = args.key,
     676 + certfile = args.cert,
     677 + server_side = True,
     678 + ssl_version=ssl.PROTOCOL_TLS
     679 + )
    651 680  
    652  - try:
    653  - httpd.socket = ssl.wrap_socket (
    654  - httpd.socket,
    655  - keyfile = args.key,
    656  - certfile = args.cert,
    657  - server_side = True,
    658  - ssl_version=ssl.PROTOCOL_TLS
    659  - )
     681 + protocol = 'https'
    660 682  
    661  - protocol = 'https'
     683 + except Exception as e:
     684 + debug(f'Failed to establish SSL: {e}')
     685 + exit(1)
    662 686  
    663  - except Exception as e:
    664  - debug(f'Failed to establish SSL: {e}')
    665  - exit(1)
    666 687  
    667  -
    668  - server = Thread(target = httpd.serve_forever, args = ())
    669  - server.daemon = True
    670  - server.start()
    671  - print(f'[{ORANGE}0.0.0.0{END}:{ORANGE}{server_port}{END}] Synergy {protocol} server is up and running!')
     688 + server = Thread(target = httpd.serve_forever, args = ())
     689 + server.daemon = True
     690 + server.start()
     691 + print(f'[{ORANGE}0.0.0.0{END}:{ORANGE}{server_port}{END}] Synergy {protocol} server is up and running!')
    672 692   
     693 + if not args.interface:
    673 694   try:
    674 695   server_public_ip = check_output("curl --connect-timeout 3.14 -s ifconfig.me", shell = True).decode(sys.stdout.encoding)
    675 696  
    skipped 1 lines
    677 698   server_public_ip = '127.0.0.1'
    678 699   pass
    679 700  
    680  - for key,val in Synergy_Httpx.basic_endpoints.items():
    681  - print(f'{INFO} Basic {key} endpoint: {protocol}://{server_public_ip}:{server_port}/{val}')
     701 + for key,val in Synergy_Httpx.basic_endpoints.items():
     702 + print(f'{INFO} Basic {key} endpoint: {protocol}://{server_public_ip}:{server_port}/{val}')
    682 703   
    683 704   
    684  - except KeyboardInterrupt:
    685  - exit(0)
     705 + # except KeyboardInterrupt:
     706 + # exit(0)
    686 707  
    687  - except Exception as e:
    688  - debug(f'Something went wrong: {e}')
    689  - exit(1)
     708 + # except Exception as e:
     709 + # debug(f'Something went wrong: {e}')
     710 + # exit(1)
    690 711   
    691 712  
    692 713   ''' Start tab autoComplete '''
    skipped 90 lines
    783 804   elif cmd == 'endpoints':
    784 805   print(f'\n{BOLD}Basic endpoints{END}:')
    785 806   for method in Synergy_Httpx.basic_endpoints.keys():
    786  - print(f'/{Synergy_Httpx.basic_endpoints[method]} ({method})')
     807 + print(f'{protocol}://{server_public_ip}/{Synergy_Httpx.basic_endpoints[method]} ({method})')
    787 808   
    788 809   print(f'\n{BOLD}User Defined{END}:')
    789 810   for method in Synergy_Httpx.user_defined_endpoints.keys():
    790 811   for key,value in Synergy_Httpx.user_defined_endpoints[method].items():
    791  - print(f'/{key} : {value} ({method})')
     812 + print(f'{protocol}://{server_public_ip}/{key} -> {value} ({method})')
    792 813   print('')
    793 814   
    794 815   
    skipped 14 lines
    809 830   verified = True if choice in ['yes', 'y'] else False
    810 831  
    811 832   if verified:
     833 + print('')
    812 834   print_meta()
    813 835   sys.exit(0)
     836 + 
    814 837   
    815 838   
    816 839  if __name__ == '__main__':
    skipped 2 lines
Please wait...
Page is in error, reload to recover