11import sys , os
22import argparse
33import colorama
4- from bottle import route , request , response , run , hook , abort , redirect , error , install , auth_basic
4+ from bottle import route , view , request , response , run , hook , abort , redirect , error , install , auth_basic , template
55import simplejson as json
66import random
77import logging
@@ -56,6 +56,39 @@ def main():
5656auth_type = 0
5757jwt_secret = "%032x" % random .getrandbits (128 )
5858
59+ #
60+ # TEMPLATES
61+ #
62+ default_header_tpl = """<html>
63+ <head><title>{{headline}} | Auth Basic GitHub Pages Proxy by comSysto</title></head>
64+ <body>
65+ <div style="font-family:sans-serif;margin:auto;padding:50px 100px 50px 100px;">
66+ <div style="width:100%;background:#1e9dcc">
67+ <img src="https://comsysto.github.io/github-pages-basic-auth-proxy/public/logo-small.png">
68+ </div>
69+ <h1>{{headline}}</h1>"""
70+
71+ default_footer_tpl = """</div>
72+ </body></html>"""
73+
74+ default_tpl = default_header_tpl + '{{body}}' + default_footer_tpl
75+
76+ healthcheck_tpl = default_header_tpl + """
77+ <div style="background:#99d100;padding:20px;color:#fff">✓ Proxy is running fine.</div>""" + default_footer_tpl
78+
79+ error_tpl = default_header_tpl + """
80+ <div style="background:#bf1101;padding:20px;color:#fff">✘ {{error.body}}</div>""" + default_footer_tpl
81+
82+ install_success_tpl = default_header_tpl + """
83+ <div style="background:#99d100;padding:20px;color:#fff">✓ Installation done.</div>
84+ <br><br>
85+ %if remote_page_call_status_code != 200:
86+ <div style="background:#bf1101;padding:20px;color:#fff">✘ Error calling the gh-pages page (Status {{remote_page_call_status_code}}). Please check the env vars (obfuscator, repositoryOwner and repositoryName) and place a index.html inside the obfuscator dir.</div>
87+ %else:
88+ <div style="background:#99d100;padding:20px;color:#fff">✓ Success calling the gh-pages page.</div>
89+ %end
90+ """ + default_footer_tpl
91+
5992#
6093# HELPERS
6194#
@@ -116,9 +149,12 @@ def normalize_proxy_url(url):
116149def proxy_trough_helper (url ):
117150 print ('PROXY-GET: {0}' .format (url ))
118151 proxy_response = requests .get (url )
119- response .set_header ('Last-Modified' , proxy_response .headers ['Last-Modified' ])
120- response .set_header ('Content-Type' , proxy_response .headers ['Content-Type' ])
121- response .set_header ('Expires' , proxy_response .headers ['Expires' ])
152+ if hasattr (proxy_response .headers , 'Last-Modified' ):
153+ response .set_header ('Last-Modified' , proxy_response .headers ['Last-Modified' ])
154+ if hasattr (proxy_response .headers , 'Content-Type' ):
155+ response .set_header ('Content-Type' , proxy_response .headers ['Content-Type' ])
156+ if hasattr (proxy_response .headers , 'Expires' ):
157+ response .set_header ('Expires' , proxy_response .headers ['Expires' ])
122158 return proxy_response
123159
124160
@@ -132,22 +168,23 @@ def run_proxy(args):
132168 #
133169 @error (401 )
134170 def error404 (error ):
135- return json . dumps ({ 'error' : error .body } )
171+ return template ( error_tpl , headline = 'Error ' + error .status , error = error )
136172
137173 @error (500 )
138174 def error500 (error ):
139- return json . dumps ({ 'error' : error .body } )
175+ return template ( error_tpl , headline = 'Error ' + error .status , error = error )
140176
141177 #
142178 # SPECIAL ENDPOINTS
143179 #
144180 @route ('/health' )
145181 def hello ():
146- return 'ok'
182+ return template ( healthcheck_tpl , headline = 'Healthcheck' )
147183
148184 @route ('/install-success' )
149185 def hello ():
150- return 'The Auth Basic GitHub Pages Proxy was installed successfully.'
186+ remote_page_call_status_code = proxy_trough_helper ('https://{0}.github.io/{1}/{2}/{3}' .format (args .owner , args .repository , args .obfuscator , '/' )).status_code
187+ return template (install_success_tpl , headline = 'Installation Success' , remote_page_call_status_code = remote_page_call_status_code )
151188
152189 #
153190 # make args available in auth callback
0 commit comments