diff --git a/lib/server.js b/lib/server.js index b85dcc3..af57241 100644 --- a/lib/server.js +++ b/lib/server.js @@ -27,16 +27,16 @@ function Server() { } Server.prototype.run = function() { - + // keep track of async calls we're waiting for callbacks on before we can start up this._asyncCalls = 0; this._asyncWait = true; - + if (this._config.platforms) this._loadPlatforms(); if (this._config.accessories) this._loadAccessories(); - + this._asyncWait = false; - + // publish now unless we're waiting on anyone if (this._asyncCalls == 0) this._publish(); @@ -53,18 +53,18 @@ Server.prototype._publish = function() { pincode: bridgeConfig.pin || "031-45-154", category: Accessory.Categories.OTHER }); - + log.info("Homebridge is running on port %s.", bridgeConfig.port || 51826); } Server.prototype._loadPlugins = function(accessories, platforms) { - + var plugins = {}; var foundOnePlugin = false; - + // load and validate plugins - check for valid package.json, etc. Plugin.installed().forEach(function(plugin) { - + // attempt to load it try { plugin.load(); @@ -78,7 +78,7 @@ Server.prototype._loadPlugins = function(accessories, platforms) { } if (!plugin.loadError) { - + // add it to our dict for easy lookup later plugins[plugin.name()] = plugin; @@ -90,19 +90,19 @@ Server.prototype._loadPlugins = function(accessories, platforms) { log.info("---"); foundOnePlugin = true; } - + }.bind(this)); // Complain if you don't have any plugins. if (!foundOnePlugin) { log.warn("No plugins found. See the README for information on installing plugins.") } - + return plugins; } Server.prototype._loadConfig = function() { - + // Look for the configuration file var configPath = User.configPath(); @@ -111,7 +111,7 @@ Server.prototype._loadConfig = function() { log.error("Couldn't find a config.json file at '"+configPath+"'. Look at config-sample.json for examples of how to format your config.js and add your home accessories."); process.exit(1); } - + // Load up the configuration file var config; try { @@ -123,11 +123,20 @@ Server.prototype._loadConfig = function() { log.error(""); throw err; } - + + var accessoryCount = (config.accessories && config.accessories.length) || 0; + + var username = config.bridge.username; + var validMac = /^([0-9A-F]{2}:){5}([0-9A-F]{2})$/; + if (!validMac.test(username)){ + throw new Error('Not a valid username: ' + username + '. Must be 6 pairs of colon-' + + 'separated hexadecimal chars (A-F 0-9), like a MAC address.'); + } + var accessoryCount = (config.accessories && config.accessories.length) || 0; var platformCount = (config.platforms && config.platforms.length) || 0; log.info("Loaded config.json with %s accessories and %s platforms.", accessoryCount, platformCount); - + log.info("---"); return config; @@ -145,7 +154,7 @@ Server.prototype._loadAccessories = function() { // Instantiate all accessories in the config log.info("Loading " + this._config.accessories.length + " accessories..."); - + for (var i=0; i