2018-06-01 23:01:31 +02:00
|
|
|
# pylint: disable=wrong-import-position
|
2018-05-21 16:40:22 +02:00
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import codecs
|
2018-12-05 21:22:06 +01:00
|
|
|
import collections
|
2018-06-07 20:47:06 +02:00
|
|
|
import hmac
|
2018-05-21 16:40:22 +02:00
|
|
|
import json
|
|
|
|
import logging
|
2018-12-05 21:22:06 +01:00
|
|
|
import multiprocessing
|
2018-05-21 16:40:22 +02:00
|
|
|
import os
|
|
|
|
import subprocess
|
2018-12-05 21:22:06 +01:00
|
|
|
import threading
|
|
|
|
|
|
|
|
import tornado
|
|
|
|
import tornado.concurrent
|
2019-01-02 12:21:26 +01:00
|
|
|
import tornado.httpserver
|
|
|
|
import tornado.netutil
|
2018-12-05 21:22:06 +01:00
|
|
|
import tornado.gen
|
|
|
|
import tornado.ioloop
|
|
|
|
import tornado.iostream
|
|
|
|
from tornado.log import access_log
|
|
|
|
import tornado.process
|
|
|
|
import tornado.web
|
|
|
|
import tornado.websocket
|
|
|
|
|
|
|
|
from esphomeyaml import const
|
2018-09-29 14:50:04 +02:00
|
|
|
from esphomeyaml.__main__ import get_serial_ports
|
2018-12-05 21:22:06 +01:00
|
|
|
from esphomeyaml.helpers import mkdir_p, run_system_command
|
|
|
|
from esphomeyaml.storage_json import EsphomeyamlStorageJSON, StorageJSON, \
|
|
|
|
esphomeyaml_storage_path, ext_storage_path
|
2018-11-03 14:06:14 +01:00
|
|
|
from esphomeyaml.util import shlex_quote
|
2018-06-01 22:49:04 +02:00
|
|
|
|
2018-12-05 21:22:06 +01:00
|
|
|
# pylint: disable=unused-import, wrong-import-order
|
|
|
|
from typing import Optional # noqa
|
2018-05-21 16:40:22 +02:00
|
|
|
|
2018-06-02 22:22:20 +02:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2018-05-21 16:40:22 +02:00
|
|
|
CONFIG_DIR = ''
|
2018-12-05 21:22:06 +01:00
|
|
|
PASSWORD_DIGEST = ''
|
|
|
|
COOKIE_SECRET = None
|
|
|
|
USING_PASSWORD = False
|
|
|
|
ON_HASSIO = False
|
|
|
|
USING_HASSIO_AUTH = True
|
|
|
|
HASSIO_MQTT_CONFIG = None
|
2018-06-07 20:47:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=abstract-method
|
|
|
|
class BaseHandler(tornado.web.RequestHandler):
|
|
|
|
def is_authenticated(self):
|
2018-12-05 21:22:06 +01:00
|
|
|
if USING_HASSIO_AUTH or USING_PASSWORD:
|
|
|
|
return self.get_secure_cookie('authenticated') == 'yes'
|
|
|
|
|
|
|
|
return True
|
2018-05-21 16:40:22 +02:00
|
|
|
|
2018-06-01 22:49:04 +02:00
|
|
|
|
2018-05-21 16:40:22 +02:00
|
|
|
# pylint: disable=abstract-method, arguments-differ
|
|
|
|
class EsphomeyamlCommandWebSocket(tornado.websocket.WebSocketHandler):
|
|
|
|
def __init__(self, application, request, **kwargs):
|
|
|
|
super(EsphomeyamlCommandWebSocket, self).__init__(application, request, **kwargs)
|
|
|
|
self.proc = None
|
|
|
|
self.closed = False
|
|
|
|
|
|
|
|
def on_message(self, message):
|
2018-12-05 21:22:06 +01:00
|
|
|
if USING_HASSIO_AUTH or USING_PASSWORD:
|
|
|
|
if self.get_secure_cookie('authenticated') != 'yes':
|
|
|
|
return
|
2018-05-21 16:40:22 +02:00
|
|
|
if self.proc is not None:
|
|
|
|
return
|
|
|
|
command = self.build_command(message)
|
2018-12-05 21:22:06 +01:00
|
|
|
_LOGGER.info(u"Running command '%s'", ' '.join(shlex_quote(x) for x in command))
|
2018-05-21 16:40:22 +02:00
|
|
|
self.proc = tornado.process.Subprocess(command,
|
|
|
|
stdout=tornado.process.Subprocess.STREAM,
|
|
|
|
stderr=subprocess.STDOUT)
|
|
|
|
self.proc.set_exit_callback(self.proc_on_exit)
|
|
|
|
tornado.ioloop.IOLoop.current().spawn_callback(self.redirect_stream)
|
|
|
|
|
|
|
|
@tornado.gen.coroutine
|
|
|
|
def redirect_stream(self):
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
data = yield self.proc.stdout.read_until_regex('[\n\r]')
|
|
|
|
except tornado.iostream.StreamClosedError:
|
|
|
|
break
|
2018-08-13 19:11:33 +02:00
|
|
|
try:
|
2018-12-05 21:22:06 +01:00
|
|
|
self.write_message({'event': 'line', 'data': data})
|
2018-08-13 19:11:33 +02:00
|
|
|
except UnicodeDecodeError:
|
2018-12-05 21:22:06 +01:00
|
|
|
data = codecs.decode(data, 'utf8', 'replace')
|
|
|
|
self.write_message({'event': 'line', 'data': data})
|
2018-05-21 16:40:22 +02:00
|
|
|
|
|
|
|
def proc_on_exit(self, returncode):
|
|
|
|
if not self.closed:
|
|
|
|
_LOGGER.debug("Process exited with return code %s", returncode)
|
|
|
|
self.write_message({'event': 'exit', 'code': returncode})
|
|
|
|
|
|
|
|
def on_close(self):
|
|
|
|
self.closed = True
|
|
|
|
if self.proc is not None and self.proc.returncode is None:
|
|
|
|
_LOGGER.debug("Terminating process")
|
|
|
|
self.proc.proc.terminate()
|
|
|
|
|
|
|
|
def build_command(self, message):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
class EsphomeyamlLogsHandler(EsphomeyamlCommandWebSocket):
|
|
|
|
def build_command(self, message):
|
|
|
|
js = json.loads(message)
|
|
|
|
config_file = CONFIG_DIR + '/' + js['configuration']
|
2018-12-18 19:31:43 +01:00
|
|
|
return ["esphomeyaml", "--dashboard", config_file, "logs", '--serial-port', js["port"]]
|
2018-05-21 16:40:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
class EsphomeyamlRunHandler(EsphomeyamlCommandWebSocket):
|
|
|
|
def build_command(self, message):
|
|
|
|
js = json.loads(message)
|
|
|
|
config_file = os.path.join(CONFIG_DIR, js['configuration'])
|
2018-12-18 19:31:43 +01:00
|
|
|
return ["esphomeyaml", "--dashboard", config_file, "run", '--upload-port', js["port"]]
|
2018-05-21 16:40:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
class EsphomeyamlCompileHandler(EsphomeyamlCommandWebSocket):
|
|
|
|
def build_command(self, message):
|
|
|
|
js = json.loads(message)
|
|
|
|
config_file = os.path.join(CONFIG_DIR, js['configuration'])
|
2018-12-18 19:31:43 +01:00
|
|
|
return ["esphomeyaml", "--dashboard", config_file, "compile"]
|
2018-05-21 16:40:22 +02:00
|
|
|
|
|
|
|
|
2018-06-03 12:16:43 +02:00
|
|
|
class EsphomeyamlValidateHandler(EsphomeyamlCommandWebSocket):
|
|
|
|
def build_command(self, message):
|
|
|
|
js = json.loads(message)
|
|
|
|
config_file = os.path.join(CONFIG_DIR, js['configuration'])
|
2018-12-18 19:31:43 +01:00
|
|
|
return ["esphomeyaml", "--dashboard", config_file, "config"]
|
2018-06-03 12:16:43 +02:00
|
|
|
|
|
|
|
|
2018-10-04 19:01:02 +02:00
|
|
|
class EsphomeyamlCleanMqttHandler(EsphomeyamlCommandWebSocket):
|
|
|
|
def build_command(self, message):
|
|
|
|
js = json.loads(message)
|
|
|
|
config_file = os.path.join(CONFIG_DIR, js['configuration'])
|
2018-12-18 19:31:43 +01:00
|
|
|
return ["esphomeyaml", "--dashboard", config_file, "clean-mqtt"]
|
2018-10-04 19:01:02 +02:00
|
|
|
|
|
|
|
|
2018-10-14 18:52:21 +02:00
|
|
|
class EsphomeyamlCleanHandler(EsphomeyamlCommandWebSocket):
|
|
|
|
def build_command(self, message):
|
|
|
|
js = json.loads(message)
|
|
|
|
config_file = os.path.join(CONFIG_DIR, js['configuration'])
|
2018-12-18 19:31:43 +01:00
|
|
|
return ["esphomeyaml", "--dashboard", config_file, "clean"]
|
2018-10-14 18:52:21 +02:00
|
|
|
|
|
|
|
|
2018-11-03 14:08:31 +01:00
|
|
|
class EsphomeyamlHassConfigHandler(EsphomeyamlCommandWebSocket):
|
|
|
|
def build_command(self, message):
|
|
|
|
js = json.loads(message)
|
|
|
|
config_file = os.path.join(CONFIG_DIR, js['configuration'])
|
2018-12-18 19:31:43 +01:00
|
|
|
return ["esphomeyaml", "--dashboard", config_file, "hass-config"]
|
2018-11-03 14:08:31 +01:00
|
|
|
|
|
|
|
|
2018-06-07 20:47:06 +02:00
|
|
|
class SerialPortRequestHandler(BaseHandler):
|
2018-05-21 16:40:22 +02:00
|
|
|
def get(self):
|
2018-06-07 20:47:06 +02:00
|
|
|
if not self.is_authenticated():
|
|
|
|
self.redirect('/login')
|
|
|
|
return
|
2018-05-21 16:40:22 +02:00
|
|
|
ports = get_serial_ports()
|
|
|
|
data = []
|
|
|
|
for port, desc in ports:
|
|
|
|
if port == '/dev/ttyAMA0':
|
2018-06-03 11:18:53 +02:00
|
|
|
desc = 'UART pins on GPIO header'
|
|
|
|
split_desc = desc.split(' - ')
|
|
|
|
if len(split_desc) == 2 and split_desc[0] == split_desc[1]:
|
|
|
|
# Some serial ports repeat their values
|
|
|
|
desc = split_desc[0]
|
2018-05-21 16:40:22 +02:00
|
|
|
data.append({'port': port, 'desc': desc})
|
2018-06-03 11:18:53 +02:00
|
|
|
data.append({'port': 'OTA', 'desc': 'Over-The-Air'})
|
|
|
|
self.write(json.dumps(sorted(data, reverse=True)))
|
2018-05-21 16:40:22 +02:00
|
|
|
|
|
|
|
|
2018-06-07 20:47:06 +02:00
|
|
|
class WizardRequestHandler(BaseHandler):
|
2018-05-21 16:40:22 +02:00
|
|
|
def post(self):
|
|
|
|
from esphomeyaml import wizard
|
|
|
|
|
2018-06-07 20:47:06 +02:00
|
|
|
if not self.is_authenticated():
|
|
|
|
self.redirect('/login')
|
|
|
|
return
|
2018-05-21 16:40:22 +02:00
|
|
|
kwargs = {k: ''.join(v) for k, v in self.request.arguments.iteritems()}
|
|
|
|
destination = os.path.join(CONFIG_DIR, kwargs['name'] + '.yaml')
|
2018-12-05 21:22:06 +01:00
|
|
|
wizard.wizard_write(path=destination, **kwargs)
|
2018-06-03 11:18:53 +02:00
|
|
|
self.redirect('/?begin=True')
|
2018-05-21 16:40:22 +02:00
|
|
|
|
|
|
|
|
2018-06-07 20:47:06 +02:00
|
|
|
class DownloadBinaryRequestHandler(BaseHandler):
|
2018-05-21 16:40:22 +02:00
|
|
|
def get(self):
|
2018-06-07 20:47:06 +02:00
|
|
|
if not self.is_authenticated():
|
|
|
|
self.redirect('/login')
|
|
|
|
return
|
|
|
|
|
2018-05-21 16:40:22 +02:00
|
|
|
configuration = self.get_argument('configuration')
|
2018-12-05 21:22:06 +01:00
|
|
|
storage_path = ext_storage_path(CONFIG_DIR, configuration)
|
|
|
|
storage_json = StorageJSON.load(storage_path)
|
|
|
|
if storage_json is None:
|
|
|
|
self.send_error()
|
|
|
|
return
|
|
|
|
|
|
|
|
path = storage_json.firmware_bin_path
|
2018-05-21 16:40:22 +02:00
|
|
|
self.set_header('Content-Type', 'application/octet-stream')
|
2018-12-05 21:22:06 +01:00
|
|
|
filename = '{}.bin'.format(storage_json.name)
|
|
|
|
self.set_header("Content-Disposition", 'attachment; filename="{}"'.format(filename))
|
2018-05-21 16:40:22 +02:00
|
|
|
with open(path, 'rb') as f:
|
|
|
|
while 1:
|
|
|
|
data = f.read(16384) # or some other nice-sized chunk
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
self.write(data)
|
|
|
|
self.finish()
|
|
|
|
|
|
|
|
|
2018-12-05 21:22:06 +01:00
|
|
|
def _list_yaml_files():
|
|
|
|
files = []
|
|
|
|
for file in os.listdir(CONFIG_DIR):
|
|
|
|
if not file.endswith('.yaml'):
|
|
|
|
continue
|
|
|
|
if file.startswith('.'):
|
|
|
|
continue
|
|
|
|
if file == 'secrets.yaml':
|
|
|
|
continue
|
|
|
|
files.append(file)
|
|
|
|
files.sort()
|
|
|
|
return files
|
|
|
|
|
|
|
|
|
|
|
|
def _list_dashboard_entries():
|
|
|
|
files = _list_yaml_files()
|
|
|
|
return [DashboardEntry(file) for file in files]
|
|
|
|
|
|
|
|
|
|
|
|
class DashboardEntry(object):
|
|
|
|
def __init__(self, filename):
|
|
|
|
self.filename = filename
|
|
|
|
self._storage = None
|
|
|
|
self._loaded_storage = False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def full_path(self): # type: () -> str
|
|
|
|
return os.path.join(CONFIG_DIR, self.filename)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def storage(self): # type: () -> Optional[StorageJSON]
|
|
|
|
if not self._loaded_storage:
|
|
|
|
self._storage = StorageJSON.load(ext_storage_path(CONFIG_DIR, self.filename))
|
|
|
|
self._loaded_storage = True
|
|
|
|
return self._storage
|
|
|
|
|
|
|
|
@property
|
|
|
|
def address(self):
|
|
|
|
if self.storage is None:
|
|
|
|
return None
|
|
|
|
return self.storage.address
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
if self.storage is None:
|
|
|
|
return self.filename[:-len('.yaml')]
|
|
|
|
return self.storage.name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def esp_platform(self):
|
|
|
|
if self.storage is None:
|
|
|
|
return None
|
|
|
|
return self.storage.esp_platform
|
|
|
|
|
|
|
|
@property
|
|
|
|
def board(self):
|
|
|
|
if self.storage is None:
|
|
|
|
return None
|
|
|
|
return self.storage.board
|
|
|
|
|
|
|
|
@property
|
|
|
|
def update_available(self):
|
|
|
|
if self.storage is None:
|
|
|
|
return True
|
|
|
|
return self.update_old != self.update_new
|
|
|
|
|
|
|
|
@property
|
|
|
|
def update_old(self):
|
|
|
|
if self.storage is None:
|
|
|
|
return ''
|
|
|
|
return self.storage.esphomeyaml_version or ''
|
|
|
|
|
|
|
|
@property
|
|
|
|
def update_new(self):
|
|
|
|
return const.__version__
|
|
|
|
|
|
|
|
|
2018-06-07 20:47:06 +02:00
|
|
|
class MainRequestHandler(BaseHandler):
|
2018-05-21 16:40:22 +02:00
|
|
|
def get(self):
|
2018-06-07 20:47:06 +02:00
|
|
|
if not self.is_authenticated():
|
|
|
|
self.redirect('/login')
|
|
|
|
return
|
|
|
|
|
2018-06-03 11:18:53 +02:00
|
|
|
begin = bool(self.get_argument('begin', False))
|
2018-12-05 21:22:06 +01:00
|
|
|
entries = _list_dashboard_entries()
|
|
|
|
version = const.__version__
|
|
|
|
docs_link = 'https://beta.esphomelib.com/esphomeyaml/' if 'b' in version else \
|
|
|
|
'https://esphomelib.com/esphomeyaml/'
|
|
|
|
|
|
|
|
self.render("templates/index.html", entries=entries,
|
2018-12-24 14:29:11 +01:00
|
|
|
version=version, begin=begin, docs_link=docs_link,
|
|
|
|
get_static_file_url=get_static_file_url)
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _ping_func(filename, address):
|
|
|
|
if os.name == 'nt':
|
|
|
|
command = ['ping', '-n', '1', address]
|
|
|
|
else:
|
|
|
|
command = ['ping', '-c', '1', address]
|
|
|
|
rc, _, _ = run_system_command(*command)
|
|
|
|
return filename, rc == 0
|
|
|
|
|
|
|
|
|
|
|
|
class PingThread(threading.Thread):
|
|
|
|
def run(self):
|
|
|
|
pool = multiprocessing.Pool(processes=8)
|
|
|
|
|
|
|
|
while not STOP_EVENT.is_set():
|
|
|
|
# Only do pings if somebody has the dashboard open
|
|
|
|
PING_REQUEST.wait()
|
|
|
|
PING_REQUEST.clear()
|
|
|
|
|
|
|
|
def callback(ret):
|
|
|
|
PING_RESULT[ret[0]] = ret[1]
|
|
|
|
|
|
|
|
entries = _list_dashboard_entries()
|
|
|
|
queue = collections.deque()
|
|
|
|
for entry in entries:
|
|
|
|
if entry.address is None:
|
|
|
|
PING_RESULT[entry.filename] = None
|
|
|
|
continue
|
|
|
|
|
|
|
|
result = pool.apply_async(_ping_func, (entry.filename, entry.address),
|
|
|
|
callback=callback)
|
|
|
|
queue.append(result)
|
|
|
|
|
|
|
|
while queue:
|
|
|
|
item = queue[0]
|
|
|
|
if item.ready():
|
|
|
|
queue.popleft()
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
item.get(0.1)
|
|
|
|
except OSError:
|
|
|
|
# ping not installed
|
|
|
|
pass
|
|
|
|
except multiprocessing.TimeoutError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if STOP_EVENT.is_set():
|
|
|
|
pool.terminate()
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
class PingRequestHandler(BaseHandler):
|
|
|
|
def get(self):
|
|
|
|
if not self.is_authenticated():
|
|
|
|
self.redirect('/login')
|
|
|
|
return
|
|
|
|
|
|
|
|
PING_REQUEST.set()
|
|
|
|
self.write(json.dumps(PING_RESULT))
|
|
|
|
|
|
|
|
|
|
|
|
def is_allowed(configuration):
|
|
|
|
return os.path.sep not in configuration
|
|
|
|
|
|
|
|
|
|
|
|
class EditRequestHandler(BaseHandler):
|
|
|
|
def get(self):
|
|
|
|
if not self.is_authenticated():
|
|
|
|
self.redirect('/login')
|
|
|
|
return
|
|
|
|
configuration = self.get_argument('configuration')
|
|
|
|
if not is_allowed(configuration):
|
|
|
|
self.set_status(401)
|
|
|
|
return
|
|
|
|
|
|
|
|
with open(os.path.join(CONFIG_DIR, configuration), 'r') as f:
|
|
|
|
content = f.read()
|
|
|
|
self.write(content)
|
|
|
|
|
|
|
|
def post(self):
|
|
|
|
if not self.is_authenticated():
|
|
|
|
self.redirect('/login')
|
|
|
|
return
|
|
|
|
configuration = self.get_argument('configuration')
|
|
|
|
if not is_allowed(configuration):
|
|
|
|
self.set_status(401)
|
|
|
|
return
|
|
|
|
|
|
|
|
with open(os.path.join(CONFIG_DIR, configuration), 'w') as f:
|
|
|
|
f.write(self.request.body)
|
|
|
|
self.set_status(200)
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
PING_RESULT = {} # type: dict
|
|
|
|
STOP_EVENT = threading.Event()
|
|
|
|
PING_REQUEST = threading.Event()
|
2018-05-21 16:40:22 +02:00
|
|
|
|
|
|
|
|
2018-06-07 20:47:06 +02:00
|
|
|
class LoginHandler(BaseHandler):
|
|
|
|
def get(self):
|
2018-12-05 21:22:06 +01:00
|
|
|
if USING_HASSIO_AUTH:
|
|
|
|
self.render_hassio_login()
|
|
|
|
return
|
2018-06-07 20:47:06 +02:00
|
|
|
self.write('<html><body><form action="/login" method="post">'
|
|
|
|
'Password: <input type="password" name="password">'
|
|
|
|
'<input type="submit" value="Sign in">'
|
|
|
|
'</form></body></html>')
|
|
|
|
|
2018-12-05 21:22:06 +01:00
|
|
|
def render_hassio_login(self, error=None):
|
|
|
|
version = const.__version__
|
|
|
|
docs_link = 'https://beta.esphomelib.com/esphomeyaml/' if 'b' in version else \
|
|
|
|
'https://esphomelib.com/esphomeyaml/'
|
|
|
|
|
2018-12-24 14:29:11 +01:00
|
|
|
self.render("templates/login.html", version=version, docs_link=docs_link, error=error,
|
|
|
|
get_static_file_url=get_static_file_url)
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
def post_hassio_login(self):
|
|
|
|
import requests
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
'X-HASSIO-KEY': os.getenv('HASSIO_TOKEN'),
|
|
|
|
}
|
|
|
|
data = {
|
|
|
|
'username': str(self.get_argument('username', '')),
|
|
|
|
'password': str(self.get_argument('password', ''))
|
|
|
|
}
|
|
|
|
try:
|
|
|
|
req = requests.post('http://hassio/auth', headers=headers, data=data)
|
|
|
|
if req.status_code == 200:
|
|
|
|
self.set_secure_cookie("authenticated", "yes")
|
|
|
|
self.redirect('/')
|
|
|
|
return
|
|
|
|
except Exception as err: # pylint: disable=broad-except
|
|
|
|
_LOGGER.warn("Error during Hass.io auth request: %s", err)
|
|
|
|
self.set_status(500)
|
|
|
|
self.render_hassio_login(error="Internal server error")
|
|
|
|
return
|
|
|
|
self.set_status(401)
|
|
|
|
self.render_hassio_login(error="Invalid username or password")
|
|
|
|
|
2018-06-07 20:47:06 +02:00
|
|
|
def post(self):
|
2018-12-05 21:22:06 +01:00
|
|
|
if USING_HASSIO_AUTH:
|
|
|
|
self.post_hassio_login()
|
|
|
|
return
|
|
|
|
|
2018-06-07 20:47:06 +02:00
|
|
|
password = str(self.get_argument("password", ''))
|
|
|
|
password = hmac.new(password).digest()
|
2018-12-05 21:22:06 +01:00
|
|
|
if hmac.compare_digest(PASSWORD_DIGEST, password):
|
2018-06-07 20:47:06 +02:00
|
|
|
self.set_secure_cookie("authenticated", "yes")
|
|
|
|
self.redirect("/")
|
|
|
|
|
|
|
|
|
2018-12-24 14:29:11 +01:00
|
|
|
_STATIC_FILE_HASHES = {}
|
|
|
|
|
|
|
|
|
|
|
|
def get_static_file_url(name):
|
|
|
|
static_path = os.path.join(os.path.dirname(__file__), 'static')
|
|
|
|
if name in _STATIC_FILE_HASHES:
|
|
|
|
hash_ = _STATIC_FILE_HASHES[name]
|
|
|
|
else:
|
|
|
|
path = os.path.join(static_path, name)
|
|
|
|
with open(path, 'rb') as f_handle:
|
|
|
|
hash_ = hash(f_handle.read())
|
|
|
|
_STATIC_FILE_HASHES[name] = hash_
|
|
|
|
return u'/static/{}?hash={}'.format(name, hash_)
|
|
|
|
|
|
|
|
|
2018-06-03 11:18:53 +02:00
|
|
|
def make_app(debug=False):
|
2018-12-05 21:22:06 +01:00
|
|
|
def log_function(handler):
|
|
|
|
if handler.get_status() < 400:
|
|
|
|
log_method = access_log.info
|
|
|
|
|
|
|
|
if isinstance(handler, SerialPortRequestHandler) and not debug:
|
|
|
|
return
|
|
|
|
if isinstance(handler, PingRequestHandler) and not debug:
|
|
|
|
return
|
|
|
|
elif handler.get_status() < 500:
|
|
|
|
log_method = access_log.warning
|
|
|
|
else:
|
|
|
|
log_method = access_log.error
|
|
|
|
|
|
|
|
request_time = 1000.0 * handler.request.request_time()
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
log_method("%d %s %.2fms", handler.get_status(),
|
|
|
|
handler._request_summary(), request_time)
|
|
|
|
|
|
|
|
class StaticFileHandler(tornado.web.StaticFileHandler):
|
|
|
|
def set_extra_headers(self, path):
|
|
|
|
if debug:
|
|
|
|
self.set_header('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')
|
|
|
|
|
2018-05-21 16:40:22 +02:00
|
|
|
static_path = os.path.join(os.path.dirname(__file__), 'static')
|
2018-12-05 21:22:06 +01:00
|
|
|
app = tornado.web.Application([
|
2018-05-21 16:40:22 +02:00
|
|
|
(r"/", MainRequestHandler),
|
2018-06-07 20:47:06 +02:00
|
|
|
(r"/login", LoginHandler),
|
2018-05-21 16:40:22 +02:00
|
|
|
(r"/logs", EsphomeyamlLogsHandler),
|
|
|
|
(r"/run", EsphomeyamlRunHandler),
|
|
|
|
(r"/compile", EsphomeyamlCompileHandler),
|
2018-06-03 12:16:43 +02:00
|
|
|
(r"/validate", EsphomeyamlValidateHandler),
|
2018-10-04 19:01:02 +02:00
|
|
|
(r"/clean-mqtt", EsphomeyamlCleanMqttHandler),
|
2018-10-14 18:52:21 +02:00
|
|
|
(r"/clean", EsphomeyamlCleanHandler),
|
2018-11-03 14:08:31 +01:00
|
|
|
(r"/hass-config", EsphomeyamlHassConfigHandler),
|
2018-12-05 21:22:06 +01:00
|
|
|
(r"/edit", EditRequestHandler),
|
2018-05-21 16:40:22 +02:00
|
|
|
(r"/download.bin", DownloadBinaryRequestHandler),
|
|
|
|
(r"/serial-ports", SerialPortRequestHandler),
|
2018-12-05 21:22:06 +01:00
|
|
|
(r"/ping", PingRequestHandler),
|
2018-05-21 16:40:22 +02:00
|
|
|
(r"/wizard.html", WizardRequestHandler),
|
2018-12-05 21:22:06 +01:00
|
|
|
(r'/static/(.*)', StaticFileHandler, {'path': static_path}),
|
|
|
|
], debug=debug, cookie_secret=COOKIE_SECRET, log_function=log_function)
|
2018-12-24 14:29:11 +01:00
|
|
|
|
|
|
|
if debug:
|
|
|
|
_STATIC_FILE_HASHES.clear()
|
|
|
|
|
2018-12-05 21:22:06 +01:00
|
|
|
return app
|
|
|
|
|
|
|
|
|
2018-05-21 16:40:22 +02:00
|
|
|
def start_web_server(args):
|
|
|
|
global CONFIG_DIR
|
2018-12-05 21:22:06 +01:00
|
|
|
global PASSWORD_DIGEST
|
|
|
|
global USING_PASSWORD
|
|
|
|
global ON_HASSIO
|
|
|
|
global USING_HASSIO_AUTH
|
|
|
|
global COOKIE_SECRET
|
2018-06-02 22:22:20 +02:00
|
|
|
|
2018-05-21 16:40:22 +02:00
|
|
|
CONFIG_DIR = args.configuration
|
2018-12-05 21:22:06 +01:00
|
|
|
mkdir_p(CONFIG_DIR)
|
|
|
|
mkdir_p(os.path.join(CONFIG_DIR, ".esphomeyaml"))
|
|
|
|
|
|
|
|
ON_HASSIO = args.hassio
|
|
|
|
if ON_HASSIO:
|
|
|
|
USING_HASSIO_AUTH = not bool(os.getenv('DISABLE_HA_AUTHENTICATION'))
|
|
|
|
USING_PASSWORD = False
|
|
|
|
else:
|
|
|
|
USING_HASSIO_AUTH = False
|
|
|
|
USING_PASSWORD = args.password
|
|
|
|
|
|
|
|
if USING_PASSWORD:
|
|
|
|
PASSWORD_DIGEST = hmac.new(args.password).digest()
|
|
|
|
|
|
|
|
if USING_HASSIO_AUTH or USING_PASSWORD:
|
|
|
|
path = esphomeyaml_storage_path(CONFIG_DIR)
|
|
|
|
storage = EsphomeyamlStorageJSON.load(path)
|
|
|
|
if storage is None:
|
|
|
|
storage = EsphomeyamlStorageJSON.get_default()
|
|
|
|
storage.save(path)
|
|
|
|
COOKIE_SECRET = storage.cookie_secret
|
2018-06-07 20:47:06 +02:00
|
|
|
|
2018-06-03 11:18:53 +02:00
|
|
|
app = make_app(args.verbose)
|
2019-01-02 12:21:26 +01:00
|
|
|
if args.socket is not None:
|
|
|
|
_LOGGER.info("Starting dashboard web server on unix socket %s and configuration dir %s...",
|
|
|
|
args.socket, CONFIG_DIR)
|
|
|
|
server = tornado.httpserver.HTTPServer(app)
|
|
|
|
socket = tornado.netutil.bind_unix_socket(args.socket, mode=0o666)
|
|
|
|
server.add_socket(socket)
|
|
|
|
else:
|
|
|
|
_LOGGER.info("Starting dashboard web server on port %s and configuration dir %s...",
|
|
|
|
args.port, CONFIG_DIR)
|
|
|
|
app.listen(args.port)
|
2018-10-04 19:01:02 +02:00
|
|
|
|
2019-01-02 12:21:26 +01:00
|
|
|
if args.open_ui:
|
|
|
|
import webbrowser
|
2018-10-04 19:01:02 +02:00
|
|
|
|
2019-01-02 12:21:26 +01:00
|
|
|
webbrowser.open('localhost:{}'.format(args.port))
|
2018-10-04 19:01:02 +02:00
|
|
|
|
2018-12-05 21:22:06 +01:00
|
|
|
ping_thread = PingThread()
|
|
|
|
ping_thread.start()
|
2018-05-21 16:40:22 +02:00
|
|
|
try:
|
|
|
|
tornado.ioloop.IOLoop.current().start()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
_LOGGER.info("Shutting down...")
|
2018-12-05 21:22:06 +01:00
|
|
|
STOP_EVENT.set()
|
|
|
|
PING_REQUEST.set()
|
|
|
|
ping_thread.join()
|
2019-01-02 12:21:26 +01:00
|
|
|
if args.socket is not None:
|
|
|
|
os.remove(args.socket)
|