masq
Version:
A simple local dns server extracted from Pow
173 lines (120 loc) • 6.26 kB
HTML
<html>
<head>
<title>dns_server.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 …</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>dns_server.coffee</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
<p>Pow’s <code>DnsServer</code> is designed to respond to DNS <code>A</code> queries with
<code>127.0.0.1</code> for all subdomains of the specified top-level domain.
When used in conjunction with Mac OS X’s <a href="http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man5/resolver.5.html">/etc/resolver
system</a>,
there’s no configuration needed to add and remove host names for
local web development.</p>
</div>
<div class="content"><div class='highlight'><pre>
dnsserver = <span class="hljs-built_in">require</span> <span class="hljs-string">"dnsserver"</span>
NS_T_A = <span class="hljs-number">1</span>
NS_C_IN = <span class="hljs-number">1</span>
NS_RCODE_NXDOMAIN = <span class="hljs-number">3</span>
<span class="hljs-built_in">module</span>.exports = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DnsServer</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">dnsserver</span>.<span class="hljs-title">Server</span></span></pre></div></div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<p>Create a <code>DnsServer</code> with the given <code>Configuration</code> instance. The
server installs a single event handler for responding to DNS
queries.</p>
</div>
<div class="content"><div class='highlight'><pre> constructor: <span class="hljs-function"><span class="hljs-params">(@configuration)</span> -></span>
<span class="hljs-keyword">super</span>()
@on <span class="hljs-string">"request"</span>, @handleRequest</pre></div></div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<p>The <code>listen</code> method is just a wrapper around <code>bind</code> that makes
<code>DnsServer</code> quack like a <code>HttpServer</code> (for initialization, at
least).</p>
</div>
<div class="content"><div class='highlight'><pre> listen: <span class="hljs-function"><span class="hljs-params">(port, callback)</span> -></span>
@bind port
callback?()</pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>Each incoming DNS request ends up here. If it’s an <code>A</code> query
and the domain name matches the top-level domain specified in our
configuration, we respond with <code>127.0.0.1</code>. Otherwise, we respond
with <code>NXDOMAIN</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> handleRequest: <span class="hljs-function"><span class="hljs-params">(req, res)</span> -></span>
pattern = @configuration.dnsDomainPattern
q = req.question ? {}
<span class="hljs-keyword">if</span> q.type <span class="hljs-keyword">is</span> NS_T_A <span class="hljs-keyword">and</span> q.class <span class="hljs-keyword">is</span> NS_C_IN <span class="hljs-keyword">and</span> pattern.test q.name
res.addRR q.name, NS_T_A, NS_C_IN, <span class="hljs-number">600</span>, <span class="hljs-string">"127.0.0.1"</span>
<span class="hljs-keyword">else</span>
res.header.rcode = NS_RCODE_NXDOMAIN
res.send()</pre></div></div>
</li>
</ul>
</div>
</body>
</html>