mirror of
https://github.com/ShaYmez/HBmonitor.git
synced 2024-11-25 09:18:53 -05:00
Update monitor.py
add simple web auth
This commit is contained in:
parent
b97de4a829
commit
db88b998a2
19
monitor.py
19
monitor.py
@ -36,6 +36,7 @@ from twisted.protocols.basic import NetstringReceiver
|
|||||||
from twisted.internet import reactor, task
|
from twisted.internet import reactor, task
|
||||||
from twisted.web.server import Site
|
from twisted.web.server import Site
|
||||||
from twisted.web.resource import Resource
|
from twisted.web.resource import Resource
|
||||||
|
import base64
|
||||||
|
|
||||||
# Autobahn provides websocket service under Twisted
|
# Autobahn provides websocket service under Twisted
|
||||||
from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory
|
from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory
|
||||||
@ -704,6 +705,24 @@ class web_server(Resource):
|
|||||||
isLeaf = True
|
isLeaf = True
|
||||||
def render_GET(self, request):
|
def render_GET(self, request):
|
||||||
logging.info('static website requested: %s', request)
|
logging.info('static website requested: %s', request)
|
||||||
|
if WEB_AUTH:
|
||||||
|
user = WEB_USER.encode('utf-8')
|
||||||
|
password = WEB_PASS.encode('utf-8')
|
||||||
|
auth = request.getHeader('Authorization')
|
||||||
|
if auth and auth.split(' ')[0] == 'Basic':
|
||||||
|
decodeddata = base64.b64decode(auth.split(' ')[1])
|
||||||
|
if decodeddata.split(b':') == [user, password]:
|
||||||
|
logging.info('Authorization OK')
|
||||||
|
return (index_html).encode('utf-8')
|
||||||
|
request.setResponseCode(401)
|
||||||
|
request.setHeader('WWW-Authenticate', 'Basic realm="realmname"')
|
||||||
|
logging.info('Someone wanted to get access without authorization')
|
||||||
|
return "<html<head></hread><body style=\"background-color: #EEEEEE;\"><br><br><br><center> \
|
||||||
|
<fieldset style=\"width:600px;background-color:#e0e0e0e0;text-algin: center; margin-left:15px;margin-right:15px; \
|
||||||
|
font-size:14px;border-top-left-radius: 10px; border-top-right-radius: 10px; \
|
||||||
|
border-bottom-left-radius: 10px; border-bottom-right-radius: 10px;\"> \
|
||||||
|
<p><font size=5><b>Authorization Required</font></p></filed></center></body></html>".encode('utf-8')
|
||||||
|
else:
|
||||||
return (index_html).encode('utf-8')
|
return (index_html).encode('utf-8')
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
Reference in New Issue
Block a user