UNPKG

masq

Version:

A simple local dns server extracted from Pow

444 lines (299 loc) 17.3 kB
<!DOCTYPE html> <html> <head> <title>configuration.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <ul id="jump_to"> <li> <a class="large" href="javascript:void(0);">Jump To &hellip;</a> <a class="small" href="javascript:void(0);">+</a> <div id="jump_wrapper"> <div id="jump_page_wrapper"> <div id="jump_page"> <a class="source" href="command.html"> command.coffee </a> <a class="source" href="configuration.html"> configuration.coffee </a> <a class="source" href="daemon.html"> daemon.coffee </a> <a class="source" href="dns_server.html"> dns_server.coffee </a> <a class="source" href="index.html"> index.coffee </a> <a class="source" href="installer.html"> installer.coffee </a> <a class="source" href="logger.html"> logger.coffee </a> <a class="source" href="utils.html"> utils.coffee </a> </div> </div> </li> </ul> <ul class="sections"> <li id="title"> <div class="annotation"> <h1>configuration.coffee</h1> </div> </li> <li id="section-1"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-1">&#182;</a> </div> <p>The <code>Configuration</code> class encapsulates various options for a Masq daemon (port numbers, directories, etc.). It’s also responsible for creating <code>Logger</code> instances and mapping hostnames to application root paths.</p> </div> <div class="content"><div class='highlight'><pre> fs = <span class="hljs-built_in">require</span> <span class="hljs-string">"fs"</span> path = <span class="hljs-built_in">require</span> <span class="hljs-string">"path"</span> async = <span class="hljs-built_in">require</span> <span class="hljs-string">"async"</span> Logger = <span class="hljs-built_in">require</span> <span class="hljs-string">"./logger"</span> {mkdirp} = <span class="hljs-built_in">require</span> <span class="hljs-string">"./utils"</span> {sourceScriptEnv} = <span class="hljs-built_in">require</span> <span class="hljs-string">"./utils"</span> {getUserEnv} = <span class="hljs-built_in">require</span> <span class="hljs-string">"./utils"</span> <span class="hljs-built_in">module</span>.exports = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Configuration</span></span></pre></div></div> </li> <li id="section-2"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>The user configuration file, <code>~/.masqconfig</code>, is evaluated on boot. You can configure options such as the top-level domain, number of workers, the worker idle timeout, and listening ports.</p> <pre><code> <span class="hljs-keyword">export</span> MASQ_DOMAINS=dev,test <span class="hljs-keyword">export</span> MASQ_WORKERS=<span class="hljs-number">3</span> </code></pre><p>See the <code>Configuration</code> constructor for a complete list of environment options.</p> </div> <div class="content"><div class='highlight'><pre> @userConfigurationPath: path.join process.env.HOME, <span class="hljs-string">".masqconfig"</span></pre></div></div> </li> <li id="section-3"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-3">&#182;</a> </div> <p>Evaluates the user configuration script and calls the <code>callback</code> with the environment variables if the config file exists. Any script errors are passed along in the first argument. (No error occurs if the file does not exist.)</p> </div> <div class="content"><div class='highlight'><pre> @loadUserConfigurationEnvironment: <span class="hljs-function"><span class="hljs-params">(callback)</span> -&gt;</span> getUserEnv (err, env) =&gt; <span class="hljs-keyword">if</span> err callback err <span class="hljs-keyword">else</span> fs.exists p = @userConfigurationPath, <span class="hljs-function"><span class="hljs-params">(exists)</span> -&gt;</span> <span class="hljs-keyword">if</span> exists sourceScriptEnv p, env, callback <span class="hljs-keyword">else</span> callback <span class="hljs-literal">null</span>, env</pre></div></div> </li> <li id="section-4"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>Creates a Configuration object after evaluating the user configuration file. Any environment variables in <code>~/.masqconfig</code> affect the process environment and will be copied to spawned subprocesses.</p> </div> <div class="content"><div class='highlight'><pre> @getUserConfiguration: <span class="hljs-function"><span class="hljs-params">(callback)</span> -&gt;</span> @loadUserConfigurationEnvironment (err, env) -&gt; <span class="hljs-keyword">if</span> err callback err <span class="hljs-keyword">else</span> callback <span class="hljs-literal">null</span>, <span class="hljs-keyword">new</span> Configuration env</pre></div></div> </li> <li id="section-5"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>A list of option names accessible on <code>Configuration</code> instances.</p> </div> <div class="content"><div class='highlight'><pre> @optionNames: [ <span class="hljs-string">"bin"</span>, <span class="hljs-string">"dnsPort"</span>, <span class="hljs-string">"domains"</span>, <span class="hljs-string">"extDomains"</span>, <span class="hljs-string">"logRoot"</span> ]</pre></div></div> </li> <li id="section-6"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>Pass in any environment variables you’d like to override when creating a <code>Configuration</code> instance.</p> </div> <div class="content"><div class='highlight'><pre> constructor: <span class="hljs-function"><span class="hljs-params">(env = process.env)</span> -&gt;</span> @logger = {} @initialize env</pre></div></div> </li> <li id="section-7"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-7">&#182;</a> </div> <p>Valid environment variables and their defaults:</p> </div> <div class="content"><div class='highlight'><pre> initialize: <span class="hljs-function"><span class="hljs-params">(env)</span> -&gt;</span></pre></div></div> </li> <li id="section-8"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-8">&#182;</a> </div> <p><code>MASQ_BIN</code>: the path to the <code>masq</code> binary. (This should be correctly configured for you.)</p> </div> <div class="content"><div class='highlight'><pre> @bin = env.MASQ_BIN ? path.join __dirname, <span class="hljs-string">"../bin/masq"</span></pre></div></div> </li> <li id="section-9"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-9">&#182;</a> </div> <p><code>MASQ_DNS_PORT</code>: the UDP port Masq listens on for incoming DNS queries. Defaults to <code>20560</code>.</p> </div> <div class="content"><div class='highlight'><pre> @dnsPort = env.MASQ_DNS_PORT ? <span class="hljs-number">20560</span></pre></div></div> </li> <li id="section-10"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-10">&#182;</a> </div> <p><code>MASQ_DOMAINS</code>: the top-level domains for which Masq will respond to DNS <code>A</code> queries with <code>127.0.0.1</code>. Defaults to <code>dev</code>. If you configure this in your <code>~/.masqconfig</code> you will need to re-run <code>sudo masq --install-system</code> to make <code>/etc/resolver</code> aware of the new TLDs.</p> </div> <div class="content"><div class='highlight'><pre> @domains = env.MASQ_DOMAINS ? env.MASQ_DOMAINS ? <span class="hljs-string">"dev"</span></pre></div></div> </li> <li id="section-11"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-11">&#182;</a> </div> <p><code>MASQ_EXT_DOMAINS</code>: additional top-level domains for which Masq will serve HTTP requests (but not DNS requests – hence the “ext”).</p> </div> <div class="content"><div class='highlight'><pre> @extDomains = env.MASQ_EXT_DOMAINS ? []</pre></div></div> </li> <li id="section-12"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-12">&#182;</a> </div> <p>Allow for comma-separated domain lists, e.g. <code>MASQ_DOMAINS=dev,test</code></p> </div> <div class="content"><div class='highlight'><pre> @domains = @domains.split?(<span class="hljs-string">","</span>) ? @domains @extDomains = @extDomains.split?(<span class="hljs-string">","</span>) ? @extDomains @allDomains = @domains.concat @extDomains</pre></div></div> </li> <li id="section-13"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-13">&#182;</a> </div> <p>Support *.xip.io top-level domains.</p> </div> <div class="content"><div class='highlight'><pre> @allDomains.push <span class="hljs-regexp">/\d+\.\d+\.\d+\.\d+\.xip\.io$/</span>, <span class="hljs-regexp">/[0-9a-z]{1,7}\.xip\.io$/</span></pre></div></div> </li> <li id="section-14"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-14">&#182;</a> </div> <p>Runtime support files live in <code>~/Library/Application Support/Masq</code>.</p> </div> <div class="content"><div class='highlight'><pre> @supportRoot = libraryPath <span class="hljs-string">"Application Support"</span>, <span class="hljs-string">"Masq"</span></pre></div></div> </li> <li id="section-15"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-15">&#182;</a> </div> <p><code>MASQ_LOG_ROOT</code>: path to the directory that Masq will use to store its log files. Defaults to <code>~/Library/Logs/Masq</code>.</p> </div> <div class="content"><div class='highlight'><pre> @logRoot = env.MASQ_LOG_ROOT ? libraryPath <span class="hljs-string">"Logs"</span>, <span class="hljs-string">"Masq"</span></pre></div></div> </li> <li id="section-16"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-16">&#182;</a> </div> <p>Precompile regular expressions for matching domain names to be served by the DNS server.</p> </div> <div class="content"><div class='highlight'><pre> @dnsDomainPattern = compilePattern @domains</pre></div></div> </li> <li id="section-17"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-17">&#182;</a> </div> <p>Gets an object of the <code>Configuration</code> instance’s options that can be passed to <code>JSON.stringify</code>.</p> </div> <div class="content"><div class='highlight'><pre> toJSON: <span class="hljs-function">-&gt;</span> result = {} result[key] = @[key] <span class="hljs-keyword">for</span> key <span class="hljs-keyword">in</span> @constructor.optionNames result</pre></div></div> </li> <li id="section-18"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-18">&#182;</a> </div> <p>Retrieve a <code>Logger</code> instance with the given <code>name</code>.</p> </div> <div class="content"><div class='highlight'><pre> getLogger: <span class="hljs-function"><span class="hljs-params">(name)</span> -&gt;</span> @loggers[name] ||= <span class="hljs-keyword">new</span> Logger path.join @logRoot, name + <span class="hljs-string">".log"</span></pre></div></div> </li> <li id="section-19"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-19">&#182;</a> </div> <p>Convenience wrapper for constructing paths to subdirectories of <code>~/Library</code>.</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">libraryPath</span> = <span class="hljs-params">(args...)</span> -&gt;</span> path.join process.env.HOME, <span class="hljs-string">"Library"</span>, args...</pre></div></div> </li> <li id="section-20"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-20">&#182;</a> </div> <p>Helper function for compiling a list of top-level domains into a regular expression for matching purposes.</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">compilePattern</span> = <span class="hljs-params">(domains)</span> -&gt;</span> <span class="hljs-regexp">/// ( (^|\.) (<span class="hljs-subst">#{domains.join(<span class="hljs-string">"|"</span>)}</span>) ) \.? $ ///</span>i</pre></div></div> </li> </ul> </div> </body> </html>